/* 
++++++++++++++++++++++++++++++++++++++++++++
  random .bImage div background image selector.   
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function LoadRandomImage(iDiv){
//random image name - either list of image names or generic "bkg_n.jpg" format. 
//using an image name list : 
//var vImageList = ["blueberries.jpg","book.jpg","chopping_board_pot.jpg","coffee_and_cup.jpg", "girl_ponder.jpg","girl_smile_02.jpg", "guy_girl_pc.jpg", "guy_girl_study.jpg", "guy_smile.jpg","highlight.jpg", "table_chairs.jpg",];
//var vRandom = Math.floor(Math.random() * vImageList.length);
//using generic names :
var vRandom = Math.floor(Math.random() * 13);
var vPath = 'url(../images/backgrounds/bkg_'+ vRandom+'.jpg)';
iDiv.style.backgroundImage = vPath;
iDiv.style.backgroundPosition = "bottom";
iDiv.style.backgroundRepeat = "no-repeat";
}
/* 
++++++++++++++++++++++++++++++++++++++++++++
  random .bImage div background image selector.   
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function LoadRandomImage2(iDiv){
//random image name - either list of image names or generic "bkg_n.jpg" format. 
//using an image name list : 
//var vImageList = ["blueberries.jpg","book.jpg","chopping_board_pot.jpg","coffee_and_cup.jpg", "girl_ponder.jpg","girl_smile_02.jpg", "guy_girl_pc.jpg", "guy_girl_study.jpg", "guy_smile.jpg","highlight.jpg", "table_chairs.jpg",];
//var vRandom = Math.floor(Math.random() * vImageList.length);
//using generic names :
var vRandom = Math.floor(Math.random() * 13);
var vPath = 'url(../images/backgrounds2/bkg_'+ vRandom+'.jpg)';
iDiv.style.backgroundImage = vPath;
iDiv.style.backgroundPosition = "bottom";
iDiv.style.backgroundRepeat = "no-repeat";
}
/* 
++++++++++++++++++++++++++++++++++++++++++++
  balance divs  
++++++++++++++++++++++++++++++++++++++++++++ 
*/

function doCompare(a, b) {
    return a - b
}

function BalanceDivs(aRow){
	//aRow contains 1 -4 column elements
	//parse each column for b Div boxes
var aColH = [];
var aStretchDivs = [];
var vStretcher;
for(var r=0;r<aRow.length;r++){
	
	var vCopyMarginAdjust = 0;
	vStretcher = null;
	var aBoxes = [];

	var aContent = aRow[r].getElementsByTagName('div');
	for(var b=0;b<aContent.length;b++){
		if (aContent[b].className.substring(0,1) == "b"){
			aBoxes.push(aContent[b]);
			
			if (aContent[b].className == "bImage"){
				//save element
				 vStretcher =  aContent[b];
				 LoadRandomImage(vStretcher);		 
			}
			else if (aContent[b].className == "bImage2"){
				//save element
				 vStretcher =  aContent[b];
				 LoadRandomImage2(vStretcher);		 
			}
			else if (aContent[b].className == "bSpacer"){
				//save element
				 vStretcher =  aContent[b];
			}
			
			else if (aContent[b].className == "bSnippet"){
				StyleSnippet(aContent[b]);	 
			}
			
			else if (aContent[b].className == "bCopy"){
				//the copy div has a bottom margin of 20px for better readability.
				//this amount must be added to all other divs in this col...				
				vCopyMarginAdjust = 20;
			}
		}	
	}
	//now check if vImage was detected in order to choose vImage or last div box as the one to stretch
	 if(vStretcher == null){
	   vStretcher = aBoxes[aBoxes.length-1];
	 }
	//finally add element to be stretched to array.
	aStretchDivs.push(vStretcher);		

	//aBoxes now contains all b elements for this column
	//use the last item to determine column height.
	vColH = aBoxes[aBoxes.length-1].offsetHeight+aBoxes[aBoxes.length-1].offsetTop+vCopyMarginAdjust;
	aColH.push(vColH);
}	
//now we know the col heights in aColH and the elements to stretch, in aStretchDivs.
//find the tallest col by sorting 
aSortH = aColH.slice(0);
aSortH.sort(doCompare);
var vMaxH = aSortH[aSortH.length-1];
for(h=0;h<aStretchDivs.length;h++){
	var vColOffset = vMaxH - aColH[h];
	
	var vBoxHeight = aStretchDivs[h].offsetHeight+vColOffset;
	aStretchDivs[h].style.height = vBoxHeight + 'px';
}
}

function GetDivs(){
	var aContent = document.getElementById('content').getElementsByTagName('div');
	var aColumns = [];
	//separate into columns
	for(i=0; i< aContent.length;i++){
		var vPrefix = aContent[i].className.substring(0,1);
		if (vPrefix == "c"){
			aColumns.push(aContent[i]);
		}
	}
	//keep count 
	var vWidthCount = 0;
	var vCol = 0;
	var aGrid = [];
	//separate into rows
	while(vCol < aColumns.length){ 
		var aRow = [];
		while(vWidthCount < 4){
			var vSuffix = parseInt(aColumns[vCol].className.substring(1));
			aRow.push(aColumns[vCol]);
			vWidthCount +=vSuffix;
			vCol +=1;
		 }
		 vWidthCount = 0;
		 BalanceDivs(aRow);
	}
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  switch element on and off
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function switchElm(elmName){
	document.getElementById(elmName).style.display = "block"
	currentDiv = div_id;
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  get name of current page.
++++++++++++++++++++++++++++++++++++++++++++ 
*/
function GetPageName(){
	var vPageURLObj =  document.location;
	var vPageURL = vPageURLObj.toString();
	var vPageFileName = vPageURL.substr(vPageURL.lastIndexOf("/")+1);
	//ie sometimes ? adds a # to the end of the filename, so parse..
	if(vPageFileName.charAt(vPageFileName.length-1)=="#"){
		var vPageFileName = vPageFileName.substr(0,vPageFileName.length-1);
	};
	
	//vPageFileName = vPageFileName.split(".")[0];//remove file suffix
	return(vPageFileName);
}
/*
++++++++++++++++++++++++++++++++++++++++++++
  show / hide divs  - used for rules / faq / reasons pages etc.
++++++++++++++++++++++++++++++++++++++++++++ 
*/
var vVisibleDiv = null;

function switchDiv(headerTXT){
	var aReasons = document.getElementById('switcher').getElementsByTagName('h1');
	for(r=0; r< aReasons.length; r++){
		var vReason = aReasons[r];
		if(vReason.innerHTML == headerTXT){
			//alert("gotcha");
		if(vVisibleDiv != null){
				vVisibleDiv.style.display = "none";
		}
			vReason.parentNode.style.display = "block";
			vVisibleDiv = vReason.parentNode;
		}
	}
}

//
/*
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷
  run page layout functions   
÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷÷ 
*/
function runJS(switchRef){
	GetDivs();
	//ie win cannot fire two funcs from onload, so .....
	if(switchRef){
		switchDiv(switchRef)
	};
}

