var allPageTags = new Array();
var howManyImages = 67;
var whichImage = Math.ceil(Math.random()*howManyImages);
var whichImage2 = Math.ceil(Math.random()*howManyImages);
var whichImage3 = Math.ceil(Math.random()*howManyImages);



function twistOffsetScroll(obj) {
	var y = $("#" + obj).offset().top;
	y -= 140;				//This is the offset to account for the Header. If you resize the header, you need to resize this.
	scrollTo(0, y);
	return false;
}
//LOADER
function loadItUp (){
	singleImage();
	processIncoming();
}
		

//POPUP opens a pop-up window. This is used for the footer links to the ToS, privacy policy, etc.
function Popup(target, height, width){
	var target;
	var height;
	var width;
	mywindow = window.open 
	(target + ".html","TwistPop","toolbar=0,menubar=0,location=0,status=0,scrollbars=1,width=" + width + ",height=" + height);
  	mywindow.moveTo(0,0);
  	if (window.focus) {mywindow.focus()}
}


//hideEm 
function hideEm(theClass) {
	//Populate the array with all the page tags
	var allPageTags=document.getElementsByTagName("*");
	
	//Cycle through the tags using a for loop
	for (i=0; i<allPageTags.length; i++) {
		//Pick out the tags with our class name
		if (allPageTags[i].className==theClass) {
			//Manipulate this in whatever way you want - (i.e. hide 'em)
			allPageTags[i].style.display='none';
		}
	}
} 

function toggleMore(item){
	var item
	var morelink = 'more_' + item
	var lesslink = 'less_' + item
	
	if (document.getElementById(item).style.display == "block") {
		document.getElementById(item).style.display="none";
		document.getElementById(lesslink).style.display="none";
		document.getElementById(morelink).style.display="block";
		}
	else {
		document.getElementById(item).style.display="block";
		document.getElementById(lesslink).style.display="block";
		document.getElementById(morelink).style.display="none";
		}	
	return false;	
}

// singleImage creates an image Div with a random image for pages other than the front page. 

function singleImage(){
	if(whichImage<10){
		document.getElementById('singleimage').style.background="url('images/3by5/0"+whichImage+".jpg') no-repeat top left";
	}
	else{
		document.getElementById('singleimage').style.background="url('images/3by5/"+whichImage+".jpg') no-repeat top left";
	}
	
}

//	threeRandomImages sets the whichImage variables to be unique random numbers, if they didn't start that way, in order to prevenet showing the same image twice on one page.
function threeRandomImages() {
	if (whichImage==0) {
		whichImage = 1;
	}
	while (whichImage==whichImage2) {
		whichImage2 = Math.ceil(Math.random()*howManyImages);
		if (whichImage2==0) {
			whichImage2 = 1;
		}
	}
	while (whichImage3==whichImage) {
		whichImage3 = Math.ceil(Math.random()*howManyImages);
		if (whichImage3==0) {
			whichImage3 = 1;
		}
	}
	while (whichImage2==whichImage3) {
		whichImage3 = Math.ceil(Math.random()*howManyImages);
		if (whichImage3==0) {
			whichImage3 = 1;
		}
	}
	servicesImage();
	portfolioImage();
	accessImage();
}


// These functions create 3 divs with the appropriate ids and styles for the HOME/Index page
 
function servicesImage(){
	if(whichImage<10){
		//document.write('<div id="services" style="background: url(\'images/3by5/0'+whichImage+'.jpg\') no-repeat top left\;">');
		document.getElementById('services').style.background="url('images/3by5/0"+whichImage+".jpg') no-repeat top left";
		
	}
	else{
		//document.write('<div id="services" style="background: url(\'images/3by5/'+whichImage+'.jpg\') no-repeat top left\;">');
		document.getElementById('services').style.background="url('images/3by5/"+whichImage+".jpg') no-repeat top left";
	}
	
}
function portfolioImage(){
	if(whichImage2<10){
		//document.write('<div id="portfolio" style="background: url(\'images/3by5/0'+whichImage2+'.jpg\') no-repeat top left\;">');
		document.getElementById('portfolio').style.background="url('images/3by5/0"+whichImage2+".jpg') no-repeat top left";
	}
	else{
		//document.write('<div id="portfolio" style="background: url(\'images/3by5/'+whichImage2+'.jpg\') no-repeat top left\;">');
		document.getElementById('portfolio').style.background="url('images/3by5/"+whichImage2+".jpg') no-repeat top left";
	}
}
function accessImage(){
	if(whichImage3<10){
		//document.write('<div id="access" style="background: url(\'images/3by5/0'+whichImage3+'.jpg\') no-repeat top left\;">');
		document.getElementById('access').style.background="url('images/3by5/0"+whichImage3+".jpg') no-repeat top left";
	}
	else{
		//document.write('<div id="access" style="background: url(\'images/3by5/'+whichImage3+'.jpg\') no-repeat top left\;">');
		document.getElementById('access').style.background="url('images/3by5/"+whichImage3+".jpg') no-repeat top left";
	}
}	

function processIncoming() {

 var query = window.location.search;
  // Skip the leading ?, which should always be there,
  // but be careful anyway
  if (query.substring(0, 1) == '?') {
    query = query.substring(1);
  }
  var data = query.split(',');
  for (i = 0; (i < data.length); i++) {
    data[i] = unescape(data[i]);
  }
	twistOffsetScroll(data[0]);
} 

function processIncomingForm() {
 var query = window.location.search;
  // Skip the leading ?, which should always be there,
  // but be careful anyway
  if (query.substring(0, 1) == '?') {
    query = query.substring(1);
  }
  var data = query.split(',');
  for (i = 0; (i < data.length); i++) {
    data[i] = unescape(data[i]);
  }
	switch(data[0]){
			case "tab_1_div_menu":
				//change status & style menu
				$("#tab_1_div_menu").addClass("active_form");
				$("#tab_2_div_menu").removeClass("active_form");
				$("#tab_3_div_menu").removeClass("active_form");				
				//display selected division, hide others
				$("div#tab_2_div").css("display", "none");
				$("div#tab_3_div").css("display", "none");
				$("div#tab_1_div").fadeIn();
			break;
			case "tab_2_div_menu":
				//change status & style menu
				$("#tab_1_div_menu").removeClass("active_form");
				$("#tab_2_div_menu").addClass("active_form");
				$("#tab_3_div_menu").removeClass("active_form");								
				//display selected division, hide others
				$("div#tab_1_div").css("display", "none");
				$("div#tab_3_div").css("display", "none");
				$("div#tab_2_div").fadeIn();
			break;
			case "tab_3_div_menu":
				//change status & style menu
				$("#tab_1_div_menu").removeClass("active_form");
				$("#tab_2_div_menu").removeClass("active_form");
				$("#tab_3_div_menu").addClass("active_form");				
				//display selected division, hide others
				$("div#tab_1_div").css("display", "none");
				$("div#tab_2_div").css("display", "none");
				$("div#tab_3_div").fadeIn();				
			break;
		}
}

function moveScroller() {
  var a = function() {
    var b = $(window).scrollTop();
    b = b+115;
    var d = $("#scroller-anchor").offset({scroll:false}).top;
    var c=$("#contact");
    if (b>d) { c.css({'position' : 'fixed', 'top' : '110px',  'margin' : '0px 40px 0px -20px', 'border-top-right-radius' : '0px', '-moz-border-radius-topright' : '0px'})
    } else {
      if (b<=d) { c.css({'position' : 'static', 'margin' : '0px 40px 0px -20px', 'border-top-right-radius' : '5px', '-moz-border-radius-topright' : '5px'})
      }
    }
  };
  $(window).scroll(a);a()
}
