jQuery.noConflict();

//onload start init
window.onload = init;

//ie6 select box width hack



//qs
var qsParm = new Array();

function init(){
	roundAllCorners();
	initIE6Selects();
	
	//setWarning(true);
	enableOtherBoxes();
	ms_miniZoom();
	
}





function initIE6Selects(){
	new YAHOO.Hack.FixIESelectWidth("materialDD");
	new YAHOO.Hack.FixIESelectWidth("coatingDD");
	new YAHOO.Hack.FixIESelectWidth("sizeDD");
	new YAHOO.Hack.FixIESelectWidth("colorsDD");
	new YAHOO.Hack.FixIESelectWidth("numberOfItemsDD");
}

function getElementsByClassNameNew(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

 

function qs() {
	qsParm['color'] = null;
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
			if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
			}
	}
//alert(qsParm['color']);
}

function roundAllCorners(){
      /*ROUND THE TOP OF THE FORM*/
      var settings1 = {
          tl: { radius: 10 },
          tr: { radius: 10 },
          bl: { radius: 0 },
          br: { radius: 0 },
          antiAlias: true,
          autoPad: false,
          validTags: ["div"]
      };

      var myBoxObject1 = new curvyCorners(settings1, "tempClass");
      myBoxObject1.applyCornersToAll();
      
      /*ROUND THE BOTTOM OF THE FORM*/
      var settings2 = {
          tl: { radius: 4 },
          tr: { radius: 4 },
          bl: { radius: 0 },
          br: { radius: 0 },
          antiAlias: true,
          autoPad: false,
          validTags: ["form"]
      };

      var myBoxObject2 = new curvyCorners(settings2, "formRegister");
      myBoxObject2.applyCornersToAll();
      
	/*ROUND THE CALCULATOR */
      var settings3 = {
          tl: { radius: 10 },
          tr: { radius: 10 },
          bl: { radius: 10 },
          br: { radius: 10 },
          antiAlias: true,
          autoPad: false,
          validTags: ["div"]
      };

      var myBoxObject3 = new curvyCorners(settings3, "labelCalc");
      myBoxObject3.applyCornersToAll();
}


function enableOther(id){
	if($(id)[$(id).selectedIndex].innerHTML == "Other (Please Specify)"){
	 jQuery("#"+id+"Other").parent(".form-row").show();
	 }
	 else {
	 jQuery("#"+id+"Other").val("");
	 jQuery("#"+id+"Other").parent(".form-row").hide();
	 }
}



function validateForm(){
var emailAddress = $("emailINP").value;
	if(!validate.isEmail(emailAddress))
	{
		$("emailMessage").show();
		$("emailLabel").style.color = "red";
		return false;
	}
	else{
		$("emailMessage").hide();
		$("emailLabel").style.color = "white";
		return true;
	}
}

function moreQuantities(){
	jQuery(".moreQtyBox input").val("");
	jQuery(".moreQtyBox").toggle();
}

function enableOtherBoxes(){
	jQuery(".othersBox").each(function(){
		if(jQuery(this).val() != ""){
			jQuery(this).parent(".form-row").show();
		}
	});

}
function closeKeepAlive() {
  if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
    new Ajax.Request("/ping/close.php", { asynchronous:false });
  }
}


function ms_miniZoom(){	
	
	
	jQuery("#agImage img").click(function(e){
		
		var imgSrc = jQuery(this).attr("src");
		var imgName = jQuery("#agImgTitle").html();
		var imgDesc = jQuery("#agImgDesc").html();
		var imgWidth = jQuery(this).width();

		
		var pageHeight = getPageSize()[1];										  
		var self = this;

		//set the height of the overlay
		if(!isIE)
			jQuery("div.overlayDark").css("height",pageHeight).fadeIn(300,function(){ 
			
			//Create the markup								  
			jQuery("body").append("<div class='miniZoomTip' id='miniZoomTip'  ><div id='miniZoomTipClose' ><a href='javascript:void(0)'>x close</a></div>"+
			"<img class='noie6trans' src='"
			+imgSrc+"'/><div id='miniZoomTitle'>"
			+imgName+"</div><div id='miniZoomDescr'>"
			+imgDesc+"</div>"
			+"</div>");

			var imgWidth = jQuery("#miniZoomTip").find("img").width();
			jQuery("#miniZoomTip").css("width",imgWidth);				
			putCenter("miniZoomTip");
 
			
			//close button
			jQuery("#miniZoomTipClose").bind("click",function(e){
				
				closeTip();
				return false;
			});
			
			return false;
			
			});
		else{
			jQuery("div.overlayDark").css("height",pageHeight).show();
			
			//Create the markup								  
			jQuery("body").append("<div class='miniZoomTip' id='miniZoomTip'  ><div id='miniZoomTipClose' ><a href='javascript:void(0)'>x close</a></div>"+
			"<img width=\"342\" height=\"300\" class='noie6trans' src='"
			+imgSrc+"'/><div id='miniZoomTitle'>"
			+imgName+"</div><div id='miniZoomDescr'>"
			+imgDesc+"</div>"
			+"</div>");

			//Hide all select boxes or else the popup will go behind ie6;
			if(isIE)
				jQuery("div.form-row select").hide();
							
			var imgWidth = jQuery("#miniZoomTip").find("img").width();
			jQuery("#miniZoomTip").css("width",imgWidth);				
			putCenter("miniZoomTip");
 
			
			//close button
			jQuery("#miniZoomTipClose").bind("click",function(e){
				
				closeTip();
				return false;
			});
			
			return false;
			
			}

			
			
		});

							

    	
};

function closeTip(){
				jQuery("div.overlayDark").hide();
				
				if(isIE)
					jQuery("div.form-row select").show();
				
				jQuery("#miniZoomTip").remove();
}


//taken from here http://codesnippets.joyent.com/user/winton/tag/javascript
function putCenter(item)  {
  item = $(item);
  var xy = item.getDimensions();
  var win = windowDimensions();
  var scrol = scrollOffset();
  item.style.left = (win[0] / 2) + scrol[0] - (xy.width / 2) + "px";
  item.style.top = (win[1] / 2) + scrol[1] - (xy.height / 2) + "px";
}

function fullScreen(item) {
  item = $(item);
  var win = windowDimensions();
  var scrol = scrollOffset();
  item.style.height = scrol[1] + win[1] + "px";
}
function windowDimensions() {
  var x, y;
  if (self.innerHeight) {
    // all except Explorer
    x = self.innerWidth;
    y = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    // Explorer 6 Strict Mode
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight;
  } else if (document.body) {
    // other Explorers
    x = document.body.clientWidth;
    y = document.body.clientHeight;
  }
  if (!x) x = 0;
  if (!y) y = 0;
  arrayWindowSize = new Array(x,y);
  return arrayWindowSize;
}
function scrollOffset() {
  var x, y;
  if (self.pageYOffset) {
    // all except Explorer
    x = self.pageXOffset;
    y = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    // Explorer 6 Strict
    x = document.documentElement.scrollLeft;
    y = document.documentElement.scrollTop;
  } else if (document.body) {
    // all other Explorers
    x = document.body.scrollLeft;
    y = document.body.scrollTop;
  }
  if (!x) x = 0;
  if (!y) y = 0;
  arrayScrollOffset = new Array(x,y);
  return arrayScrollOffset;
}
////END put center



//taken from lightbox.js
function getPageSize(){
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}


