/// <reference path="jquery-1.2.6-intellisense.js" />
Shadowbox.loadSkin('classic', '/includes/shadowbox/skin');
Shadowbox.loadLanguage('en', '/includes/shadowbox/lang');
Shadowbox.loadPlayer(['flv', 'html', 'iframe', 'img'], '/includes/shadowbox/player');

var options = {
    animSequence: "sync",
    initialHeight: 320,
    initialWidth: 320
};

$(document).ready(function(){
	//general
	$("a").focus(function() {
	  $(this).blur();
	});
	
	//shadowbox
	Shadowbox.init(options);
	$("a.hidden").click(function(){
        var linkIndex = $("a.hidden").index(this);            
        Shadowbox.open($(".mediaGallery a[rel]").get(linkIndex));
        return false;
    });
	
	//fancy tiles
	//$("div.fancyTile").wrapInner('<div class="padLayer"></div>');
	$("div.fancyTile").find("div.padLayer").find("*:last-child").css("margin-bottom","0");
	
	//sliding tiles
	$(".slidingTile h3.slideMenu:first").addClass("first");
	$(".slidingTile h3.slideMenu:first a").addClass("active");
	$(".slidingTile h3.slideMenu:last").addClass("last");
	$(".slidingTile div.slideContent:last").after("<div class=\"lastBottom\"></div>");
	$(".slidingTile div.slideContent:not(:first),.slidingTile div.lastBottom").hide();
	$(".slidingTile div.slideContent:last").addClass("lastContent");
	$("div.slidingTile").find("div.slideContent").find("*:last-child").css("margin-bottom","0px");
	$(".slidingTile h3").click(function(){
		if (!$(this).find("a").hasClass("active")){
			$(this).next("div.slideContent").slideToggle("fast")
			.siblings("div.slideContent:visible").hide("fast");
			$(this).find("a").toggleClass("active");
			$(this).siblings("h3").find("a").removeClass("active");
			$("div.slidingTile").find("div.lastBottom").hide();
			if ($(this).hasClass("last")){
				$("div.slidingTile").find("div.lastBottom").show();
			};
		};
		return false;
	});
	//single sliding tile
	if ($(".slidingTile h3.slideMenu").length == 1){
	    $(".slidingTile div.lastBottom").show();
	    $(".slidingTile h3.slideMenu").removeClass("last");
	}
	
	// select the second menu "Ask a Consultant" if the first menu is "At a Glance" - this is a change request as sthe "At a Glance" menu is very long.
		if($(".slidingTile h3.slideMenu a").eq(0).html() == "At a Glance" && $(".slidingTile h3.slideMenu a").eq(1).html() == "Ask a Consultant"){
		$(".slidingTile h3.slideMenu").eq(1).click();
	}
	
	//Boxes
	//$(".greyBox,.greyBoxWide").wrapInner('<div class="bottomWrap"><div class="padLayer clearfix"></div></div>');
	//$(".whiteBox,.whiteBoxWide").wrapInner('<div class="bottomWrap"><div class="padLayer"></div></div>');
	
	if ($(".tabBox")){
		//$(".tabBox .tabBoxTabs li:first").addClass("active");
		//$(".tabBox .tabBoxContent:first").show();
	};	
	$(".tabBox .tabBoxTabs li").click(function(){
		if (!$(this).hasClass("active")){
			$(this).addClass("active")
			.siblings("li").removeClass("active");
			var contentIndex = $(".tabBox .tabBoxTabs li").index(this);
			var newContent = $(".tabBox .tabBoxContent:eq(" + contentIndex + ")");
			$(newContent).fadeIn("fast");
			$(newContent).siblings(".tabBoxContent:visible").hide();
		};
		return false;
	});	
	
	
	//deals tile
	$("div.dealsTile").find("div.tileContent").find("*:last-child").css("margin-bottom","0px");
	
	//deals ticker	
	if ($(".dealsTicker")){
		initDealsTicker();
	};
	$(".dealsTicker a.dealsNum").click(function(){  
		stopInterval();
		if (!$(this).hasClass("active")){
			ShowDeal($(".dealsNum").index(this));
		}
		return false;
	});
	$(".dealsTicker a.prevdeal").click(function(){				  
		stopInterval();
		prevDeal();
		return false;
	});
	$(".dealsTicker a.nextdeal").click(function(){				  
		stopInterval();
		nextDeal();
		return false;
	});
	
	//destinations	
	if ($(".dest .dealContent")){
	    $(".dest .dealContent:first").show();  //safety in case no deals get display:block
	}
	$(".dest a.prevdeal").click(function(){				  
		var destIndex = $(".dest a.prevdeal").index(this);
		prevDestDeal(destIndex);
		return false;
	});
	$(".dest a.nextdeal").click(function(){
	    var destIndex = $(".dest a.nextdeal").index(this);				  
		nextDestDeal(destIndex);
		return false;
	});
	
	//deals abstracts
	$(".whiteBoxWide .itemAbstract:last").css("border","none");
	$(".dealsList .itemAbstract:first").css("padding-top","0");
	$(".dealsList .itemAbstract:last").css("border","0")
		.css("padding-bottom","0");
		
    //list filters
    $(".pNumTop a.filterLink").click(function(){
        $(".listFilter").toggle();
        return false;
    });
    
    //reviews
    $("div.review div.reviewText").find("*:last-child").css("margin-bottom","0px");
    $("div.review:last").css("border","0");
    //$("ul.ratingList :radio.star").rating({cancel: "Cancel", cancelValue: "0"});  
	
	//country page
	$(".mapToggle").show();
	$(".mapToggle a").toggle(
			function(){
				$(this).html("List view");
				$(this).toggleClass("listView");
				$(".resortListMap .resortMap").fadeIn("fast");
				$(".resortListMap .resortList ol").removeClass("links");
				$(".resortListMap .resortList ol").addClass("markers");
				return false;
			},			
			function(){
				$(this).html("Map view");
				$(this).toggleClass("listView");
				$(".resortListMap .resortMap").hide();
				$(".resortListMap .resortList ol").removeClass("markers");
				$(".resortListMap .resortList ol").addClass("links");
				return false;
			}
		);
	
	//split page
	$("div.splitPage").append('<div class="bottomPad"></div>');
	
	//search filters
	$(".searchFilterHead").click(function(){
		$(this).next("div.listFilter").slideToggle("fast")
		.siblings("div.listFilter:visible").hide("fast");
		$(this).toggleClass("active");
		$(this).siblings("div.listFilter").removeClass("active");			
		return false;
	});
	
	//staff
	$("div.staff:last").css("border","0");
	
	//map
	$(".dirlink").click(function(){
		if (!$(this).hasClass("active")){
			$(this).addClass("active")
			.siblings(".dirlink").removeClass("active");
			var contentIndex = $(".dirlink").index(this);
			var newContent = $(".dirform:eq(" + contentIndex + ")");
			$(newContent).fadeIn("fast");
			$(newContent).siblings(".dirform:visible").hide();
		};
		return false;
	});	
	
	
});


//deals ticker
var dealsInterval;
function initDealsTicker()
{
	$(".dealsTicker div.dealContent:not(:first)").hide();
	$(".dealsTicker a.dealsNum:eq(0)").addClass("active");
	dealsInterval = setInterval("nextDeal()", 8000);	
}
function ShowDeal(contentIndex)
{
	var newContent = $(".dealsTicker div.dealContent:eq(" + contentIndex + ")");
	var newNumber = $(".dealsTicker a.dealsNum:eq(" + contentIndex + ")");
	$(newContent).fadeIn("slow");
	$(newContent).siblings("div.dealContent:visible").fadeOut("normal");
	$(newNumber).toggleClass("active")
	.siblings("a.dealsNum").removeClass("active");	
}

function prevDeal()
{
	var currentDeal = $(".dealsTicker div.dealContent").index($("div.dealContent:visible"));
	var prevDeal = currentDeal - 1;
	
	if (prevDeal<0){
		prevDeal = 	$(".dealsTicker div.dealContent").length - 1;
	}	
	ShowDeal(prevDeal);	
}

function nextDeal()
{
	var currentDeal = $(".dealsTicker div.dealContent").index($("div.dealContent:visible"));
	var nextDeal = currentDeal + 1;
	
	if (nextDeal>$(".dealsTicker div.dealContent").length - 1){
		nextDeal = 	0;
	}	
	ShowDeal(nextDeal);
}
function stopInterval()
{
	clearInterval(dealsInterval);
}

//Destinations Deals
function ShowDestDeal(destIndex,contentIndex)
{
	var newContent = $(".dest:eq(" + destIndex + ") .dealContent:eq(" + contentIndex + ")");
	$(newContent).fadeIn("slow");
	$(newContent).siblings("div.dealContent:visible").fadeOut("normal");
	$(newContent).toggleClass("active")
	.siblings("div.dealContent").removeClass("active");		
}

function prevDestDeal(destIndex)
{
	var currentDeal = $(".dest:eq(" + destIndex + ") .dealContent").index($(".dest:eq(" + destIndex + ") .dealContent:visible"));
	var prevDeal = currentDeal - 1;
	
	if (prevDeal<0){
		prevDeal = 	$(".dest:eq(" + destIndex + ") .dealContent").length - 1;
	}	
	ShowDestDeal(destIndex,prevDeal);	
}

function nextDestDeal(destIndex)
{
	var currentDeal = $(".dest:eq(" + destIndex + ") .dealContent").index($(".dest:eq(" + destIndex + ") .dealContent:visible"));
	var nextDeal = currentDeal + 1;
	
	if (nextDeal>$(".dest:eq(" + destIndex + ") .dealContent").length - 1){
		nextDeal = 	0;
	}	
	ShowDestDeal(destIndex,nextDeal);
}



//Google Maps
function getElabels(markers){
    var batch = [];
    
    for (var i = 0; i < markers.length; i++) {
    
        var point = new GLatLng(markers[i][0],markers[i][1]);
        var title = markers[i][2];
        var helptitle = markers[i][5];
        if(helptitle == "") { helptitle = title ; }
        var url = markers[i][3];
        var markerclass = "mapMarker";
        var linkclass = "botleft";
        if (markers[i][4] != ""){
            linkclass = markers[i][4];
        }
        var pixeloffset = new GSize(-10,0);
        switch (linkclass)
        {
            case "botright":
                var offsetX =  -1*((title.length)*7 + 10) ;  //estimated average character width @7px            
                pixeloffset = new GSize(offsetX,0);
                break;
            case "topright":
                var offsetX =  -1*((title.length)*7 + 10);  //estimated average character width @7px          
                pixeloffset = new GSize(offsetX,32);
                break;
            case "topleft":
                pixeloffset = new GSize(-10,32);
                break;
            case "botleft":
                pixeloffset = new GSize(-10,0);
                break;
        }        
        var html = '<div class="' + linkclass + '"><a href="' + url + '" title="Click for more information on ' + helptitle + '">' + title + '</a><img src="/css/images/spacer.gif" width="1" height="1" alt=""></img><div class="pointer"></div></div>';
        var label = new ELabel(point, html, markerclass ,pixeloffset ,"", true);        
        batch.push(label);
    }
    return batch;
}

// Used to set radiobutton in repeaters
function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;

}


function SetSearchInput(objInput) {
    if (objInput.value == 'Enter keyword(s)') {
        objInput.className = 'textInput';
        objInput.value = '';
    }
}

function ReSetSearchInput(objInput) {
    if (objInput.value == '') {
        objInput.className = 'textInput nofocus';
        objInput.value = 'Enter keyword(s)';
    }
}

function PrintPage() {
    if (window.print()) {
        window.print();
    }
}

//validation and form functions
function ShowAstPhone(){
    $(".mandPhone").show();
    $(".mandEmail").hide();
}
function ShowAstEmail(){    
    $(".mandEmail").show();
    $(".mandPhone").hide();
}

/* display the help rollover thing */
function DisplayHelp(objRow, helpText, leftOffset) {
    if (helpText != "") {
        var posArray = findPos(objRow);
        document.getElementById('helpText').style.left = posArray[0] - 200 - leftOffset + "px";
        document.getElementById('helpText').style.top = posArray[1] + 25 + "px";
        document.getElementById('helpMain').innerHTML = helpText;
        $("#helpText").show();

        if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
            $("select").hide();
        }
    }
}

function HideHelp(objRow, helpText) {
    $("#helpText").hide();
    
    if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) { 
            $("select").show();
        }

}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

/* 
Updates the quantity of items in the basket. 
Called from code behind ShopingBasket and ProductDetails controls.
*/
function SetBasketItems(count, basketTable) {
    if (count == "") count = 0;
    if (document.getElementById("ucHeader_litOrderItems")) {
        $("#ucHeader_litOrderItems").html(count);

        if (basketTable != "") {
            $("#ucHeader_litShoppingBasket").html(basketTable);

            showBasket();
        }
    }
}

function showBasket() {
    $("#CartContents").fadeIn("slow");
    setTimeout(function() { $("#CartContents").fadeOut("slow"); }, 5000);
}

function hideBasket() {
    $("#CartContents").hide();
}

function OpenPrintOrderForm(url) {
    window.open(url, "PrintQuintessenceOrderForm", "", "");
}

function GetCookie() {
    var strName = "varCook";
    if (document.cookie.indexOf(strName) == -1) {
        return false;
    }
    else {
        cookieStart = document.cookie.indexOf(strName);
        cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
        cookieValEnd = document.cookie.indexOf(";", cookieStart);
        if (cookieValEnd == -1) {
            cookieValEnd = document.cookie.length;
        }
        cookieValue = document.cookie.substring(cookieValStart, cookieValEnd);
    }
    if (cookieValue = "True") {
        return ("True");
    }
}

function SetCookie(name, value, expires, path, domain, secure) {
    var strDNS = GetDNS();
    document.cookie = name + "=" + escape(value) +
	        ((expires) ? ";expires=" + expires.toGMTString() + 1000 * 60 * 20 : "") +
        	((path) ? ";path=" + path : "") +
    //( (domain) ? ";domain=" + domain : "") +
        	((domain) ? ";domain=" + strDNS : "") +
	        ((secure) ? ";secure" : "");
    return true
}

function GetDNS() {
    cookieDomain = document.location.hostname;
}
