// JavaScript Document
var scripts_isIE = jQuery.browser.msie;
var scripts_isIE6 = (scripts_isIE && jQuery.browser.version.substr(0,1)=="6");

var selectedHomeContentID = 3;
var showingContentIndex = -1; //for first time only.
var stopTimer = false;
var contentTimeout = null;
var selectedFlash = "";

$(function(){

     // ROLLOVERS

   $("a.ro").mouseover(function(){

	var imgArr = $(this).children("img").attr("src").split(".");
	var imgPart1 = imgArr[0];
	var imgPart2 = imgArr[1];
	$(this).children("img").attr("src",imgPart1+"-over."+imgPart2);

  }).mouseout(function(){

	var imgOut = $(this).children("img").attr("src").replace(/-over/,"");
	$(this).children("img").attr("src",imgOut);
  });

  $("#btnAgree").children("a").click(function(){
	closePopup("terms_lyr");
	$('#terms_lyr').remove();
  });

  $("#btnDisagree").children("a").click(function(){
	alert("You must agree to the terms in order to continue.");
  });

  $("#top_link1").children("a").click(function(){
	var pid = $(this).parent().parent().attr("id");
	slideNavText(pid);
  });

  $('#map_top10').css('opacity',0).fadeTo('fast',0.85);
  $('#map_top10').children("div").css('opacity',0).fadeTo('fast',1.0);
  $('#map_top10').children("div").children("ul").css('opacity',0).fadeTo('fast',1.0);
  
  $("#toplinkC").click(function(event) {
	event.preventDefault();
	if(document.getElementById("toplnkC2").style.display=="block"){
		$("#toplnkC2").slideRight();
		alert("one");
	}
	else{
		$("#toplnkC2").slideLeft();
		alert("two");
	}
	//$("#div1").innerHTML="This is testing";
});

});

function initPage(){
      RotateContentData(0,false); //launch this after terms is agreed to
      //openPopup('terms_lyr');
}

function HomeContentData(index, navID, mainImage, thumbLyr, thumbLyrOn, hdrImage, hdrAltTag, swfPath, divID, swfVideoPath, swfImagePath, swfURL, swfTarget, swfText, swfXMLPath)
{
	this.index = index;
	this.navID = navID;
	this.mainImage = mainImage;
	this.thumbLyr = thumbLyr;
	this.thumbLyrOn = thumbLyrOn;
	this.hdrImage = hdrImage;
	this.hdrAltTag = hdrAltTag;
	this.swfPath = swfPath;
	this.divID = divID;
	this.swfVideoPath = swfVideoPath;
	this.swfImagePath = swfImagePath;
	this.swfURL = swfURL;
	this.swfTarget = swfTarget;
	this.swfText = swfText;
	this.swfXMLPath = swfXMLPath;
}

function RotateContentData(index,fromClick)
{

	var contentInit=false;
	if(showingContentIndex==-1) contentInit = true;

	var data = contentData[index];

	if(fromClick || (index==0 && showingContentIndex!=-1))
	{
		// IF USER CLICKS, STOP TIMER
		// OR 
		// IF WE HAVE ALREADY COMPLETED 1 CYCLE THRU, STOP TIMER
		stopTimer = true; 
		clearTimeout(contentTimeout);
			//alert("stopTimer=" + stopTimer);
	}

	
	if (data != null && (fromClick || !stopTimer)) 
	{

		selectedHomeContentID = index;
		
		//CHANGE NAV BUTTONS
		$(".main_box_nav_item").show();
		$(".main_box_nav_item_over").css("visibility","hidden");
		$("#"+contentData[data.index].thumbLyr).hide();
		$("#"+contentData[data.index].thumbLyrOn).css("visibility","visible");
		$("#"+contentData[data.index].thumbLyrOn).css("z-index",1000);

		//HIDE ALL CONTENT
		$("#main_box_img").hide();
		$("#main_box_google").hide();
		$("#map_view").hide();
		$("#map_top10").hide();
		$("#main_box_flash1").hide();
		$("#main_box_flash2").hide();
		$("#main_box_flash3").hide();

		var flashDivHTML = $("#"+data.divID).html();
		var strFlashDivHTML = flashDivHTML.toUpperCase();

		if(selectedFlash!="" && data.divID!=selectedFlash && strFlashDivHTML.indexOf("<IMG")==-1)
		{
			$("#"+selectedFlash).html("");
		}

		setTimeout(function() { changeHeaderImage(data.hdrImage,data.hdrAltTag); },100);

		if(data.swfPath!="")
		{
		   if(data.divID!=selectedFlash)
		   {
			var so = new SWFObject(data.swfPath, "flashMiddle", "640", "360", "9.0.47", "#ffffff");
			so.addParam("wmode", "transparent");
			so.addParam("scale", "noscale");
			so.addParam("allowFullScreen", "true");
			so.addParam("salign", "ct");

			if(data.swfVideoPath!="") so.addVariable("video_path", data.swfVideoPath);
			if(data.swfImagePath!="") so.addVariable("image_path", data.swfImagePath);
			if(data.swfURL!="")       so.addVariable("url",        data.swfURL);
			if(data.swfTarget!="")    so.addVariable("target",     data.swfTarget);
			if(data.swfText!="")      so.addVariable("text",       data.swfText);
			if(data.swfXMLPath!="")   so.addVariable("xml_path",   data.swfXMLPath);

			so.write(data.divID);
			selectedFlash = data.divID;
		   }

		   $("#"+data.divID).show();

		} else {

		    selectedFlash="";

		    if(data.divID!="")
		    {
			$("#main_box_img").children("img").attr("src",data.mainImage);
			$("#"+data.divID).fadeIn("normal");

			if(data.divID=="main_box_google")
			{
			   //$("#map_view").show();
			   $("#map_top10").show();
			}

		    } else {
			$("#main_box_img").children("img").attr("src",data.mainImage);
			$("#main_box_img").fadeIn("normal");
		    }
		}

		showingContentIndex = index;
		
	}


	if(fromClick)
	{
		// DO NOT RESTART TIMER AFTER USER CLICK!!
		//contentTimeout = setTimeout('restartTimer();',15000); //restart again if no clicks for 8 secs
	}

	if(contentInit && !stopTimer)
	{
		contentTimeout = setTimeout('rotateContent();',12000);
	}
}

function changeHeaderImage(img,alt)
{
   $("#main_box_hdr").children("img").attr("src",img);
   $("#main_box_hdr").children("img").attr("alt",alt);
}

function resetContentRotation()
{
	if (contentTimeout != null || stopTimer == true)
	{
		clearTimeout(contentTimeout);
	}

	if(!stopTimer)
		contentTimeout = setTimeout('rotateContent();',12000);

	else
		contentTimeout = setTimeout('restartTimer();',15000); //loop until stopTimer is reset
}

function rotateContent()
{
	var id = selectedHomeContentID + 1;
	if (id >= contentData.length) id = 0;
	RotateContentData(id,false);
	if(!stopTimer) resetContentRotation();
}

function restartTimer()
{
	stopTimer=false;
	resetContentRotation();
}

function resetContentRotation(){
	setTimeout('rotateContent();',12000);
}

function showPopupDivCenter(divID) 
{
    if (scripts_isIE6) $('#'+divID).bgiframe();
    centerLyr(divID);
    rePositionLyr(true,divID);
    $("#"+divID).css("visibility", "visible");
}


function centerLyr(divID)
{
    $('#'+divID).css("position","absolute");
    var newTop = ( $(window).height() - $('#'+divID).height() ) / 2+$(window).scrollTop();
    if(newTop<0) newTop=0;
    $('#'+divID).css("top", newTop + "px");
    $('#'+divID).css("left", ( $(window).width() - $('#'+divID).width() ) / 2+$(window).scrollLeft() + "px");
}

function rePositionLyr(status,divID) 
{
	switch(status) {
		case true:
			$(window).bind('resize', function() {
				centerLyr(divID);
			});
			$(window).bind('scroll', function() {
				centerLyr(divID);
			});
		break;
		case false:
			$(window).unbind('resize');
			$(window).unbind('scroll');
		break;
	}
}

function hidePopupDiv(divID) 
{
    $("#"+divID).css("visibility", "hidden");
}

function maskSite(divID)
{
    toShow="";

   var newHeight=$(document).height();


  // Build the background overlay div
    toShow += "<div class='pp_overlay'></div>";

   $('body').append(toShow);

   $('div.pp_overlay').css('opacity',0).fadeTo('slow',0.65, function(){
	showPopupDivCenter(divID);
	$('div.pp_overlay').css("z-index","2000");
	$('div.pp_overlay').height(newHeight);
	$('#'+divID).css("z-index","3000");
   });
}

function unmaskSite()
{
   $('div.pp_overlay').remove();
}


function closePopup(divID)
{
   hidePopupDiv(divID);
   unmaskSite();
   RotateContentData(0,false);
}

function openPopup(divID)
{
   maskSite(divID);
}

function slideNavText(divID){
   //divID: toplinks (rockefeller), toplinks-union, toplinks-times
   
   var el = $("#"+divID);

   var leftval = "206px";
   if(divID=="toplinks-union") leftval = "237px";

   if($(el).css("left")=="0px")
      var left = leftval;
   else
      var left = "0px";

   $(el).animate({left:left}, 300);
}