/*
*	-----------------------------------------------
*	Client: Verbatim USA
*	Author:   Justin Johnson | jjohnson@edreamz.com
*	Modified:  April, 2007
*	Copyright: Copyright (C) 2007 E-dreamz, Inc. 
*	----------------------------------------------- 
 */

/*
 *  @function init
 */
	function init(){
		$("#worldwide").change( function() { jsRedirect($("#worldwide").val()) } );
		$("#email_about").change( function() { slideForm($("#email_about").val()) } );
		$("#sales_country").change( function() { slideFormCountry($("#sales_country").val()) } );
		startList();
		fixIEBgFlicker();
		/*setTimeout(initOverLabels, 50);*/
		$('a[rel*=facebox]').facebox();
	}

/*
 *  @function 	changeHeader
 *  @params		cls | String | css class to apply
 *  @desc		applies appropriate css to #hd h1 to match flash movie
 */
	function changeHeader(cls){
		$("#hd h1").removeClass().addClass(cls);
		$("#flashMovie").removeClass().addClass(cls);
		
	}

/*
 *  @function	jsRedirect()
 *  @params		url | String | URL to redirect to
 *  @desc		redirects to the location specified by URL
 */
	function jsRedirect(url){
		if(url.length){
			window.location = url;
		}
		
	}
	
function fixIEBgFlicker(){
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}	
}

function slideForm(formVal){
	
	var pClass = $('#downloads').attr('class');
	
	if(pClass == 'open' && formVal != 'Download'){
		$('#downloads').slideToggle().attr('class','closed');
	}
	
	//downloads
	if(formVal == 'Download'){
		var downloadsClass = $('#downloads').attr('class');
		if(downloadsClass !='open'){
			$('#downloads').slideToggle("normal").attr('class','open')
		}
		
		$('#continue').css({display:'none'});			
	}	
	
	//button
	if(formVal == 'Sales'){
		$('#continue').css({display:'block'});			
	}	
	
}


function slideFormCountry(formVal){
	
	var coClass = $('#Colombia').attr('class');
	var aClass = $('#Argentina').attr('class');
	var chClass = $('#Chile').attr('class');
	var mClass = $('#Mexico').attr('class');
	var mClass = $('#Ecuador').attr('class');
	
	if(coClass == 'open' && formVal != 'Colombia'){
		$('#Colombia').slideToggle().attr('class','closed');
	}
	
	if(aClass == 'open' && formVal != 'Argentina'){
		$('#Argentina').slideToggle().attr('class','closed');
	}
	
	if(chClass == 'open' && formVal != 'Chile'){
		$('#Chile').slideToggle().attr('class','closed');
	}
	
	if(mClass == 'open' && formVal != 'Mexico'){
		$('#Mexico').slideToggle().attr('class','closed');
	}
	
	if(mClass == 'open' && formVal != 'Ecuador'){
		$('#Ecuador').slideToggle().attr('class','closed');
	}
	
	//Colombia
	if(formVal == 'Colombia'){
		var coClass = $('#Colombia').attr('class');
		if(coClass !='open'){
			$('#Colombia').slideToggle("normal").attr('class','open')
		}		
	}	
	
	//Argentina
	if(formVal == 'Argentina'){
		var aClass = $('#Argentina').attr('class');
		if(aClass !='open'){
			$('#Argentina').slideToggle("normal").attr('class','open')
		}		
	}	
	
	//Chile
	if(formVal == 'Chile'){
		var chClass = $('#Chile').attr('class');
		if(chClass !='open'){
			$('#Chile').slideToggle("normal").attr('class','open')
		}		
	}	
	
	//Mexico
	if(formVal == 'Mexico'){
		var mClass = $('#Mexico').attr('class');
		if(mClass !='open'){
			$('#Mexico').slideToggle("normal").attr('class','open')
		}		
	}	
	
	//Ecuador
	if(formVal == 'Ecuador'){
		var mClass = $('#Ecuador').attr('class');
		if(mClass !='open'){
			$('#Ecuador').slideToggle("normal").attr('class','open')
		}		
	}	
	
}


/*
 * 	startList is used to add hover functions to li in IE
 * 
 */

startList = function() {
if (document.all&&document.getElementById) {
	for(j=0; j<4; j++){
navRoot = document.getElementById("subnav"+j);
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
 }
}

/* utility functions for search bar label */
function initOverLabels () {
  if (!document.getElementById) return;      

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // labels with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {

    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute ('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      } 

      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to label elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i]. getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.textIndent = (hide) ? '-9999px' : '0px';
      return true;
    }
  }
}



// scripts for produt images on detail page
function changeImg(src) {
	var oldSrc = document.getElementById('mainImg').src.split('/');
	oldSrc[oldSrc.length-1] = src;
	document.getElementById('mainImg').src = oldSrc.join('/');
}
function restoreImg(src) {
	var oldSrc = document.getElementById('mainImg').src.split('/');
	oldSrc[oldSrc.length-1] = src;
	document.getElementById('mainImg').src = oldSrc.join('/');
}
function largeImg(src) {
	var oldSrc = document.getElementById('mainImg').src.split('/');
	oldSrc[oldSrc.length-1] = src;
	window.open(oldSrc.join('/'),"viewLarge","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=770,height=600,top=30,left=30");
}
function newWindow() {
	window.open("","windowname","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=450,height=425,top=30,left=30");
}





/*
 *  calls the init function when the document is ready
 */
$(document).ready(function(){init()});




