﻿/**
* Copyright (c) 2007, Impact Proximity
* All rights reserved.
* @author Jerome Conde <j.conde@impactbbdo.ae>
* @version 1.0.1
*/
var OpenLightBox = null;
$(document).ready(function(){

	//global vars
	var location = window.location;
	var locale = "en_gb";
	if(location.href.indexOf("ar_ae") > -1) locale = "ar_ae";
	var hostname = location.protocol+"//"+location.hostname+(location.port ? ":"+location.port : ""); 
	
	var isIE6 = $.browser.msie && $.browser.version == "6.0";
	var $page = $(".Page");
	var scenes = "Home Music Football Brands Games Express";
	var currScene;
	var prevScene = $.trim($(".Scene").attr("class").split(" ")[1]);
	var $crumb = $("ul.BreadCrumb li");
	if($crumb.size() < 3) currScene = $.trim($("ul.BreadCrumb li:last").text());
	else currScene = $.trim($("ul.BreadCrumb li:eq(1)").text());
		currScene = currScene.substring(0,1).toUpperCase() + currScene.substring(1);
		if(currScene == "Default") currScene = "Home";
	
		
	/** background issue */
	$page.css({backgroundImage:"none"});
	if($crumb.size() > 1)
	$page.css({backgroundImage:"url(../Common/Image/Background/MainBackground.jpg)"});
	else
	$page.css({backgroundImage:"url(Common/Image/Background/MainBackground.jpg)"});
	
	/** IE background flickering */
	if(isIE6) document.execCommand("BackgroundImageCache",false,true);

	/**
	* @desc Add FirstChild/LastChild class for ie
	*/
	var FirstAndLastChild = function() {
		if($.browser.msie || $.browser.opera){
			$("li:first-child").addClass("FirstChild");
			$("li:last-child").addClass("LastChild");
		}
		if($.browser.safari){
			$("li:last-child").addClass("LastChild");
		}
	}
	FirstAndLastChild();
	
	/**
	* @desc hover effect for all caption images inside Content
	*/
	var hoverImage = function(){
		var hoverImages = "div.Content .Survey a img";
		hoverImages += ",div.Content .Survey a img";
		hoverImages += ",div.Register a img";
		hoverImages += ",div.PepsiBrands li a img";
		hoverImages += ",div.PepsiBrands2 li a img";
		hoverImages += ",ul.Ringtone li";
		hoverImages += ",ul.Navigation2 li a img";
		hoverImages += ",div.Content input[type=image]";
		$(hoverImages).bind("mouseover",function(){
			$(this).css({opacity:.6});
		})
		.bind("mouseout",function(){
			$(this).css({opacity:1});
		})
		;
	}
	hoverImage();
	
	/**
	*	@desc Default text input value handler
	*/
	var autoClear = function(){
		$("input.SearchInput")
		.bind('click',function(){
			if(this.value == "search") this.value = "";
		})
		.bind('blur',function(){
			if(this.value == "") this.value = "search";
		})
		;
	}
	//autoClear();
	

	//Overlay
	
	OpenLightBox = function(){
	  var $page = $("body"), _pageH = $page.height();
	  var $ovr = $("<div class='OverlayMask' />").height(_pageH).css({opacity:.5}).appendTo($page);
	  var $canvas = $("<div class='Overlay' />").width(638).height(368).css({marginLeft:(638/2)*-1,marginTop:(368/2)*-1});
	  var $close = $("<span class='Close'>X</span>");
	  var $flashMovie = $("<div class='WrapVideo' />").css({width:'auto',margin:'0 10px',height:385}).append("<div id='VideoPlayer' />")
	  var $minBox = $("<div class='CurveCorner' />");
	  $canvas.appendTo($page);
	  $close.appendTo($canvas);
	  $minBox.appendTo($canvas);
	  $flashMovie.appendTo($minBox);
		$close.click(function(){
			$canvas.remove();
			$ovr.remove();
		});
		$close.insertAfter($flashMovie);
	  if(locale == "en_gb") {
	  var loader = new SWFObject('Common/Flash/Media/gallery_en_gb.swf?1', "PreLoader", "638", "368", "8", "#ffffff");
		  loader.addParam("wmode", "transparent");
		  loader.addParam("scale", "noscale");
		  loader.addVariable("thumbnails_Path","Common/Flash/Media/thumbnails.xml?1");
		  loader.addVariable("images_Path", "Common/Flash/Media/images.xml?1");
		  loader.write("VideoPlayer");
	   }
	   
	   if(locale == "ar_ae") {
	  var loader = new SWFObject('Common/Flash/Media/gallery_ar_ae.swf?1', "PreLoader", "638", "368", "8", "#ffffff");
		  loader.addParam("wmode", "transparent");
		  loader.addParam("scale", "noscale");
		  loader.addVariable("thumbnails_Path","Common/Flash/Media/thumbnails.xml?1");
		  loader.addVariable("images_Path", "Common/Flash/Media/images.xml?1");
		  loader.write("VideoPlayer");
	   }
	   
	   
	   
	   
	  //var flashvars = {}, params = {wmode:'opaque',allowScriptAccess:'always',allowFullScreen:true}, attributes = {};
 	  //swfobject.embedSWF("../Flash/Media/gallery.swf", "VideoPlayer", "530", "322", "9.0.0","expressInstall.swf", flashvars, params, attributes);

	}
	
	

	
	
	
	/**
	* @desc Navigation0 handler
	*/
	var nav0 = function(){
		$("ul.Navigation0 li a").hover(
			function(){
				var bgImage = $(this).parent().css("backgroundImage");
				$(this).parent().css({backgroundImage:bgImage.replace(".gif","Active.gif")});
			},
			function(){
				var bgImage = $(this).parent().css("backgroundImage");
				$(this).parent().css({backgroundImage:bgImage.replace("Active.gif",".gif")});
			}
		);
	}
	nav0();
	
	/**
	* @desc change handler for SWFAddress
	*/
	function changeHandler(Event){
		var eventValue = Event.value.toLowerCase();
		if(eventValue == "/") return;
		var hashLength = eventValue.split("/").length;
		if(hashLength < 3 && currScene == "Home") return;
		var cell1 = eventValue.split("/")[1];
		/** pascal case cell1 as fix for small caps urls */
		cell1 = cell1.substring(0,1).toUpperCase() + cell1.substring(1);
		var cell2 = eventValue.split("/")[2];
		var $scene = $("." + cell1);
		if(hashLength < 3) $scene = $(".Home");
		var targetURI = location.href.replace(location.pathname,eventValue).split("#")[0];
		if(cell2 && cell2.indexOf("default") < 0 && hashLength > 2){
			loadProfile($scene,targetURI,function(){
				pageTracker._trackPageview(eventValue);
			});
		}
		else if(cell2 && cell2.indexOf("default") > -1 && cell1==currScene && $scene.find(".ContentLoaded").length){
			unloadProfile($scene,function(){
				if($scene.find(".Cans").length)
					$scene.find(".Cans").animate({opacity:1},500);
			});
		}
		else{
		 loadSection($scene,targetURI,function(){
			pageTracker._trackPageview(eventValue);
		 });
		}
	}
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, changeHandler);

	/**
	* @desc change the url hash value
	*/
	var setHash = function(targetURI){
		var hashValue = targetURI;
		hashValue = hashValue.replace(hostname,"");
		hashValue = hashValue.replace(hostname.replace("www.",""),"");
		hashValue = hashValue.replace(/^\.\./g,"");
		if(hashValue.charAt(0) != "/") hashValue = "/"+hashValue;
		if(hashValue.split("/").length < 3 && hashValue.toLowerCase().indexOf("default") > -1 && currScene=="Home") return;
		SWFAddress.setValue(hashValue);
	}
	
	/**
	* @desc Assign event handlers to main navigation links
	*/
	$("ul.Navigation2 li a").unbind().bind("click",function(){
		var targetURI = $(this).attr("href");
			if(!$(this).parent().is(".Locale")){
				setHash(targetURI);
			}
			else{
				$("embed,object").remove();
				var urlHash = SWFAddress.getValue() != "/" ? SWFAddress.getValue() : location.href;
				if(locale == "ar_ae")
				location.href = urlHash.replace(/ar_ae/g,"en_gb");
				if(locale == "en_gb")
				location.href = urlHash.replace(/en_gb/g,"ar_ae");
			}
		this.blur();
		return false;
	})
	;
	

	/** sucker fish drop down */
	$("ul.Navigation2 > li").hover(
		function(){$("ul",this).css({display:'block'})}, 
		function(){$("ul",this).css({display:'none'})}
	);

	
	
	/**
	* @desc Assign handler to the main logo
	*/
	$("h1 a").bind('click',function(e){
		if(currScene != "Home")
		setHash($("ul.Navigation2 li a:first").attr("href"));
		if(e.preventDefault) e.preventDefault();
	})
	;
	
	/**
	* @desc Distribute cloned header to different scenes
	*/
	var $header = $(".Scene div.Header").clone(true);
	function createHeader(){
		var arrScenes = scenes.split(" ");
		for(var i=0,j=arrScenes.length;i<j;i++){
			if(arrScenes[i] != currScene) {
				cloneHeader(arrScenes[i]);
			}
		}
		/** show active menu image */
		$("."+currScene).find(".Navigation2 a[rel="+currScene+"] img")
		.each(function(){
			if(this.src.indexOf("Active.gif") < 0)
				this.src = this.src.replace(".gif","Active.gif");
		})
		;
	}
	
	/**
	* @desc Clone header block
	*/
	function cloneHeader(section){
		var header = $header.clone(true);
			header.find(".Navigation2 img").each(function(){
				this.src = this.src.replace("Active.gif",".gif");
			});
			header.find(".Navigation2 a[rel='"+section+"'] img").each(function(){
				this.src = this.src.replace(".gif","Active.gif");
			});
		$("<div></div>")
		.addClass("Scene "+section)
		.appendTo($page)
		.append(header)
		;
	}
	
	/**
	* @desc reposition page view on window resize
	*/
	$(window).resize(function(){
		positionPage();
	});
	
	/**
	* @desc apply class to dynamically rendered html
	*/
	var applyClass1 = function(){
		FirstAndLastChild();
		hoverImage();
		invokePopup();
		externalLinks();
		/** hide alternative images */
		if(hasFlash){
			$(".QuickLink img,.SpotLight img,.HomeMovie img")
			.css({display:'none'})
		}
	}
	var externalLinks = function(){
		$("a[rel=external]").bind("click",function(){$(this).attr("target","_blank")});
	}
	externalLinks();

	var getFlashMovie = function(movieName) {
			var isIE = navigator.appName.indexOf("Microsoft") != -1;
			return (isIE) ? window[movieName] : document[movieName];
		}
	/**
	* @desc loader function
	*/
	var preLoader = function(fn){
		var ajaxloader = "Common/Image/Generic/ajax-loader.gif";
		if($crumb.length > 1) ajaxloader = "../Common/Image/Generic/ajax-loader.gif";
		var swfloader = "Common/Flash/Preloader.swf";
		if($crumb.length > 1) swfloader = "../Common/Flash/Preloader.swf";
		$("<div></div>").addClass("XhrLoader")
		//.css({background:'#000 url('+ajaxloader+') 50% 50% no-repeat',width:$(window).width()+'px',height:$(window).height()+'px'})
		.css({background:'#000',width:$(window).width()+'px',height:$(window).height()+'px'})
		.css({position:'absolute',left:'0px',top:'0px',opacity:0,zIndex:100})
		.appendTo($("body"))
		//.animate({opacity:.8},4000,fn)
		;
		/** freeze home flash */
		/*if(currScene == "Home" && $("#h1SWF").find("object,embed").length && typeof getFlashMovie("HomeVideoPlayer").removeFlash =="function"){
			getFlashMovie("HomeVideoPlayer").removeFlash();
			getFlashMovie("HomeSpotLight").removeFlash();
			setTimeout(function(){$(".XhrLoader").animate({opacity:.6},4000,fn)},700);
		}
		else*/ 
		setTimeout(function(){$(".XhrLoader").animate({opacity:.6},2000,fn)},700);
		
		$("<div></div>").attr("id","FlashLoader").css({width:'120px',height:'120px'})
		.css({position:'absolute',left:'50%',top:'50%',zIndex:101,marginLeft:'-60px',marginTop:'-60px'})
		.appendTo("body")
		;
		$("<img>").attr("src",ajaxloader).css({marginTop:'35px',marginLeft:'35px',opacity:.7})
		.appendTo("#FlashLoader")
		;
		var loader = new SWFObject(swfloader, "PreLoader", "120", "120", "8", "#ffffff");
		  loader.addParam("wmode", "transparent");
		  loader.addParam("scale", "noscale");
		  loader.write("FlashLoader");
	}
	 
	var postLoader = function(fn){
		$("#FlashLoader").remove();
		$(".XhrLoader").animate({opacity:0},1000,function(){
			fn();
			$(this).remove();
		});
	}
	
	/**
	* @desc Position the page on load based on bread crumb values
	*/
	var positionPage = function(){
		var $currScene = $("."+currScene);
		var x = parseInt($currScene.css('left'));
		var y = parseInt($currScene.css('top'));
		var X = x-($(window).width() - $currScene.width())/2;
		var Y = y-($(window).height() - $currScene.height())/2;
		
		if($(window).width() < 1024 || $(window).height() < 600){
			if(currScene == "Home") x += 100;
			if(currScene == "Music") x += 100;
			X = x; Y = y;
		}
		//$page.css({left:-X+'px',top:-Y+'px'});
		$page.animate({left:-X+'px',top:-Y+'px'},500);
	}
	
	/**
	* @desc Pan the canvas
	*/
	var pan = function($scene,fn){
		var x,y;
		x = parseInt($scene.css('left'));
		y = parseInt($scene.css('top'));
		if(!(x && y)) return;
		var wWidth = ($(window).width() - $scene.width())/2;
		var wHeight = ($(window).height() - $scene.height())/2;
		var X = x-wWidth;
		var Y = y-wHeight;

		/** set current and previous scene */
		prevScene = currScene;
		currScene = $.trim($scene.attr('class').split(" ")[1]);
		/** Hide home header and side bar during panning */
			var homeHeader = $(".Home .Header");
			var homeSideBar = $(".Poll,.Survey,.Register,.SpotLight",$(".Home"));
			homeHeader.css({left:'-9999px'});
			homeSideBar.css({opacity:0});
		if(currScene == "Home") {
			homeHeader.css({left:'auto'});
			homeSideBar.css({opacity:1});
		}
		/** unplug accordion swf video player before going to any section */
		$("ul.Accordion li div.VideoPlayer").css({display:'none'})
		.find(".Flash").find("embed,object").remove();
		;

		$page.animate({left:-X+'px',top:-Y+'px'},2500,'easeOutCubic',function(){
			homeHeader.css({left:'auto'});
			homeSideBar.css({opacity:1});
			if($(window).width() < 1024 || $(window).height() < 600){
				positionPage();
			}
			if(fn) fn();
		});
	}
	
	/**
	* @desc loadSection routine
	*/
	
	var loadSection = function($scene,targetURI,fnTrack){
		/**
		* prohibit safari 2.0.x in executing ajax request
		*/
		if($.browser.safari && $.browser.version < 500){
			window.location = targetURI;
			return;
		}
		rollerFlag = false;
		preLoader(function(){
			$scene.find(".ContentLoaded").remove();
			if($scene.find(".ProfileContent").length){
				unloadProfile($scene);
			}
			var contentLoaded = false;
			if(!contentLoaded){
				/** Load page through ajax */
				$.get(targetURI,function(raw){
					if(typeof fnTrack=="function"){
						fnTrack();
					}
					var rawHtml;
					if($crumb.length < 2) rawHtml = raw.replace(/\.\.\//g,"");
					else rawHtml = raw;
					if($scene.hasClass("Home")){
						//rawHtml = raw.replace(/Common\//g,"../Common/");
						rawHtml = rawHtml.replace(/href="/g,"href=\"../");
					}
					if(!$scene.find(".Content").size()){
						$("<div></div>")
						.addClass("Content").addClass("ContentLoaded")
						.html($(rawHtml).find(".Content").html())
						.appendTo($scene)
						;
					}
					else{
						$scene.find(".Content")
						.html($(rawHtml).find(".Content").html())
						.addClass("ContentLoaded")
						.removeClass("ProfileContent")
						;
						initScenes($scene);
					}
					
					/** set title bar */
					var title = rawHtml.match(/(\<title\>)(.+)(\<\/title\>)/);
					if(title[2]) title = title[2];
					SWFAddress.setTitle(title);
					
					var fnToExecute = function(){
						applyClass1();
						initScenes($scene);
						pan($scene,function(){
							attachHandlers($scene);						
							renderFlashContent($scene);
						});
						
					};
					postLoader(fnToExecute);
					
				});
			}
			else{ /** if content is already loaded */
				postLoader(function(){
						applyClass1();
						initScenes($scene);
						pan($scene,function(){
							attachHandlers($scene);
							renderFlashContent($scene);
						});
					});
			}
		});
	}
	
	var loadProfile = function($scene,targetURI,fnTrack){
		/**
		* prohibit safari 2.0.x in executing ajax request
		*/
		if($.browser.safari && $.browser.version < 500){
			window.location = targetURI;
			return;
		}
		preLoader(function(){
				$scene.find(".ProfileContent").fadeOut(2500,function(){$(this).remove()});
				rollerFlag=true;
				/** Load page through ajax */
				$.get(targetURI,function(raw){
				
				if(typeof fnTrack=="function"){
						fnTrack();
					}
					
					var rawHtml;
					if($crumb.length < 2) rawHtml = raw.replace(/\.\.\//g,"");
					else rawHtml = raw;
					if($scene.hasClass("Home")) rawHtml = raw.replace(/Common\//g,"../Common/");
					var $profileHtml;
					
					if(!$scene.find(".ContentLoaded").length){
						$(rawHtml).find(".Content")
						.addClass("ProfileContent")
						.find(".Accordion li:not(:first) .Expand").css({display:'none'}).end()
						.appendTo($scene)
						.find("DIV")
						.filter(".FootballProfile,.MusicProfile,.Specification,.PepsiBrand,.PepsiBrands,.GamesContent,.LatestVideosExpand")
						.css({display:'none'})
						;
					}
					else {
						$profileHtml = $(rawHtml).find(".Content").find("DIV")
						.filter(".FootballProfile,.MusicProfile,.Specification,.PepsiBrand,.PepsiBrands,.GamesContent,.LatestVideosExpand")
						.css({display:'none'})
						;
						$("<div></div>")
						.addClass("Content")
						.addClass("ProfileContent")
						.html($profileHtml)
						.find(".Accordion li:not(:first) .Expand").css({display:'none'})
						.end()
						.appendTo($scene)
						;
					}
					
					/** set title bar */
					var title = rawHtml.match(/(\<title\>)(.+)(\<\/title\>)/);
					if(title[2]) title = title[2];
					SWFAddress.setTitle(title);
					
					/*if(!$(".ContentLoaded",$scene).length){
						$scene.find(".Content")
						.append($(rawHtml).find(".Content").find(".PepsiBrands2,.Poll,.QuickLink")).end()
						.append($(rawHtml).find(".Content").find(".QuickLink").next()) //get the <script> block after .QuickLink div
						.find(".QuickLink").css({display:'block'}).end()
						;
					}*/
						
						//if($scene.find(".PepsiBrand,.BrandsContent").length) $scene.find("ul.Cans").animate({opacity:0},500);	
					
						$scene.find(".ProfileContent")
						.find(".FootballProfile,.MusicProfile,.Specification,.PepsiBrand")
						.end().find(".VideoPlayer")
						.css({display:'none'})
						;
						applyClass1();

					postLoader(function(){
						initScenes($scene);
						renderFlashContent($scene);
						attachHandlers($scene);
						pan($scene,function(){
							//attachHandlers($scene);
						});
					});
					
				});
		});
	}
	
	/** Wallpaper rollover effect function */
	var wallpaper = function($wallpaper){
			$wallpaper.find("li a").unbind()
			.bind("click",function(){
					return false;
				})
			;
			$wallpaper.find("li span a").unbind()
			.bind("click",function(){
					$(this).attr("target","_blank");
				})
			;
			/** thumbnails hover effect */
			$wallpaper.find("li").hover(
				function(){
					$(this).find("img").animate({width:'112px',height:'81px',marginTop:'-7px'},{duration:200,queue:false
						,complete:function(){$(this).parent().parent().find("span").slideDown(50)}
					});
				},
				function(){
					$(this).find("span").slideUp(50,function(){
						$(this).parent().find("img").animate({width:'90px',height:'65px',marginTop:'0px'},{duration:300,queue:false
							,complete:function(){$(this).parent().parent().find("span").slideUp(50)}
						});
					});
				}
			);
			
	}
	
	/** Scrolling functions */
	$maskWidth = 353;
	var scrollLeft = function($car,diff,$arrow){
		if(locale == "en_gb"){
			var currLeft = parseInt($car.css('marginLeft')) * -1;
			if(currLeft >= diff) {if($arrow) $arrow.css({opacity:.2});return;}
			else $car.parent().prev().css({opacity:1});
			$car.animate({marginLeft:'-=5px'},1,function(){scrollLeft($car,diff,$arrow);});
		}
		if(locale == "ar_ae"){
			var currRight = parseInt($car.css('marginRight')) * -1;
			if(currRight <= 0) {if($arrow) $arrow.css({opacity:.2});return;}
			else $car.parent().prev().css({opacity:1});
			$car.animate({marginRight:'+=5px'},1,function(){scrollLeft($car,diff,$arrow);});
		}
	}
		
	var scrollRight = function($car,diff,$arrow){
		if(locale == "en_gb"){
			var currLeft = parseInt($car.css('marginLeft')) * -1;
			if(currLeft <= 0) {if($arrow) $arrow.css({opacity:.2});return;}
			else $car.parent().next().css({opacity:1});
			$car.animate({marginLeft:'+=5px'},1,function(){scrollRight($car,diff,$arrow)});
		}
		if(locale == "ar_ae"){
			var currRight = parseInt($car.css('marginRight')) * -1;
			if(currRight >= diff) {if($arrow) $arrow.css({opacity:.2});return;}
			else $car.parent().next().css({opacity:1});
			$car.animate({marginRight:'-=5px'},1,function(){scrollRight($car,diff,$arrow)});
		}
	}
	
	/** Intialise carousel (UL) width and scroll arrows */
	var initCarousel = function($Lists){
		$Lists.each(function(){
			var $li = $(this).find("li");
				var liWidth = $li.width();
				//set width of every UL
				$(this).width((liWidth+24)*$li.length+200);
				if(locale == "en_gb"){
					if(liWidth*$li.length < $(this).parent().width())
						$(this).parent().next().css({opacity:.2});
					else
						$(this).parent().next().css({opacity:1});
					if(parseInt($(this).css("marginLeft")) < 0)
						$(this).parent().prev().css({opacity:1});
					else
						$(this).parent().prev().css({opacity:.2});
				}
				if(locale == "ar_ae"){
					if(liWidth*$li.length < $(this).parent().width())
						$(this).parent().prev().css({opacity:.2});
					else
						$(this).parent().prev().css({opacity:1});
					if(parseInt($(this).css("marginRight")) < 0)
						$(this).parent().next().css({opacity:1});
					else
						$(this).parent().next().css({opacity:.2});
				}
		});
	}
	
	/**
	* @desc Accordion effects + video and wallpaper handler
	*/
	var accordion = function($scene){
		$(".VideoList li img",$scene).unbind("mouseover,mouseout");
		$(".WallpaperList li span",$scene).css({display:'none'});
		$(".VideoPlayer",$scene).css({display:'none'});
		if($scene.find(".CopyDeck div.Copy").length) $scene.find(".CopyDeck div.Copy").css({display:'none'});
		
		
		$(".Accordion li",$scene).unbind().find(".Expand").css({display:'block'});
		
		initCarousel($scene.find("ul.Accordion li").find("ul.VideoList,ul.WallpaperList"));
		
		
		var expands = $(".Accordion:last li",$scene).find(".Expand");
		expands.css({display:'none'})//slideUp(500)
		.eq(1)
		.each(function(){
			/** collapse videos if empty */
			if($(this).find(".VideoList li").length){
				$(this).slideDown(500,function(){
					$(this).parent().addClass("ActiveArrow");
				});
			}
			/** expand profile cabinet */
			else{
				expands.eq(0).slideDown(500,function(){
					$(this).parent().addClass("ActiveArrow");
				});
			}
		})
		;
				
		$(".Accordion li",$scene).bind('click',function(e){
			if(e.target.tagName == "DIV" || $scene.find(".VideoPlayer:visible").length) return;
			var $anchor = $(this).find("a:first");
			$(this).parent().find(".Expand:visible")
				.slideUp(500,function(){
					$scene.find(".VideoPlayer").fadeOut(500);
					$(this).parent().removeClass("ActiveArrow");
				});
			$(this).find(".Expand:hidden")
				.slideDown(500,function(){
					$(this).parent().addClass("ActiveArrow");
				});
		})
		.find("a").bind('click',function(e){if(e.preventDefault) e.preventDefault()}).end()
		.find(".Expand,.VideoPlayer,.VideoPlayer *").bind('click',function(e){
			if(e.stopPropagation) e.stopPropagation();
		}).end()
		;
			/** Video list hover effect */
			var rate1SWF = "";
			var rate2SWF = "";
			if(typeof rate1=="object") rate1SWF = rate1.attributes.swf;
			if(typeof rate2=="object") rate2SWF = rate2.attributes.swf;
			$(".VideoList li img",$scene).unbind()
			.bind('mouseover',function(){
				$(this).animate({width:'112px',height:'81px',marginTop:'-10px'},200);
			})
			.bind('mouseout',function(){
				$(this).animate({width:'90px',height:'65px',marginTop:'0px'},200);
			})
			.bind('click',function(){
				if(!hasFlash) {
					alert("Flash plugin is needed to view the videos.");
					return false;
				}
				musicIcon("mute");
				var swfSrc = $(this).parent().attr("href");
				var altImage = $(this).attr("alt");
				var behindScene = $(this).parent().next().attr("href");
				/** pass behind scene swf to video player */
					if(behindScene) {
						$vidPlayer.find(".BehindScene a").attr("href",behindScene).parent().css({display:'block'});
					} 
					else $vidPlayer.find(".BehindScene a").attr("href","#").parent().css({display:'none'});
				
				$vidPlayer.fadeIn(1000,function(){
					$vidPlayer.find(".Flash").css({background:'url('+altImage+')'});
					/** render flash video */
						var videoQry = swfSrc;
						var rating = videoQry.match(/rating=[\d]/g) ? videoQry.match(/rating=[\d]/g)[0] : "rating=0";
						var videoID = videoQry.match(/videoid=[\d]+/g) ? videoQry.match(/videoid=[\d]+/g)[0] : "videoID=0";
						/** track viewed videos */
						var trackFile = videoQry.split("?") ? videoQry.split("?")[1] : "";
						var trackQry = "/Track.ashx?" + trackFile.replace("flvPath","File");
							if(trackQry != ""){
								$.get(trackQry,function(data){});
							}
						var rateQry = "?"+videoID+"&"+rating;
						if(typeof fvp=="object"){
							fvp.setAttribute("swf",swfSrc);
							fvp.write("fvpSWF");
							rate1.setAttribute("swf",rate1SWF+rateQry);
							rate1.write("rateFVP");
						}
						if(typeof mvp=="object"){
							mvp.setAttribute("swf",swfSrc);
							mvp.write("mvpSWF");
							rate2.setAttribute("swf",rate2SWF+rateQry);
							rate2.write("rateMVP");
						}
					
				})
				.find(".ButtonClose a").unbind().bind("click",function(){return false})
				.end()
				.find(".ButtonClose img")
				.bind('click',function(){
					$vidPlayer.find("embed,object").remove();
					$vidPlayer.fadeOut(500,function(){});
					return false;
				})
				;
			})
			;
			
			var $vidPlayer = $(".VideoPlayer",$scene);
				$vidPlayer.find(".Action .BehindScene a")
				.unbind()
				.bind("click",function(){
					var behindSrc = this.href;
					if(behindSrc == "#") return;
					if(typeof fvp=="object"){
						fvp.setAttribute("swf",behindSrc);
						fvp.write("fvpSWF");
					} 
					if(typeof mvp=="object"){
						mvp.setAttribute("swf",behindSrc);
						mvp.write("mvpSWF");
					}
					return false;
				})
				;
				$vidPlayer.find(".Action .Fav a").unbind()
				.bind("click",function(){
					bookmarkPage("PepsiArabia-"+currScene,window.location);
					return false;
				})
				;
			
			/** Video and Wallpaper scrolling */
			//$(".VideoHolder").css({overflow:'visible'});
			$("li.Videos div.ScrollRight,li.Wallpaper div.ScrollRight",$scene).unbind()
			.bind('mouseover',function(){
				if($(this).css("opacity") == .2 && $(this).prev().prev().css("opacity") == .2) return;
				var $theCar = $(this).prev().find("ul:first");
				var diff = $theCar.width()-200 + 20 - $maskWidth;
				scrollLeft($theCar,diff,$(this));
			})
			.bind('mouseout',function(){
				var $theCar = $(this).prev().find("ul:first");
				$theCar.stop();
			})
			;
			$("li.Videos div.ScrollLeft,li.Wallpaper div.ScrollLeft",$scene)
			.bind('mouseover',function(){
				if($(this).css("opacity") == .2 && $(this).next().next().css("opacity") == .2) return;
				var $theCar = $(this).next().find("ul:first");
				var diff = $theCar.width()-200 + 20 - $maskWidth;
				scrollRight($theCar,diff,$(this));
			})
			.bind('mouseout',function(){
				var $theCar = $(this).next().find("ul:first");
				$theCar.stop();
			})
			;
			
			/** Wallpaper roll over effect */
			wallpaper($("li.Wallpaper ul.WallpaperList",$scene));
			
	}
	
	/** Inner spotlight handler */
	var innerSpotlight = function($scene){
		$scene.find(".QuickLink a:not([rel=popup])")
		.bind('click',function(){
			setHash(this.href);
			return false;
		})
		;
	}
	
	/**
	* @desc attach handlers and animations to scenes
	*/
	var starTimer;
	var rollerFlag = false;
	var attachHandlers = function($scene){
		switch($scene.attr("class").split(" ")[1]){
			case "Home":
				break;
			
			case "Music":
				/** QuickLink */
				innerSpotlight($scene);
				
				/** stars sequencer */ 
				clearTimeout(starTimer);
				var $stars = $scene.find(".MusicStars li");
				var starSequencer = function($Li,fn){
					if(!$Li.length) {if(fn) fn();if(typeof stepper=="function") setTimeout(stepper,3000);return;}
					$Li.animate({opacity:1},200,function(){
						starSequencer($(this).next(),fn)});
				}
				var slideDownSideBar2 = function (){
					$scene.find(".MusicNews,.Poll").slideDown(1000);
				}
				var fadeInInfo2 = function (){
					$scene.find(".MusicStrip").animate({opacity:1},1000,slideDownSideBar2)
					.find("p").css({opacity:1})
					;
				}
				if(!rollerFlag) starSequencer($stars.eq(0),fadeInInfo2);

				/** music 3rd level */
				if($crumb.size() > 2 && !$scene.find(".ContentLoaded").length) slideDownSideBar2();
				
				/** Stars in row  */
				$scene.find(".MusicStars li").unbind();
				$scene.find(".MusicStars li")
				.bind('mouseover',function(){
					clearTimeout(starTimer);rollerFlag=true;
					if($scene.find(".MusicStrip:visible").length)
					$scene.find(".MusicStrip")
						.animate({opacity:0},1000,function(){$(this).css({display:'none'})});
					$("img",this).animate({opacity:.7},{duration:500,queue:false});
				})
				.bind('mouseout',function(){
					$("img",this).animate({opacity:1},{duration:500,queue:false});
				})
				.bind('click',function(){
					clearTimeout(starTimer);rollerFlag=true;
					var targetURI = $(this).find("A").attr("href");
					setHash(targetURI);
					return false;
				})
				;
				/** scroll right/left buttons !important:may change if more stars are added to the list*/
				$scene.find(".MusicStarsHolder").css({overflow:'visible'});
				var $starsList = $scene.find(".MusicStarsList");
				var $scrollRight = $(".MusicStars .ScrollRight");
				var $scrollLeft = $(".MusicStars .ScrollLeft");
				
				$starsList.find("li:eq(0),li:eq(1),li:eq(2),li:eq(3)").addClass("Set1");
				$starsList.find("li:eq(4),li:eq(5),li:eq(6)").addClass("Set2");
				$starsList.find("li:eq(7)").addClass("Set3")
				//add LastSet for below reference
				.addClass("LastSet")
				//style for Bridgette
				.css({margin:'20px 0 0 180px'})
				;
				//$starsList.find("li:eq(7)").addClass("Set3");
				var $set = $(".Set1,.Set2,.Set3");
				
				if(!rollerFlag){
					$scrollRight.css({opacity:1});$scrollLeft.css({opacity:.2});
					$set.css({display:'none'});
					$(".Set1").css({display:'block'});
					var index = 1;
				}
				if(rollerFlag){
					if($(".Set1:visible").length) {$scrollRight.css({opacity:1});$scrollLeft.css({opacity:.2});index=1}
					if($(".Set2:visible").length) {$scrollRight.css({opacity:.2});$scrollLeft.css({opacity:1});index=2}
					if($(".Set3:visible").length) {$scrollRight.css({opacity:.2});$scrollLeft.css({opacity:1});index=3}
				}
				
				$scrollRight.unbind().bind("click",function(){
					if($(this).css("opacity") == .2) return false;
					clearTimeout(starTimer);rollerFlag=true;
					//starTimer = setTimeout(stepper,12000);
					index++;
					$starsList.animate({opacity:0},250,function(){
						$scrollLeft.css({opacity:1});
						$set.css({display:'none'});
						$(".Set"+index).css({display:'block'});
						$starsList.animate({opacity:1},250);
						if($(".LastSet").is(":visible")) {$scrollRight.css({opacity:.2});}
					});
					return false;
				})
				;
				$scrollLeft.unbind().bind("click",function(){
					if($(this).css("opacity") == .2) return false;
					clearTimeout(starTimer);rollerFlag=true;
					//starTimer = setTimeout(stepper,10000);
					index--;
					$starsList.animate({opacity:0},500,function(){
						$scrollRight.css({opacity:1});
						$set.css({display:'none'});
						$(".Set"+index).css({display:'block'});
						$starsList.animate({opacity:1},500);
						if($(".Set1").is(":visible")) $scrollLeft.css({opacity:.2});
					});
					return false;
				})
				;
				
				var stepper = function(){
					if($scrollRight.css("opacity") != .2){
						//$scrollRight.triggerHandler("click");
						index++;
						$starsList.animate({opacity:0},250,function(){
							$scrollLeft.css({opacity:1});
							$set.css({display:'none'});
							$(".Set"+index).css({display:'block'});
							$starsList.animate({opacity:1},250);
							if($(".LastSet").is(":visible")) {$scrollRight.css({opacity:.2});}
						});
					}
					else{
						$starsList.animate({opacity:0},250,function(){
							$set.css({display:'none'});
							$(".Set1").css({display:'block'});
							$starsList.animate({opacity:1},250);
							$scrollRight.css({opacity:1});
							$scrollLeft.css({opacity:.2});
							index=1;
						});
					}
					starTimer = setTimeout(stepper,5000);
				}
				
				
				/** invoke accordion effect */
				$scene.find(".ProfileContent").find("div").fadeIn(3000,function(){});
				accordion($scene);
				
				/** close profile content */
				$scene.find(".ButtonClose a").unbind().bind('click',function(){
					if($scene.find(".ProfileContent").size()){
						setHash($(this).attr("href"));
					}
					return false;
				});
				
				break;//End Music
			
			case "Football":
				/** QuickLink */
				innerSpotlight($scene);
				
				/** player sequencer */
				var $players = $scene.find(".Players li");
				var sequencer = function ($Li,fn){
					if(!$Li.length) {if(fn) fn();return;};
					$Li.animate({opacity:1},150,function(){
					sequencer($(this).next(),fn)});
				}
				var slideDownSideBar = function (){
					$scene.find(".FootballNews,.Poll").slideDown(1000);
				}
				var fadeInInfo = function (){
					$scene.find(".FootballStrip").animate({opacity:1},1000,slideDownSideBar)
					.find("p").css({opacity:1})
					;
				}
				sequencer($players.eq(0),null);
				
				/** football 3rd level */
				if($crumb.size() > 2 && !$scene.find(".ContentLoaded").length) 
				slideDownSideBar();
				
				/** Players matrix  */
				var $tip = $scene.find("div.LeftPanel .Tooltip");
				$tip.html("<p>"+$scene.find(".Players li:first span.Player").html()+"</p>");
				$scene.find(".Players li").unbind();
				$scene.find(".Players li")
				.bind('mouseover',function(){
					var tooltip = $(this).find('span.Player').html();
					$tip.html('').append("<p />").find("p").html(tooltip);
					if($scene.find(".FootballStrip:visible").length)
					$scene.find(".FootballStrip")
						.animate({opacity:0},1000,function(){$(this).css({display:'none'})});
					$("img",this).animate({opacity:.6},{duration:500,queue:false});
				})
				.bind('mouseout',function(){
					$("img",this).animate({opacity:1},{duration:500,queue:false});
				})
				.bind('click',function(){
					var targetURI = $(this).find("A").attr("href");
					setHash(targetURI);
					return false;
				})
				;
				
				/** invoke accordion effect */
				$scene.find(".ProfileContent").find("div").fadeIn(3000,function(){});
					accordion($scene);
				
				/** close profile content */
				$scene.find(".ButtonClose a").unbind();
				$scene.find(".ButtonClose a").bind('click',function(){
					if($scene.find(".ProfileContent").size()){
						setHash($(this).attr("href"));
					}
					return false;
				});
				
				break; //End Football
			

			case "Brands":		
				/** cans sequencer */ 
				var $cans = $scene.find(".Cans li");
				var canSequencer = function ($Li,fn){
					if(!$scene.find(".ProfileContent").length) $scene.find(".Cans").css({opacity:1});
					if(!$Li.length) {if(fn) fn();return;}
					$Li.animate({opacity:1},200,function(){
						canSequencer($(this).next(),fn)});
				}
				var slideDownSideBar3 = function (){
					$scene.find(".PepsiBrands,.PepsiBrands2,.Poll").slideDown(800);
				}
				canSequencer($cans.eq(0),slideDownSideBar3);
				
				
				/** fade in profile content */
				$scene.find(".Specification,.PepsiBrand").fadeIn(3000);
				if($scene.find(".ProfileContent").length) {
					$scene.find("ul.Cans").animate({opacity:0},1000);
				}
				
				/** brands 3rd level */
				if($crumb.size() > 2 && !$scene.find(".ContentLoaded").length)
					slideDownSideBar3();
				
				/** Cans matrix */
				$scene.find(".Cans li").unbind();
				$scene.find(".Cans li")
				.bind('mouseover',function(){
					$("img",this).animate({opacity:.5},{duration:500,queue:false});
				})
				.bind('mouseout',function(){
					$("img",this).animate({opacity:1},{duration:500,queue:false});
				})
				.bind('click',function(){
					var targetURI = $(this).find("A").attr("href");
					setHash(targetURI);
					return false;
				})
				.find("object,embed").unbind()
				.bind("click",function(e){
					if(e.preventDefault) e.preventDefault();
					return false;
				})
				;
				
				/** close profile content */
				$scene.find(".ButtonClose a").unbind();
				$scene.find(".ButtonClose a").bind('click',function(){
					var thisHref = this.href;
					
					if($scene.find(".PepsiBrand,.BrandsContent").length) $scene.find("ul.Cans").animate({opacity:1},1000);
					
					if($scene.find(".ProfileContent").size()){
						setHash($(this).attr("href"));
					}
					return false;
				});
				
				/** Brands panel handler */
				$scene.find(".PepsiBrands li a").unbind();
				$scene.find(".PepsiBrands li a")
				.bind("click",function(){
					setHash($(this).attr("href"));
					return false;
				});
				
				break;//End Brands
			
			case "Games":
				/** QuickLink */
				innerSpotlight($scene);

				/** downloads games videos */
				var $panels = $scene.find("div.Downloads,div.PlayGames,div.LatestVideos");
				var dgvSequencer = function($panel,fn){
					if(!$panel.length) {if(fn) fn();return;}
					$panel.animate({opacity:1},600,function(){
						dgvSequencer($(this).next(),fn);
					});
				}
				var slideDownSideBar4 = function (){
					$scene.find("div.MainNews,div.Poll").slideDown(500);
				}
				if(!$(".ProfileContent",$scene).length)
				dgvSequencer($panels.eq(0),slideDownSideBar4);
				else slideDownSideBar4();


				$scene.find(".GamesContent,.LatestVideosExpand").fadeIn(3000,function(){});
				
				/** load profile content */
				$panels.css({cursor:'pointer'})
				.find("li a").unbind().bind("click",function(e){
						var targetURI = $(this).attr("href");
						setHash(targetURI);
						if(e.preventDefault) e.preventDefault();
						return false;
					}).end()
				.unbind()
				.bind("click",function(e,var1){
					$("li a:eq(0)",this).trigger("click");
				})
				.bind('mouseover',function(){
					$(this).animate({opacity:.6},{duration:500,queue:false});
				})
				.bind('mouseout',function(){
					$(this).animate({opacity:1},{duration:500,queue:false});
				})
				;
				
				/** unload profile */
				$scene.find("div.GamesContent p.ButtonClose a,div.LatestVideosExpand p.ButtonClose a,div.GamesContent div.Downloading p.ButtonClose a")
				.unbind()
				.bind("click",function(){
					if($scene.find("#gvpSWF").find("object,embed").length){
						$scene.find("#gvpSWF").find("object,embed").remove();
					}
					if($scene.find(".ProfileContent").size()){
						setHash($(this).attr("href"));
					}
					return false;
				})
				;
				
				/** download panel inside play games */
				$scene.find("div.PlayGamesExpand").parent().find("div.Downloads")
				.find("li a").unbind()
				.bind("click",function(){
					if($scene.find(".ProfileContent").size()){
						setHash($(this).attr("href"));
					}
					return false;
				})
				;
				
				/** remove events handlers on div.Downloads */
				$scene.find("div.Downloading").css({cursor:'default'}).unbind("mouseover").unbind("mouseout")
				.find(".Ringtone li a").unbind()
				.bind("click",function(){
					$(this).attr("target","_blank");
				})
				;
				
				/** wall paper carousel for downloads */
					/** wall paper hover effect */
					wallpaper($("div.Downloads ul.WallpaperList",$scene));

				var $maskWidth = 353; //width of the carousel mask
				
				/** initialise carousel */
				initCarousel($scene.find(".Downloading").find("ul.WallpaperList"));
				
				$("div.DownloadWallpaper div.ScrollRight",$scene).unbind()
				.bind('mouseover',function(){
					var $theCar = $(this).prev().find(".WallpaperList");
					$theCar.find("li span").css({display:'none'});
					var tn = $("li",$theCar);
					var diff = (tn.size() * (90+24)) + 10 - $maskWidth;
					scrollLeft($theCar,diff,$(this));
				})
				.bind('mouseout',function(){
					var $theCar = $(this).prev().find(".WallpaperList");
					$theCar.stop();
				})
				;
				
				$("div.DownloadWallpaper div.ScrollLeft",$scene).unbind()
				.bind('mouseover',function(){
					var $theCar = $(this).next().find(".WallpaperList");
					$theCar.find("li span").css({display:'none'});
					var tn = $("li",$theCar);
					var diff = (tn.size() * (90+24)) + 10 - $maskWidth;
					scrollRight($theCar,diff,$(this));
				})
				.bind('mouseout',function(){
					var $theCar = $(this).next().find(".WallpaperList");
					$theCar.stop();
				})
				;
				
			/** Games:Videos carousel */
			var $vidPlayer = $scene.find(".VideoPlayer2");
			var rate3SWF = "";
			if(typeof rate3=="object"){
				rate3SWF = rate3.attributes.swf;
			}
			//$(".VideoHolder").css({overflow:'visible'});
			$("div.VideoScroll ul.VideoList li img",$scene).unbind()
			.bind('mouseover',function(){
				$(this).animate({width:'112px',height:'81px',marginTop:'-10px'},200);
			})
			.bind('mouseout',function(){
				$(this).animate({width:'90px',height:'65px',marginTop:'0px'},200);
			})
			.bind('click',function(){
				if(!hasFlash) {
					alert("Flash plugin is needed to view the videos.");
					return false;
				}
				//render video player swf and rate swf
				var videoQry = $(this).parent().attr("href");
				var rating = videoQry.match(/rating=[\d]/g) ? videoQry.match(/rating=[\d]/g)[0] : "rating=0";
				var videoID = videoQry.match(/videoid=[\d]+/g) ? videoQry.match(/videoid=[\d]/g)[0] : "videoid=0";
				/** track viewed videos */
				var trackFile = videoQry.split("?") ? videoQry.split("?")[1] : "";
				var trackQry = "/Track.ashx?" + trackFile.replace("flvPath","File");
					if(trackQry != ""){
						$.get(trackQry,function(data){});
					}
				var rateQry = "?"+videoID+"&"+rating;
				gvp.setAttribute("swf",videoQry);
				gvp.write("gvpSWF");
				rate3.setAttribute("swf",rate3SWF+rateQry);
				rate3.write("rateGVP");
				return false;
			})
			;
			
			/** initialise carousel */
			initCarousel($scene.find("div.VideoScroll ul.VideoList"));
			
			/** render first video and rate swf */
			$vidPlayer.each(function(){
				musicIcon("mute");
				var videoQry = $scene.find("ul.VideoList li a:first").attr("href");
				var rating = videoQry.match(/rating=[\d]/g) ? videoQry.match(/rating=[\d]/g)[0] : "rating=0";
				var videoID = videoQry.match(/videoid=[\d]+/g) ? videoQry.match(/videoid=[\d]+/g)[0] : "videoID=0";
				/** track viewed videos */
				var trackFile = videoQry.split("?") ? videoQry.split("?")[1] : "";
				var trackQry = "/Track.ashx?" + trackFile.replace("flvPath","File");
					if(trackQry != ""){
						$.get(trackQry,function(data){});
					}
				var rateQry = "?"+rating+"&"+videoID;
				if(typeof gvp=="object"){
					gvp.setAttribute("swf",videoQry);
					setTimeout(function(){gvp.write("gvpSWF");},5000)
				}
				if(typeof rate3=="object"){
					rate3.setAttribute("swf",rate3SWF+rateQry);
					rate3.write("rateGVP");
				}
			})
			.find(".ActionBar2 li.Fav a").bind("click",function(){
				bookmarkPage("PepsiArabia-Videos",window.location);
				return false;
			})
			;
			
			$("div.VideoScroll div.ScrollRight",$scene)
			.bind('mouseover',function(){
				var $theCar = $(this).prev().find("ul:first");
				var tn = $("li",$theCar);
				var diff = (tn.size() * (90+24)) + 20 - $maskWidth;
				//$theCar.width(tn.size()*(90+24)+200+'px');
				scrollLeft($theCar,diff,$(this));
			})
			.bind('mouseout',function(){
				var $theCar = $(this).prev().find("ul:first");
				$theCar.stop();
			})
			.bind('click',function(){return false;})
			;
			$("div.VideoScroll div.ScrollLeft",$scene)
			.bind('mouseover',function(){
				var $theCar = $(this).next().find("ul");
				var tn = $("li",$theCar);
				var diff = (tn.size() * (90+24)) + 20 - $maskWidth;
				scrollRight($theCar,diff,$(this));
			})
			.bind('mouseout',function(){
				var $theCar = $(this).next().find("ul");
				$theCar.stop();
			})
			.bind('click',function(){return false;})
			;
				
				/** main news ticker */
				var $ticker = $scene.find("div.MainNews li");
				var tickerTimer;
				$ticker.not(":eq(0)").css({display:'none'});
				var ticker = function($item){
					tickerTimer = setTimeout(function(){
						$item.slideUp(400,function(){
							var next;
							$(this).css({display:'none'});
							if($(this).next().html()) next = $(this).next();
							else next = $ticker.eq(0);
							next.slideDown(600,function(){
								ticker($(this));
							});
						});
					},3000);
				}
				//ticker($ticker.eq(0));
				
				break;
				
			case "Express":
				$("div.ExpressContent div.DesignYourT").animate({opacity:1},500,function(){
					$("div.ExpressContent div.MyTego").animate({opacity:1},500);
				});
				break;
				
			default:
		}
	}
	
	
	/**
	* @desc initialise scenes
	*/
	var initScenes = function($scene){
		survey($scene);
		switch($scene.attr("class").split(" ")[1]){
			case "Home":
				break;
			
			case "Music":
				if(!$scene.find(".ProfileContent").length){
					$scene.find(".MusicStars li,.MusicStrip").css({opacity:0}).end()
					.find(".MusicNews,.Poll").css({display:'none'})
					;
				}
				$scene.find(".ScrollRight,.ScrollLeft").css({opacity:.2});

				break;
			
			case "Football":
				$scene.find(".Players li").animate({opacity:.5},500).end()
				;
				
				break;

			case "Brands":
				$scene.find(".Cans li").css({opacity:0}).end()
				.find(".PepsiBrands,.PepsiBrands2,.Poll").css({display:'none'})
				;
				if($(".ProfileContent",$scene).length)
					$scene.find(".Cans li").css({opacity:1}).end()
					.find(".PepsiBrands,.PepsiBrands2,.Poll").css({display:'block'})
					;
				
				break;
				
			case "Games":
				$scene.find("div.Downloads,div.PlayGames,div.LatestVideos").css({opacity:0}).end()
				.find("div.MainNews,div.Poll").css({display:'none'});
				;
				if($(".ProfileContent",$scene).length){
					$scene.find("div.Downloads,div.PlayGames,div.LatestVideos").css({opacity:1});
				}
				
				$("div.Downloads ul.WallpaperList li span").css({display:'none'});
				
				break;
			
			case "Express":
				$("div.ExpressContent div").css({opacity:0});
				break;
			
			default:
				
		}
	}
	
	/**
	* @desc unload profile
	*/
	var unloadProfile = function($scene,fn){
		$scene.find(".ProfileContent")
		.find(".FootballProfile,.MusicProfile,.Specification,.PepsiBrand,.GamesContent,.LatestVideosExpand")
		.fadeOut(1000,function(){
				$scene.find(".ProfileContent").remove();
				if(fn) fn();
			}
		);
	}
	
	
	/**
	* @desc render flash contents
	*/
	var renderFlashContent = function($scene){
		/** Homepage video player */
		if($("#h1SWF",$scene).length){
			h1.addVariable("flvPath","Common/Flash/Test.swf");
			h1.write("h1SWF");
		} 
		else $("#h1SWF").find("object,embed").remove();
		
		/** Homepage Spotlight */
		if($("#h2SWF",$scene).length){
			h2.write("h2SWF");
		}
		else $("#h2SWF").find("object,embed").remove();
		
		/** Music Inner Spotlight */
		if($(".QuickLink",$scene).length > 1) $(".QuickLink:eq(0)",$scene).attr("id","");
		if($("#sp1SWF",$scene).length){
			if(!$("#sp1SWF",$scene).find("object,embed").length)
			sp1.write("sp1SWF");
		}
		else $("#sp1SWF").find("object,embed").remove();
		
		/** Football Inner Spotlight */
		if($(".QuickLink",$scene).length > 1) $(".QuickLink:eq(0)",$scene).attr("id","");
		if($("#sp2SWF",$scene).length){
			if(!$("#sp2SWF",$scene).find("object,embed").length)
			sp2.write("sp2SWF");
		}
		else $("#sp2SWF").find("object,embed").remove();
		
		/** Games Inner Spotlight */
		if($("#sp3SWF",$scene).length){
			if(!$("#sp3SWF",$scene).find("object,embed").length)
			sp3.write("sp3SWF");
		}
		else $("#sp3SWF").find("object,embed").remove();
		
		/** Express Inner Spotlight */
		if($("#sp4SWF",$scene).length){
			if(!$("#sp4SWF",$scene).find("object,embed").length)
			sp4.write("sp4SWF");
		}
		else $("#sp4SWF").find("object,embed").remove();
		
		/** Pepsi New */
		if($("#pbSWF",$scene).length){
			if(!$("#pbSWF",$scene).find("object,embed").length)
			pb.write("pbSWF");
			$("#pbSWF",$scene).find("embed").css({display:'block'});
		}
		else $("#pbSWF").find("object,embed").remove();
		if($("#pb2SWF",$scene).length){
			if(!$("#pb2SWF",$scene).find("object,embed").length)
			pb2.write("pb2SWF");
			$("#pb2SWF",$scene).find("embed").css({display:'block'});
		}
		else $("#pb2SWF").find("object,embed").remove();
		
		if($("#gvpSWF").length)
		$("#gvpSWF").find("object,embed").remove();
		if($("#fvpSWF").length)
		$("#fvpSWF").find("object,embed").remove();
		if($("#mvpSWF").length)
		$("#mvpSWF").find("object,embed").remove();
		
		
		/** music player */
			//compute position of the music player
			var left = parseInt($scene.css("left"))+parseInt($scene.css("width"))/2+383;
			var top = parseInt($scene.css("top")) + 512;
			
			if($("#MusicPlayer1").length){
				$("#MusicPlayer1").css({left:left+"px",top:top+"px"})
				/*if(currScene == "Home") musicIcon("mute");
				else musicIcon("play");*/
			}else{
			$("<div></div>").attr("id","MusicPlayer1")
			.addClass("MusicPlayer1").css({width:'130px',height:'130px',left:left+"px",top:top+"px",position:'absolute',zIndex:101})
			.appendTo($page);
			var swfMPlayer = "Common/Flash/MusicPlayer_"+locale+".swf";
			if($crumb.length > 1) swfMPlayer = "../Common/Flash/MusicPlayer_"+locale+".swf";
			var mPlayer = new SWFObject(swfMPlayer, "MusicPlayer", "130", "130", "8", "#ffffff");
				mPlayer.addParam("wmode", "transparent");
				mPlayer.addParam("scale", "noscale");
				//mPlayer.write("MusicPlayer1");
				//if($crumb.length > 1) setTimeout(function(){musicIcon("play");},1000);
			}
		
	}
	renderFlashContent($("."+currScene));
	
	/** music icon player */
	var musicIcon = function(action){
		return;
		if(!hasFlash) return;
		if(typeof getFlashMovie("MusicPlayer").playSound == "function" && $("#MusicPlayer1").length){
			if(action == "play") getFlashMovie("MusicPlayer").playSound();
			if(action == "mute") getFlashMovie("MusicPlayer").muteSound();
		}
	}
	
	/** lenient flash plugin detection */
	var hasFlash = $(".Page").find("embed,object").length ? true : false;
	
	/**
	* @desc handles survey and its result
	*/
	var survey = function($scene){
		var msg1 = "please select your choice";
		if(locale == "ar_ae") msg1 = "الرجاء إختيار أحد الأجوبة";
		$scene.find(".Survey form").bind("submit",function(){return false;});
		$scene.find(".Survey .SurveyButton").bind('click',function(){
		if(!$scene.find("input.Radio1:checked").length) {
				$scene.find(".Survey .SurveyError").remove();
				$("<span>"+msg1+"</span>")
				.addClass("SurveyError")
				.css({display:'none',color:'red',position:'relative',top:'4px',left:'10px'})
				.appendTo($scene.find(".Survey fieldset:eq(3)"))
				.fadeIn(500)
				;
				if(locale == "ar_ae") $scene.find(".Survey .SurveyError").css({left:'80px'});
				return false;
			}
			
			//get result through AJAX
			var surveyQry = "";
			surveyQry = $scene.find(".Survey form").attr("action");
			surveyQry = '/'+surveyQry;
			var choice = $scene.find("input.Radio1:checked").val();
			surveyQry += "&choiceid=" + choice;
			$.get(surveyQry,function(result){
				if($crumb.length > 1) result = result.replace("Common/","../Common/");
				$scene.find(".SurveyError").css({display:'none'});
				$scene.find(".Survey").animate({left:'-300px'},500);
				$scene.find(".SurveyResult").html($(result).html())
				.find(".SurveyButton").css({display:'none'}).end()
				.animate({left:'0px'},500);
				;
			});
			
			return false;
		});
		
		$scene.find(".SurveyResult .SurveyButton").css({display:'none'}).bind('click',function(){
			$scene.find(".Survey").animate({left:'0px'},500);
			$scene.find(".SurveyResult").animate({left:'300px'},500);
		})
		;
	}
	var invokePopup = function(){
		$("a[rel=popup]").unbind("click").bind("click",function(){
			var width = this.className.split(" ")[0];
			var height = parseInt(this.className.split(" ")[1])+10;
			if(width || height) popupWin(this.href,"Popup2",{width:width,height:height});
			else popupWin(this.href,"Popup",{width:668,height:620})
			return false;
		});
	}
	invokePopup();
	
	/**
	* initialize and attach handlers on load
	*/
	if($.browser.safari) setTimeout(positionPage,200); /* delay positioning in Safari, a weird bug*/
	//else if(/chrome/.test(navigator.userAgent.toLowerCase())) setTimeout(positionPage,100);
	else positionPage();
	initScenes($("."+currScene));
	attachHandlers($("."+currScene));
	if($crumb.length > 2) $("."+currScene).find(".Content").addClass("ProfileContent");
	else $("."+currScene).find(".Content").addClass("ContentLoaded");
	createHeader();
	
});
//end of jQuery routines


/** functions to be invoked by flash's ExternalInterface */
function loadMusic(){
	/** load Haifa profile */
	$("ul.Navigation2 li a[rel=Music]").triggerHandler("click");
}
function loadHaifa(){
	/** load Haifa profile */
	$("ul.Navigation2 li a[rel=Music]").parent().find("li a:eq(3)").triggerHandler("click");
}
function loadFootball(){
	$("ul.Navigation2 li a[rel=Football]").triggerHandler("click");
}
function loadGames(){
	$("ul.Navigation2 li a[rel=Games]").triggerHandler("click");
}
function loadPepsiCan(){
	$(".PepsiBrands li a:eq(0)").triggerHandler("click");
}
function loadPepsiCan2(){
	$(".PepsiBrands li a:eq(1)").triggerHandler("click");
}
function popupDance(){
	popupWin("http://www.pepsi.com.au/dance/game/index.php","Dance",{width:840,height:620});
}
function popupEmoticons(){
	if(window.location.href.indexOf("ar_ae") > 0){
		popupWin("Applications/Arabic/Emoticon/Default.html","Dance",{width:800,height:600});
	}
	else{
		popupWin("Applications/English/Emoticon/Default.html","Dance",{width:800,height:600});
	}
}
function popupStarAcademy(){
	popupWin("http://www.pepsiarabia.com/staracademy5/Default.asp","Dance",{width:720,height:540});
}

function popupStreetSkillsEn(){
	//popupWin("http://www.pepsiarabia.com/StreetSkills/Default_en_gb.html","Dance",{width:100%,height:100%});
	window.open("http://www.pepsiarabia.com/StreetFootball/Default_en_gb.aspx");
}

function popupStreetSkillsAr(){
	//popupWin("http://www.pepsiarabia.com/StreetSkills/Default_ar_ae.html","Dance",{width:720,height:540});
	window.open("http://www.pepsiarabia.com/StreetFootball/Default_ar_ae.aspx");
}

function popupDesignYourShirtEn(){
	//popupWin("http://www.pepsiarabia.com/DesignYourTShirt/Default_en_gb.aspx","Dance",{width:100%,height:100%});
	window.open("http://www.pepsiarabia.com/DesignYourTShirt/Default_en_gb.aspx");
}

function popupDesignYourCanEn(){
	//popupWin("http://www.pepsiarabia.com/DesignYourTShirt/Default_en_gb.aspx","Dance",{width:100%,height:100%});
	window.open("http://www.pepsiarabia.com/DesignYourCan/Winners_en_gb.aspx");
}

function popupDesignYourCanAr(){
	//popupWin("http://www.pepsiarabia.com/DesignYourTShirt/Default_en_gb.aspx","Dance",{width:100%,height:100%});
	window.open("http://www.pepsiarabia.com/DesignYourCan/Winners_ar_ae.aspx");
}


function popupDesignYourShirtAr(){
	//popupWin("http://www.pepsiarabia.com/DesignYourTShirt/Default_ar_ae.aspx","Dance",{width:720,height:540});
	window.open("http://www.pepsiarabia.com/DesignYourTShirt/Default_ar_ae.aspx");
}

function popupMyTegoEn(){
	//popupWin("http://www.pepsiarabia.com/DesignYourTShirt/Default_en_gb.aspx","Dance",{width:100%,height:100%});
	window.open("http://www.mytego.com/partners/pepsi");
}

function popupMyTegoAr(){
	//popupWin("http://www.pepsiarabia.com/DesignYourTShirt/Default_ar_ae.aspx","Dance",{width:720,height:540});
	window.open("http://www.mytego.com/partners/pepsi");
}
function popupFootvolleyEn(){
	window.open("http://www.pepsiarabia.com/amrdiab");
}
function popupFootvolleyAe(){
	window.open("http://www.pepsiarabia.com/amrdiab");
}

function popupThingsToDoEn(){
	window.open("http://www.pepsiarabia.com/max/Default_en_gb.aspx");
}

function popupThingsToDoAr(){
	window.open("http://www.pepsiarabia.com/max/Default_ar_ae.aspx");
}

/**
* @desc Popup window function
*/
function popupWin(sLink,sName,oAttrib){
	var popup;
	var width = oAttrib.width;
	var height = oAttrib.height;
	var left,top;
	left = oAttrib.left || (screen.width - width)/2;
	top = oAttrib.top || (screen.height - height)/2;
	try{
		popup = window.open(sLink,sName,"scrollbars=yes,width="+width+",height="+height+",left="+left+",top="+top+"");
		popup.focus();
	}
	catch(error){
		alert("You are trying to access a broken link. Please contact the website administrator.");
		return false;
	}
}

/**
* @desc Bookmark function
*/
function bookmarkPage(title,url){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "main");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}
