

function collectionToArray(col) {
    a = new Array();
    for (i = 0; i < col.length; i++)
        a[a.length] = col[i];
    return a;
}

var tickerDuration = 13;
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() {

    // 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() {
                var thisLetterbox = this.getAttribute("rel").split("|");
                thisLetterbox = parseInt(thisLetterbox[0]);

                if (bBusy == false) {
               
                    if (thisLetterbox != iCurrentLetterbox) {
                        bBusy = true;
                        
                        resetTicker();
                        invokeLetterbox(thisLetterbox);
                    } 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{
		fan_FlashVideo();
	}catch(e){}
}

function fan_FlashVideo(){
	if($("fansTime_Letterbox")){
		if($("fansTime_Letterbox") && !($("fansTime_Letterbox_Flash"))){
			var so = new SWFObject("/common/swf/campaigns/am/digitsV2.swf", "fansTime_Letterbox_Flash", "748", "306", "8", "#000000");
			so.addParam("allowScriptAccess", "sameDomain");
			so.addParam("menu", "false");
			so.addVariable("dateString", "15.09.2011"); 
			so.addVariable("strapline", "New Dyson technology"); 
			so.addVariable("maxNum", "37");
			try{
				so.write("fansTime_Letterbox"); 
			}catch(e){}
		}else if($("fansTime_Letterbox_Flash")){
			$("fansTime_Letterbox_Flash").removeClassName("hidden");	
		}	
	}

    
	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", "Find out more"); 
			so.addVariable("straplineTxt", "The fastest to heat the room evenly."); 			
			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) {

    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() {
   
    closeDownFlashItems();
    if (iCurrentLetterbox + 1 < iLetterboxes) {
        invokeLetterbox(iCurrentLetterbox + 1);
    } else {

        invokeLetterbox(0);
    }
}

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 (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() {

        }
        });
    }
}

