function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function getElementHeight() {

		xPos = document.getElementById('browseProducts').offsetHeight + document.getElementById('ideasAndInspiration').offsetHeight;
		
		var left = document.getElementById('left');
		var newheight = xPos - 10;
		
		if (left.offsetHeight < newheight){
		
			left.style.height = newheight + 'px';
			
		}
		
		/*if (left.offsetHeight > xPos){
			
			var add = xPos - document.getElementById('browseProducts').offsetHeight;
			var newheight = document.getElementById('ideasAndInspiration').offsetHeight + add;
			
			document.getElementById('ideasAndInspiration').style.height = left.offsetHeight + 'px';
			
		}*/
		
		//return xPos;

}

function count_tabs(){

	var counter = 0;

	for(var i = 1; i <= 3; i++)
		{
		
		var tabname = 'tab' + i;
		
		if(document.getElementById(tabname) != null)
			{
			counter++;
			}
		}
	
	return counter;
}

function settoggle(){

	toggle('tab1');
	
	
	
	var thing = document.getElementById('delivery_address');
	
	if (document.getElementById('checkout')){
	
		if (document.forms['checkout'].delivery_flag.checked == true) thing.style.display = 'block';
		
	}

}	

function delivery_toggle(){
	
		var thing = document.getElementById('delivery_address');
	
		if (thing.style.display == 'none'){
	
			thing.style.display = 'block';
		
		} else {
	
			thing.style.display = 'none';
	
		}
	
	return true;

}

function toggle(x){
				
				var num_tabs = count_tabs();
			
				for( y=1; y <= num_tabs; y++ ){
					
					var tabname = 'tab' + y;
					var tabheadername = 'tabheader' + y;
					var tabelement = document.getElementById(tabname);
					var tabheaderelement = document.getElementById(tabheadername);
					
					if (tabname != x) {
						
						tabelement.style.display = 'none';
						tabheaderelement.className = '';
						
					} else {
						
						tabelement.style.display = 'block';
						tabheaderelement.className = 'current';
					
					}
				
				}
	return false;	
}

function confirmSubmit()
{

var agree=confirm("Are you sure you wish to delete this item?");

if (agree)

	return true ;

else

	return false ;

}


function imgswap(img){

	var thing = document.getElementById('masterimage');
	
	thing.src = 'images/products/full/' + img;
	
	return false;

}

function enlarge() {

	var enlarge_link = document.getElementById('enlargeImg');
	
	enlarge_link.onclick=function() {
		
		//FIND CURRENT PRODUCT ID FROM QUERY STRING
		var url = window.location.search;

		var vars = url.split("&");
		
		var prodId = "";
		
		for(var i=0; i<vars.length; i++) {
			prodId=vars[i];
		}
		
		var product = prodId.split("=");
		
		product=product[1];
		
		//FIND CURRENT PRODUCT
		var image = document.getElementById("masterimage");
		
		var img_array = image.src.split("/");
		
		var img="";
		
		for(var i=0; i<img_array.length; i++) {
			img=img_array[i];
		}
		
		//OPEN NEW WINDOW
		var newwin=window.open('enlarge.php?start_img='+img+'&product_id='+product,'','width=480,height=600,scrollbars=yes,toolbars=no');
		newwin;
		return false;
	}
	
}

var solutions;
var curritem=0;

//Animates the featured solutions on the homepage
function homePageScroller() {

    solutions = $('#header div').fadeOut('slow').size();
    $('#header div:eq(' + curritem + ')').fadeIn('slow');
	
	if (solutions > 1) {
	
    	setInterval(animateSolution,5000); //time in milliseconds
	
	}
	
}

function animateSolution() {

    $('#header div:eq(' + curritem + ')').fadeOut('slow');
    curritem = ++curritem%solutions;
   	$('#header div:eq(' + curritem + ')').fadeIn('slow');
	
}

function videoToggle ()
{

	if ($('#video').length == 0) return false;
	
	$('#video_panel').addClass('hidden');
	
	$('#video').click(function() {
	
		$('#video_panel').toggle();
		
		return false;
	
	});
	
	$('#video_close').click(function() {
	
		$('#video_panel').toggle();
		
		return false;
	
	});

}

$(document).ready(function() {

	homePageScroller();
	videoToggle();

});
				
//LOAD IN FUNCTIONS ONLOAD
function loadFunctions() {
	settoggle();
	getElementHeight();
	if (document.getElementById('masterimage')) { enlarge(); } 
}

window.onload=function() {
	loadFunctions();
}