var links = new Array();
var entries = new Array();
var items = new Array();
var current = 0;
var yt_current = 0;
var f_current = 0;
var nyt_data = new Array();

function nyt_next(n) {
	current = current + n;
	var max = nyt_data.results.length - 1;
	if (current >= max) {
		current = max;
		$('nyt_nxt_btn').setStyle({visibility: 'hidden', cursor: 'default'});
		$('nyt_nxt_btn').onclick = function() { null }
		$('nyt_nxt_icon').onclick = function() { null }
		$('nyt_nxt_icon').setStyle({cursor: 'default', visibility: 'hidden'});
		$('nyt_pre_btn').setStyle({visibility: 'visible', cursor: 'pointer'});
		$('nyt_pre_btn').onclick = function() { nyt_next(-1) }
		$('nyt_pre_icon').onclick = function() { nyt_next(-1) }
		$('nyt_pre_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
	} else if(current < 1) {
		current = 0;
		$('nyt_pre_btn').setStyle({visibility: 'hidden', cursor: 'default'});
		$('nyt_pre_icon').setStyle({cursor: 'default', visibility: 'hidden'});
		$('nyt_pre_btn').onclick = function() { null }
		$('nyt_nxt_btn').setStyle({visibility: 'visible', cursor: 'pointer'});
		$('nyt_nxt_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
		$('nyt_nxt_btn').onclick = function() { nyt_next(1) }
		$('nyt_pre_icon').onclick = function() { null }
		$('nyt_nxt_icon').onclick = function() { nyt_next(1) }	
	} else {
		$('nyt_pre_btn').setStyle({visibility: 'visible', cursor: 'pointer'});
		$('nyt_pre_btn').onclick = function() { nyt_next(-1) }
		$('nyt_nxt_btn').setStyle({visibility: 'visible', cursor: 'pointer'});
		$('nyt_nxt_btn').onclick = function() { nyt_next(1) }
		$('nyt_pre_icon').onclick = function() { nyt_next(-1) }
		$('nyt_nxt_icon').onclick = function() { nyt_next(1) }
		$('nyt_nxt_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
		$('nyt_pre_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
	}
	
	image = (nyt_data.results[current].small_image_url) ? '<img style="padding: 10px; float: right" src="'+ nyt_data.results[current].small_image_url +'" />' : '';
	
	title = (nyt_data.results[current].title.split(';')[1] != undefined) ? nyt_data.results[current].title.split(';')[1] : nyt_data.results[current].title;
	string = ('<a href="'+nyt_data.results[current].url+'" target="_blank" style="text-decoration: none">'+'<h2 style="font-size: 120%; margin: 5px">'+title+"</h2></a>"+image+"<p style='padding: 0px 5px 0px 5px'>" + nyt_data.results[current].body + '... <a href="' + nyt_data.results[current].url + '" target="_blank">[read more]</a></p>');
	$('nytimes_article').update(string);
}

function nytJSON(data) {
	document.observe('dom:loaded', function() {
		current = 0;
		nyt_data = data;
	});
}

function youtubeJSON(data) {
	document.observe('dom:loaded', function() {
		entries = data.feed.entry;
		yt_current = 0;
		yt_parse();
	});
}

function yt_parse() {
	for (var i=0; i < 4; i++) {
		var index = (yt_current * 4) + i;
		var item = entries[index];
		var url = item.media$group.media$content[0].url;
		var title = '<span style="color: #1487C0; font-weight: bold">' + item.title.$t.substring(0, 30) + '... </span>';		
		$('yt_img'+i).update('<a onclick="getVid(\'' + url + '\')" style="cursor: pointer"><img style="width: 135px" align=center src="'+item.media$group.media$thumbnail[0].url+'" />' + title + '</a>');
		//alert('yt_current after yt_parse: ' + yt_current);
	}
}

function yt_next(n) {
	//alert('yt_current at the beginning of yt_next: ' + yt_current);
	yt_current = yt_current + n;
	
	//alert('yt_current after adding n: ' + yt_current);
	
	if (yt_current >= Math.floor((entries.length/4) - 1)) {
		yt_current = Math.floor((entries.length/4) - 1);
		$('yt_nxt_btn').setStyle({visibility: 'hidden', cursor: 'default'});
		$('yt_nxt_btn').onclick = function() { null }
		$('yt_nxt_icon').onclick = function() { null }
		$('yt_nxt_icon').setStyle({visibility: 'hidden', cursor: 'default'});
	} else if(yt_current < 1) {
		yt_current = 0;
		$('yt_pre_btn').setStyle({visibility: 'hidden', cursor: 'default'});
		$('yt_pre_btn').onclick = function() { null }
		$('yt_pre_icon').setStyle({cursor: 'default', visibility: 'hidden'});
		$('yt_pre_icon').onclick = function() { null }
	} else {
		$('yt_pre_btn').setStyle({visibility: 'visible', cursor: 'pointer'});
		$('yt_pre_btn').onclick = function() { yt_next(-1) }
		$('yt_pre_icon').onclick = function() { yt_next(-1) }
		$('yt_pre_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
		$('yt_nxt_btn').setStyle({visibility: 'visible', cursor: 'pointer'});
		$('yt_nxt_btn').onclick = function() { yt_next(1) }
		$('yt_nxt_icon').onclick = function() { yt_next(1) }
		$('yt_nxt_icon').setStyle({cursor: 'pointer',visibility: 'visible'});
	}
	
	//alert('yt_current after button magic: ' + yt_current);
	
	for (var i=0; i < 4; i++) {
		var index = (yt_current * 4) + i;
		var item = entries[index];
		var url = item.media$group.media$content[0].url;
		var title = '<span style="color: #1487C0; font-weight: bold">' + item.title.$t.substring(0, 30) + '... </span>';
		$('yt_img'+i).update('<a onclick="getVid(\'' + url + '\')" style="cursor: pointer"><img style="width: 135px" align=center src="'+item.media$group.media$thumbnail[0].url+'" />' + title + '</a>');
	}
	//alert('yt_current at the end of fnext(): '+yt_current);
}

function getVid(url) {
	var params = { allowScriptAccess: "always" };
	var atts = { id: "myytplayer" };
	swfobject.embedSWF(url + '&rel=1&border=0&fs=1&autoplay=1', 
			'yt_feed', '300', '280', '9.0.0', false, 
			false, {allowfullscreen: 'true'});
}

function jsonFlickrFeed(data) {
	document.observe('dom:loaded', function() { 
		items = fParse(data);
	});
}

function linksJSON(data) {
	document.observe('dom:loaded', function() {
		links = data;
	});
}

function fParse(data) {
	
	var pre_links_arr = [];
	var post_links_arr = [];
	
	
	
	for (var i=0; i < f_current * 4; i++) {
		//alert('appending to pre_links');
		pre_links_arr.push('<a href="' + data[i].medium + '" rel="lightbox[flickr]"></a>');
	}
	
	for(var i=f_current*4+4; i < data.length; i++){
		//alert('appending to post_links');
		post_links_arr.push('<a href="' + data[i].medium + '" rel="lightbox[flickr]"></a>');
	}
	
	for (var i=0; i < 4; i++) {
		var index = (f_current * 4) + i;
		var item = data[index];
		$('aimg'+i).href = item.medium;
		$('f_img'+i).setStyle({ backgroundImage: 'url(' + item.small + ')', backgroundPosition: 'center center' });
	}

	$('f_links_pre').update(pre_links_arr.join(''));
	$('f_links_post').update(post_links_arr.join(''));
	
	return data;
}


		


function fNext(n) {
	f_current = f_current + n;
	if(f_current < 1) {
		f_current = 0;
		$('f_pre_btn').setStyle({visibility: 'hidden',cursor: 'default'});
		$('f_pre_btn').onclick = function() { null }		
		$('f_pre_icon').setStyle({cursor: 'default', visibility: 'hidden'});
		$('f_pre_icon').onclick = function() { null }
		$('f_nxt_btn').setStyle({visibility: 'visible',cursor: 'pointer'});
		$('f_nxt_btn').onclick = function() { fNext(1) }
		$('f_nxt_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
		$('f_nxt_icon').onclick = function() { fNext(1) }
	} else if (f_current >= Math.floor(items.length / 4) - 1) {
		f_current = Math.floor((items.length / 4) - 1);
		$('f_nxt_btn').setStyle({visibility: 'hidden',cursor: 'default'});
		$('f_nxt_btn').onclick = function() { null }
		$('f_nxt_icon').setStyle({cursor: 'default', visibility: 'hidden'});
		$('f_nxt_icon').onclick = function() { null }
		$('f_pre_btn').setStyle({visibility: 'visible',cursor: 'pointer'});
		$('f_pre_btn').onclick = function() { fNext(-1) }
		$('f_pre_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
		$('f_pre_icon').onclick = function() { fNext(-1) }
	} else {
		$('f_pre_btn').setStyle({visibility: 'visible',cursor: 'pointer'});
		$('f_pre_btn').onclick = function() { fNext(-1) }
		$('f_pre_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
		$('f_pre_icon').onclick = function() { fNext(-1) }
		$('f_nxt_btn').setStyle({visibility: 'visible',cursor: 'pointer'});
		$('f_nxt_btn').onclick = function() { fNext(1) }
		$('f_nxt_icon').setStyle({cursor: 'pointer', visibility: 'visible'});
		$('f_nxt_icon').onclick = function() { fNext(1) }
	}
	
	var pre_links_arr = [];
	var post_links_arr = [];
	
	for (var i=0; i < f_current * 4; i++) {
		//alert('appending to pre_links');
		pre_links_arr.push('<a href="' + items[i].medium + '" rel="lightbox[flickr]"></a>');
	}
	
	for(var i=f_current*4+4; i < items.length; i++){
		//alert('appending to post_links');
		post_links_arr.push('<a href="' + items[i].medium + '" rel="lightbox[flickr]"></a>');
	}
	
	for (var i=0; i < 4; i++) {
		var index = (f_current * 4) + i;
		var item = items[index];
		$('aimg'+i).href = item.medium;
		$('f_img'+i).setStyle({ backgroundImage: 'url(' + item.small + ')', backgroundPosition: 'center center' });
	}
	
	var max = Math.floor(items.length / 4) - 1;
	
	$('place').update(f_current + ' of ' + max);
	
	$('f_links_pre').update(pre_links_arr.join(''));
	$('f_links_post').update(post_links_arr.join(''));
}