// if color values not suffixed with _SLEEVETYPE, work around
function maybe_suffix_color(_color){ try {
 var sleeve_type = $$(".sleeve_header")[0].firstChild.data;
 sleeve_type_suffix = sleeve_type.replace(/[^a-zA-Z]/g, "_");
 if (sleeve_type_suffix.match(/MARBLE_FEATHER/)) return _color;// these have a different naming convention
 if (sleeve_type_suffix.match(/Y_KNOT/)) sleeve_type_suffix = "YKNOT";
 if ( !(_color.match(/_/) && _color.match(sleeve_type_suffix)) ) {
	// miva not sending proper color, attempt to determine...
	return _color+"_"+sleeve_type_suffix;
 } else	return _color;
} catch(e){ /*alert("Failed in maybe_suffix_color: "+e);*/ return _color } }

function behaviors(){
	//change product color based on query string
	var qs = window.location.search;
	if (qs.length > 0) {
		qs = qs.toQueryParams();
		if (qs.color){
			qs.color = maybe_suffix_color(qs.color);
			//swap the product image
			$("product_image_tag").src="images/sleeves/sm/"+qs.color+".jpg";
			//swap the lightbox 
			$("product_image_lightbox").href="images/sleeves/lg/"+qs.color+".jpg";
			//$("product_image_lightbox").title=qs.color.split("_").join(" ")+ " SLEEVE";
			
			//dumb kludge for Y-KNOT
			$("product_image_lightbox").title=qs.color.gsub("YKNOT","Y-KNOT") + " SLEEVE";

			//set the order form menus to the color
			$$("option.formy").each(function(e,i){
				if (e.getAttribute("rel") == qs.color) {
					e.setAttribute("selected","selected");
					$("product_image_lightbox").title = e.getAttribute("rel").split("_").join(" ");
				}
			});	
		}		
	} else {
		
		//if no color, set the product color to the first menu item
		var c = new Array;
		$$("select.formy option").each(function(e,i){
			if (e.value !="#"){
				if (e.readAttribute("rel")){
					v = e.value;
					c.push(e.readAttribute("rel"));
				} else {
					c.push(maybe_suffix_color(e.value));
				}
			}
		});
		c = $A(c);
		if ($("product_image_tag"))
			$("product_image_tag").src = "images/sleeves/sm/"+c.first()+".jpg";
	}
	

	//change product image and lightbox when new product is selected from dropdown
	$$("select.formy").each(function(e,i){
		//change image and lightbox
		Event.observe(e,"change",function() {
			var c = new Array;
			//create an array of rel values for the menu
			e.select("option").each(function(o,n){
				var vv = o.value;
				if (o.readAttribute("rel"))
					c[vv] = o.readAttribute("rel");
				else
					c[vv] = maybe_suffix_color(o.value);
			});
			var v = c[e.value];
			$("product_image_tag").src="images/sleeves/sm/"+v+".jpg";
			$('product_image_lightbox').title = v.split("_").join(" ") + " SLEEVE";
			$('product_image_lightbox').href="images/sleeves/lg/"+v+".jpg";
		});
	});
	
	
	//ajax form submission buttons
	$$("img.addtobasketimage, img.addtobasketimageLAMP").each(function(e,i){
		Event.observe(e,"click",function(el){
				var is_valid=true;
				//button needs to have "rel" property set to the value of parent form's id attribute
				if (!e.readAttribute("rel")){
					var f = $("six_inch");
					qq = "quantity_six_inch";
				} else{
					var f = $(e.readAttribute("rel"));
					var qq = "quantity_"+e.readAttribute("rel");
				}
				//all this just to isolate the value of the quantity field
				var qqq = $(qq).value;
				if (qqq < 1){
					is_valid=false;
				}
				//select the dropdown value via select.class=formy
				f.select("select.formy").each(function(p,n){
					if (p.getValue()=="#"){
						is_valid=false;
					}
				});
				if (is_valid){
					f.submit();
					new Ajax.Updater($('ajax_overlay_div'), 'shopping_bag_preview.php',{ evalScripts: true, method: 'post'});
				} else {
					alert("Please select a color and quantity for your order.");
				}
		});
	
		});
}



//this is used to validate the email forms
function validate_form(f){
	var has_error = false;
	
	//first, set the form to white
	f.select('.required').each(function(e,i){
		e.style.backgroundColor="#ffffff";
	});
	
	//set empty form fields to yellow
	f.select('.required').each(function(e,i){
		if (e.value.length < 1){
			has_error = true;
			e.style.backgroundColor="#ffffcc";
		}
	});
	
	//alert if empty forms
	if (has_error==true){
		alert("Please fill out the highlighted fields");
		return false;
	} else {
		//return true - submit handled in the form
		return true;
	}
}


//via ligthtbox.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];
}


//stupid ie6 dropdown toggle
function show_dropdowns(){
	$$("select.formy").map(function(e){e.setStyle({display:"block"})});
}



