shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x){};
	return o;
};


function updateNav(id, value){
	$(id).select("a").each(function(o){
		o.href += "?n=" + value
	})
}
function showCart(){

var cookies=document.cookie;  //read in all cookies
var start = cookies.indexOf("ss_cart_" + serialNumber + "="); 
var cartvalues = "";
var linecount = 0;
var start1;
var end1;
var tmp;

if (start == -1)  //No cart cookie
{
  document.write("<span id=\"items\">0 Items<\/span>");
  document.write("<span id=\"price\">($00.00)<\/span>");
}
else   //cart cookie is present
{
  start = cookies.indexOf("=", start) +1;  
  var end = cookies.indexOf(";", start);  

  if (end == -1)
  {
    end = cookies.length;
  }

  cartvalues = unescape(cookies.substring(start,end)); //read in just the cookie data

  start = 0;
  while ((start = cartvalues.indexOf("|", start)) != -1)
  {
    start++;
    end = cartvalues.indexOf("|", start);
    if (end != -1)
    {
      linecount++;

      if (linecount == 2) // Total Quantity of Items
      {
        tmp = cartvalues.substring(start,end);
        colon = tmp.indexOf(":", 0);
         document.write("<span id=\"items\">");
        document.write(tmp.substring(colon+1,end - start));
        if ((tmp.substring(colon+1,end - start)) == 1 )
        {
          document.write(" Item");
        }
        else
        {
          document.write(" Items");
        }
        document.write("<\/span>")
      }
     

      if (linecount == 3)  // Product Subtotal
      {
        tmp = cartvalues.substring(start,end);
        colon = tmp.indexOf(":", 0);
        document.write("<span id=\"price\">(");
        document.write(tmp.substring(colon+1,end - start));
        document.write(")<\/span>");
      }

      start = end;
    }
    else
      break;
    }
  } // end while loop

  //close minicart HTML
}
/* end showCart() */

function fixSearchResults(){
	$$('.search-pipeline').each(function(element){
		if(element.next().nodeName == "DIV"){
			element.remove();
		}
	})
}
function initPage(){
	var viewportHeight = document.viewport.getHeight()
	var wrapperHeight = $$(".wrapper")[0].getHeight();	
	var newWrapperHeight = viewportHeight-(parseInt($$(".absfooter")[0].getHeight()) + parseInt($$(".wrapper")[0].getStyle("paddingTop")));
	if(wrapperHeight+parseInt($$(".absfooter")[0].getHeight()) < viewportHeight){
//		if(wrapperHeight < newWrapperHeight){
			$$(".wrapper")[0].setStyle({height: viewportHeight-(parseInt($$(".absfooter")[0].getHeight()) + parseInt($$(".wrapper")[0].getStyle("paddingTop")))+ "px"});
	//	}
	}
}
function BasicPop(loc, w, h){
	newwindow=window.open(loc,"PopWin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width="+w+",height="+h);
}


function setTab(obj, index){
	obj = $(obj)
	$$(".hometabs .tabs li").each(function(o){		
		if(o == obj){
			o.addClassName('active')
		}else{
			o.removeClassName("active")
		}
	})
	
	$$("div.tab").each(function(o){
		if(o.hasClassName("tab" + index)){
			o.setStyle({display: "block"})
		}else{
			o.setStyle({display: "none"})
		}
		
	})
}
function updateImage(src){
	if($("zoom_mediumimg")){
		$("zoom_mediumimg").src = src
		$("zoom_mediumimg").setAttribute("index", -1)
	}
}
var defaultImage = null;
function updateImageOnclick(obj){
	if($("zoom_mediumimg")){
		var src = null;
		$(obj).select("option").each(function(e){if(e.selected)src = e.readAttribute("img")})
		
		if(defaultImage == null){
			defaultImage = $("zoom_mediumimg").src;
		}
		if(src == null){
			$("zoom_mediumimg").src = defaultImage
			$("zoom_mediumimg").setAttribute("index", 1)
		}else{
			$("zoom_mediumimg").src = src
			$("zoom_mediumimg").setAttribute("index", -1)
		}
	}
}
function processSubmit(frm){
	var res = false;
	frm.select("div.select input").each(function(o){if(o.value != -1)res = true;})
	if(res == false){
		alert("You need to made a selection before clicking the Add to Cart button.")
	}else{
		frm.select("div.select input").each(function(o){if(o.value == -1)o.writeAttribute("name", "itemnum__")})
	}
	return res;
}


function updateQuantity(){
	var productIds = "";
	$$(".select ul li").each(function(o){
		if(o.value != -1){
			productIds += o.value + ',';
		}
	})
	new Ajax.Request("getquantity.php?id=" + productIds + "&rand=" + Math.random(), {
		method: "get",
		onSuccess: function(transport){
			var results = transport.responseXML.documentElement.getElementsByTagName("result");
			for(var i = 0; i < results.length; i++){
				if(results[i].getAttribute("quantityonhand") === "0"){
					var option = $("num_" + results[i].getAttribute("id"));
					if(option){
						option.update(option.innerHTML + " - <span class=\"outofstock\">Out Of Stock</span>");
					}
				}
			}
			updateSelectWidth();
		}
	})
}

function updateSelectWidth(){
	var zIndex = 100;
	$$("div.select").each(function(div){
		div.setStyle({zIndex: zIndex--});
		if(div.select("ul li").length > 10 ){
			div.select("ul")[0].addClassName("height300");
		}
		var maxWidth = 0;
		div.select("ul li").each(function(o){
			maxWidth = Math.max(maxWidth, getTextWidth(o.innerHTML));
		})
		div.setStyle({width: maxWidth + 30 + "px"})						
	})
}

function updateLinks(nav){
	$$(".content a").each(function(o){
		if(o.href.indexOf("javascript") != -1 && o.href.indexOf("#") != -1){
			if(o.href.indexOf("?") > 0){
				o.href = o.href + "&n=" + nav;
			}else{
				o.href = o.href + "?n=" + nav;
			}
		}
	})
}

/* Select functionality */

					function getTextWidth(text){
						if(!$("textWidth")){
							$$("body")[0].insert(
								(new Element("span", {id: "textWidth"})).setStyle({visibility: "hidden"})
							)
						}
						return $("textWidth").update(text).getWidth();
					}
				function selectValue(obj){
					obj = $(obj);
					obj.up().select("li").each(function(o){o.removeClassName("active")})
					obj.addClassName("over").addClassName("active");
					obj.up("div").removeClassName("select-active");
					obj.up("div").select("input")[0].writeAttribute("value", obj.readAttribute("value")); 
					obj.up("div").select("span.value")[0].update(obj.innerHTML)
					return false;
				}
				function selectMouseOver(obj){
					$(obj).addClassName("over");
					obj.up().select("li").each(function(o){
						if(o != obj){
							o.removeClassName('over')
						}
					})
				}
				function selectActivate(obj){
					if($(obj).up('div').hasClassName('select-active')){
						$(obj).up('div').removeClassName('select-active')
						$$("body")[0].stopObserving("click", selectClickEverywhere)
					}else{
						$(obj).up('div').select("li").each(function(o){
							if(o.hasClassName('active')){
								o.addClassName("over")
							}else{
								o.removeClassName("over")
							}
						})
						$(obj).up('div').addClassName('select-active')
						$$("body")[0].observe("click", selectClickEverywhere)
					}
				}
				function selectClickEverywhere(e){
					var obj = Event.element(e)
					var up = obj.up("div.select")
					
					if(!up){
						$$("div.select").each(function(o){o.removeClassName("select-active")})
					}else{
						$$("div.select").each(function(o){
							if(o != up){
								o.removeClassName("select-active")
							}
						})
					}
				}
		/*
function showMore(){
	$("shortdesc").hide();
	$("fulldesc").show();
	return false;
}
function showLess(){
	$("shortdesc").show();
	$("fulldesc").hide();	
	return false;
}
*/

function activateTab(li){
	var tabIndex = -1;
	$$(".tabs li").each(function(o, index){
		if(li == o){
			tabIndex = index;
			o.down("span").addClassName("active")
		}else{
			o.down("span").removeClassName("active")
		}
	})
	
	$$(".tabscontentbox .tabscontent").each(function(d, index){
		if(index == tabIndex){
			d.show();
		}else{
			d.hide();
		}
	})
}
function showVideo(){
	showOverlay({showLoading: false, onclose: closeVideo});
	
	var scrollOffset = document.viewport.getScrollOffsets();
	var newTop = scrollOffset.top + 150;
	$("video").setStyle({top: newTop + "px"}).show();
	return false;
}
function closeVideo(){
	hideOverlay();
	$("video").hide();
	return false;
}


function prevColors(id){
	if($(id).up(".tablebox").select(".prev")[0].hasClassName("disabled")){
		return;
	}
	
	var tbl = $(id);
	newLeft = parseInt(tbl.getStyle("left"))
	if(!newLeft){
		newLeft = 0;
	}
	
	newLeft += 83;
	
	tbl.setStyle({left: newLeft + "px"})
	
	if(newLeft == 38){
		$(id).up(".tablebox").select(".prev")[0].addClassName("disabled")
	}else{
		$(id).up(".tablebox").select(".prev")[0].removeClassName("disabled")
	}
		
	if(tbl.getWidth() + newLeft == 536){
		$(id).up(".tablebox").select(".next")[0].addClassName("disabled")
	}else{
		$(id).up(".tablebox").select(".next")[0].removeClassName("disabled")
	}
}

function nextColors(id){
	if($(id).up(".tablebox").select(".next")[0].hasClassName("disabled")){
		return;
	}
	
	var tbl = $(id);
	
	newLeft = parseInt(tbl.getStyle("left"))
	if(!newLeft){
		newLeft = 0;
	}
	
	newLeft -= 83;
	
	tbl.setStyle({left: newLeft + "px"})
	
	if(newLeft == 38){
		$(id).up(".tablebox").select(".prev")[0].addClassName("disabled")
	}else{
		$(id).up(".tablebox").select(".prev")[0].removeClassName("disabled")
	}
	
	if(tbl.getWidth() + newLeft == 536){
		$(id).up(".tablebox").select(".next")[0].addClassName("disabled")
	}else{
		$(id).up(".tablebox").select(".next")[0].removeClassName("disabled")
	}
	//console.log(tbl.getWidth() + newLeft)
}
