/*
-----------------------------------------------
Pure Couture Global Javascript Functions
Author:  Frank Manno, Aquabox.ca
Version: December 03, 2005 - 'tis the season!
----------------------------------------------- */

// The addLoadEvent function takes as an argument another function which 
// should be executed once the page has loaded. Unlike assigning directly 
// to window.onload, the function adds the event in such a way that any 
// previously added onload functions will be executed first.
// By Simon Willison
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// Cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
// By Scott Andrew
function addEvent(elm, evType, fn, useCapture){
   
   if (elm.addEventListener){      
      elm.addEventListener(evType, fn, useCapture);
      return;
   }
   else if (elm.attachEvent){
      var r = elm.attachEvent('on' + evType, fn);
      return r;
   }
   else{
      elm['on' + evType] = fn;
   }   
   
}


var Global = {
   // Courtesy of Jonathan Snook and Max Starkenburg: 
   // - http://www.snook.ca/archives/000370.php
   // - http://cnx.rice.edu/
   getElementsByClassName : function(classname){
     var rl = new Array();
     var re = new RegExp('(^| )'+classname+'( |$)');
     var ael = document.getElementsByTagName('*');
     var op = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
     if (document.all && !op) ael = document.all;
     for(i=0, j=0 ; i<ael.length ; i++) {
       if(re.test(ael[i].className)) {
               rl[j]=ael[i];
               j++;
       }
     }
     return rl;
   },
   
   getNodeElementsByClassName : function(node, classname){
      var a = [];
      var re = new RegExp('\\b' + classname + '\\b');
      var els = node.getElementsByTagName("*");
      for(var i=0,j=els.length; i<j; i++)
         if(re.test(els[i].className))a.push(els[i]);
      return a;
   }
};

var SizingDetails = {
   
   init : function()
   {
      if (!document.getElementById) return false;
      if (!document.getElementsByTagName) return false;
      
      var sizeTab = document.getElementById("sizeTab");
      
      if (sizeTab){
         SizingDetails.addEvents(sizeTab);
      }
      
   },
   
   addEvents : function(obj)
   {
      addEvent(obj, 'click', 
         function()
         {
            if (obj.className.toLowerCase() == "active"){
               obj.className = "";
               obj.href = "javascript:void(0)";
               Effect.SlideUp('chartContainer');
            } else {
               obj.className = "active";
               Effect.SlideDown('chartContainer');
               obj.href = "javascript:void(0)";
            }
            
         },false);
   }
};

function enableShipToOther(){
   shipToOther = document.getElementById("order_ship_to_other");
   if (shipToOther){
      shipToOther.onclick = displayShippingDetails;

      // Hide shipping details box
      shipDetails = document.getElementById("shippingDetails");
   
      if (!shipToOther.checked) { 
         shipDetails.style.display = "none";
      }
   }
}

function displayShippingDetails(e){
   var check;
   shipDetails = document.getElementById("shippingDetails");
   
   if (window.event && window.event.srcElement){
      
      // If label is clicked, capture checkbox instead
      if (window.event.srcElement.nodeName.toLowerCase() == "label"){
         check = window.event.srcElement

         // Checkbox hasn't been reached
         while (check.nodeName.toLowerCase() != "input" && check.nodeName.toLowerCase() != "body"){
            check = check.previousSibling;
         }


      } else {
         check = window.event.srcElement;
      }
   } 
   if (e && e.target){
      
      if (e.target.nodeName.toLowerCase() == "label"){
         check = e.target;

         // Checkbox hasn't been reached
         while (check.nodeName.toLowerCase() != "input" && check.nodeName.toLowerCase() != "body"){
				check = check.previousSibling;
         }
                     
      } else {
         check = e.target;
      }

   }
   
   // Hide/Show shipping details form
   if (check.checked){
      shipDetails.style.display = "";
   } else {
      shipDetails.style.display = "none";
      
      // Reset form fields, clear errors if any
      shipForm = document.getElementById("shippingForm");      
      if (shipForm){
         inputs = shipForm.getElementsByTagName("input");
         iLen = inputs.length;
         for (i=0; i < iLen; i++){
            inputs[i].value = "";
         }
         
         errorMsgs = document.getElementById("errorExplanation")
         if (errorMsgs) errorMsgs.style.display = "none";
         
         errorDivs = shipForm.getElementsByTagName("div");
         divLen = errorDivs.length;
         for (i=0; i < divLen; i++){
            if (errorDivs[i].className == "fieldWithErrors")
               errorDivs[i].className = "";
         }
         
      }
   }

}

function displayShippingDetails_safari(e){
   var check;
   if (e && e.target) check = e.target.parentNode;
}

// Image rotation for catalog page
function rotateProductImages(prodImage, imgSrc, prodId){    
	//if (src == "#") return false;
	curImg = document.getElementById(prodImage);
	trace("Before:" + document.getElementById(prodImage).src);
	host = curImg.src.substr(0, curImg.src.indexOf("/images"));
	document.getElementById(prodImage).src = host + imgSrc;
	trace("After:" + document.getElementById(prodImage).src);
	ImageEffects.initImage(document.getElementById(prodImage));
}

function preloadCache(cacheArray, imageArray){
	var cache = cacheArray;
   for (i=0; i < imageArray.length; i++){
       cache[i] = new Image;
       cache[i].src = imageArray[i];
   }
	return cache;
}

// Cross-browser photo fading effects.  Original code courtesy of
// Richard Rutter, http://clagnut.com/sandbox/imagefades/
//
// Modifications by Frank Manno, Aquabox.ca
var fadeEffect = null;
var ImageEffects = {
	initImage : function(image) {
		this.setOpacity(image, 0);
		image.style.visibility = "visible";
		this.fadeIn(image.id,0);
	},

	fadeIn : function(objId,opacity) {
		if (document.getElementById) {
			obj = document.getElementById(objId);
			if (opacity <= 100) {
				this.setOpacity(obj, opacity);
				opacity += 10;
				fadeEffect = window.setTimeout("ImageEffects.fadeIn('"+objId+"',"+opacity+")", 200);
			}
		}
	},

	setOpacity : function(obj, opacity) {
		opacity = (opacity == 100)?99.999:opacity;
		// IE/Win
		obj.style.filter = "alpha(opacity:"+opacity+")";
		// Safari<1.2, Konqueror
		obj.style.KHTMLOpacity = opacity/100;
		// Older Mozilla and Firefox
		obj.style.MozOpacity = opacity/100;
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		obj.style.opacity = opacity/100;
	}
}

Array.prototype.getIndex = function(val){
	for( i=0; i < this.length; i++ ){
		if( this[i] == val ) return i;
	}
	return -1;
}

addLoadEvent(SizingDetails.init);
addLoadEvent(enableShipToOther);

function trace( msg ){
  if( typeof( jsTrace ) != 'undefined' ){
    jsTrace.send( msg );
  }
}
