/*
homepage_routines.js
*/

var panelIsOpen = false;
var currentOpenPanel = "none";
var currentOpenNumber = 0;
var numberOfBillboards = 0;
var currentBB = 0;
var isPausedRot = false;
var closeIcon = 0;
var RTLtoggle = false;
var moviePause = false;

function setUp() {

	runAfter = arguments[0];
    var bbSpeed = $('#billboards').attr('intervalTime');
   
	numberOfBillboards = $('#billboards').children().size();
	zCount = numberOfBillboards;
    aCount = 1;
	
	//reWrite the BB area...
    newBBhtml = "";
	e = 0;	
	$('#billboards').children().each(function(index) {
		xContent = $(this).html();
		xHref = $(this).attr('href'); 
		xTarget = $(this).attr('target'); 
		if (xHref) {
			tString = '<div id="bb_' + e + '" style="z-index:' + zCount + '" class="singleBB"><a href="' + xHref + '" target="' + xTarget + '">' + xContent + '</a></div>';
		} else {
			tString ='<div id="bb_' + e + '" style="z-index:' + zCount + '" class="singleBB">' + xContent + '</div>';
		}
		
		newBBhtml += tString;
		e++;
		zCount--;
  	});

    $('#billboards').html(newBBhtml);
    $('#billboards').hover(pauseRot, unPauseRot);
	
	
    //reWrite the BB Icons:
    newIconHTML = "";
    iconArray = new Array();
    aCount = 1;
	
    $.each($('#bbIcons img'), function() {
        oneIconHTML = '<div id="icon_' + aCount + '" class="iconMainClass">';
			
        oneIconHTML += '<div id="iconBG" class="iconBGstyle">';
		
        oneIconHTML += '<div id="theIcon" class="iconStyle">';
        oneIconHTML += '<img id="icon" src="' + $(this).attr("src") + '" alt="' + $(this).attr("alt") + '" border="0"/>';
        oneIconHTML += '</div>';
		
        oneIconHTML += '<div id="iconWhite" class="iconWhiteStyle">';
        oneIconHTML += '</div>';
		
        oneIconHTML += '</div>';
		
        oneIconHTML += '<div id="iconYellow" class="iconYellowstyle">';
        oneIconHTML += '<img id="icon" src="img/iconYellow.png" border="0" />';
        oneIconHTML += '</div>';
		
		
        oneIconHTML += '</div>';
        newIconHTML += oneIconHTML;

        iconArray.push(oneIconHTML);
        aCount++;
    });

    $('#bbIcons').remove();
			
    //Set icon button onEnter stuff
    $('.iconMainClass').mouseenter(iconOver);
    highlightIcon(1);
	
    //Start BB rotation:
    bbSpeed = parseInt(bbSpeed) * 1000;
    window.setInterval(rotateTheBB, bbSpeed);
	
	if (runAfter){
		eval(runAfter);
	}
}




function pauseRot() {
    isPausedRot = true;
}

function unPauseRot() {
    isPausedRot = false;
}

//only called by the interval
function rotateTheBB() {
    if (!panelIsOpen && !isPausedRot && !moviePause) {

	nextBB = currentBB + 1;
        if (nextBB >= numberOfBillboards) {
            nextBB = 0;
        }
		
        rotation(currentBB, nextBB);
    }
}

//called by rotateTheBB and iconOver
function rotation(cur, next) {
    $("#bb_" + cur).css({
        'z-index' : '10'
    });
    $("#bb_" + next).css({
        'z-index' : '9',
        'display' : 'block'
    });
    $("#bb_" + cur).fadeOut(400);
	
    highlightIcon(next + 1);
    unhighlightIcon(cur + 1);
	
    currentBB = next;

	///alert(typeof(EVENT_ON_VISIBLE));
    if(typeof(EVENT_ON_VISIBLE) != 'undefined') {
        $('#bb_' + currentBB).trigger(EVENT_ON_VISIBLE);
    }
}

function highlightIcon(iconID) {
    $("#icon_" + iconID).find("#iconWhite").css({
        "visibility" : "hidden"
    });
    $("#icon_" + iconID).find("#iconBG").stop().animate({
        top: '-8px'
    }, 400);
    closeIcon = iconID;
}

function unhighlightIcon(iconID) {
    $("#icon_" + iconID).find("#iconWhite").css({
        "visibility" : "visible"
    });
    $("#icon_" + iconID).find("#iconBG").stop().animate({
        top: '0px'
    }, 400);
}


function iconOver() {
    isPausedRot = true;
    myName = $(this).attr('id');
    myNumber = parseInt(myName.substring(myName.length - 1));
	
    if (myNumber - 1 != currentBB) {
        rotation(currentBB, myNumber-1);
    }
}

