var thisUrl,lastUrl,theplayer;

(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);

$(function(){
	$('#flownav  a')
		.css( {backgroundPosition: "-20px 35px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-20px 110px)"}, {duration:400})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(40px 35px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-20px 35px"})
			}})
		})
});

function setButtons(u){
var links = $("#clips a").get();
for (var i = 0; i < links.length; i++) {
	testUrl = links[i].getAttribute("href", 2);
		if(testUrl == u){
		//this lets you know what main nav to manage
			$(links[i]).addClass("onbnt");
			temp = $(links[i]).text + "";
			if(temp.toLowerCase().indexOf('delicious')){indexDel = i}
		}
		else{$(links[i]).removeClass("onbnt");}
	}
	
	if(document.getElementById("clips2") != null){
		var extralinks = document.getElementById("clips2").getElementsByTagName("li");
		$(extralinks).removeClass("pausebnt");
		// loop those links and alter their click behaviour
		for (var ii = 0; ii < extralinks.length; ii++) {
			testUrl = extralinks[ii].getElementsByTagName("a")[0].getAttribute("href", 2);
			if(testUrl == u){$(extralinks[ii]).addClass("pausebnt");$(links[indexDel]).addClass("onbnt");}
		}
	}
}

function finishedPlay(obj){
var p1 = 'Mediafarm-StepsToDeliciousPart1956.flv';
var p2 = 'Mediafarm-StepsToDeliciousPart2693.flv';
var p3 = 'Mediafarm-StepsToDeliciousPart3262.flv';
var p4 = 'Mediafarm-StepsToDeliciousPart4FlavoringTheSoup877.flv';

	switch (thisUrl){
		case p1:
			thisUrl = p2;
			$f().play(thisUrl);
			setButtons(thisUrl);
		break;
		case p2:
			thisUrl = p3;
			$f().play(thisUrl);
			setButtons(thisUrl);
		break;
		case p3:
			thisUrl = p4;
			$f().play(thisUrl);
			setButtons(thisUrl);
		break;
		default : //finished playing non delicious clip
	}
}

 $(document).ready(function(){

//  install Flowplayer inside a#player
$f("player", {src:'/internet/cancer-patients/media/swf/flowplayer-3.0.7.swf', wmode: 'transparent'}, {
		
	clip: {

		// use baseUrl so we can play with shorter file names
		baseUrl: 'http://blip.tv/file/get',
		// use first frame of the clip as a splash screen
		autoPlay: false,
		autoBuffering: true,
		width:560
	} 
});

theplayer = $f();

// get all links that are inside div#clips

		var links = document.getElementById("clips").getElementsByTagName("a");
		theplayer.onBegin(function(){setButtons(this.getClip().url.split("/").pop());});
		// loop those links and alter their click behaviour

		theplayer.onFinish(function(){finishedPlay(this);});
		for (var i = 0; i < links.length; i++) {
			links[i].onclick = function() {
				thisUrl = this.getAttribute("href", 2);
				setButtons(thisUrl);
				// play the clip specified in href- attribute with Flowplayer
				$f().play(thisUrl);
				// by returning false normal link behaviour is skipped
				return false;
			}
		}
		
		if(document.getElementById("clips2") != null){
			var extralinks = document.getElementById("clips2").getElementsByTagName("a");
			// loop those links and alter their click behaviour
			for (var ii = 0; ii < extralinks.length; ii++) {
				extralinks[ii].onclick = function() {
					// play the clip specified in href- attribute with Flowplayer
					thisUrl = this.getAttribute("href", 2);
					$f().play(thisUrl);
					setButtons(thisUrl);
					// by returning false normal link behaviour is skipped
					return false;
				}
			}
		}
		$("#flownav a").click(togglePlay);
		$("div.lowerblock").hide();
 });
 
 function togglePlay(obj){
		var aId = obj['target'].id;
		var divId = "f" + aId;
		var aObj = document.getElementById(aId);
		var divObj = document.getElementById(divId);
		if ($(divObj).is(':hidden')) {
			$("div.lowerblock").hide();
			$(divObj).show('slow');
		}
	 }