/*
 * Facts About Fees JS Application
 * http://factsaboutfees.bankofamerica.com
 *
 * Author: Patrick Dunphy, Organic Inc
 * Date: 2009-08-19
 * 
 * refactored by Jason Cliff, Organic Inc
 * Date: 2009-09-01+
 * 
 * Production Code minified using JS Minifier - http://fmarcia.info/jsmin/test.html 
 * 
 */

var timeout    = 1000;
var closetimer = 0;
var ddmenuitem = 0;
var cache = {};


$().ready(function(){  
	
	setCache();
	
	// top all navigation elements for keyboard & mouse devices
	
	setNavigation();
	
	setADA();
	
	//side bar tab events
	
	setTabSwitch();
	
	setPageContent();

	//for each h2 append class name - CSS specific bg image per page headings
	
	setH2andSiteMap();
	
	//for each glossary term
	
	// for each close definition link add alt & image
	setCloseDef();
	
	// for each glossary link
	setGlossery();
		
	// what it cost widget
		setCostWidget();
	
	// signUp Links
		setUpLinks();

	// footnote info
		setFootNotes();
		
	// flash movie
		setUpFlash();
	
	// misc
		checkPage();
		getADAContent();
	
		$("#siteMap").append("<img src='assets/img/bg_sitemap_btm.jpg' alt='' border='' height='5' width='942' style='margin-left:-24px' />");


});

function setH2andSiteMap() {
	
	var h2length = $('h2').each(function(i){
			//append class name for CSS reference
			//this controls the title graphics which are css sprites
			$(this).attr('class', "sh" + (i+1));
			//add offLeft class so text still readable for screenreader users.
			$(this).html("<span class='offLeft'>"+ $(this).html() + "</span>");
	    }
	).length;
	
	$('h2#sitemap').removeClass("sh" + (h2length-1)); //put the site map back
	$('body#hp h2#sitemap img').attr("src","assets/img/spacer.gif");
}

function setCloseDef() {
	
	$('.closeDef').each(
		function(){
			$(this).attr("alt","Close");
			$(this).html("<img src='../../assets/img/ico-glossary-close.gif' height='13' width='13' border='0' />");

	});

}


function setCache() {
	
	cache.page = {
			id:page.id,
			legal:page.pageLegal,
			costwidget:{
				dom:{
					intro:$('#cwIntro'),
					number:$('#widgetNumber'),
					copy:$('#widgetCopy'),
					results:$('#cwResults'),
					input:$('.cwInput')
				},
				label:page.costWidgetLabel,
				defopt:page.costWidgetDefault,
				items:page.costWidgetItems
				}
			
		};
	
	cache.navigation = {
			dom:{
				primarynav:$('#primaryNav'),
				eventsgroup:{
					nav:null,
					pnavsub:$('.pNavSub')
				}
			}
		};
	
	cache.navigation.dom.eventsgroup.nav = cache.navigation.dom.primarynav.find('li a');
	
	cache.tabs = {
		dom:{
			readmore:$('#readMore'),
			discovermore:$('#discoverMore')
		}
	};
	
	cache.ada = {
		
		dom:{
			adatoggle:$('#adaToggle'),
			adacontent:$('#adaContent'),
			flashcontent:$('#flashContent'),
			adamessage:$('#adaMessage')
		}
		
	};
	
	cache.footnotes = {
		dom:{
			footnote:$("#footnotes")
		}
	};
	
}

function setTabSwitch() {
	
	var c = cache.tabs.dom;
	
	c.readmore.bind('click',readMore);
	c.discovermore.bind('click',discoverMore);
	
	
}


function setADA() {
	
	var c = cache.ada.dom;
	
	c.adatoggle.click(function(){
		c.adacontent.removeClass('offLeft');
		c.flashcontent.css('display','none');
		c.adamessage.css('display','none');
		// turn off audio controls
		$('#audio-controls').css('display','none');
	});
	
}

function setNavigation() {
	
	var c = cache.navigation.dom.eventsgroup;
	
	c.nav
		.bind('mouseover focus', topnav_open)
		.bind('mouseout blur',  topnav_timer);

	c.pnavsub.focus(function(){
		navSubON(this);
	}).mouseover(function(){
		navSubON(this);
	}).blur(function(){
		navSubOFF(this);
	}).mouseout(function(){
		navSubOFF(this);
	});	


	c.nav.focus(function(){
		navON(this);
	}).mouseover(function(){
		navON(this);			
	}).mouseout(function(){
		navOFF(this);
	}).blur(function(){
		navOFF(this);
	});  
	
}


function setCostWidget() {
		
	
	var	c		= cache.page.costwidget,
		cdom		= c.dom,
		label 		= c.label, // default intro text
		defopt 		= c.defopt, // default dropdown value
		items		= c.items,
		id 		= cache.page.id,
		output		= "",
		dom		= {
					intro:cdom.intro,
					number:cdom.number,
					copy:cdom.copy,
					results:cdom.results,
					input:cdom.input
				};
		
	
		
	dom.intro.html(label);
	

	if(defopt === ""){
		dom.intro.css('top','5px');
		if(items[0].VALUE !== ""){

			strNumber = items[0].VALUE;
			strNumber = strNumber.replace("00","<sup>00</sup>");
			dom.number.html(strNumber);
			dom.copy.html(items[0].DESCRIPTION);
			dom.number.css('display','block');
			dom.copy.css('display','block');
			dom.intro.css('display','none');				
			
		
		}
		
		
		if(id == "104"){ // exception for page 104
			dom.results.css('display','block');
		}
	}else{
		dom.input.css('display','block');
	

		output = "<form name=\"costWidget\" action=\"javascript:updateWidget();\" method=\"post\">";
		//output += "<label for=\"cwQuestion\" class=\"offLeft\">label1</label>";

		output += "<select id=\"cwQuestion\"><option value='0'>" + defopt +"</option>";

		// get dropdown values
		
		for(var i=0, l=items.length;i<l;i++){
			output += "<option value=\"" + items[i].OPTION + "\">" + items[i].OPTION + "</option>";
		}

		output += "</select>";
		output += "<input type=\"image\" src=\"../../assets/img/btn_costWidget.jpg\" alt=\"Change\" class=\"btn\" />";
		output += "</form>";
		//;
		dom.input.html(output);
	}
}

function setFootNotes() {
		
		var legal = cache.page.legal;
		output = "";
		
		for(i=0,l=legal.length;i<l;i++){
			output += "<div class=\"fnData\" id=\"fn_" + legal[i].ID + "\"><sup>" + legal[i].ID + "</sup><span>" + legal[i].INNER + "</span></div><div class=\"breaker\"></div>";
		}
		cache.footnotes.dom.footnote.html(output);
}

function setUpFlash() {
	
	var flashvars = {
			//_traceBox:true,
			_pageID:pageXML,
			_getPollResults: "http://64.106.216.174/BofA_PollFramework/GetPollResults.aspx",
			_homePage:homePage,
			_xmlLocStr: strRoot + "servlets/Qanda",
			//_xmlLocStr: "http://192.168.160.21:8080/feeEd/servlets/Qanda",
			_langStr: "en",
			_assetPathStr: strRoot + "assets/flash/",
			_initialPathStr: strRoot,
			_navigationPathStr: strRoot + "assets/flash/setup/",
			_playerPathStr: strRoot + "assets/flash/"
		};
		
		var params = {
			menu: "false",
			allowScriptAccess : "always",
			bgColor:"0xFF0000",
			wmode: "transparent"
		};

		//if we have a problem with calling this feeEducation, we can change it
		var attributes = {
			id: "feeEducation",
			name: "feeEducation"
		};

		if(homePage !== 'false'){  
		
			swfobject.embedSWF("assets/flash/shell.swf", "hpFlash", "990", "583", "9.0.47", "",flashvars,params,attributes);
			setUpAudioControls();
			cacheFlashMovie('feeEducation');
		}

		//if content Page

		if(cache.page.id.indexOf("00") == -1){
			swfobject.embedSWF("../../assets/flash/shell.swf", "sidebarMovie", "340", "593", "9.0.47", "",flashvars,params,attributes);
			setUpAudioControls();
			cacheFlashMovie('feeEducation');
			
		}
	
}


function setGlossery() {
	
	$('.glossaryLink').click(function(e){
	
			
		$('.glWrapperDiv').remove();
		
		
		strElement = $(this).attr("id");

		
		var strGlossaryId = $(this).attr("href");
		var arrGlossaryId = strGlossaryId.split("=");
		strGlossaryId = arrGlossaryId[1];
		
		
		arrTerms = page.phrases;
		for(var i=0;i<arrTerms.length;i++){
			
			if(strGlossaryId == page.phrases[i].id){

				strGlossaryBlock = 	"<span class='glWrapperDiv'>";
				strGlossaryBlock +=		"<span class='glTop'>";
				strGlossaryBlock +=			"<span class='def'>Definition</span>";
				strGlossaryBlock +=			"<span class='close'><a href='#' onclick='hideTerm("+ strGlossaryId +");return false;' class='closeDef'><img src='../../assets/img/ico-glossary-close.gif' width='13' border='0' alt='Close Definition' height='13'></a></span>";
				strGlossaryBlock +=			"<span class='breaker'></span></span><span class='glBody'>";
				strGlossaryBlock +=	page.phrases[i].pageTitle;
				strGlossaryBlock +=	"</span><span class='glBtm'>&nbsp;</span></span>";


			}

		}
		
		$("#pageFooter").append(strGlossaryBlock);


		var pos =   $("#" + strElement).offset();  
		var width = $("#" + strElement).width();
		var height = $("#" + strElement).height();
		
	 	

		$(".glWrapperDiv").css({"left":(pos.left + width)-104 + "px","top":pos.top+height + "px"});
		$(".glWrapperDiv").fadeIn("fast");
		$("#glPopup").css("border","1px solid #00f");
		$(".glBody").attr("tabindex","-1"); 
		$(".glBody").focus();


	}); 
	
		
	$('.glBody').blur(function(){
		$(".closeDef").focus();
	});  		
	
}


function setPageContent() {
	// all this is largely to support the 404 page or any other page that has a variable path from the root.
	var 	pc 	= page.content,
		output	= "",
		ap	= function() {var a; try { a = assetspath; } catch(e) { a = ""; }; return a; },
		reg	= /\<\%=WebLib\.contextPath\(request\)\%\>/g;
	
	//try { ap = assetspath;	} catch(e) { ap = ""; }
	
	output = pc.replace(reg,ap);
	
	$('#contentNode').html(output);
	
}


function setUpLinks() {
	
	var 	data 		= page.signup,
		output 		= "",
		lastword 	= /(\b\w+[.?!]?$)/i,
		libobj;
	
	for(var i=0,l=data.length;i<l;i++){
		
		if (data[i].INNER.length > 0) {
		
			libobj = eval("("+data[i].INNER+")");
			
			// there are to params required of the three possible: header, url and body
			// there are only two configurations supported right now: 1)header and url, 2) header and body
			
			libobj.heading = libobj.heading.replace(lastword, "<span class=\"nowrap\">$1</span>" );
			
			if (libobj.heading) { 
				output += "<li";
				if (libobj.url) {
					output += "><a class='external' title='Link opens a new window.' target='_blank' href='" + libobj.url + "'>" + libobj.heading + "&nbsp;<img src='../../assets/img/blue_square.gif' alt='New Window' height='12' width='13' border='0' /></span></a>";
					
				} else if (libobj.body) {
					output += " class='noLink'><p>" + libobj.heading + "</p>" + libobj.body;
				}
				output += "</li>";
				
			}
		}
	}
	
	
	if(page.id == "403"){$('#relatedLinks').css('display','none');}
	
	$("#relLinkList").html(output);
	
}




function getADAContent(){  

        var strQandA = "<a name='adaVersion' id='adaVersion'></a>";
        
        
        //servlets/Qanda?count=3&format=JSON
        $.getJSON( strRoot + "servlets/Qanda?format=JSON",function(json) {


                    $.each(json.questions,function(i,item) {

			strQandA += "<div class='qA'>";                    	
                      
                        qNa_Id 			= json.questions[i].id;
                        qNa_Title 		= json.questions[i].title;
                        qNa_questionText 	= json.questions[i].questionText;
                        qNa_questionAskedBy 	= json.questions[i].questionAskedBy;
                        qNa_answerText 		= json.questions[i].answerText;
                        qNa_answerLearnMoreURL	= json.questions[i].answerLearnMoreURL;
                        qNa_pageRefId		= json.questions[i].pageRefId;
                       
			strQandA += "<div><h2>" + qNa_questionAskedBy + "</h2>";                    	
			strQandA += "<p>" + qNa_questionText + "</p></div>";  
			strQandA += "<div><h2>Janet Answered</h2>";                    	
			strQandA += "<p>" + qNa_answerText + "</p></div>";  
			strQandA += "<p>" + qNa_answerLearnMoreURL + "</p>";  
                        strQandA += "</div>";    

                    });
		
		strQandA += "<p>Return to the <a href='index.jsp' id='adaToggle' class='adaLink'>Flash version</a>.</p>";

		$('#adaContent').html(strQandA);

	});

	
}

function navON(strElement){

		strElement = $(strElement).attr("class");
		switch (strElement){
			case "nav1":
			  $("#navBankFees a.nav1").css("background-position","0 0");
			  break;
			case "nav2":
			  $("#navCardFees a.nav2").css("background-position","-140px 0");
			  break;
			case "nav3":
			  $("#navPractices a.nav3").css("background-position","-302px 0");
			  break;
			case "nav4":
			  $("#navTools a.nav4").css("background-position","-480px 0");
			  break;
		}			
}

function navOFF(strElement){

		strElement = $(strElement).attr("class");
		switch (strElement){
			case "nav1":
			  $("#navBankFees a.nav1").css("background-position","0 -40px");
			  break;
			case "nav2":
			  $("#navCardFees a.nav2").css("background-position","-140px -40px");
			  break;
			case "nav3":
			  $("#navPractices a.nav3").css("background-position","-302px -40px");
			  break;
			case "nav4":
			  $("#navTools a.nav4").css("background-position","-480px -40px");
			  break;
		}			
}


function navSubON(strElement){

		strNav = $(strElement).parent().parent().parent().attr("id");
		//$("#" + strNav + " ul").removeClass("offLeft");
		switch(strNav){
			case "navBankFees":
				$("#navBankFees a.nav1").css("background-position","0 0");
				break;
			case "navCardFees":
				$("#navCardFees a.nav2").css("background-position","-140px 0");
				break;
			case "navPractices":
				$("#navPractices a.nav3").css("background-position","-302px 0");
				break;
			case "navTools":
				$("#navTools a.nav4").css("background-position","-480px 0");
				break;
		}
		checkPage();
}

function navSubOFF(strElement){

		strNav = $(strElement).parent().parent().parent().attr("id");
		//$("#" + strNav + " ul").addClass("offLeft");
		//topnav_timer();
		switch(strNav){
			case "navBankFees":
				$("#navBankFees a.nav1").css("background-position","0 -40px");
				break;
			case "navCardFees":
				$("#navCardFees a.nav2").css("background-position","-140px -40px");
				break;
			case "navPractices":
				$("#navPractices a.nav3").css("background-position","-302px -40px");
				break;
			case "navTools":
				$("#navTools a.nav4").css("background-position","-480px -40px");
				break;
		}
		checkPage();
}


function checkPage(){
	
	// Check where you are in page & set on state
	pageLevel = $('body').attr('id');

	if(pageLevel.indexOf("p1") === 0){
		$("#navBankFees a.nav1").css("background-position","0 0");
	}		
	
	if(pageLevel.indexOf("p2") === 0){
		$("#navCardFees a.nav2").css("background-position","-140px 0");
	}	

	if(pageLevel.indexOf("p3") === 0){
		$("#navPractices a.nav3").css("background-position","-302px 0");
	}	

	if(pageLevel.indexOf("p4") === 0){
		$("#navTools a.nav4").css("background-position","-480px 0");
	}
}


function readMore(){
	changeSection('readMore',page.id);
	cmCreateManualLinkClickTag(page.path,'ReadMoreTab',page.id);
	movieControls.control_panel.visibility.show();
	return false;

}

function discoverMore(){//explore more
	changeSection('discoverMore',page.id);
	cmCreateManualLinkClickTag(page.path,'ExploreMoreTab',page.id);
	movieControls.control_panel.visibility.hide();
	return false;

}

function updateWidget(){

	var	 dropdownIndex 	= document.getElementById('cwQuestion').selectedIndex,
		 dropdownValue 	= document.getElementById('cwQuestion')[dropdownIndex].value,
		 items		= cache.page.costwidget.items,
		 dom 		= {
					intro:cache.page.costwidget.dom.intro,
					number:cache.page.costwidget.dom.number,
					copy:cache.page.costwidget.dom.copy,
					results:cache.page.costwidget.dom.results
				};


	//;
	
	if(dropdownValue != "0"){
	
		if(dropdownIndex>=0){

			strNumber = items[dropdownIndex-1].VALUE;
			strNumber = strNumber.replace("00","<sup>00</sup>");

			dom.number.html(strNumber);
			if((strNumber == "No Charge")||(strNumber == "Varies")){
				//;
				dom.number.css('font-size','1.2em');
				dom.copy.css('top','-10px');
				
			}else{
				dom.number.css('font-size','2em');
				dom.copy.css('top','-20px');
			}

			dom.copy.html(items[dropdownIndex-1].DESCRIPTION);

			dom.number.css('display','block');
			dom.copy.css('display','block');
			dom.intro.css('display','none');
			dom.results.css('display','block');
		}
	}else{
		;
		$('#cwQuestion').focus();
	}
}

function topnav_open() {
	var nav = cache.navigation.dom.primarynav;
	topnav_canceltimer();
	topnav_close();
	
	strClass = $(this).attr('class');
	strID = $(this).attr('id');
	
	if ((strID === 'navBankFees') || (strClass === 'nav1')) {
		ddmenuitem = "#navBankFees ul";
		nav.css('background-position','0px -120px');
	}

	if ((strID === 'navCardFees') || (strClass === 'nav2')) {
		ddmenuitem = "#navCardFees ul";
		nav.css('background-position','0px -80px');
	}

	if ((strID === 'navPractices') || (strClass === 'nav3')) {
		ddmenuitem = "#navPractices ul";
		nav.css('background-position','0px -40px');
	}
	if ((strID === 'navTools') || (strClass === 'nav4')) {	
		ddmenuitem = "#navTools ul";
		nav.css('background-position','0px 0px');
	}
	$(ddmenuitem).removeClass("offLeft");
}

function topnav_close() {  

	$(ddmenuitem).addClass("offLeft");
	cache.navigation.dom.primarynav.css('background-position','0px -160px');
}

function topnav_timer() {  
	closetimer = window.setTimeout(topnav_close, timeout);
}

function topnav_canceltimer() {  
	if(closetimer)  {  
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}


function expandSidebar(movie){
	
	$("#tabPanel").css({width:944}).show('slide',{direction:'right'},500);

}

function collapseSidebar(movie){
	$("#tabPanel").css({width:346}).show('slide',{direction:'left'},500);
}



// new from main page

function pageAnimate(x){

	if (!cache.animate) {
		cache['animate'] = {
			sidebar:$('#sidebar'),
			tabnav:$('.tabNav'),
			feeed:$('#feeEducation'),
			cwinput:cache.page.costwidget.dom.input,
			orgwidth:$('#sidebar').width()
			
		};
	}
	
	var 	sidebar 	= cache.animate.sidebar,
		tabnav		= cache.animate.tabnav,
		feeed		= cache.animate.feeed,
		cwinput		= cache.animate.cwinput,
		width_open 	= 945,
		width_curr	= sidebar.width(),
		width_closed	= cache.animate.orgwidth;
		
	if (width_curr === width_closed) {
		
		cwinput.css('display','none');
		tabnav.css("display","none");
		
		sidebar.css({'background-image':'url(../../assets/img/bg_tabPanel_944.gif)', 'background-position':'-6px -27px'});
		feeed.width(width_open);
		sidebar.animate( { left: 0});
		
		
	} else if (width_curr === width_open) {
		
		sidebar.css({'background-image':'url(../../assets/img/bg_tabPanel.gif)','background-position':'0px 0px'});
		cwinput.css('display','block');
		tabnav.css("display","block");
		feeed.width(340);
		sidebar.animate( { left: "60em"});
	}
	
	
}


function changeSection(x,y){

	//;

	if (x == "discoverMore"){

		$('#container ul.tabNav').css('backgroundImage','url(../../assets/img/tabActive_2.gif)');
		$('li.tabA a').css('color','#666');
		$('li.tabB a').css('color','#2679cd');	
		
	}

	if (x == "readMore"){

		$('#container ul.tabNav').css('backgroundImage','url(../../assets/img/tabActive_1.gif)');
		$('li.tabA a').css('color','#2679cd');
		$('li.tabB a').css('color','#666');	

	}

	
	// talk to flash - toggle movie
	
	cache.flash.changeSection(x);


}



//add in the two function pageAnimate(_width) and changeSection(_id)
//flash will call these functions.

function cacheFlashMovie(movieName) {
	
	var swf = swfobject.getObjectById(movieName);
	
	if (!swf) {
		window.setTimeout(function(){cacheFlashMovie(movieName);},50);
	} else {
		cache.flash = swf;
		movieControls.setUp();
	}
		
	
}



function updateShellDemo(loc) {
	//;
	
	var assetPathStr = loc;
	var typeID = "watch";
	var directLink = "http://infocenter.bankofamerica.com/pay_bills/add_a_payee/demo";
	var ratingNum = 4;
	var chapterNum=0;
	var linkID = "279";
	cache.flash.sendToFlash(assetPathStr,typeID,directLink,linkID,ratingNum,chapterNum);
	cache.flash.focus();
    }


function hideTerm(id){

	$('.glWrapperDiv').fadeOut('fast');
	var strLinkFocus = "#gt_" + id;
	$(strLinkFocus).focus();

}

// Glossary Page

function showTerm(letter){

	
	$('.glTerms').each(
		function(){
		$(this).hide();
	});	
	$("#glTerms" + letter).fadeIn('show');
	
	
}


//deepLink into Flash movie

function loadTabContent(url){
	changeSection('discoverMore',cache.page.id);
	pageAnimate(938);
	//updateShellDemo('../../assets/flash/demos/Mobile/getting_started/book1_sign_in/demo/');
	updateShellDemo(url);
}


// Volume control
function setUpAudioControls() {
	
	var on_id 		= movieControls.onbtn.attr('id'), //  flashcontrol.on.attr('id'),
		off_id 		= movieControls.offbtn.attr('id'),
		volcontrol 	= movieControls.audioslider.attr('id');
				
		
	$('#audioslider').slider({
		orientation: 'vertical',
		value:100,
		slide:function(event,ui) { movieControls.movie.setVolume(ui.value); }
	});
	
	
	$('#audio-controls').bind('click',function(e){
			
			// when the <img> tag has no id and the img is the event target for a mouse click.
			// the <a> tag is the event target for the "enter" button.
			var src 	= $(e.target).attr('id') ? $(e.target) : $(e.target).parent('a'), 
				src_id 	= $(src).attr('id');
			
			if (e.target !== this) { //make sure we don't move the whole audio-controls panel for the following
				
				if (src_id === movieControls.onbtn.attr('id')) {
					e.preventDefault();
					movieControls.buttons.hide.on_button();
					movieControls.buttons.show.off_button();
					movieControls.movie.start();
					
				} else if (src_id === movieControls.offbtn.attr('id')) {
					e.preventDefault();
					movieControls.buttons.show.on_button();
					movieControls.buttons.hide.off_button();
					movieControls.movie.stop();
					
				} else if (src_id === movieControls.volcon.attr('id')) {
					e.preventDefault();
					movieControls.volume_slider.show.slider();
					movieControls.volume_slider.hide.button();
					movieControls.volume_slider.show.background();
					movieControls.volume_slider.control.focus();
					
				} else if (src_id === movieControls.volup.attr('id')) {
					e.preventDefault();
					movieControls.volume_slider.show.slider();
					movieControls.volume_slider.hide.button();
					movieControls.volume_slider.show.background();
					movieControls.volume_slider.volume.up();
				} else if (src_id === movieControls.voldn.attr('id')) {
					e.preventDefault();
					movieControls.volume_slider.show.slider();
					movieControls.volume_slider.hide.button();
					movieControls.volume_slider.show.background();
					movieControls.volume_slider.volume.down();
				}
			} else {
				e.preventDefault();
				movieControls.volume_slider.hide.slider();
				movieControls.volume_slider.show.button();
				movieControls.volume_slider.hide.background();
				
				
			}
		}
	).bind('focus', function(e){
		
		var 	src 	= $(e.target).attr('id') ? $(e.target) : $(e.target).parent('a'), 
			src_id 	= $(src).attr('id');
			
		if (e.target !== this) { 
				
			if (src_id === movieControls.onbtn.attr('id')) {
				
				movieControls.buttons.hide.on_button();
				movieControls.buttons.show.off_button();
				
			} else if (src_id === movieControls.offbtn.attr('id')) {
				
				movieControls.buttons.show.on_button();
				movieControls.buttons.hide.off_button();
				
			} 
		} 
		
	}).bind('blur',function(e){
		
		
		if (e.target !== this) { 
			
			movieControls.setUp();
			
		} 
		
	});
	
	
	
	$('#audioslider a').blur(function(e) {
		e.preventDefault();
		
		movieControls.volume_slider.hide.slider();
		movieControls.volume_slider.hide.background();
		movieControls.volume_slider.show.button();
		
		
	}).focus(function(e){
		e.preventDefault();
		
		movieControls.volume_slider.hide.button();
		movieControls.volume_slider.show.slider();
		movieControls.volume_slider.show.background();
		
	});
	
	
	$('#volumeup, #volumedown').bind('blur',function(e){
		
		e.preventDefault();
		
		movieControls.volume_slider.hide.slider();
		movieControls.volume_slider.hide.background();
		movieControls.volume_slider.show.button();
		
	});
	
	
}

//movie controls

function MovieControls() {
	
	var 	audioslider 	= $('#audioslider'),
			volume_control 	= $('#volume-control'),
			audio_controls 	= $('#audio-controls'),
			knobb 			= $('#audioslider a'),
			button_off		= $('#button-off'),
			button_on		= $('#button-on'),
			volumeup		= $('#volumeup'),
			volumedown		= $('#volumedown'),
			that			= this;

	this.onbtn 			= button_on;
	this.offbtn 		= button_off;
	this.audioslider 	= audioslider;
	this.volcon 		= volume_control;
	this.volup 			= volumeup;
	this.voldn 			= volumedown;
	
	this.volume_slider = {
		
		show:{
			slider:function(slider) {
				
				audioslider.css('left',0);
			},
			button:function(){
				
				volume_control.css('left',0);
			},
			background:function(){
				
				audio_controls.addClass('slider-background');
			}
			
		},
		
		hide:{
			slider:function(slider) {
				
				audioslider.css('left',-5000);
			},
			button:function(){
				
				volume_control.css('left',-5000);
			},
			background:function(){
				
				audio_controls.removeClass('slider-background');
			}
			
		},
		
		control:{
			focus:function() {
				
				knobb.focus();
			}
		},
		
		volume:{
			up:function() {
				var curvolume = that.movie.getVolume();
				if (curvolume <= 100) {
					curvolume = curvolume + 5;
				}
				that.movie.setVolume(curvolume);
			},
			
			down:function() {
				var curvolume = that.movie.getVolume();
				if (curvolume >= 0) {
					curvolume = curvolume - 5;
				}
				that.movie.setVolume(curvolume);
			}
		}
		
	};
	
	
	this.buttons = {
		
		show: {
			off_button:function() {
				
				button_off.css('left',40);
			},
			
			on_button:function(){
				
				button_on.css('left',40);
			}
		},
		
		hide: {
			off_button:function() {
				
				button_off.css('left',-5000);
			},
			
			on_button:function(){
				
				button_on.css('left',-5000);
			}
		}
		
	};
	
	this.movie = {
		stop:function(){
			cache.flash.jsStopVideo();
		},
		
		start:function(){
			cache.flash.jsStartVideo();
		},
		
		setVolume:function(l){
			
			that.audioslider.slider('option','value',l);
			cache.flash.jsVolume(l);
		},
		
		getVolume:function() {
			return that.audioslider.slider('option','value');
		},
		
		state:function() {
			var state;
			
			try {
				state = cache.flash.jsGetState();
				
				if (state === true) {
					state = "on";
				} else {
					state = "off";
				}
				
				return state;
				
			} catch(e) {
				
				window.setTimeout(that.movie.state,50);
			
			}		
		}
	};
	
	this.control_panel = {
		visibility:{
			show:function() {
				audio_controls.css('display','block');
			},
			
			hide:function() {
				audio_controls.css('display','none');
			}
		}
	};
	
	this.setUp = function() {
		var buttonstate =  that.movie.state();
		
		if (buttonstate) {
			//console.log("current state: " + buttonstate);
			if (buttonstate === "on") {
				that.buttons.hide.off_button();
				that.buttons.show.on_button();
			} else if (buttonstate === "off"){
				that.buttons.hide.on_button();
				that.buttons.show.off_button();
			}
		} else {
			window.setTimeout(function(){that.setUp();},50);
		}
	};
	
	
	
}


var movieControls = new MovieControls();


