// Quantity of items in the carousel
var qtyItems = 0;
// Query string
var qs = null;
// Array used for deep linking
var urlsArray = new Array();
// Array used to get the relationship item <-> page
var pageIndexes = new Array();
// Carousel initialization code
$(document).ready(function(){
	var urlXML = "";
	qs = $.param.querystring();
	$('<div class="loader-msg">Loading</div>').appendTo('body');
	if (isAuthenticated)
	{
		urlXML = "includes/xml/homepage.xml";
	
	}
	else 
	{
		urlXML = "includes/xml/homepageNotLogged.xml";
	}
	
	$.ajax({
			type: "GET",
			url: urlXML,
			dataType: "xml",
			success: function(xml) {
				// Markup of the product information
				var productInfoMarkup = "";
				// Markup of the bottom navigation carousel
				var carouselMarkup = "";
				// Markup of dynamic css rules
				var cssRules = "<style type='text/css'>\n";
				var page = 0;
				$(xml).find('item').each(function(index){
					productInfoMarkup += '<div id="' + $(this).attr('name') + '" class="homeContainer">\n<div class="leftColumnhome">\n';
					if ($(this).find('subheader').text())
						productInfoMarkup += $(this).find('subheader').text() + '\n<div class="leftDivider png-fix"></div>\n';
					productInfoMarkup += $(this).find('copy').text() + '\n</div>\n<div class="productHeadline">'
					+ $(this).find('header').text() + '</div>\n</div>\n';
					carouselMarkup += '<a href="?' + $(this).find('url').text()
					+ '" rel="' + $(this).attr('name')
					+ '" title="' + $(this).find('thumbnailText').text() + '">'
					+ '<img src="' + $(this).find('thumbnail').text()
					+ '" onclick="pageTracker._trackEvent(\'view\',\'home_scroll\',\''
					+ $(this).find('trackingTag').text() + '\');" alt="'
					+ $(this).find('thumbnailText').text() + '" /></a>\n';
					urlsArray[index] = $(this).find('url').text();
					if(index % 5 == 0 && index > 0) {
						page++;
					}
					pageIndexes[index] = page;
					cssRules += '#main-section #' + $(this).attr('name') + ' .productHeadline' + ' { '
					+ 'background-image:url(' + $(this).find('largeImage').text() + '); }\n';
					qtyItems++;
				});
				cssRules += "</style>";
				$("head").append(cssRules);
				$("#carousel").append(productInfoMarkup);
				$(carouselMarkup).insertAfter(".activeIndicator");
				initializePage();
				$(".loader-msg").hide().remove();
			},
			error: function(e){
				$(".loader-msg").hide().remove();
				$('<div class="error-msg">We&rsquo;re sorry.<br/><b>Data loading error!</b><br/>Please try again in a few minutes!</div>').appendTo('body');
			}
		});
	// Load the information
	function initializePage(){
		var initialItem = null;
		var urlIndex = $.inArray(qs, urlsArray);
		if(qs != null && qs != "" && urlIndex >= 0) {
			initialItem = $("div.items a").eq(urlIndex).attr("rel");
			applyHomesIFR();
		} else {
			initialItem = $("div.items a:first").attr("rel");
			applyHomesIFR();
		
		}
		$("#carousel:first-child").addClass('current');
		//var w = document.documentElement.clientWidth;
		//var h = document.documentElement.clientHeight;
		var w = $(window).width();
		var h = $(window).height();
		//alert(w);
		//alert(h);
//		if (w < 1100)
//		{
// 		    $(".homeContainer").css("width", 1257);
// 		}
// 		else
// 		{
// 		    $(".homeContainer").css("width", w);
// 		}
 		if (h < 734)
 		{
		    $(".homeContainer").css("height", 734);
		    h=734;
		}
		else
		{
		    $(".homeContainer").css("height", h);
		}
	    $(".homeContainer").css("width", w);
		//$(".homeContainer").css("height", h);
		if ((navigator.appVersion.indexOf("MSIE 7.")!=-1) ){
	        $('#wrapper #container #mainWrapper').css("padding-bottom","10px");
	    }
		$("#homeItems").easySlider({
		    auto: true,
		    continuous: true,
		    carouselParent: "div#carousel",
		    carouselItem: "div.homeContainer",
		    pause: 8000
	    });
        $(".left_control_arrow").css("top", (h/2));
		$(".right_control_arrow").css("top", (h/2));
		$('.CharityInfo').click(printMWSClick);			
	    applyOfferIFR();
		// Show demo video popup
		$(".showDemoPopup").live("click",function(){
			$("#demoPopup").modal();
			return false;
		});
		$(".showDemoPopup2").live("click",function(){
			$("#demoPopup2").modal();
			return false;
		});
		$(".showDemoPopup3").live("click",function(){
			$("#demoPopup3").modal();
			return false;
		});
		$(".showDemoPopup3").live("click",function(){
			$("#demoPopup3").modal();
			return false;
		});
	}// End of intializePage()
	 
});// DOM Ready

// Fires the DART Tagging
function spotlightTagging() {
	var axel = Math.random() + "";
	var a = axel * 10000000000000;
	var spotpix = new Image();
	spotpix.src = "http://ad.doubleclick.net/activity;src=1419543;type=hills544;cat=hilsh499;ord=" + a + "?";
}
function spotCookbook(href) {
    var axel = Math.random() + "";
    var a = axel * 1000000000000000000;
    document.url = href;
    var doPing = new Image();
    // the URL below is a spotlight tag for page3
    doPing.src = 'http://ad.doubleclick.net/activity;src=1419543;type=hills544;cat=hilsh499' + a + '?';
    
    return true;
}

function printMWSClick(){  
    var w = 580;
    var h = 350;
    var r = 10;      
	$('.MWSPopUp').modal();
	$('#modalPopUpContainer').css('width', w);
	$('#modalPopUpContainer').css('height', h);
	$('#modalPopUpContainer a.modalCloseImg').css('right', r);
	$('#modalPopUpContainer a.modalCloseImg').css('top', r);
	applyMWSPopUpIFR();        
	return false;
}

