﻿var iCount = 0;
var iPrev = 0;
var ImageArr, DescArr, AltsArr;
var ImageHeights;
var webserver;
var aid;
var bPlay = true;
var tTran;
var bLoaded = false;
var LoadedIndex = 0;

function preLoad() {
    ImageHeights = new Array();
    
    preLoadImage();
    
}

function preLoadImage() {
    if ( LoadedIndex < ImageArr.length ) {
        imgPreloader = new Image();
    		
        imgPreloader.onload=function(){
            LoadedIndex++;
	        ImageHeights[(LoadedIndex-1)] = imgPreloader.height;
	        preLoadImage( LoadedIndex );
        }
        imgPreloader.src = '/images/homebillboard/' + ImageArr[LoadedIndex];
    } else {
        bLoaded = true;
        iCount = Math.floor(Math.random()*(ImageArr.length));
		document.getElementById('BillControl' + (iCount+1)).className = "BillControlBoxOn";
        document.getElementById('imgLoading').style.display = 'none';
        primeTour('/images/homebillboard/' + ImageArr[iCount], AltsArr[iCount], DescArr[iCount], ImageHeights[iCount]);
		window.setTimeout("transition3()", 6000);
    }
}

function changeImage( type ) {
    if ( bLoaded ) {
        iPrev = iCount;
	    document.getElementById('BillControl' + (iCount+1)).className = "BillControlBox";
        iCount = iCount + type;
        if ( iCount >= ImageArr.length )
	        iCount = 0;
	    else if ( iCount < 0 ) 
	        iCount = ImageArr.length -1 ;
		    
	    if ( bPlay )
	        playStop();
	    document.getElementById('BillControl' + (iCount+1)).className = "BillControlBoxOn";    
        tourFade('/images/homebillboard/' + ImageArr[iCount], AltsArr[iCount], DescArr[iCount], 500, ImageHeights[iCount], ImageHeights[iPrev]);
    }
}

function gotoBill( xxx ) {
    if ( iCount > xxx ) {
        changeImage( xxx - iCount );
    } else if ( iCount == xxx ) {
        return;
    } else {
        changeImage( xxx - iCount );
    }
}

function prepBoxes() {
    var obj = document.getElementById('dvBillboardControls')
    obj.innerHTML = "";
    for (var xxx = 0; xxx < ImageArr.length; xxx++) {
        obj.innerHTML += "<div id=\"BillControl" + (xxx + 1) + "\" class=\"BillControlBox\" onclick=\"gotoBill(" + xxx + ");\">" + (xxx + 1) + "</div>";
    }
}

function init3(){
	if ( Images.indexOf(";") > -1 ) {
		ImageArr = Images.split(";");
		DescArr = Descriptions.split("||");
		AltsArr = Alts.split(";");
		
		prepBoxes();
		
		preLoad();
		
		if ( bLoaded ) {
		    document.getElementById('BillControl' + iCount).className = "BillControlBoxOn";
		    primeTour('/images/homebillboard/' + ImageArr[iCount], AltsArr[iCount], DescArr[iCount], ImageHeights[iCount]);
		
		    window.setTimeout("transition3()", 6000);
		}
	} else if ( Images.length > 0 ) {
		primeTour( '/images/homebillboard/' + Images, Descriptions, Alts );
		document.getElementById('imgLoading').style.display = "none";
	}
}

function primeTour( imgTour, altTour, description, imgHeight ) {
	document.getElementById('imgVirutalTour2').src = imgTour;
	document.getElementById('imgVirutalTour2').alt = altTour;
	document.getElementById('dvVirtualTourDesc2').href = description;
}


function playStop() {
    if ( bPlay ) {
        bPlay = false;
        if ( tTran ) {
            clearTimeout(tTran);
        }
    } else {
        bPlay = true;
        transition3();
    }
}

function transition3() {
    if ( bPlay ) {
        iPrev = iCount;
	    iCount++;
		document.getElementById('BillControl' + (iCount)).className = "BillControlBox";
	    if ( iCount >= ImageArr.length )
		    iCount = 0;
		
		document.getElementById('BillControl' + (iCount+1)).className = "BillControlBoxOn";
		
	    tourFade('/images/homebillboard/' + ImageArr[iCount], AltsArr[iCount], DescArr[iCount], 2000, ImageHeights[iCount], ImageHeights[iPrev]);
			
	    tTran = window.setTimeout("transition3()", 6000);
	}
}

function tourFade( imgTour, altTour, description, delay, imgHeight, imgPrevHeight ) {
	document.getElementById('imgVirutalTour').src = document.getElementById('imgVirutalTour2').src;
	document.getElementById('imgVirutalTour').alt = document.getElementById('imgVirutalTour2').alt;
	document.getElementById('dvVirtualTourDesc').href = document.getElementById('dvVirtualTourDesc2').href;

	changeOpac(100,'dvVirtualTour');
	changeOpac(0,'dvVirtualTour2');
	
	document.getElementById('imgVirutalTour2').src = imgTour;
	document.getElementById('imgVirutalTour2').alt = altTour;
	document.getElementById('dvVirtualTourDesc2').href = description;

	opacity('dvVirtualTour2', 0, 100, delay);
	//opacity('dvVirtualTour', 100, 0, delay);
}

function opacity(id, opacStart, opacEnd, millisec) { 
	//speed for each frame 
	//var speed = Math.round(millisec / 100); 
	//var timer = 0; 

	//determine the direction for the blending, if start and end are the same nothing happens 
	/*if(opacStart > opacEnd) { 
		for(i = opacStart; i >= opacEnd; i--) { 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		} 
	} else if(opacStart < opacEnd) { 
		for(i = opacStart; i <= opacEnd; i++) 
			{ 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		} 
	}*/
	changeOpac(0, id);
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100); 
	object.MozOpacity = (opacity / 100); 
	object.KhtmlOpacity = (opacity / 100); 
	object.filter = "alpha(opacity=" + opacity + ")"; 
	
	if(opacity < 100) {
	    setTimeout("changeOpac(" + (opacity+10) + ",'" + id + "')",50); 
	}
}

