$(function(){
	saveHistory(smilePath); //add this page to the user's history
	
	$("#content .copy p").add("#content .copy table td:odd").add("#content .copy li").wrapInner("<span></span>");//wrap body copy in span to give background color
	
	if($.inArray(smilePath, getSmiles()) == -1 && smilePath.indexOf('smile/') == -1){
		//if the page isn't in the user's smiles, show the button and add the click handler
		$("#content .smile").click(function(){			
			saveSmile(smilePath);			
			if(pageTracker){pageTracker._trackEvent("Smile", "add_smile", smilePath)} //GA tracking
			logOCSale('isconv=1|pover=Action|itemcount=1|itemvalue=0.00|m1='+smilePath); //DGM tracking
			$(this).slideUp("fast");
			return false; 
		}).slideDown("fast");
	}	

	//add tracking to download Brochure link
	$('.download a').click(function(){
		if(pageTracker){
			pageTracker._trackPageview('/downloads/brochure');
		}
		logOCSale('isconv=1|pover=brochure|itemcount=1|itemvalue=0.00|m1=download_brochure');
	});


	if( swfobject.hasFlashPlayerVersion( reqFlashVersion ) ) 
	{
		//FLASH VERSION OF SITE
   		swfobject.embedSWF( baseFlash + "contentnav.swf?noCache=" + Math.round(Math.random() * 65536), "flashNav", "420", "100%", reqFlashVersion, "/flash/expressInstall.swf", flash_vars, flash_params, flash_attributes);
   		swfobject.embedSWF( baseFlash + "themeloader.swf?noCache=" + Math.round(Math.random() * 65536), "flashTheme", "315", "100%", reqFlashVersion, "/flash/expressInstall.swf", flash_vars, flash_params, flash_attributes);
   		if($("#videoPlayer").length) 
		{		
			var vp_flash_vars = $.extend(flash_vars, {
				video: videoPath, 
				image: imagePath,
				themeColor: playerColor
			});
	   		swfobject.embedSWF( baseFlash + "videoplayer.swf?noCache=" + Math.round(Math.random() * 65536), "videoPlayer", "336", "224", reqFlashVersion, baseFlash + "expressInstall.swf", vp_flash_vars, flash_params_video, flash_attributes);
	  	}
	  	if($("#contentSwf").length && (typeof contentSwfPath !="undefined")) 
		{	
			var cs_flash_params = $.extend(flash_params, {
				allowFullScreen: "true"
			});	
			swfobject.embedSWF(contentSwfPath + "?noCache=" + Math.round(Math.random() * 65536), "contentSwf", "336", "240", reqFlashVersion, "/flash/expressInstall.swf", flash_vars, cs_flash_params, flash_attributes);
	  	}

	  	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			//add fix for fixed position of IE6
			$("#flashNavContainer").fixedPosition();
			$("#flashThemeContainer").fixedPosition();
		}
   	
   	}
   	else {
	   	//HTML VERSION OF SITE
	   	$("html").addClass("noFlash");
	
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			//add fix for fixed position of IE6
			$("#promo").fixedPosition();
		}
		
		setUpNav();
		initNav();		
	}	
	
	
	$.get(basePath + "assets/data/mainnav.xml", function(data){ 
		var userHistory = getHistory();

		var menu = $(data).find("item>url").parent("item"); //get the collection of items with urls (faster than item:has(>url) )

		var unvisited = $.grep(menu, function(item){
			//remove visited pages from the array
			return ($.inArray($(item).find("url").text().replace("-", "_"), userHistory) == -1)
		});

		unvisited.sort(function(a,b){
			//sort the array based on rank
			rankA = parseInt($(a).find("rank").text());
			rankB = parseInt($(b).find("rank").text());		
			return (rankA < rankB) ? -1 : (rankA > rankB) ? 1 : 0;
		})
		
		var recommended = $(unvisited[0]);	//the highest ranked, unvisited item	
		
		$("#content .checkOut a")
			.attr("href", basePath + recommended.find("url").text().replace(/_/g, "-"))
			.text(recommended.find("title").text())
			.click(function(){
				if(pageTracker){
					//track clicks on the recommended link
					pageTracker._trackEvent("Check_Out", "click_recommended", recommended.find("url").text().replace(/_/g, "-"))
				}
			})
			.parent(".checkOut").fadeIn();
		//populate the link path and title, fade in the checkout section

		if($("html").hasClass("noFlash")){		
			buildSmileMenu(menu);
		}
	});
});

function getSmileCookie(){
	var smiles = $.cookie(smileCookie);
	$("#flashNav").get(0).sendSmileToFlash(((smiles)? smiles : -1));
}

