(function($) { // Preload images
  var cache = [];0
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(document).ready( function () {


	$("html").height("100%");
	$("body").height("100%");
	
	if (getCookie("PreferBandwidth") == "Low") {
	var flashvars = {config_file:"/ar/flash/p2main/low/visualimg.xml"};
		//var flashvars = {config_file:"/ar/flash/p2main/xml/config.xml"};
		$.ajax({
			url:flashvars.config_file,
			type:'GET',
			cache:true,
			global:false,
			dataType:$.browser.msie ? 'text' : 'text/xml',
			error: function(){
				//alert('Error loading XML document');
			},
			success: function(xml) {
				
				var data;
				 if ($.browser.msie && typeof xml == "string") {
				   data = new ActiveXObject("Microsoft.XMLDOM");
				   data.async = false;
				   data.loadXML(xml);
				 } else {
					xmlParser = new DOMParser();
					data = xmlParser.parseFromString(xml, 'text/xml');
				 }
				 
				 var result_img = new Array();
				 var result_link = new Array();
				 var result_class = new Array();
				 var result_class2 = new Array();
				 var result_target = new Array();
				 var idx = 0; 
				 $(data).find('promo').each(function() {
				  result_img[idx] = $(data).find('promo').find('path').eq(idx).text();
				  jQuery.preLoadImages(result_img[idx]);
				  result_target[idx] = $(data).find('promo').find('link').eq(idx).attr("target");
				  result_link[idx] = $(data).find('promo').find('link').eq(idx).text();	
				  result_class[idx] = $(data).find('promo').find('change_class').eq(idx).attr("className");
				  result_class2[idx] = $(data).find('promo').find('change_class').eq(idx).attr("footerClass");
				  idx++;
				  });
				 function background_src(img,src,className,className2,target){
					
					var newTimer;
					var newMain = $("<div id='newmain'></div>");				
					//var tempLi = $("<li></li>");
					var newLink = $("<a href='"+ src[tempNumber] +"' target='" + target[tempNumber] + "'></a>");				
					
					//newUl.html(tempLi.html(newLink)).addClass('link_list');
					var transFlashImage = $("<img src='" + img[tempNumber] + "' alt='' />");
					transFlashImage.addClass('imagePannel');
					transFlashImage.hide();
					newLink.html(transFlashImage);
					
					
					var newUl = $("<ul></ul>").addClass('LISTROLL');
					for(var i = 0; i<img.length; i++)
					{
						var newImg = $("<img src='/ar/p25/images/low/keyvisual/ico_now_off.gif' alt='' />");
						var newA = $("<a></a>");//.bind("click",function(event){alert(i);});
						newA.html(newImg);
						var newLi = $("<li></li>").html(newA);
						newUl.append(newLi);
					}
					jQuery.fx.speeds.slow = 1000;
					$("#main_promo").html(newLink).append(newUl);
					
					$(".LISTROLL li a img").bind("mouseover",function(event){$(this).attr("src","/ar/p25/images/low/keyvisual/ico_now_over.gif")}).bind("mouseout",function(event){$(this).attr("src","/ar/p25/images/low/keyvisual/ico_now_off.gif")});//binding event handler 
					var tempLen = $(".LISTROLL li").length;
					$(".LISTROLL li").bind("click",function(event){
						$("#header").removeClass().addClass(className[$(".LISTROLL li").index($(this))]);
						$("#gnb").removeClass().addClass(className[$(".LISTROLL li").index($(this))]+"2");
						$("#footer").removeClass("footer_black").removeClass("footer_white").addClass(className2[$(".LISTROLL li").index($(this))]);						
						//clearTimeout(timer);
						var ind = $(".LISTROLL li").index($(this));//alert(ind); 
						var indThis = $(".LISTROLL li").index($(this));					
						//alert(indThis);
						tempNumber = indThis;
						if($(".LISTROLL li").length -1 == indThis) {tempNumber=0; }
						else { tempNumber++; }					
						ChangeImageAndLink(ind);
						
						for(var i = 0; i<tempLen; i++)
						{
							if( i  == indThis)
							{
								clearTimeout(timer);
								$(".LISTROLL li a img").eq(i).attr("src","/ar/p25/images/low/keyvisual/ico_now_on.gif");
								$(".LISTROLL li a img").eq(i).unbind("mouseover").unbind("mouseout");							
							}
							else 
							{
								clearTimeout(timer);
								$(".LISTROLL li a img").eq(i).attr("src","/ar/p25/images/low/keyvisual/ico_now_off.gif");
								$(".LISTROLL li a img").eq(i).bind("mouseover",function(event){$(this).attr("src","/ar/p25/images/low/keyvisual/ico_now_over.gif")}).bind("mouseout",function(event){$(this).attr("src","/ar/p25/images/low/keyvisual/ico_now_off.gif")});//binding event handler 							
							}
						}
						timer = setTimeout('$(".LISTROLL li").eq(tempNumber).click();',30000); // rolling speed down 2010.10.01
					});
					function ChangeImageAndLink(numd)
					{
						
						$(".imagePannel").fadeOut("slow",function(event){$(this).attr("src",img[numd]).parent().attr("href",src[numd]).attr("target",target[numd]);$(this).fadeIn("slow")});
					}
					$(".LISTROLL li").eq(tempNumber).click();
					
				 };
				 
				
				 background_src(result_img,result_link,result_class,result_class2,result_target)

			}
		}); 	
		viewlayer("footer_highband", "");
	}	
	else {
		viewlayer("footer_lowband", "");	
		var flashvars = {
			config_file: "/ar/flash/p2main/xml/config.xml"
		};
		var params = {
			wmode: "opaque",
			allowScriptAccess: "always"
		};
		var attributes = {};
		swfobject.embedSWF("/ar/flash/p2main/main.swf", "main_promo", "100%", "100%", "9.0.0", "/ar/flash/p2main/js/expressInstall.swf", flashvars, params, attributes);
		swffit.fit("main_promo", 962, 768);	
		resizeSP();	
	}
	
})
$(window).resize(function(){ 
	if(getCookie("PreferBandwidth") == "Low") return; 
	else resizeSP() 
});
//window.onresize = function() { resizeSP() };	
var tempNumber = 0;
var timer;

var spHt = 250;
var minTop = 518;
var minHt = 768;
var curTop = minTop;

function resizeSP() {

	$("html").height("100%");
	$("body").height("100%");
	newTop = -1;
	ht = $("body").height();

	if (ht < minHt) {newTop = minTop;}
	else {newTop = ht - spHt;}
	
	if (newTop != curTop) {	
		curTop = newTop;
		$("#sp_content").css("top",newTop+"px");		
	}
}



// CHANGE CSS CLASS FUNCTION - FOR PROMO FLASH

function change_css_class(i, c) {
	$("#"+i).attr("class",c);
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

// SECONDARY PROMO CAROUSEL

$(function() {
    // Create carousel
    // At launch, not needed as there are only 4 secondary promos
	if($("#secondary_boxes_ul > li").length <= 4) {} // If List item less than 4, nothing run
	else {
		$("#secondary_boxes").jCarouselLite({
			btnGo: ["#sb_pg1", "#jc", "#jc", "#jc", "#sb_pg2", "#jc", "#jc", "#jc", "#sb_pg3", "#jc", "#jc", "#jc", "#sb_pg4", "#jc", "#jc", "#jc"],
			btnNext: "#jc",
			btnPrev: "#jc",
			visible: 4,
			scroll: 4,
			circular: false,
			start: 4
		});
		
		// Attach click events to pagination controls
		$(".sb_page").click(function() {
			$(".sb_sel").removeClass("sb_sel");
			$(this).addClass("sb_sel");
		});
		$("#secondary_boxes li").css("display","block"); 
		setTimeout('$("#sb_pg1").click()',300);
	}


	if (!($.browser.msie && ($.browser.version < 7.0))) {
		$("#secondary_boxes li img").each(function() {
			src = $(this).attr("src");
			// Preload the hover states
			jQuery("<img>").attr("src",$(this).attr("src").replace(/\./,"_over."));
			
			$(this).mouseover(function() {
				if (!src.match("_over"))
					$(this).attr("src",$(this).attr("src").replace(/\./,"_over."));
			}).mouseout(function() {
				$(this).attr("src",$(this).attr("src").replace(/_over/,""));
			});
		})
	}
});
