// JavaScript Document
var indWidth = 1;
var arPageWidths	= new Array(728, 1200);
var arContainerTop	= new Array(90, 0);
var arLeaderboard	= new Array(-102, 0);
var arNavBG			= new Array(259, 169);
var arCol1Widths	= new Array(185, 295);
var arCol2Widths	= new Array(525, 885);
var arToggleImgPos	= new Array(-37, 0);
var arResizeTable	= new Array(490, 840);
var arSearchWidth	= new Array(130, 170);
var arFooterWidth	= new Array(868, 1340);
var arFooterSpacing	= new Array(3, 30);
var arMainNavLeft	= new Array(-16, arCol1Widths[1]);

//if(typeof(arPageToggle)=='undefined') var arPageToggle = new Array();
var arPageToggle = new Array();

$(document).ready(function () {
	InitScreenWidth();
});

function InitScreenWidth() {
	//change to smaller screen size if screen resolution smaller than the default width
	if(intScreenWidthSession==null) {
		//button hasn't been pressed, use default setting based on window size
		if($(window).width() < 1340) {
			TogglePageWidth(0);
		}
	} else if(intScreenWidthSession==0) {
		//button HAS been pressed, use the session value
		TogglePageWidth(0);
		indWidth=intScreenWidthSession;
	}
	
	if(indWidth==1) {
		//show toggle arrow
		$("#toggle_img").animate({
			left:"0px"
		}, 400);
	}
	//initiate page width toggle animations
	$("#toggle_width").click(function() {
	  TogglePageWidth(1000);
	});
}

function TogglePageWidth(intSpeed) {
	  indWidth = indWidth + 1;
	  if(indWidth >= arPageWidths.length) indWidth = 0;
	  //animate changes
      
	  $("#divTop").animate({ 
        width:arPageWidths[indWidth]+"px"
      }, intSpeed );
	  $("#divContent").animate({ 
        width:arPageWidths[indWidth]+"px"
      }, intSpeed );
	  
	  $("#container").animate({ 
        paddingTop:arContainerTop[indWidth]+"px"
      }, intSpeed );
	  $("#divLeaderboard").animate({ 
        top:arLeaderboard[indWidth]+"px"
      }, intSpeed );
	  $("#divNavBG").animate({ 
        top:arNavBG[indWidth]+"px"
      }, intSpeed );
	  $("#ulNav").animate({ 
        left:arMainNavLeft[indWidth]+"px"
      }, intSpeed );
	  
	  
      $("#col1").animate({ 
        width:arCol1Widths[indWidth]+"px"
      }, intSpeed );
      $("#col2").animate({ 
        width:arCol2Widths[indWidth]+"px"
      }, intSpeed );
	  //change toggle arrow image
      $("#toggle_img").animate({ 
        left:arToggleImgPos[indWidth]+"px"
      }, intSpeed );
	  
      $("#txtSearch").animate({ 
        width:arSearchWidth[indWidth]+"px"
      }, intSpeed );
	  
      $("#footer1").animate({ 
        width:arFooterWidth[indWidth]+"px"
      }, intSpeed );
      $("#tblFooter").animate({ 
        width:arPageWidths[indWidth]+"px"
      }, intSpeed );
	  
      $("#footer2 li").animate({ 
        marginRight:arFooterSpacing[indWidth]+"px"
      }, intSpeed );
	  
	  
	  
	  //standard full width tables
	  $(".resize-table").animate({
		width:arResizeTable[indWidth]+"px"
	  }, intSpeed);
	  
	  //custom page alterations
	  var i;
	  for(i=0; i<arPageToggle.length; i++){
		  if(arPageToggle[i][3]=="width")  $(arPageToggle[i][2]).animate({width:arPageToggle[i][indWidth]}, intSpeed);
		  if(arPageToggle[i][3]=="height") $(arPageToggle[i][2]).animate({height:arPageToggle[i][indWidth]}, intSpeed);
	  }
	  
	  SaveSessionWidth()
}
function SaveSessionWidth() {
	//ajax request
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url=strRoot+"ajax/screen-width.php";
	url=url+"?screenwidth="+indWidth;
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}



// --- BUTTONS ---

function InitButtons(intDefault, strSize) {
	//intButtonSelected = intDefault;
	
	//auto assign element id numbers and fade non selected
	$('.button_'+strSize).each(
		function(intIndex){
			$(this)[0].id = "button_"+strSize+intIndex;
			if(intIndex!=intDefault) $(this).fadeTo(0, 0.5);
		}
	);
	
	$(".button_"+strSize).click(function(){
		strID = GetButtonID($(this));
		ButtonSelect(strID,strSize);
	});
	
	//activate default button action
	ButtonSelect(intDefault, strSize);
}

function ButtonSelect(strID, strSize) {
	//select/highlight the button and modify opacity
	$(".button_"+strSize).each(
		function(){
			strThisID = GetButtonID($(this));
			if(strThisID==strID)	$(this).fadeTo(500, 1);
			else					$(this).fadeTo(500, 0.5);
		}
	);
	//move button_select outline to position
	intHorPos = $("#button_"+strSize+strID).attr("offsetLeft") - 13;
	intVerPos = $("#button_"+strSize+strID).attr("offsetTop") - 10;
	
	$(".button_"+strSize+"_selected").animate({ 
		left: intHorPos+"px",
		top:  intVerPos+"px"
	}, 500 );
	
	//perform button action
	ButtonAction(strID);
}

function GetButtonID(objButton) {
	strButton = objButton[0].id;
	return strButton.substring(12);
}
function GetID(obj, intPos) {
	strID = obj[0].id;
	return strID.substring(intPos);
}

