﻿/*  THIS TICKER SCRIPT REQUIRES THE JQUERY LIBRARY TO RUN  */

//  ---------------------------------------------------------------------------
//	variables
//  ---------------------------------------------------------------------------

    var aPreloadImages		= new Array();	//	preloaded images 
    var currentImage		= null;			//	current image being displayed
    var isRunning			= false;		//	flag to mark whether the animation is currently running or not
    var oPreloadTimer		= null;			//	preload images auto swap
    var iPreloadTimerLength	= 10000;		//	timer length for preload images auto swap


    
//  ---------------------------------------------------------------------------
//	homepage promo banner image preloader
//  ---------------------------------------------------------------------------

    $(document).ready(function() {

        //  main homepage promotion banner
        $("#PromoBannerNav a").each(function() {
            if (this.getAttribute("rel").match("preloadImage")) {
    	
			    //	create new image and create flag to check if image is loaded
			    var oImg	    = new Image();
			    oImg.srcToLoad  = this.getAttribute("rel").split(":")[1];
			    oImg.isLoaded   = false;
			    oImg.url        = this.href;
			    oImg.onload     = function () {
			        this.isLoaded = true;
			    }
			    
			    //  store new image in array
			    aPreloadImages.push(oImg);
    			
			    //	store current image number (array items start at 0 - length starts at 1) and bind click event
			    this.imgNum = aPreloadImages.length - 1;
			    $(this).bind("click", function() {
				    setPreloadImage(this.imgNum);
			    });

		    //	previous link
		    } else if (this.getAttribute("rel") == "preloadNavPrev") {
			    $(this).bind("click", function() {
				    setPreloadImage(currentImage - 1);
			    });
    			
		    //	next link
		    } else if (this.getAttribute("rel") == "preloadNavNext") {
			    $(this).bind("click", function() {
				    setPreloadImage(currentImage + 1);
			    });
		    }
        });
        
        //  fade in the navigation
        if (aPreloadImages.length > 1) {
            $("#PromoBannerNav").fadeIn("slow");
        }
        
        //	display the first image
	    if (aPreloadImages.length != 0) {
		    setPreloadImage(0);
	    }
    });
    
    function setPreloadImage(num) {
        //	run animation if the image is not already displayed and the isRunning flag is false
	    if (currentImage != num && isRunning == false) {
	    
	        //	flag animation as running and update the current image number
		    isRunning		= true;
		    currentImage	= num;
		    
		    //	check current image numbers for moving to the start and end of the preloaded images array
		    if (currentImage < 0) {
			    currentImage = aPreloadImages.length - 1;
		    } else if (currentImage == aPreloadImages.length) {
			    currentImage = 0;
		    }
		    
		    //  check image src
	        if (aPreloadImages[currentImage].src == "") {
	            aPreloadImages[currentImage].src = aPreloadImages[currentImage].srcToLoad;
	        }
	        
	        //  show the image: unbind the current click event, set cursor to default (show that it's not clickable) and fade out image
            $("#BannerImage").unbind("click").css("cursor", "default").fadeOut("slow", function() {
                showPreloadImage();
            });
	    }
    }
	    
    function showPreloadImage() {
        //  if the image has successfully loaded, fade out current image and fade in new one
        if (aPreloadImages[currentImage].isLoaded == true) {
	        
	        //	stop the timer
		    stopPreloadTimer();
	        
		    //	create the image to be inserted and fade in image
	        $("#BannerImage").html("<img src='" + aPreloadImages[currentImage].src + "' />").fadeIn("slow", function() {
			
		        //	change the cursor to pointer (show that the image is clickable) and bind new click event to load url
		        $(this).css("cursor", "pointer").bind("click", function() {
		        
		            var url = aPreloadImages[currentImage].url;
		            
		            if (url.match("http://"))
		            {
		                parent.document.location.href = url;
		            }
		            else
		            {
		                var domains = new Array("http://local.dialaphone.co.uk/", "http://stagingredesign.dialaphone.co.uk/", "http://www.dialaphone.co.uk/", "http://local.mobileexpress.co.uk/", "http://stagingredesign.mobileexpress.co.uk/", "http://www.mobileexpress.co.uk/");
    		            
		                for (var i = 0; i < domains.length; i++) {
		                    if (url.match(domains[i])) {
		                        url = url.replace(domains[i], "");
		                    }
		                }
    		            
		                if (parent.siteUrl) {
			                parent.document.location.href = parent.siteUrl + url;
			            }
			        }
		        });
		        
				
		        //	start the timer
		        if (aPreloadImages.length > 1) {
		            startPreloadTimer();
		        }
	
		        //	end - flag animation as stopped
		        isRunning = false;
	        });
	    } else {
	        setTimeout("showPreloadImage()", 1000);
	    }
    }
    
    function stopPreloadTimer() {
	    clearTimeout(oPreloadTimer);
    }

    function startPreloadTimer() {
	    //	make sure the timer has stopped
	    stopPreloadTimer();
	    oPreloadTimer = setTimeout("setPreloadImage(" + (currentImage + 1) + ")", iPreloadTimerLength);
    }