﻿
function collectionToArray(col) {
    a = new Array();
    for (i = 0; i < col.length; i++)
        a[a.length] = col[i];
    return a;
}

var tickerDuration = 10;
var noOfAdverts = 3;
var resetHighlightVert
var objCurrentLetterbox;
var iCurrentLetterbox;
var letterboxPaused;
var letterboxes;
var iLetterboxes;

var iNavTickers;
var aNavTickers;

var letterboxNav;
var iLetterboxNav

var iHighlight;

var bBusy = false;
var ajaxWrapper = '/local/library/ajaxPageLibHome.asp'

resetHighlightVert = 8;
var resetHighlightHorizontal = 748;

/* Test to see if IE6 / 7, as the highlight is in the wrong position due to styling of these browsers */

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
    var ieversion = new Number(RegExp.$1)
    if (ieversion <= 7) {
        resetHighlightHorizontal = 749;
        resetHighlightVert = 7;
    }
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function initLetterbox() {
	if($("LetterboxNav")){
		var thisLetterbox, thisLetterboxID;
		
		// Get all letterboxes
		letterboxes = $$("div .lbparent");
		
		letterboxes = collectionToArray(letterboxes);
		
		if (letterboxes.length > 0) {
			// Hide all letterboxes
			for (var i = 0; i < letterboxes.length; i++) {
				letterboxes[i].style.display = "none";
			}
	
			iLetterboxes = letterboxes.length;
	
			// Show the first letterbox
			iCurrentLetterbox = querySt("LetterboxID");
	
			if (isNaN(iCurrentLetterbox)) {
				iCurrentLetterbox = 0
			} else {
				iCurrentLetterbox = iCurrentLetterbox - 1
			}
	
			iHighlight = iCurrentLetterbox
	
			objCurrentLetterbox = letterboxes[iCurrentLetterbox];
			objCurrentLetterbox.style.display = "block";
			objCurrentLetterbox.style.zIndex = "100";
		}
	
		// Get the links for the letterbox
		letterboxNav = $("LetterboxNav").getElementsByTagName("a");
		iLetterboxNav = letterboxNav.length;
		
		
		if (iLetterboxNav > 0) {
	
			aNavTickers = $("LetterboxNav").getElementsByTagName("span");
			aNavTickers = collectionToArray(aNavTickers);
			iNavTickers = aNavTickers.length;
			if (iNavTickers > 0) {
				for (var i = 0; i < aNavTickers.length; i++) {
					if (aNavTickers[i].className == "ticker") {
					} else {
						aNavTickers.splice(i, 1);
						i--;
					}
				}
			}
			iNavTickers = aNavTickers.length;
	
			// Start loading letterbox pages
			if (letterboxes.length > 0) {
				loadLetterbox(iLetterboxNav + 1, 0); // totalLetterboxes, startingLetterbox
			}
			
			for (var i = 0; i < iLetterboxNav; i++) {
	
				letterboxNav[i].onclick = function() {
					thisLetterbox = this.getAttribute("rel").split("|");
					thisLetterboxID = thisLetterbox[1];
					thisLetterbox = parseInt(thisLetterbox[0]);
	
					if (bBusy == false) {
				   
						if (thisLetterbox != iCurrentLetterbox) {
							bBusy = true;
							
							resetTicker();
							invokeLetterbox(thisLetterbox, thisLetterboxID);
						} else {
							bBusy = false;
							startTicker(tickerDuration * ((240 - aNavTickers[iCurrentLetterbox].style.width.replace(/px,*\)*/g, "")) / 240));
							this.className = "selected";
						}
					}
	
	
	
					if (thisLetterbox < iCurrentLetterbox) {
						resetHighlightVert = resetHighlightVert + (-74 * (iCurrentLetterbox - thisLetterbox));
					} else {
						resetHighlightVert = resetHighlightVert + 74 * (thisLetterbox - iCurrentLetterbox);
					}
	
					//resetHighlightVert = resetHighlightVert + 74 * (thisLetterbox);
	
					//new Effect.Move('moveableHighlight', { x: resetHighlightHorizontal, y: resetHighlightVert, mode: 'absolute', duration: 0.5 });
					//iCurrentLetterbox = thisLetterbox;
					iHighlight = thisLetterbox;
					return false;
				}
	
				letterboxNav[i].onmouseover = function() {
					if (this.className != "selected" && this.className != "paused") {
						this.className = "moused";
					}
					return false;
				}
	
				letterboxNav[i].onmouseout = function() {
					if (this.className != "selected" && this.className != "paused") {
						this.className = "";
					}
					return false;
				}
			}
		}else{
			//Reset and run any flash videos for that letterbox
			try{
				//Close any existing flash items within the letterbox before running the latest letterbox content/flash
				closeDownFlashItems();
				runHomeLetterboxFlash(); 
			}catch(e){}	
		}
	
		letterboxNav[iHighlight].className = "selected";
		letterboxNav[iHighlight].parentNode.className = "highlight";
		
		startTicker(tickerDuration);
	}
}

function runHomeLetterboxFlash(){
	try{
		fanPush_FlashVideo();
		fanHot_FlashVideo();
	}catch(e){}
}

function fanPush_FlashVideo(){
	if($("fansPush_Letterbox")){
		if($("fansPush_Letterbox") && !($("fansPush_Letterbox_Flash"))){
			var so = new SWFObject("/common/swf/campaigns/am/fansHPLB.swf", "fansPush_Letterbox_Flash", "748", "306", "8", "#737481");
			so.addParam("allowScriptAccess", "sameDomain");
			so.addParam("menu", "false");
			try{
				so.write("fansPush_Letterbox"); 
			}catch(e){}
		}else if($("fansPush_Letterbox_Flash")){
			$("fansPush_Letterbox_Flash").removeClassName("hidden");	
		}	
	}
}

function fanHot_FlashVideo(){
	if($("fansHot_Letterbox")){
		if($("fansHot_Letterbox") && !($("fansHot_Letterbox_Flash"))){
			var so = new SWFObject("/common/swf/campaigns/am/white.swf", "fansHot_Letterbox_Flash", "748", "306", "8", "#000000");
			so.addParam("allowScriptAccess", "sameDomain");
			so.addParam("menu", "false");
			so.addVariable("buttonLabelTxt", "Learn more"); 
			so.addVariable("straplineTxt", "Heats the whole room faster than any other."); 			
			try{
				so.write("fansHot_Letterbox"); 
			}catch(e){}
		}else if($("fansHot_Letterbox_Flash")){
			$("fansHot_Letterbox_Flash").removeClassName("hidden");	
		}	
	}
}

function closeDownFlashItems(){
	var flashFiles, i;
	
	if($$("#hpLetterbox embed") || $$("#hpLetterbox object")){
		var flashEmbedItems = $$("#hpLetterbox embed");
		var flashObjectItems = $$("#hpLetterbox object");
		for(i = 0; i < flashEmbedItems.length ; i++){
			flashEmbedItems[i].addClassName("hidden");			
		}
		for(i = 0; i < flashObjectItems.length ; i++){
			flashObjectItems[i].addClassName("hidden");			
		}
	}	
}

function loadLetterbox(totalLB, iLetterboxLoad) {
    var thisLetterbox = letterboxNav[iLetterboxLoad].rel.split("|");
    if (thisLetterbox[0] < parseInt(totalLB)) {
        new Ajax.Updater('Letterbox' + parseInt(iLetterboxLoad + 1), ajaxWrapper + '?ajaxPageID=' + thisLetterbox[1], { method: 'get', onComplete: function () {

			//Run any flash videos for that letterbox
			try{
				runHomeLetterboxFlash(); 
			}catch(e){}
			
            loadLetterbox(totalLB, parseInt(iLetterboxLoad + 1));
            eventTracking(eventType, subType, actionType);
        }
        });
    }
}

function invokeLetterbox(iLetterbox, letterboxID) {

    objThisLetterbox = letterboxes[iLetterbox];

    // Turn off all link classes
    $$('#LetterboxNav a').each(function(linkClass) {
        linkClass.className = "";
        linkClass.parentNode.className = "";
    });

    bBusy = true;

    var resetHighlight
    if ((iLetterbox) < iCurrentLetterbox) {
        resetHighlight = -74 * (iCurrentLetterbox - (iLetterbox ));
    } else {
        resetHighlight = (74 * ((iLetterbox) - iCurrentLetterbox));
    }

    new Effect.Move('moveableHighlight', { x: 0, y: resetHighlight, duration: 0.3, afterFinish: function() { bBusy = false;} });
    iHighlight = iHighlight + 1;

    iCurrentLetterbox = iLetterbox

    letterboxNav[iLetterbox].className = "selected";
    letterboxNav[iLetterbox].parentNode.className = "highlight";

    $$("div .lbparent").each(function(letterboxDiv) {
        letterboxDiv.style.zIndex = "10";
    });


    objThisLetterbox.style.zIndex = "100";
	
	//Hide any flash videos for the letterbox that is about to be faded out
	try{
		//Close any existing flash items within the letterbox before running the latest letterbox content/flash
		closeDownFlashItems();
	}catch(e){}	
	
    new Effect.Appear(objThisLetterbox, { duration: 0.6, afterFinish: function () {
        objCurrentLetterbox.style.display = "none";
        objCurrentLetterbox = objThisLetterbox;
        iCurrentLetterbox = iLetterbox;
        aNavTickers[iCurrentLetterbox].style.width = "0px";
		
		//Run any flash videos for that letterbox
		try{
			runHomeLetterboxFlash(); 
		}catch(e){}	
		
        startTicker(tickerDuration);

        //bBusy = false;
    }
    });
}

function nextLetterbox() {
    var thisLetterbox;
	
	closeDownFlashItems();
    if (iCurrentLetterbox + 1 < iLetterboxes) {
		thisLetterbox = letterboxNav[iCurrentLetterbox + 1].getAttribute("rel").split("|");
        invokeLetterbox(iCurrentLetterbox + 1, thisLetterbox[1]);
    } else {
		thisLetterbox = letterboxNav[0].getAttribute("rel").split("|");
        invokeLetterbox(0, thisLetterbox[1]);
    }
}

function startTicker(iDuration) {
    new Effect.Morph(aNavTickers[iCurrentLetterbox], {
        queue: { position: 'front', scope: 'tickerQueue' },
        style: 'width:240px;', duration: iDuration, afterFinish: function() {
            bBusy = true;
            nextLetterbox();
        }
    });

}

function resetTicker() {
    Effect.Queues.get('tickerQueue').invoke('cancel');
}

document.observe("dom:loaded", function() { initLetterbox(); moveAdverts(); });
//Event.observe(window, 'load', function() { initLetterbox(); moveAdverts(); });

var iAdvertPosition;
var aAdverts;
var iAdvertQuantity;
var iAdvertWidth = 230;
var bAdvertBusy = false;

function moveAdverts() {
	if($("AdvertBack") &&  $("AdvertForward")){
		if (noOfAdverts < 2) {
			$("AdvertControls").className = "disNone";
		}
	
		$("AdvertBack").onclick = function() { moveAdvert(230); return false; }
		$("AdvertForward").onclick = function() { moveAdvert(-230); return false; }
	
		iAdvertPosition = 0;
	
		aAdverts = $("AdvertContainer").getElementsByTagName("div");
		aAdverts = collectionToArray(aAdverts);
	
		if (aAdverts.length > 0) {
			for (var i = 0; i < aAdverts.length; i++) {
				if (aAdverts[i].className = "advert") {
	
				} else {
					aAdverts.splice(i, 1);
					i--;
				}
			}
		}
		iAdvertQuantity = aAdverts.length;
	
		$("AdvertBack").style.display = "none";
		$("AdvertForward").style.display = "none";
		advertNavigation();
	}
}

function advertNavigation() {
    if (iAdvertPosition >= 0) {
        new Effect.Fade($("AdvertBack"), { duration: 0.5 });
    } else {
        new Effect.Appear($("AdvertBack"), { duration: 0.5 });
    }
    if (iAdvertPosition <= -((iAdvertQuantity * iAdvertWidth) - iAdvertWidth)) {
        new Effect.Fade($("AdvertForward"), { duration: 0.5 });
    } else {
        new Effect.Appear($("AdvertForward"), { duration: 0.5 });
    }
}

function moveAdvert(iDistance) {
    if (iAdvertPosition + iDistance <= 0 && iAdvertPosition + iDistance >= -((iAdvertQuantity * iAdvertWidth) - iAdvertWidth)) {
        iAdvertPosition += iDistance;
        advertNavigation();
        new Effect.Move($("AdvertContainer"), { x: iAdvertPosition, mode: 'absolute', duration: 0.5, afterFinish: function() {

        }
        });
    }
}

