function updateCounter() { 
  curCount = $('#counterbox').html();
  nextCount = curCount - (1 + Math.floor(Math.random()*2));
  $('#counterbox').html(nextCount);
  
  $.cookie('counterval', nextCount, { expires: 1 });
  
  if (nextCount <= 89 && !isSlowedDown) {
	clearInterval(counterInterval);
    counterInterval = setInterval(updateCounter, 10000);
	isSlowedDown = true;
  }
  
  if (nextCount < 40) clearInterval(counterInterval);
}

function showTerms() { 
    wleft = getScreenCenterX()-100;
    wtop  = getScreenCenterY()-100;

    window.open("terms-and-conditions.php","Window1","scrollbars=1,left="+wleft+",top="+wtop+",resizable=no,menubar=no,width=782,height=478,toolbar=no");
}

function getScreenCenterY() {
 var y = 0;
 y = getScrollOffset()+(getInnerHeight()/2);
 return(y);
}

function getScreenCenterX() {
 return(document.body.clientWidth/2);
}

function getInnerHeight() {
 var y;
 if (self.innerHeight) // all except Explorer
 {
  y = self.innerHeight;
 }
 else if (document.documentElement &&
 document.documentElement.clientHeight)
 // Explorer 6 Strict Mode
 {
  y = document.documentElement.clientHeight;
 }
 else if (document.body) // other Explorers
 {
  y = document.body.clientHeight;
 }
  return(y);
}

function getScrollOffset() {
 var y;
 if (self.pageYOffset) // all except Explorer
 {
  y = self.pageYOffset;
 }
 else if (document.documentElement &&
 document.documentElement.scrollTop)
 // Explorer 6 Strict
 {
  y = document.documentElement.scrollTop;
 }
 else if (document.body) // all other Explorers
 {
  y = document.body.scrollTop;
 }
 return(y);
}


clearField = function(eleM, compString){
	var fieldValue = eleM.value;
	if(fieldValue.toLowerCase() == compString.toLowerCase()){
		eleM.value = '';
	}
}

fillField = function(eleM, compString){
	var fieldValue = eleM.value;
	if(fieldValue.toLowerCase() == ''){
		eleM.value = compString;
	}
}

var seemore = 2;
function seeMoreResults(){
	for(var counter=1;counter<10;counter++){
         if(document.getElementById('see_more'+counter)!=null){
              document.getElementById('see_more'+counter).style.display = 'none';
         }
    }

    document.getElementById('see_more'+seemore).style.display = 'block';

    seemore++;
    if(seemore == 3){
         seemore = 1;
    }
}

// See real results
var seemore = 2;
function seeRealResults(){
	for(var counter=1;counter<10;counter++){
         if(document.getElementById('realstories'+counter)!=null){
              document.getElementById('realstories'+counter).style.display = 'none';
         }
    }

    document.getElementById('realstories'+seemore).style.display = 'block';

    seemore++;
    if(seemore == 5){
         seemore = 1;
    }
}







var counterInterval;
var isSlowedDown = false;

$(document).ready(function () {
  var activeLink = $('#homelink');
							
  $('#homelink').click(function () {
    activeLink.css('background', '');								 
    $(this).css('background','url(images/home-active.jpg) no-repeat');
	activeLink = $(this);
  })					
  
  $('#whylink').click(function () {
    activeLink.css('background', '');								 								
    $(this).css('background','url(images/why-active.jpg) no-repeat');
	activeLink = $(this);	
  })			  
  
  $('#storylink').click(function () {
    activeLink.css('background', '');								 								  
    $(this).css('background','url(images/real-active.jpg) no-repeat');
	activeLink = $(this);	
  })  
  

  
  if (counterval = $.cookie('counterval')) {
     $('#counterbox').html(counterval);  
  } 
  else {
     counterval = 239;
	 $('#counterbox').html('239');   
  }
  
  if (counterval >= 40) {
    counterInterval = setInterval(updateCounter, 7000);  
  }
})
