var loadingFrame = 1;
var loadingTimer;
$.ajaxSetup({cache: true});

function animateLoading() {
	if (!$("#spinner_loading").is(':visible')){
		clearInterval(loadingTimer);
		return;
	}
	$("#spinner_loading > div").css('top', (loadingFrame * -40) + 'px');

	loadingFrame = (loadingFrame + 1) % 12;
};

function show_loading() {
	clearInterval(loadingTimer);
	$("#spinner_loading").show();
	$("#spinner_loading").css({'left': $('#big').width()/2-20, 'top': -$('#big').height()/2-20}).show();
	loadingTimer = setInterval('animateLoading()', 66);
}

function load_img() {
	clearTimeout(slideshow_timeout);
	if(current_type == 'pic') {
		var img = $('#big .big_img_new').get(0);
		if(typeof(img.complete) == 'undefined' || img.complete) {
			$("#big #spinner_loading").hide();
			$("#big .big_img").remove();
			$("#big .big_img_new").css('margin-top', Math.round(($("#big").height()-$("#big .big_img_new").height())/2) );
			$("#big .big_img_new").show();
			$("#big .big_img_new").addClass('big_img');
			$("#big .big_img_new").removeClass('big_img_new');
			if(slideshow_started) {
				var next_image = new Image();
				next_image.src = contents[slideshow_current].url+"_big.jpg";
				slideshow_timeout = setTimeout('view_content('+slideshow_current+')', 5000);
			}
		} else {
			if($("#big").is(":has(#spinner_loading:hidden)")) {
				show_loading();
			}
			load_img_timer=setTimeout('load_img()', 50);
		}
	} else {
		view_content(slideshow_current);
	}
}

function view_content(id) {
	if(slideshow_started) {
		if(slideshow_current>=contents.length-1) {
			slideshow_current = 0;
		} else {
			slideshow_current = parseInt(slideshow_current) + 1;
		}
		$('#thumb_'+id).fadeTo(200, 1);
		$('#thumb_'+id).css({ border: '1px dashed #333' });
		$('#thumb_'+id).unbind('mouseout');
		$('#thumb_'+id).unbind('mouseover');
	}
	if(contents[id].type == 'pic') {
		if(max_height) { $("#big").css('height', max_height+'px'); }
		//var img_thumb = $("<img class='thumb_img' src='"+contents[id].url+"_thumb.jpg' height='"+contents[id].height+"' />");
		var img = $("<img class='big_img_new' style='display:none' src='"+contents[id].url+"_big.jpg' width='"+contents[id].width+"' height='"+contents[id].height+"' />");
		if(contents[id].link_url.length) { 
			var anc = $("<a href='"+unescape(contents[id].link_url)+"' target='_blank'></a>"); 
			anc.append(img);
			img = anc;
		}
		if(!$('#big .big_img').length) {
			img_spinner = $("<div id='spinner_loading' style='display:none'><div></div></div>");
			$("#big").html(img_spinner);
		} 
		$("#big").prepend(img);
		$("#big .big_img_new").hide();
		current_type = 'pic';
		load_img_timer=setTimeout('load_img()', 20);
	} else {
		d = new Date();
		fid = d.getTime();
		var div = $('<div id='+fid+'></div>');
		$("#big").html(div);
		
		$("#"+fid).flowplayer({ 
			src: '/player/flowplayer.swf',
			width: '580',
			height: '360'
		}, {
			clip: { 
				autoBuffering: false, 
				scaling: 'fit',
				bufferLength: 3
			}, play: { 
	            label: 'Play Video', 
    	        replayLabel: 'Play again',
				fadeSpeed: 3000,
				height: 70
	        }, screen: {
				bottom: 0
			}, canvas: { 
    	        backgroundColor: '#F7FBFD'
	        }, playlist: [ 
				{ 
					url: contents[id].splash_url, 
					provider: 'http',
					onFinish: function () {
						if(slideshow_started) {
							this.getClip(1).update({autoPlay:true});
						}
					}
				}, { 
					url: contents[id].url, 
					provider: 'lighttpd', 
					autoPlay: false,
					onFinish: function () { 
						if(slideshow_started && !this.isFullscreen()) {
							current_type = 'video';
							load_img_timer = setTimeout('load_img()', 10);
						}
						this.play(1);
					}
				}
			],
			plugins: { 
				lighttpd: {  
					url: 'flowplayer.streaming.swf',  
					queryString: '?start=${start}'
				},
				controls: {
					url: 'flowplayer.controls.swf',
					opacity: 0.55,
					bottom: 7,
					width: '90%',
					height: 30,
					borderRadius: 12,
					left: '50%',
					autoHide: 'always'
				}
			}
		});
		$("#big").css('height', 'auto');
	}
}