/*
 * The "encLoadBanner()" is the main function to start the banner.
 * It can be placed inside "$(document).ready(function()" jquery document ready
 * funciton or inside the "onload=function()" javascript document onload function.
 * both will work. The advantage of placing inside "onload=function()" is, 
 * the banner image will be loaded after the complete website rendered in to browser.
 */


onload=function()
{
    encLoadBanner();
}

encLoadBanner = function()
{
    encImg = encPreloadImages(encImageArray, encNumOfImages);
    if(encEnableFooter)
    {
        encThumbs = encPreloadImages(encThumbNailImageArray, encNumOfImages);        
    }
    
    jQuery("#bannerBody").html("");
    
    jQuery("div#bannerContainerCover").css("background-image", "url(" + encImg[encImg.length-1].src + ")");
    encTransformBanner(0);
    
    if(encEnableFooter)
    {
        var tmpCellWidth = Math.floor(encBannerWidth/encNumOfImages);
        var tmpLastCellWidth = tmpCellWidth + (encBannerWidth%encNumOfImages);
        
        var footerContents = "<table cellpadding='0' cellspacing='0' width='" + encBannerWidth + "' align='center'><tr>";
        for(i=0; i<encNumOfImages; i++)
        {
            if((i+1) == encNumOfImages)
                footerContents += "<td class='footerCell' width='" + tmpCellWidth + "'>";
            else
                footerContents += "<td class='footerCell' width='" + tmpLastCellWidth + "'>";
                
            footerContents += "<div class='imgBgDiv_i' style='background:url(fileadmin/template01/img/thumbnailbg.jpg); padding:2px 0 0 2px; margin-right:5px;'><div id='thumbDiv_" + i + "' class='imgDiv' style='background:url(" + encThumbs[i].src + ") 0px 0px no-repeat;'></div></div>";
            footerContents += "<ul id='footerContents_" + i + "'>";
            footerContents += "<li class='footerTitle'></li>";
            footerContents += "<li class='footerDesc'></li>";
            footerContents += "<li class='footerLink'></li>";
            footerContents += "</ul>";
            footerContents += "</td>";
        }
        footerContents += "</tr></table>";
        jQuery("#bannerFooterNav").html(footerContents);
        jQuery("#bannerFooter").fadeTo("fast", 0.4);
        showFooter();
        jQuery(".imgDiv").fadeTo("fast", 0.75);
    }
    
    
    if(! encDisableTextsAll && encEnableFooter)
    {
        for(i=0; i<encNumOfImages; i++)
        {
            jQuery(".footerTitle:eq(" + i + ")").html(encBannerTexts[i][0]);
            if(encEnableDescription) jQuery(".footerDesc:eq(" + i + ")").html(encBannerTexts[i][1]);
            if(encBannerTexts[i][2] != "" & encEnableReadMore)
            {
                jQuery(".footerLink:eq(" + i + ")").html("<div class='bttnMore' ><a id='linkMore" + i + "' href='" + encBannerTexts[i][2] + "'>Details</a></div>");
            }
        } 
    }
    
    if(encEnableThumbImageLink && encEnableFooter)
    {
        for(i=0; i<encNumOfImages; i++)
        {
            link = encBannerTexts[i][2];
            jQuery("#thumbDiv_" + i).attr("onclick", "window.location.href='" + link + "'");
			
            //jQuery("#linkMore" + i).attr("onfocus", "alert('TEST');encTransformBanner(" + i + ");");
        }
		jQuery("#linkMore0").focus(function() {encTransformBanner(0); encAutoRotateBanner =false;});
		jQuery("#linkMore1").focus(function() {encTransformBanner(1); encAutoRotateBanner =false;});
		jQuery("#linkMore2").focus(function() {encTransformBanner(2); encAutoRotateBanner =false;});
		jQuery("#linkMore3").focus(function() {encTransformBanner(3); encAutoRotateBanner =false;});

		jQuery("#linkMore0").blur(function() {var tmpBannerTimer = setTimeout("encAutorotate(1)", encAutoRotateTimeout); encAutoRotateBanner =true;});
		jQuery("#linkMore1").blur(function() {var tmpBannerTimer = setTimeout("encAutorotate(2)", encAutoRotateTimeout); encAutoRotateBanner =true;});
		jQuery("#linkMore2").blur(function() {var tmpBannerTimer = setTimeout("encAutorotate(3)", encAutoRotateTimeout); encAutoRotateBanner =true;});
		jQuery("#linkMore3").blur(function() {var tmpBannerTimer = setTimeout("encAutorotate(0)", encAutoRotateTimeout); encAutoRotateBanner =true;});


	}
    
    if(encEnableFooter)
    {
        a=0;
        jQuery(".imgDiv").each(function()
        {
            var ids = a;
            jQuery(this).mouseover(function()
            {
                jQuery(this).fadeTo("fast", 1);
                var cssObj = {"margin-top": "0px", "margin-bottom": "0px"}
                jQuery(this).css(cssObj);
                encBusy = true;
                encTransformBanner(ids);
                jQuery(document).pngFix();
            })
            a++;
        }).mouseout(function() {
           jQuery(this).fadeTo("fast", 0.75);
           var cssObj = {"margin-top": "0px", "margin-bottom": "0px"}
           jQuery(this).css(cssObj);
           encBusy = false;
           jQuery(document).pngFix();
        });
        
        jQuery(document).pngFix(); 
    }
    
    if(encAutoRotateBanner)
    {
        var tmpBannerTimer = setTimeout("encAutorotate(0)", encAutoRotateTimeout);
    }
}

showFooter=function()
{
    jQuery("div#bannerFooter").animate({ width: '75em', opacity: '0.7' }, 'slow');
}

encTransformBanner = function(ids)
{
    encCurrentBanner = ids;
    var currentBg = jQuery("div#bannerContainerCover").css("background-image");
    jQuery("div#bannerContainer").css("background-image", currentBg);
    var cssObj = {"opacity": "0.1", "background-repeat": "no-repeat", "background-image": "url(" + encImg [ids].src + ")"}
    jQuery("div#bannerContainerCover").css(cssObj);
    
    if(encTransitionType == "slide")
    {     
        jQuery("div#bannerContainerCover").css("background-position", "-50px 0");                
        jQuery("div#bannerContainerCover").stop().animate({backgroundPosition:"(0 0)", opacity:"1"}, {duration:600});
    }    
    else if(encTransitionType == "slideDown")
    {      
        jQuery("div#bannerContainerCover").css("background-position", "0 -50px");                
        jQuery("div#bannerContainerCover").stop().animate({backgroundPosition:"(0 0)", opacity:"1"}, {duration:600});
    }
    else
    {      
        jQuery("div#bannerContainerCover").stop().animate({opacity:"1"}, {duration:1000});
    }
}

var lastTimeStamp = new Date();

encAutorotate = function(bannerID)
{
	var currentTimeStamp = new Date();
	var waitRotate = false;
	if ((lastTimeStamp.getTime()+5000)>currentTimeStamp.getTime()) {
		waitRotate = true;	
	}
	//alert (currentTimeStamp+" - "+lastTimeStamp);
	if ((encAutoRotateBanner == true) & (waitRotate == false)) {
		if(encCurrentBanner > -1)
		{
			bannerID = encCurrentBanner;
			encCurrentBanner = -1;
		}
		
		if(!encBusy)
		{
			if(bannerID < (encNumOfImages-1) && bannerID >= 0)
				bannerID++;
			else
				bannerID =0;
				
			encTransformBanner(bannerID);
		}
		lastTimeStamp = currentTimeStamp;	
		var tmpBannerTimer = setTimeout("encAutorotate(" + bannerID + ")", encAutoRotateTimeout);
	}
}

encPreloadImages = function(images, size)
{
    var tmpArray = new Array(size);
    for(i=0; i<size; i++)
    {
        tmpArray[i]      = new Image;
        tmpArray[i].src  = images[i];
    }
    return tmpArray;
}
