﻿Element.implement({  
      //implement show  
    show: function() {  
        this.setStyle('display','');  
    },  
    //implement hide  
    hide: function() {  
        this.setStyle('display','none');  
    }  
});  

var Lingfield = new Class({
    options: {
        test : 5
        //onContentChanged: $empty
    },
    
    initialize: function(name, options){
    
        $('lingfield-banner').setStyle('opacity', 0);
        $('lingfield-banner').setStyle('visibility', 'hidden');
    
        this.CONTENT_HEIGHT_SECNAV  = 367;
        this.CONTENT_HEIGHT_TERTNAV = 338;
        this.NAV_WIDTH_FIX = 0;        
        
        this.features = {};        
        this.features.supportsFlash = true;
        this.features.flashLoaded = false;
        this.features.animate = false;
        this.features.customInitMessage = {};
        this.features.customInitMessage.text = '';
        this.features.customInitMessage.url = '';
        
        this.featureDetect = this.featureDetect.bind(this);
        this.featureDetect();       
        
        this.contentHeight = this.CONTENT_HEIGHT_SECNAV;        
        
        //start flash detection
        var objPlayerInfo = swfobject.getFlashPlayerVersion();
        
        if (this.features.supportsFlash && objPlayerInfo && objPlayerInfo.major > 7)
        {
            var swfParams = {
              wmode: "transparent", 
              allowScriptAccess:"always",
              swliveconnect:"true"
            };
            var attributes = { id:"lingfieldpointflash", name:"lingfieldpointflash" };
            swfobject.embedSWF("flash/lingfield_point.swf", "flash-content", "980", "615", "8.0.0", "expressInstall.swf", {}, swfParams, attributes);
            this.features.flashLoaded = true;
        }
        
        Shadowbox.init({skipSetup:true, handleOversize: 'none', overlayOpacity:0.8});
        
        this.loaderAnim = false;
        this.currentSection = 'home';
        this.contentOpen = false;
        this.currentSite = 'lingfield';
 		
		//process navigation
		this.contentElement = $('menu-content-inner');
        this.buildNav = this.buildNav.bind(this);
        this.buildNav();
        
        //build bottom panel/misc
        this.initLinks(this);
        
        this.showMainBar = function()
        {
            $('lingfield-banner').fade(1);
            if (window.location.hash.length > 0)
            {
                var objData = null;
            
                var hash = window.location.hash.replace('#', '');
                var arrHash = hash.split('/');
                if (arrHash[2]){ // If hasqs
                    
                    objData = {};
                    
                    var arrKeys = new Array();
                    var arrData = new Array();
                    
                    if (arrHash[2].indexOf('&') != -1)
                    {
                        var tmpArrData = arrHash[2].split('&');
                        for ( var i=0, len=tmpArrData.length; i<len; ++i ){
                            arrData = tmpArrData[i].split('=');
                            objData[arrData[0]] = arrData[1];
                        }
                    }
                    else
                    {
                        arrData = arrHash[2].split('=');
                        objData[arrData[0]] = arrData[1];
                    }
                    
                }
                this.loadContent.delay(100, this, [arrHash[0], arrHash[1], objData, true]);
            }
            else
            {
                this.changeSite(this.currentSite, true, false, false);//.bind(this);  
            }
            
            if (this.features.customInitMessage.text.length > 0)
            {
                this.showInfoBar(this.features.customInitMessage.text, this.features.customInitMessage.url, 'info');
            }
            else if(!this.features.flashLoaded)
            {
                this.showInfoBar('Please install Adobe Flash Player for an optimal browsing experience with this website.', 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash', 'info');
            }            
            
//            var self = this;
            
//            var historyChanged = function(location) {
//                if (location && location.lastIndexOf('/') != -1)
//                {
//                    var tmpArr = location.split('/');                              
//                    
//                    //test for exceptions (any pages using querystring data)
//                    if (tmpArr[1].toLowerCase().lastIndexOf('currentavailability-results') == -1 && tmpArr[1].toLowerCase().lastIndexOf('news-details') == -1 && tmpArr[1].toLowerCase().lastIndexOf('contact-brochurerequest') == -1)
//                    {
//                        alert('section: ' + self.currentSection);
//                        alert('newsection: ' + tmpArr[1].toLowerCase())
//                        //if (tmpArr[1].toLowerCase() != self.currentSection){  self.loadContent(tmpArr[0], tmpArr[1], null, true); }
//                    }
//                }
//            };   
            
            dsHistory.addFunction(this.showMainBar, this, null);     
            
//            dhtmlHistory.initialize();
//            dhtmlHistory.addListener(historyChanged);
        }                
        this.preload(this);
    },
    
    initLinks: function(scope){
        $('close-menu-header').hide();
        $('menu-content-inner').fade(0);
        $('flash-overlay').setStyle('visibility', 'hidden');
        
        var menucontent = $('menu-content');
        
        scope.morphOpen = new Fx.Morph('menu-content');
        scope.morphClose = new Fx.Morph('menu-content');
        scope.infoMorphOpen = new Fx.Morph('info-bar');
        scope.infoMorphClose = new Fx.Morph('info-bar');
        
        $('menu').addEvent('mouseenter', function(e) {
	        e.stop();
	        scope.contentElement.setStyle('height', scope.contentHeight);
	        scope.showContent(true);
        }.bind(scope));
        
        scope.morphOpen.addEvent('complete', function(e, arg1, arg2){
            //fired when content is fully open
            $('menu-content-inner').fade(1);
            scope.contentOpen = true;
            scope.flashScroll(true);
        }.bind(scope));    
        
        $('close-menu-header').addEvent('click', function(e) {
	        e.stop();
	        scope.contentElement.setStyle('height', 0);
	        scope.showContent(false);
        }.bind(scope));
        
        scope.morphClose.addEvent('complete', function(e, arg1, arg2){
            scope.contentOpen = false;
            scope.flashScroll(false);
        }.bind(scope));         
        
        $('logo').addEvent('click', function(e){
            e.stop();
            scope.changeSite('lingfield', true, true);
        }.bind(scope));
        
        scope.footerLinks = $('lingfield-footer').getElements('a');
                
        scope.footerLinks[1].addEvent('click', function(e){
            e.stop();
            if (!scope.contentOpen)
            {
                scope.showContent(true);
            }
            scope.loadContent('lingfield', 'Sitemap');        
        }.bind(scope));
        
        scope.footerLinks[2].addEvent('click', function(e){
            e.stop();
            if (!scope.contentOpen)
            {
                scope.showContent(true);
            }
            scope.loadContent('lingfield', 'Privacy');        
        }.bind(scope));
        
        $('home-search').addEvent('click', function(e){
            e.stop();
            if (!scope.contentOpen)
            {
                scope.showContent(true);
            }
            scope.loadContent('lingfield', 'CurrentAvailability-Results', {type: $('banner-search-type').get('value'), size: $('banner-search-size').get('value')});
                
        }.bind(scope));
        
        $('info-bar-close').addEvent('click', function(e){
            e.stop();
            scope.closeInfoBar();        
        }.bind(scope));
    },
    
    preload: function(scope){
        scope.fadeLoader.delay(200, scope, [false]);
        var imgs = [
                    //nav
                    'images/tabs/top-bg.gif',
                    'images/tabs/top-left.gif',
                    'images/tabs/top-left-lingfield.gif',
                    'images/tabs/top-left-on.gif',
                    'images/tabs/top-left-on-lingfield.gif',
                    'images/tabs/top-right.gif',
                    'images/tabs/top-right-on.gif',
                    //main 'bottom' bar
                    'images/menu/logo.gif',
                    'images/menu/menu.gif',
                    'images/menu/availability-bg.gif',
                    'images/menu/menu-arrow.gif',
                    'images/menu/menu-arrow-open.gif',
                    //headers
                    'images/header/lingfield.gif',
                    'images/header/beehive.gif',
                    'images/header/lingfieldhouse.gif',
                    'images/header/meadow.gif',
                    'images/header/woollofts.gif'
                    ];
        var imgLoader = new Asset.images(imgs, { onComplete: function(){ scope.showMainBar.delay(4000, scope); } });
    },
    
    buildNav: function(){
    
        this.topnav = new Element('ul', {id:'main-nav-flash'});
        this.secnav = new Element('div', {id:'main-nav-sec-wrapper'});
        this.secnav.adopt(new Element('div', {id:'main-nav-shim'}));
        this.tertnav = new Element('div', {id:'main-nav-tert-wrapper'});
        
        $('menu-content-nav').adopt(this.topnav);
        $('menu-content-nav').adopt(this.secnav);
        $('menu-content-nav').adopt(this.tertnav);
    
        this.sites = [];
        
        $$('#navigation-list .top-level').each(function(el, index, array){
            
            var tmpLI = el.clone(false, false);
            var tmpA = el.getFirst('a').clone(true, false);
            var tmpUL = el.getFirst('ul').clone(true, true);
            var tmpSubNav = null;
            tmpLI.set('id', 'top-nav-' + el.get('id'));
            if (index == 0) { tmpLI.addClass('first'); }
            if ((index + 1) == array.length) { tmpLI.addClass('last'); }
            
            if(tmpUL)
            {
                tmpSubNav = new Element('div', {id:'sec-nav-' + el.get('id'), 'class':'sec-nav'});
                this.sites.push(el.get('id'));
                tmpSubNav.adopt(tmpUL);
                this.secnav.adopt(tmpSubNav);
                tmpSubNav.hide();
                
                var tmpSecLIS = tmpUL.getElements('li.sec-level');
                tmpSecLIS.each(function(secEl, index, array){
                    //check for third level nav
                    var tmpSecUL = secEl.getFirst('ul');
                    if (tmpSecUL)
                    {
                        var tmpTertNav = new Element('div', {id:'tert-nav-' + secEl.get('id'), 'class':'tert-nav'});
                        var tmpTertUL = tmpSecUL.dispose();
                        if (tmpTertUL)
                        {
                            // get all tertiary level LIs and process them
                            var tmpTertLIS = tmpTertUL.getElements('li');
                            tmpTertLIS.each(function(tertEl, index, array){
                                tertEl.set('id', 'tert-level-' + tertEl.get('id'));
                                var tmpTertA = tertEl.getFirst('a');
                                tmpTertA.addEvent('click', function(e) {
                                    e.stop();
                                    var strHref = tmpTertA.href.substring(tmpSecA.href.lastIndexOf('Content') + 8).split('/');
                                    this.loadContent(strHref[0].toLowerCase(), strHref[1].replace('.aspx', ''));
            			            
                                }.bind(this));
                            }.bind(this));
                            tmpTertNav.adopt(tmpTertUL);
                        }
                        
                        this.tertnav.adopt(tmpTertNav);
                    }
                    //second level click - change the section
                    var tmpSecA = secEl.getFirst('a');
                    tmpSecA.addEvent('click', function(e) {
                        e.stop();
                        var strHref = tmpSecA.href.substring(tmpSecA.href.lastIndexOf('Content') + 8).split('/');
                        this.loadContent(strHref[0].toLowerCase(), strHref[1].replace('.aspx', ''));
                    }.bind(this));
                    secEl.set('id', 'sec-level-' + secEl.get('id'));
                }.bind(this));
            }
            
            if (tmpSubNav)
            {
                //top level click - change the 'site'
                tmpA.addEvent('click', function(e) {
                    e.stop();
                    this.changeSite(el.get('id'), true);//.bind(this);
                }.bind(this));
            }
            
            tmpLI.adopt(tmpA);
            this.topnav.adopt(tmpLI);
            
        }, this);        
        
        var magpie = $('magpie').clone(true, false);
        magpie.set('id', 'top-nav-magpie');
        
        if (this.NAV_WIDTH_FIX != 0) { magpie.getFirst('a').setStyle('margin-left', 126 + this.NAV_WIDTH_FIX) }
        this.topnav.adopt(magpie);
        
        var thefuture = $('thefuture').clone(true, false);
        thefuture.set('id', 'top-nav-thefuture');
        this.topnav.adopt(thefuture);
    },
    
    featureDetect: function() {
        //check for Safari on Mac, nav width bug
        if (Browser.Engine.webkit)
        {
            this.NAV_WIDTH_FIX = -4;
        }
        
        if (Browser.Engine.gecko)
        {
            if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
            {
                var ffversion = new Number(RegExp.$1);
                if (ffversion >=3)
                { 
                    this.features.supportsFlash = true; 
                }
                else
                {
                    this.features.supportsFlash = false;
                    this.features.customInitMessage.text = 'Please upgrade to Firefox 3, for an optimal browsing experience with this website.';
                    this.features.customInitMessage.url = 'http://www.getfirefox.com/'; 
                }
                
            }
        }
        else
        {
            this.features.supportsFlash = true;
        }
        
        this.features.animate = false;
    },
    
    flashScroll: function (blnScroll) {
        var obj = this.getFlash();
        if (obj && typeof obj.flashScroll != "undefined") {
            if (blnScroll)
            {
                obj.flashScroll("true");
            }
            else
            {
                obj.flashScroll("false");
            }
        }    
    },
    
    changeFlashBackground : function (strBackground) {
        if(!this.features.flashLoaded)
        {
            $('flash-content').setStyle('background', 'transparent url(images/' + strBackground + '/Home/flash-bg.jpg) no-repeat scroll left top');
        }
        else
        {
            var obj = this.getFlash();        
            if (obj && typeof obj.changeBackgroundFlash != "undefined") { obj.changeBackgroundFlash(strBackground, 'true'); }    
        }
    },
    
    changeSite: function(siteID, blnGoHome, blnOpenIfClosed, trackHistory){
        $$('div.tert-nav').each(function(el, idx, arr){
            el.hide();
            el.getElements('li.tert-level').each(function(el){
                el.removeClass('current');
            });
        });
    
        this.sites.each(function(site, index, array){
            $('sec-nav-' + site).hide();
            $('header-' + site).hide();
            $('top-nav-' + site).removeClass('current');
            if ($('top-nav-' + site).getFirst('a')) { $('top-nav-' + site).getFirst('a').removeClass('current'); }
        });
        
        $('sec-nav-' + siteID).show();
        $('header-' + siteID).show();
        $('top-nav-' + siteID).addClass('current');
        if ($('top-nav-' + siteID).getFirst('a')) { $('top-nav-' + siteID).getFirst('a').addClass('current'); }
        if (siteID != this.currentSite) { this.changeFlashBackground(siteID); }
        
        this.currentSite = siteID;        
        if (blnGoHome && blnGoHome == true) { this.loadContent(this.currentSite, 'home', null, false, trackHistory);}
        if (blnOpenIfClosed && this.contentOpen == false) { this.showContent(true); }
    },
    
    fadeLoader: function(blnShow){
       if (this.contentOpen)
       {
            var loader = new Fx.Morph('menu-content-loading', {duration:'350'});
            var overlay = new Fx.Morph('loading-overlay', {duration:'350'});
           
            if(blnShow)
            {
                overlay.start({'opacity':0.1}).chain(function(){
                    loader.start({'opacity':1});
                }.bind(this));
            }
            else
            {
                loader.start({'opacity':0}).chain(function(){
                    overlay.start({'opacity':0});
                });
            }
        }
    },
    
    getFlash: function ()
    {
        return document.getElementById("lingfieldpointflash");
    },
    
    generateRandom : function(iLower, iUpper)
    {
        var iRnd = (Math.floor(Math.random()*(iUpper-iLower))+ iLower);
        //var iRnd = (Math.round((iUpper-1)*Math.random())+1);
        return iRnd;
    },
    
    showInfoBar: function(message, url, cls)
    {
        var clsEl = $('info-bar').getFirst('p');
        var msgEl = $('info-bar-link');
        if (url && url.length > 0)
        {
            msgEl.addEvent('click', function(e){
                e.stop();
                window.open(url);
            });
        }
        else
        {
            msgEl.addEvent('click', function(e){
                e.stop();
            });
        }
        msgEl.set('text', message);
        clsEl.erase('class');
        clsEl.addClass(cls);
        this.infoMorphOpen.start('div.info-bar-open');
    },    
    
    closeInfoBar: function()
    {
        this.infoMorphClose.start('div.info-bar-closed');
    },
    
    showContent: function(blnShow)
    {
        if (blnShow)
        {
            this.morphOpen.start('div.menu-content-open');
	        $('flash-overlay').fade(1);
	        $('close-menu-header').show();
	        $('open-menu-header').hide();
        }
        else
        {
            $('menu-content-inner').fade(0);
	        this.morphClose.start('div.menu-content-closed');
	        $('flash-overlay').fade(0);
	        $('close-menu-header').hide();
	        $('open-menu-header').show();
        }
    },
    
    loadContentFromHistory: function(params, historyObj){
        
//            //alert('boo');
//            var prms = strSiteID;
//            var history = strContentID;
//            strSiteID = strSiteID.strSiteID;
//            strSiteID = strSiteID.strContentID;
//            strSiteID = strSiteID.objData;
//            strSiteID = strSiteID.blnShowContent;
//            strSiteID = strSiteID.trackHistory;
//            
//            historyObject = history.historyObject;
            
        this.loadContent(params.strSiteID, params.strContentID, params.objData, params.blnShowContent, params.trackHistory, historyObj);
        
    },
    
    loadContent: function(strSiteID, strContentID, objData, blnShowContent, trackHistory, historyObject){
        
        
        
        this.changeSite(strSiteID, false);//.bind(this);
        //update our 'current' section reference        
        if (trackHistory == null || trackHistory == true)
        {
		    window.location.hash = this.currentSite + '/' + strContentID;
		    if (!historyObject || (historyObject && !historyObject.calledFromHistory)) {
		      dsHistory.addFunction(this.loadContentFromHistory, this, {'strSiteID':strSiteID, 'strContentID':strContentID, 'objData':objData, 'blnShowContent':blnShowContent, 'trackHistory':trackHistory});
            }

		}
		
		if (window.pageTracker)
		{
    		window.pageTracker._trackPageview('/' + this.currentSite + '/' + strContentID);
    	}
    	else
    	{
    	    var googleJS = new Asset.javascript( 'http://www.google-analytics.com/ga.js',    { id : 'googlejs', onload : function() { window.pageTracker = _gat._getTracker("UA-482175-9"); window.pageTracker._trackPageview('/' + this.currentSite + '/' + strContentID);} } );
    	}
    		
        this.contentElement.fade(0);
        
        $$('li.sec-level').each(function(el){ el.removeClass('selected'); });        
        $$('li.tert-level').each(function(el){ el.removeClass('selected'); });
        
        var secNavItem = $('sec-level-' + strSiteID.toLowerCase() + '-' + strContentID.toLowerCase());
        if (secNavItem) { secNavItem.addClass('selected'); }
        
        if (strContentID.indexOf('-') > -1)
        {
            //we have a "tertiary" link
            var strContentArr = strContentID.split('-');
            var tertNav = $('tert-nav-' + strSiteID + '-' + strContentArr[0].toLowerCase());
            if (tertNav)
            {
                tertNav.show();
                var tertNavLI = $('tert-level-' + strSiteID + '-' + strContentID.toLowerCase());
                if (tertNavLI)
                {
                    tertNavLI.addClass('selected');
                }
                this.contentElement.setStyle('height', this.CONTENT_HEIGHT_TERTNAV);
                this.contentHeight = this.CONTENT_HEIGHT_TERTNAV;
            }
            else
            {
                this.contentElement.setStyle('height', this.CONTENT_HEIGHT_SECNAV);
                this.contentHeight = this.CONTENT_HEIGHT_SECNAV;
            }
            
            var secNavItem = $('sec-level-' + strSiteID.toLowerCase() + '-' + strContentArr[0].toLowerCase());
            if (secNavItem)
            {
                secNavItem.addClass('selected');
            }
        }
        else if ($('tert-nav-' + strSiteID + '-' + strContentID.toLowerCase()))
        {
            $('tert-nav-' + strSiteID + '-' + strContentID.toLowerCase()).show();
            this.contentElement.setStyle('height', this.CONTENT_HEIGHT_TERTNAV);
            this.contentHeight = this.CONTENT_HEIGHT_TERTNAV;
        }
        else
        {
            this.contentElement.setStyle('height', this.CONTENT_HEIGHT_SECNAV);
            this.contentHeight = this.CONTENT_HEIGHT_SECNAV;
        }        
        
        var options = {url:'Content/' + this.currentSite + '/' + strContentID + '.aspx', evalScripts: false, async: true}
        if (objData) { options.data = objData; }
        
        var req = new Request.HTML(options, this);
	    
	    req.addEvent('onFailure', function() {
	        this.contentElement.set('text', 'The request failed.');
			this.fadeLoader.delay(1000, this, [false]);
		    this.contentElement.fade(0.99);
		    this.fireEvent('onContentChanged', false);
	    }.bind(this));
	    
	    req.addEvent('onComplete', function(responseTree, responseElements, responseHTML, responseJavaScript) {
		        
		        //fade existing content out
		        this.contentElement.fade(0);		        
		        this.currentSection = strContentID;
		        
			    //clear existing content
			    this.contentElement.set('text', '');
			    
			    //get all the anchors from the 'new' content
			    var anchors = [];
			    
			    responseElements.each(function(el){
			        //first lets check for anchors
			        if (el.nodeName.toLowerCase() == "a")
			        {
			            anchors.push(el);
			        }
			        if (el.nodeName.toLowerCase() == "div")
			        {
			            if (el.id == "content-wrapper")
			            {
			                if (el.hasClass('hascss'))
			                {
			                    var cssDiv = el.getFirst('div');
			                    if(cssDiv)
			                    {
			                        var blnIsCssLoaded = false;
			                        var strCssPath = cssDiv.get('id');
			                        strCssPath = strCssPath.replace('-', '/').toLowerCase();
			                        
			                        for ( var i=0, len=document.styleSheets.length; i<len; ++i ){
			                            
			                            if (document.styleSheets[i] && document.styleSheets[i].href)
			                            {
			                                if (document.styleSheets[i].href.toLowerCase().indexOf(strCssPath) != -1)
			                                {
			                                    blnIsCssLoaded = true;
			                                }
			                            }
			                        }			                        
			                        if (!blnIsCssLoaded) { var myCSS = new Asset.css('css/' + strCssPath + '.css'); }
			                    }
			                }
			            }
			        }
			    }.bind(this));
			    
			    //loop through them all
			    anchors.each(function(anchor){
			        //this.contentProgressBar.set(30, 'Processing links...');
			        //manipulte the anchors href to get the section id
			        if (!(anchor.rel && (anchor.rel == "nojs" || anchor.rel == "shadowbox")))
			        {
			            //add click event to change content rather than follow the link
			            anchor.addEvent('click', function(e) {
		                    //stop the anchor opening the link
		                    e.stop();
		                    //change the content
		                    if (anchor.rel == "hasqs")
		                    {
		                        var tmpArr = anchor.href.split('?')
		                        var strHref = tmpArr[0];
		                        strHref = strHref.substring(strHref.lastIndexOf('Content') + 8).split('/');
		                       
		                        var objData = {};
		                        var arrKeys = new Array();
		                        var arrData = new Array();
		                        
		                        if (tmpArr[1].indexOf('&') != -1)
		                        {
		                            var tmpArrData = tmpArr[1].split('&');
		                            for ( var i=0, len=tmpArrData.length; i<len; ++i ){
                                        arrData = tmpArrData[i].split('=');
		                                objData[arrData[0]] = arrData[1];
                                    }
		                        }
		                        else
		                        {
		                            arrData = tmpArr[1].split('=');
		                            objData[arrData[0]] = arrData[1];
		                        }
		                        this.loadContent(strHref[0].toLowerCase(), strHref[1].replace('.aspx', ''), objData);
		                    }
		                    else
		                    {
		                        var strHref = anchor.href.substring(anchor.href.lastIndexOf('Content') + 8).split('/');
                                this.loadContent(strHref[0].toLowerCase(), strHref[1].replace('.aspx', ''));
                            }
    			        
	                    }.bind(this));
			        }
			        
			    }, this);			    
			    
		        //Inject the new DOM elements into the content div.
                this.contentElement.adopt(responseTree);
               
                if(blnShowContent && this.contentOpen == false) { this.showContent.delay(250, this, [true]); }
	            
                //fade out our load progress
                this.fadeLoader.delay(1000, this, [false]);;
			    
			    this.fireEvent('onContentChanged', true);	            
			    
			    eval(responseJavaScript);
			     
                //fade in our content
                this.contentElement.fade(1);
                
		    }.bind(this));
	    
	    //$('test-wrapper').setStyle('visibility', 'hidden');
	    
	    req.setHeader('IsLFAjaxReq','true');
	    this.fadeLoader(true);
	    this.fireEvent('onContentChanging', false);
	    req.get();
    }
});
Lingfield.implement(new Options, new Events);

Lingfield.Fx = {};

Lingfield.Fx.rotatorLayerCache = new Array();
Lingfield.Fx.rotatorStatusCache = new Array();
Lingfield.Fx.rotatorIntervalCache = new Array();

Lingfield.Fx.setupRotator = function(divid, delay){
    //add layers to cache
    
    Lingfield.Fx.rotatorLayerCache[divid] = $$('#' + divid + ' img'); 
    Lingfield.Fx.rotatorStatusCache[divid] = 0;
    delay = (delay == undefined) ? 6000:delay; 
    
    Lingfield.Fx.rotatorIntervalCache[Lingfield.Fx.rotatorIntervalCache.length] = setInterval(function(){ 
        Lingfield.Fx.rotateSlide(divid)
    }, delay);     
};

Lingfield.Fx.rotateSlide = function(divid){
    
    
    Lingfield.Fx.rotatorLayerCache[divid][Lingfield.Fx.rotatorStatusCache[divid]].fade('out', {});     
    Lingfield.Fx.rotatorStatusCache[divid] = (Lingfield.Fx.rotatorStatusCache[divid] == Lingfield.Fx.rotatorLayerCache[divid].length - 1) ? 0 : Lingfield.Fx.rotatorStatusCache[divid]+1;     
    Lingfield.Fx.rotatorLayerCache[divid][Lingfield.Fx.rotatorStatusCache[divid]].fade('in');     
};

Lingfield.Fx.clearRotatorCache = function(){

    for ( var i=0, len=Lingfield.Fx.rotatorIntervalCache.length; i<len; ++i ){
        window.clearInterval(Lingfield.Fx.rotatorIntervalCache[i]);
    }
    Lingfield.Fx.rotatorLayerCache = new Array();
    Lingfield.Fx.rotatorStatusCache = new Array();
    Lingfield.Fx.rotatorIntervalCache = new Array();  
};