var theFlashIsReady = false; // setting flash communication to not ready

$(document).ready(function(){
	
	var theLocation = window.location.href;
	var theSelection = theLocation.split("#");
	theSelection = theSelection[1];
	
	$(".expand_collapse_content").hide(); //collapse all bars by default
	
	if(theSelection == "why_were_different"){playVideo();} // play the why we're different video if it's in the url parameters
	else if(theSelection == "video_library"){openVidLibrary(theSelection)} // if there are any parameters in the url, go to openVidLibrary function
		
	var theVidLibraryCookie = $.cookies.get("vidLibraryStates"); // check for cookies
	var theFeaturedCookie = $.cookies.get("featured"); // check for cookies
	
	if(theFeaturedCookie){$('#tabs').tabs({ selected: 0 });} // if featured was last clicked, default to that
	else if(theVidLibraryCookie && !theFeaturedCookie){$('#tabs').tabs({ selected: 1 });} // if vid library was last clicked, default to that
	else{$('#tabs').tabs({ selected: 0 });} // if nothing was clicked, default to featured
	
	// loop through the cookie, and open the correct video library accordingly
	if(theVidLibraryCookie){
		var theCookieArray = theVidLibraryCookie.split("-");
		for (var i=0; i<theCookieArray.length; i++) {
			if(theCookieArray[i] == 'o'){
				$(".bar_nav").eq(i).attr('class', 'bar_nav open');
				$(".bar_nav").eq(i).next().show();
				var theClosedTitle = $(".bar_nav a").eq(i).attr("title");
				theClosedTitle = theClosedTitle.replace("Expand", "Collapse");
				$(".bar_nav a").eq(i).attr("title", theClosedTitle);
			}
			else if(theCookieArray[i] == 'c'){
				$(".bar_nav").eq(i).attr('class', 'bar_nav');
				$(".bar_nav").eq(i).next().hide();
			}
		}
	}

	/*$("#tabVidLibrary a").click(function(){
		getFlashMovieObject("hm_theater").sendToActionScript("0");
	});*/
	
	// expand and collapse bars on click
	$(".bar_nav a").click(
		function(event){
			$.cookies.set("openVidLibrary", "changed");
			var theID = $(this).attr("id") + "_content";
			if(($(this).parent().attr("class") == "bar_nav") || ($(this).parent().attr("class") == "bar_nav close")){
				var theTitle = $(this).attr("title");
				theTitle = theTitle.replace("Expand", "Collapse");
				$(this).attr("title", theTitle);
				event.preventDefault();
				$(this).parent().removeClass("close");
				$(this).parent().addClass("open");
				$("#" + theID).slideDown();
				setCookies("setVidLibrary");
				return;
			}
			
			if($(this).parent().attr("class") == "bar_nav open"){
				var theTitle = $(this).attr("title");
				theTitle = theTitle.replace("Collapse", "Expand");
				$(this).attr("title", theTitle);
				event.preventDefault();
				$(this).parent().removeClass("open");
				$(this).parent().addClass("close");
				$("#" + theID).slideUp("normal");
				setCookies("setVidLibrary");
				return;
			}
		}
	);
	
	// set cookies on click
	$("#tabVidLibrary a").click(function(){setCookies("setVidLibrary");});
	$("#tabFeatured a").click(function(){setCookies("featured");});
	
	// scroll up to top and call out to flash when anchors with a rel of 'flashtheater' are click
	var theHref;
	$("a[rel='flashtheater']").click(function(e){
		theHref = $(this).attr('href');
		theHref = theHref.substr(1);
		$.scrollTo('0', 300, function(){
			callFlash(theHref);
		});
		e.preventDefault();
		dynamictracker("/flashtheater/slideshow/autoplay/" + theHref);
	});
	
	$("a[rel='slideshow']").click(function(event){
		theHref = $(this).attr('href');
		theHref = theHref.substr(1);
		if(theFlashIsReady){
			$.scrollTo('0', 300, function(){
				callFlash(theHref);
			});
		}
		dynamictracker("/flashtheater/slideshow/autoplay/" + theHref);
		event.preventDefault();
	});
	
	
	// open the correct bar, or play correct video when anchors with a class of 'open_video_library' are clicked
	$("a.open_video_library").click(function(e){
		e.preventDefault();
		var theHref = $(this).attr("href");
		theHref = theHref.substr(1);
		if(theHref == "why_were_different"){
			playVideo();
		}
		else{openVidLibrary(theHref);}
	});
});

function setCookies(type){
	$.cookies.del("featured");
	switch (type){
		case "setVidLibrary":
			var theCookieString = "";
			$(".bar_nav").each(function(i){
				if(this.className == "bar_nav open"){
					theCookieString += "o-";
				}
				else{theCookieString += "c-";}
			});
			$.cookies.set("vidLibraryStates", theCookieString);
		break;
		
		case "featured":
			$.cookies.set("featured", "open");
		break;
	}
}

function checkForFlash(){
	setTimeout(playVideo, "100");
}

function playVideo(){
	if(!theFlashIsReady){setTimeout(checkForFlash, "100");}
	else{callFlash("300");}
}

function callFlash(itemToCall){
	getFlashMovieObject("hm_theater").sendToActionScript(itemToCall);
} 
	
function openVidLibrary(libraryToOpen){
	var theLocation = window.location.href;
	setCookies("setVidLibrary");
	var $tabs = $('#tabs').tabs();
	$tabs.tabs('select', 1);
	if(libraryToOpen == "video_library" && theLocation.indexOf("video_library") != -1){
		window.location.href = "index.htm";
	}
	if(libraryToOpen != "video_library"){
		var theClosedTitle = $(".bar_nav a").attr("title");
		theClosedTitle = theClosedTitle.replace("Collapse", "Expand");
		$(".bar_nav a").attr("title", theClosedTitle);
		$(".bar_nav").attr("class", "bar_nav");
		$(".expand_collapse_content").hide();
		$("#" + libraryToOpen + " .bar_nav").addClass("open");
		$("#" + libraryToOpen + " .bar_nav a")
		$("#" + libraryToOpen + " .expand_collapse_content").show();
		var theOpenTitle = $("#" + libraryToOpen + " .bar_nav a").attr("title");
		theOpenTitle = theOpenTitle.replace("Expand", "Collapse");
		$("#" + libraryToOpen + " .bar_nav a").attr("title", theOpenTitle);
		$.scrollTo('485', 300);
		getFlashMovieObject("hm_theater").sendToActionScript("0");
	}
}

// function for communicating with flash
function getFlashMovieObject(movieName){
  if (window.document[movieName]){
	return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1){
	if (document.embeds && document.embeds[movieName])
	  return document.embeds[movieName];
  }
  else {
	return document.getElementById(movieName);
  }
}

// funcion to check if flash is ready to be communicated with
function flashReady(value){
	theFlashIsReady = true;
}

function riaTrack(eventName, eventType, eventId) {
	//alert("riaTrack: "+eventName);
	// NOTE: used for Rich Internet Application Tracking - FlashTrackAction and AJAX Track
	if (typeof(window["urchinTracker"])=="function") urchinTracker(eventName);
}
