$.expr[':'].icontains = function(obj, index, meta, stack){
return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
};

var ie = /MSIE/.test(navigator.userAgent); 

var site = {baseURL: "",playerLoaded: false, defaultVideo: null};

site.init = function(baseURL) {
	logger.log("site.init");
	this.baseURL = baseURL;
	
	$(".site-nav .subnav").hover(function() { $(this).children("div").show(); }, function() { $(this).children("div").hide(); });
};

site.buildVideoPlayer = function(id)
{
	logger.log("building player "+id);
	
	var url = this.baseURL + "/flowplayer/flowplayer-3.1.5.swf";
	flowplayer(id, {"src":url, 'wmode':'opaque', version: [9, 115], cachebusting: ie },{plugins:{controls:{ autoHide: "always", hideDelay:500} },onLoad: site.onPlayerLoaded});
	site.checkPlayerLoaded(id); 
};

site.checkPlayerLoaded = function() {
	if(!$f(0).isLoaded())
	{
		logger.log("player still not loaded");
		setTimeout(function() { site.checkPlayerLoaded();}, 500);
	}
	else
	{
		site.onPlayerLoaded();
	}
};

site.onPlayerLoaded = function(){
	logger.log("onPlayerLoaded");
	if(site.playerLoaded)
		return;
		
	site.playerLoaded = true;
	logger.log("player loaded");
	if(site.defaultVideo != null)
	{
		logger.log("Playing Queued Video: ",site.defaultVideo);
		logger.log($f(0));
		$f(0).play(site.defaultVideo);
		site.defaultVideo = null;
	}
};

site.homePage = {};
site.homePage.init = function()
{
	$(".homepage-content .hero-text, .homepage-content .homepage-list, .homepage-hero .big-link").hide(0);
	$(".left-panel-bg, .right-panel-bg").width(0);
	$(".left-panel-bg, .right-panel-bg").height(467);
	
	$(window).load(function()
	{
		var loadSpeed = "slow";
		if(ie)
			loadSpeed = 0;
			
		$(".homepage-content .hero-text").animate({opacity: 'show'}, loadSpeed, function()
		{
			$(".left-panel-bg, .right-panel-bg").animate({width: 221}, 'slow', function()
			{
				$(".homepage-content .homepage-list").animate({opacity: 'show'}, loadSpeed, function()
				{
					$(".homepage-hero .big-link").animate({width: 'show'}, 'slow');
				});
			}); // i could ski down this function lol.......
		});
	});
};

site.videoListPage = { selectedSuperSection:null, selectedSection: null, selectedSubSection: null, videoPlayerId: "videoPlayer"};

site.videoListPage.init = function(defaultSection) {
	logger.log("site.videoListPage.init");
	
	site.buildVideoPlayer(this.videoPlayerId);
	
	var firstChange = true;
	
	$.address.change(function(event) {
		logger.log("address.change", event);
		pageTracker._trackPageview("/honeywell-challenges.php"+event.value);
		if(event.value == "/")
		{			 
			site.videoListPage.playIntro();
			firstChange = false;
			return;
		}
		
		var parts = "";
		
		if(event.value.charAt(0) == "/")
			parts = event.value.substring(1).split("/");
		else
			parts = event.value.split("/");
		 
		if(parts.length == 1)
		{
			site.videoListPage.selectSuperSection(parts[0]);
			if(firstChange)
				site.videoListPage.playIntro();
		}
		else if(parts.length == 2)
			site.videoListPage.selectSection(parts[0], parts[1]);
		else if(parts.length == 3)
			site.videoListPage.selectSubSection(parts[0],parts[1],parts[2]);
			
		firstChange = false;
     });
	site.videoListPage.modifyBigLink();
	
	$(".video-list-container-content a").click(function() {
		$.address.value($(this).attr("href").split("#")[1]);
		return false;
	});

};

site.videoListPage.playIntro = function ()
{
	logger.log("site.videoListPage.playIntro");
	if(this.selectedSuperSection != null)
	{
		$("#"+this.selectedSuperSection+"-section-list").hide();
		$("a[href='#"+this.selectedSuperSection+"']").removeClass('selected');
		this.selectedSuperSection = null;
	}
	if(this.selectedSection != null)
	{
		$("#"+this.selectedSection).hide();
		$("a[href='#"+this.selectedSuperSection+"/"+this.selectedSection+"']").removeClass('selected');
		this.selectedSection = null;
	}
	
	if(this.selectedSubSection != null)
	{
		$("a[href='#"+this.selectedSuperSection+"/"+this.selectedSection+"/"+this.selectedSubSection+"']").removeClass('selected');
		this.selectedSubSection = null;
	}
	
	$(".subsection-list").hide();
	
	site.videoListPage.playVideo("Intro.f4v");
	
};

site.videoListPage.selectSuperSection = function(superSection)
{
	logger.log("site.videoListPage.selectSection",superSection);
	$("#"+superSection+"-section-list").animate({ width: 'show', opacity: 'show' }, 'fast');
	if(this.selectedSuperSection != null)
	{
		$("#"+this.selectedSuperSection).animate({opacity: 'hide'}, 'fast');
		$("#"+this.selectedSuperSection+"-section-list").animate({opacity: 'hide' },'fast');
		$("a[href='#"+this.selectedSuperSection+"']").removeClass('selected');
		//this.selectedSuperSection =  null;
	}
	
	if(this.selectedSection != null)
	{
		$("#"+this.selectedSuperSection+"-section-sublist").animate({width:'hide', opacity: 'hide' },'fast');
		$("#"+this.selectedSection).animate({opacity: 'hide' },'fast');
		$("a[href='#"+this.selectedSuperSection+"/"+this.selectedSection+"']").removeClass('selected');
		//this.selectedSection = null;
	}
	
	if(this.selectedSubSection != null)
	{
		$("a[href='#"+this.selectedSuperSection+"/"+this.selectedSection+"/"+this.selectedSubSection+"']").removeClass('selected');
		//this.selectedSubSection = null;
	}
	//$("#"+superSection+"-section-list").delay('fast').animate({opacity: 'show' }, 'fast');
	this.selectedSuperSection = superSection;
	
	$("a[href='#"+superSection+"']").addClass('selected');
	//site.videoListPage.changeBackground($(".section-list a[href='#"+superSection+"']").attr("bgpath"));
	site.videoListPage.modifyBigLink();
}

site.videoListPage.selectSection = function(superSection, section) {
	logger.log("site.videoListPage.selectSection",[superSection, section]);
	
	$("#"+superSection+"-section-sublist").animate({ width: 'show', opacity: 'show' }, 'fast');
	if(this.selectedSuperSection != superSection)
	{
		site.videoListPage.selectSuperSection(superSection);
	}
	
	if(this.selectedSection != null)
	{
		$("#"+this.selectedSuperSection+"-section-sublist ul").animate({ opacity: 'hide' }, 'fast');
		$("a[href='#"+this.selectedSuperSection+"/"+this.selectedSection+"']").removeClass('selected');
	}
	
	if(this.selectedSubSection != null)
	{
		$("a[href='#"+this.selectedSuperSection+"/"+this.selectedSection+"/"+this.selectedSubSection+"']").removeClass('selected');
		this.selectedSubSection = null;
	}
	
	$("#"+section).delay('fast').animate({ opacity: 'show' }, 'fast');
	this.selectedSection = section;
	
	$("a[href='#"+superSection+"/"+section+"']").addClass('selected');
	site.videoListPage.playVideo($(".section-list a[href='#"+superSection+"/"+section+"']").attr("video"));
	//site.videoListPage.changeBackground($(".section-list a[href='#"+superSection+"-"+section+"']").attr("bgpath"));
	site.videoListPage.modifyBigLink();
};

site.videoListPage.changeBackground = function(bgName){
	$(".video-list-container-bg").fadeOut("fast",function(){$(this).attr("src", site.baseURL+"/images/backgrounds/"+bgName)})
								 .load(function() { $(this).fadeIn(); });
};

site.videoListPage.selectSubSection = function(superSection, section, subSection) {
	logger.log("site.videoListPage.selectSubSection", [superSection, section, subSection]);
	if(this.selectedSuperSection != superSection)
	{
		site.videoListPage.selectSuperSection(superSection);	
	}
	
	if(this.selectedSection != section)
	{
		site.videoListPage.selectSection(section);
	}
	
	if(this.selectedSubSection != null)
	{
		$("a[href='#"+this.selectedSuperSection+"/"+this.selectedSection+"/"+this.selectedSubSection+"']").removeClass('selected');
	}
	
	this.selectedSubSection = subSection;
	$("a[href='#"+superSection+"/"+section+"/"+subSection+"']").addClass('selected');
	site.videoListPage.playVideo($(".subsection-list a[href='#"+superSection+"/"+section+"/"+subSection+"']").attr("video"));
	site.videoListPage.modifyBigLink();
};

site.videoListPage.playVideo = function(videoName)
{
	if(!site.playerLoaded)
	{
		logger.log("Player not yet loaded, queuing video:",videoName);
		site.defaultVideo = site.baseURL+"/videos/"+videoName;
	}
	else
	{
		$f(this.videoPlayerId).play(site.baseURL+"/videos/"+videoName);
	}
};

site.innovationListPage = {};

site.innovationListPage.firstPageLoad = true;

site.innovationListPage.init = function() {
	logger.log("site.innovationListPage.init");
	
	$(".innovations-container-header select").change(function(){
		var sector = $(".innovations-container-header #sector option:selected").text();
		var challenge = $(".innovations-container-header #challenge option:selected").text();
		$.address.value(sector+"/"+challenge);
		
		
	});	
	
	$.address.change(function(event) {
		 logger.log("address.change", event);
		 
		 var sector = "";
		 var challenge = "";
		 if(event.value == "/")
		 {			 
			 sector = "all";
			 challenge = "all";
		 }
		 else
		 {
			 var parts = event.value.split("/");
			 
			 sector = $(".innovations-container-header #sector option:icontains('"+parts[1]+"')").val();
			 challenge = $(".innovations-container-header #challenge option:icontains('"+parts[2]+"')").val();
		 }
		 
		 if(site.innovationListPage.firstPageLoad && sector=="all" && challenge=="all")
		 {
			 	
		 }
		 else
		 {
			 $(".innovations-container-header #sector").val(sector);
			 $(".innovations-container-header #challenge").val(challenge);
			 
			 $(".innovations-container-content").scrollTop(0);
				$(".innovations-container-content").fadeOut('fast',function(){
					$(".innovation").hide();
					$(".innovation:[sectors*='|"+sector+"|'][challenges*='|"+challenge+"|']").show();
					
					$(".innovations-container-content").fadeIn('fast');
				});
		 }
		 
		 site.innovationListPage.firstPageLoad = false;
    });
	
	$(".innovations-container-content .innovation").hover(function() { $(this).addClass("innovation_hover"); }, function() { $(this).removeClass("innovation_hover"); });
	
};

site.innovationPage = {videoPlayerId: "videoPlayer" };

site.innovationPage.init = function(videoURL) {
	logger.log("site.innovationPage.init");
	
	site.buildVideoPlayer(this.videoPlayerId);
	
	if(!site.playerLoaded)
	{
		logger.log("Player not yet loaded, queuing video:",videoURL);
		site.defaultVideo = site.baseURL+"/videos/"+videoURL;
	}
	else
	{
		$f(this.videoPlayerId).play(site.baseURL+"/videos/"+videoURL);
	}
	
};


var logger = {};

logger.log = function(message, obj)
{
	if(window.console && window.console.log)
	{
		
		if(obj)
			console.log(message,obj);
		else
			console.log(message);
	}
};

