 var DINEngschriftLT = {
  src: '/basemedia/flash/DINEngschriftLT.swf'
  ,ratios: [8, 1.41, 11, 1.31, 15, 1.29, 25, 1.25, 33, 1.23, 43, 1.22, 65, 1.21, 69, 1.2, 70, 1.21, 122, 1.2, 123, 1.19, 1.2]
};
sIFR.activate(DINEngschriftLT);

/*globals for home fader*/
var tid;
var count = 0;
var curr = 0;
var inAnimControl = false;

$(document).ready(function(){

    $("body").addClass("jsEnabled");

	/******************************************************************************/
	/*		Homepage jCarousel				*/
	//counter controls
	var count = 0;
	var codeString = "<ol>";
	$("#homepage .jcarousel-list li").each(function() {
		count++;
		if(count == '1')
		{
			codeString = codeString + "<li class='selected' id='"+count+"'><a href='#' class='jcarousel-control'>"+count+"</a></li>";
		}
		else
		{
			codeString = codeString + "<li class='' id='"+count+"'><a href='#' class='jcarousel-control'>"+count+"</a></li>";
		}
	});
	codeString = codeString + "</ol>";
	$("#homepage div.jcarousel-controls").html($("div.jcarousel-controls").html() + codeString);
    
        
	/*******************************************************/
	/*		Store Locator 			*/
	$(".locator ul li a").click(function()
	{
		var value = $(this).attr('href').replace("#", "");
		$("form fieldset select.state option").each(function(i)
		{ 
			if($(this).attr('value') == value)
			{
				//input into ddl the value selected
				$(this).attr('selected', "selected");
			}
			else
			{
				$(this).attr('selected', "");
			}
		});
	});
	/*******************************************************/
	/*		Store Homepage 			*/
	$("#homepage div.store div.map ul li a").click(function()
	{
		var value = $(this).attr('href').replace("#", "");
		$("fieldset select.state option").each(function(i)
		{ 
			if($(this).attr('value') == value)
			{
				//input into ddl the value selected
				$(this).attr('selected', "selected");
			}
			else
			{
				$(this).attr('selected', "");
			}
		});
	});
	/*******************************************************/
	/*		Store Locator Results		*/
	$(".locator-results ul li h4 a").click(function()
	{
		var unhide = $(this).attr('href').replace("#", "");
				
		$(".locator-results ul li div.details").addClass('hide');
		$(".locator-results ul li").removeClass('selected');
		
		$(".locator-results ul li div.details").each(function(i)
		{ 
			var id = $(this).attr('id');
			if(id == unhide)
			{
				$(this).removeClass('hide');
				$(this).parent().addClass('selected');
			}
		});
		
	});
	/********************************************************/
	/*			Locator check if only one store displaying				*/
	var storeCount = 0;
	$(".locator-results ul li").each(function() {
		storeCount++;
	});
	if(storeCount == '1')
	{
		//display the full details of the one store
		$(".locator-results ul li").addClass('selected');
		$(".locator-results ul li div.details").removeClass('hide');
		$(".locator-results div.paging").addClass('hide');
	}
	/********************************************************/
	/*		betta advice		*/
	$("#tabSet ul.tabs li").click(
		function()
		{
			$("#tabSet ul.tabs li").each(
				function(i)
				{
					$(this).removeClass("selected");
				}
			)
			$(this).addClass("selected");
			//change information to available to view
			var info = $(this).children().attr("href").replace("#", "");
			$("div.infoContainer").each(
				function(i)
				{ 
					if($(this).attr('id') == info)
					{
						$(this).removeClass("hide");
						$(this).find("ul.slide-grid").attr('id', "slide-grid")
						
					}
					else
					{
						$(this).addClass("hide");
						$(this).find("ul.slide-grid").removeAttr('id');
					}
				}
			)
			return false;
		}
	);
	/*slide grid*/
	$("ul.dynamicFeatures li a").click(
		function()
		{
			var checker = parseInt($(this).attr('href').replace("#", ""));
			var parentId = $(this).parent().parent().parent().attr('id');
			var slideto = 0;
			var counter = 0;
			$("ul.dynamicFeatures li a").each(
				function(i)
				{
					var currParentId = $(this).parent().parent().parent().attr('id');
					if(parentId == currParentId)
					{
						counter++;
						if(checker == $(this).attr('href').replace("#", ""))
						{
							slideto = counter;
						}
					}
				}
			)
			var top = 0; //position to move to top
			var left = 0; //position to move to left
			var height = 602;
			var element = slideto;//$(this).attr('href').replace("#", "");
			var count = 0;
			//get which row the element to show lies
			if(slideto > 3)
			{
				for(element=slideto; element > 3; element = element - 3)
				{
					count++;
				}
			}
			top = -(height * count);
			if(top == 0)
			{
				top = 1;
			}
			//get which column the element is in
			if(element == 1) //element in first column
			{
				left = 1;
			}
			else if(element == 2)//element in second column
			{
				left = -608;
			}
			else //element = 3 then third column
			{
				left = -1216;
			}
			
						
			//animate the sliding
			 if((window.XMLHttpRequest=='function(){return new ActiveXObject("Microsoft.XMLHTTP")}') || ($.browser.msie)) {
				$("ul#slide-grid").animate({top: top, left: left }, 'slow');
				
		 	}
			else {
				$("ul#slide-grid").css({top: top, left: left });
			}
			return false;
		}
	);
	
	/*flash inserts*/
	if(document.getElementById('flashCatalogue')) {
		//var so = new SWFObject("basemedia/flash/flipbook.swf", "sotester", "885", "634", "8", "#000000");
		var so = new SWFObject("/basemedia/flash/flipbook.swf", "sotester", "885", "634", "8", "#000000");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		so.addVariable("fv_cataloguePath", $("#flashCatalogue").attr("rel"));
		so.write("flashCatalogue");
	}
	if(document.getElementById("flashRotator")){
		var so = new SWFObject("basemedia/flash/rotator.swf", "sotester", "595", "262", "8", "#000000");
		var xml = $("#flashRotator").attr("rel");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		so.addVariable("xmlLocation", xml);
		so.write("flashRotator");
	}
	
	/*print page*/
	$("a.printpage").click(
	    function()
	    {
	        printPage();
	        return false;
	    }
	);
	
	/*scratch and win form field replacement*/
	if(document.getElementById("sawForm"))
	{
	    /*RADIO BUTTONS*/
	    oneselected = false;
	    firstin = true;
	    oneselected2 = false;
	    /*hide the radio buttons with the image version*/
	    $("#sawForm span.rd").each(
	        function(i)
	        {
	            $(this).parents("label").append("<a href='#' class='rd-overlay'></a>");
	            /*if($(this).children("input").attr("checked") == true)
	            {
	                oneselected = true;
	                $(this).siblings("a").addClass("rd-overlay-selected");	            
	            }
	            $(this).children("input").click(
	                function()
	                {
	                    $("#sawForm a.rd-overlay").removeClass("rd-overlay-selected");
	                    $(this).parents("span.rd").siblings().addClass("rd-overlay-selected");
	                }
	            );*/
	        }
	    );
	    
	    /*check if something in hidden input already*/
        gndr = $("#sawForm input.gender-holder").attr("value");
        if(gndr)
        {
            if(gndr.length > 0)
            {
                if(gndr == "Male" || gndr == "male")
                {
                    $("#sawForm label.male a.rd-overlay").addClass("rd-overlay-selected");
                    $("#sawForm label.female a.rd-overlay").removeClass("rd-overlay-selected");
                }
                else if(gndr = "Female" || gndr == "female")
                {
                    $("#sawForm label.male a.rd-overlay").removeClass("rd-overlay-selected");
                    $("#sawForm label.female a.rd-overlay").addClass("rd-overlay-selected");
                }
                else
                {
                }
            }
        }
        else
        {
        }
	    
	    //firstin = true;
	    $("#sawForm a.rd-overlay").click(
	        function()
	        {
	            if($(this).attr("class").match("selected"))
	            {}
	            else
	            {
	                foundon = false;
	                $("#sawForm a.rd-overlay").removeClass("rd-overlay-selected");
	                $(this).addClass("rd-overlay-selected");
	                gender = $(this).parents("label").attr("class");
	                if(gender.match("Female") || gender.match("female"))
	                {
	                    $("#sawForm input.gender-holder").attr("value", "Female");
	                }
	                else if(gender.match("Male") || gender.match("male"))
	                {
	                    $("#sawForm input.gender-holder").attr("value", "Male");
	                }
	                /* $("#sawForm span.rd input").each(
	                    function(i)
	                    {
	                        if(foundon == false)
	                        {
	                            if($(this).attr("checked") == true)
	                            {
	                            }
	                            else
	                            {
	                                this.checked = 'checked';
	                                foundon = true;
	                            }
	                        }
	                    }
	                 );*/
	            }
	            return false;
	        }
	    );
	    
	    $("#sawForm a.rd-overlay").focus(
	        function()
	        {
	            //alert("focused");
	            $("#sawForm a.rd-overlay").keypress(
	                function(e)
	                {
	                    var charCode = (e.which) ? e.which : e.keyCode;
	                    
	                    if(charCode == 32)
	                    {
	                        
	                            $("#sawForm a.rd-overlay").removeClass("rd-overlay-selected");
	                            $(this).addClass("rd-overlay-selected");
	                            gender = $(this).parents("label").attr("class");
	                            if(gender.match("Female") || gender.match("female"))
	                            {
	                                $("#sawForm input.gender-holder").attr("value", "Female");
	                            }
	                            else if(gender.match("Male") || gender.match("male"))
	                            {
	                                $("#sawForm input.gender-holder").attr("value", "Male");
	                            }
	                    }
	                }
	            );
	        }
	    );
	    
	    /*CHECKBOX*/
	    $("#sawForm span.chk").each(
	        function(i)
	        {
	            $(this).parents("label").append("<a href='#' class='chk-overlay'></a>");
	            $(this).children("input").click(
	                function()
	                {
	                    $(this).parents().siblings("a").toggleClass("chk-overlay-selected");
	                }
	            );
	            if($(this).children("input").attr("checked") == true)
	            {
	                $(this).siblings("a").toggleClass("chk-overlay-selected");
	            }
	        }
	    );
	    $("#sawForm a.chk-overlay").click(
	        function()
	        {
	            $(this).toggleClass("chk-overlay-selected");
	            if($(this).attr("class").match("selected"))
	            {
	                $(this).siblings("span.chk").children("input").attr("checked", "checked");
	            }
	            else
	            {
	                $(this).siblings("span.chk").children("input").attr("checked", "");
	            }
	            return false;
	        }
	    );
	    
	    $("#sawForm a.chk-overlay").focus(
	        function()
	        {
	            $("#sawForm a.chk-overlay").keypress(
	                function(e)
	                {
	                    var charCode = (e.which) ? e.which : e.keyCode;
	                    
	                    if(charCode == 32)
	                    {
	                        $(this).toggleClass("chk-overlay-selected");
                            if($(this).attr("class").match("selected"))
                            {
                                $(this).siblings("span.chk").children("input").attr("checked", "checked");
                            }
                            else
                            {
                                $(this).siblings("span.chk").children("input").attr("checked", "");
                            }
	                    }
	                }
	            );
	        }
	    );
	    
	    /*SELECT*/
	    /*build the js controlled object*/
	    $("#sawForm select.js-select").each(
	        function(i)
	        {
	            storesavail = false;
	            objid = $(this).attr("id");
	            //create the js object
	            $(this).parent().append('<div class="js-select '+objid+'"><div class="controls"><a href="#" class="slide-up"></a><a href="#" class="slide-down"></a></div><div class="scope"><ul></ul></div></div>');
	            liobjs = "";
	            $("select#"+objid+" option").each(
	                function()
	                {
	                    storesavail = true;
	                    //get the value of the object
	                    val = $(this).attr("value");
	                    //get the html of the object
	                    html = $(this).html();
	                    if(html == "Please Select" || html == "Please select" || html == "please select" || html == "Please select...")
	                    {/*do nothing*/}
	                    else
	                    {
	                        liobjs = liobjs + '<li id="'+val+'"><span>'+html+'</span><a href="#" class="overlay"></a></li>'
	                    }
	                }
	            ); 
	            $(this).siblings(".js-select").children(".scope").children("ul").html(liobjs);
	            //assign behaviour to the up and down buttons
	            $("."+objid+" div.controls a.slide-down").click(
	                function()
	                {
	                    //get the height of the li objs
	                    h = parseInt($("."+objid+" div.scope ul li").height()) + parseInt($("."+objid+" div.scope ul li").css("padding-bottom"));
	                    //get the number of objects
	                    n = 0;
	                    $("."+objid+" div.scope ul li").each(function(i){n++;});
	                    liobjshow = parseInt($("."+objid+" div.scope").height()) / h;
	                    maxtop = h * (n - liobjshow);
	                    if(maxtop < 0){maxtop = 0;}
	                    maxtop = maxtop*-1;
	                    
	                    currtop = parseInt($("."+objid+" div.scope ul").css("top"));
	                    if(currtop == maxtop)
	                    {
	                        //do nothing your at the max position
	                    }	                    
	                    else
	                    {
	                        newtop = currtop - h;
	                        $("."+objid+" div.scope ul").css("top", newtop+"px")
	                    }
	                    
	                    return false;
	                }
	            );
	            $("."+objid+" div.controls a.slide-up").click(
	                function()
	                {
	                    //get the height of the li objs
	                    h = parseInt($("."+objid+" div.scope ul li").height()) + parseInt($("."+objid+" div.scope ul li").css("padding-bottom"));
	                    //get the number of objects
	                   
	                    mintop = 0;
	                    
	                    currtop = parseInt($("."+objid+" div.scope ul").css("top"));
	                    if(currtop == mintop)
	                    {
	                        //do nothing your at the min position
	                    }	                    
	                    else
	                    {
	                        newtop = currtop + h;
	                        $("."+objid+" div.scope ul").css("top", newtop+"px")
	                    }
	                    
	                    return false;
	                }
	            );
	            //hover for ie6
	            $("."+objid+" div.scope ul li a.overlay").hover(
	                function()
	                {
	                   $(this).parents("li").addClass("hover");
	                },
	                function()
	                {
	                    $(this).parents("li").removeClass("hover");
	                }
	            );
	            
	            //assign behaviour to the list links
	            $("."+objid+" div.scope ul li a.overlay").click(
	                function()
	                {
	                    $("."+objid+" div.scope ul li").removeClass("selected");
	                    $(this).parents("li").addClass("selected");
	                    
	                    //change the option in the hidden related select to the option that was selected
	                    liid = $(this).parents("li").attr("id");
	                    $("select#"+objid).attr("value", liid);
	                    return false;
	                }
	            );
	            
	            //assign the current selected li
	            if($("select#"+objid).attr("value"))
	            {
	                //get the value of the option that is selected
	                selid = $("select#"+objid+" option:selected").attr("value");
	                $("."+objid+" div.scope ul li#"+selid).addClass("selected");
	            }
	            //hide the select
	            $(this).css("display", "none");
	            //on blur of postcode box display getting stores message
                $("label.postcode").siblings(".txt-tiny").children("input").blur(
                    function()
                    {
                        $("div.js-select").append("<div class='loader'><p>Please wait, Retrieving stores...</p></div>");
                    }
                );
	            //sifr the elements in the js-select
                sIFR.replace(DINEngschriftLT, {
	                selector: '.js-select ul li span',
	                css: [
                        '.sIFR-root { color: #ffffff; }'
                      ]
                });                
	        }
	    );
	   
	}
	
	 /*tooltip popup*/
    $("div.scratchandwin div.tooltip").hover(
        function()
        {
            $(this).children(".rollover").removeClass("hidden");
        },
        function()
        {  
            $(this).children(".rollover").addClass("hidden");
        }
    );
	
	/*saw result flash insert*/
	if(document.getElementById("sawResult"))
	{
	    //local
	    //var so = new SWFObject("basemedia/flash/scratch.swf", "sotester", "350", "320", "8", "#ffffff");
	    //server
	    var so = new SWFObject("/basemedia/flash/scratch.swf", "sotester", "350", "320", "8", "#000000");
	    var r = $("div.section-blue").attr("id");
	    var t = $("div.section-blue").attr("rel");
	    var l = $("div.section-blue a").attr("href");
	    //alert("r: " + r + "\nt: " + t + "\nl: " + l);
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		so.addVariable("passedresult", r);
		so.addVariable("passedtime", t);
		so.addVariable("passedlink", l);
		so.write("sawResult");
		
		
	}
	
	/*******************************************************/
	/*		Homepage Box Fader Locator 			*/
	if(document.getElementById("homeFader"))
	{
	    count = $("#homeFader div.scope div.item").length;
	    //assign z-index's to items to set depths
	    $("#homeFader div.scope div.item").css("z-index", "100").css("display", "none");
	    //showing z-index set to 101;
	    $("#homeFader div.scope div.item:first").css("z-index", "102").css("display", "block").css("position", "absolute").css("top", "0px").css("left", "0px");
	    $("#homeFader div.scope div.item").css("z-index", "100").css("position", "absolute").css("top", "0px").css("left", "0px");
	    
	    /*get the first link into the details link*/
	    var link = $("#homeFader div.scope div.item:first a").attr("href");
	    $("#homeFader").parents(".homepage-features-box").children(".details").attr("href", link);
	    
	    /*set up auto fade*/
	    tid = setTimeout("autoFader('next')", 4000);
	    
	    /*set up clicks*/
	    $("#homeFader a.next").click(
	        function()
	        {
	            if(inAnimControl == false)
	            {
	                clearTimeout(tid);
	                autoFader('next');
	            }
	            return false;
	        }
	    );
	    
	    $("#homeFader a.prev").click(
	        function()
	        {
	            if(inAnimControl == false)
	            {
	                clearTimeout(tid);
	                autoFader('prev');
	            }
	            return false;
	        }
	    );
	}
	
	/* lite box */
	$("a.ad-space-content").click(
	    function(){
	        var posY = document.documentElement.scrollTop
	        $("select").hide();
	        $(".site").append('<div class="lite-box"><div class="black-bg"></div><div class="lb-holder"><div class="lb-content"></div></div></div>');
	        $(".lite-box").css("top", posY);
	        $("BODY").css("overflow", "hidden");
	        $("HTML").css("overflow", "hidden");
	        resizeWindow();
	        var vidURL = $(this).attr("href");
	        vidURL = vidURL + " .content"
	        $(".lite-box .lb-content").load(vidURL,
	            function (){
	            	/* Register flash content. Requires "hasFlash" class in destination file */
	            	if ( $(".content").hasClass("hasFlash") )
	            	{
		                swfobjectNew.registerObject("FlashVideo", "9.0.0");
	            	}
	                $(".lb-header A.btn,.black-bg").click(
	                    function(){
	                        $(".lite-box").remove();
	                        $("select").show();
	                        $("BODY").css("overflow", "auto");
	                        $("HTML").css("overflow", "auto");
	                    }
	                )
	            }
	        );
	        return false;
	    }
	);
	$(window).resize(function(){
		if($("div").hasClass("lite-box"))
		{
			resizeWindow ();
		}
	});
	
	//air con calculator js
	
	
});

/* For lite box */
function resizeWindow () {
    var windowHeight = $(window).height();
    var contentTopPosition = (windowHeight - 300)/2;
    $(".lite-box .black-bg").height(windowHeight);
    $(".lite-box .black-bg").css("width", $(window).width());
    $(".lite-box").css("width", $(window).width());
    windowHeight = windowHeight-60
    var lbHolderObj = {
    	top: contentTopPosition + "px"
    }
    $(".lite-box .lb-holder").css(lbHolderObj );
}

function autoFader(dir)
{
    inAnimControl = true;
    var next;
    count = count = $("#homeFader div.scope div.item").length;
    
    if(dir == "next")
    {
        next = curr + 1;
        if(next == count)
        {
            next = 0;
        }
    }
    else if(dir == "prev")
    {
        next = curr - 1;
        if(next == -1)
        {
            next = count - 1;
        }
    }
    
    //set the item to fade in over the top of the curr item
    $("#homeFader div.scope div.item:eq("+next+")").css("z-index", "104");
    
    //fades
    $("#homeFader div.scope div.item:eq("+curr+")").fadeOut(1000)
    $("#homeFader div.scope div.item:eq("+next+")").fadeIn(1000, fadeComplete);
    curr = next;
}

function fadeComplete()
{
    prev = curr - 1;
    $("#homeFader div.scope div.item:eq("+prev+")").css("z-index", "100");
    $("#homeFader div.scope div.item:eq("+curr+")").css("z-index", "102");
    //update the box link to point to the now showing items link
    var link = $("#homeFader div.scope div.item:eq("+curr+")").children("a").attr("href");
    $("#homeFader").parents(".homepage-features-box").children(".details").attr("href", link);
    
    inAnimControl = false;
    tid = setTimeout("autoFader('next')", 4000);
}

function enableJSClass()
{
	document.body.className = "jsEnabled";
}

function printPage() { 

	if (window.print) { 
		window.print() ; 
	} else { 
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; 
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = ""; 
	}
}

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};

// AIR CON CALCULATOR FUNCTIONS

function Calculate () 
{
	id = jQuery("form").attr("id");
	form = document.getElementById(id);
	var size = 0;
	if(form.area.value == 0)
	{
		size = form.length.value * form.width.value;
	}
	else
	{
		size = form.area.value;
	}
	
	var kwresult =  size * GetRadioValue(form.daytime) * GetRadioValue(form.insulated) * GetRadioValue(form.west) * .001;
	var bturesult = size * GetRadioValue(form.daytime) * GetRadioValue(form.insulated) * GetRadioValue(form.west) * 3.142;
	
	form.kw.value = kwresult.toFixed(2);
    form.btu.value = bturesult.toFixed(2);
	//show the hidden inputs
	jQuery("#calcd").removeClass("offleft");
}

function GetRadioValue(radioObj)
{
	if(!radioObj)
		return 0;
		
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
		return 0;
		
	for(var i = 0; i < radioLength; i++)
	{	
		if(radioObj[i].checked == true) 
		{
			return radioObj[i].value;
		}
	}
}

function ResetArea()
{
	id = jQuery("form").attr("id");
	form = document.getElementById(id);
	form.area.value = 0;
}

function ResetLengthWidth()
{
	id = jQuery("form").attr("id");
	form = document.getElementById(id);
	form.length.value = 0;
	form.width.value = 0;
}