function setPopupMenu(){
  var licko = document.getElementById("li");  
  licko.onmouseover = function() { this.className += ' clover'; }
	licko.onmouseout = function() { this.className = this.className.replace(' clover', '') }
}

function loadThumbnail(id){
  document.getElementById("thumbnail").src=id.src;
  window.loadedThumbnail=id.src;
}

function popup(url,name,width,height){
	var left = (screen.availWidth-width)/2;
  var top = (screen.availHeight-height)/2;
  var popupWin = window.open(url,name,'scrollbars=no,location=no,directories=no,status=no,resizable=yes,menubar=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top);
	popupWin.focus();
}

function openParam(url){
  popup(url,"param",410,200);
}

function showThumbnail(){
  var imgObject = document.getElementById("thumbnail");
  var imgSrc = new String(imgObject.src);
  var newImg = new Image();
  newImg.src =imgSrc; 
  var path = imgSrc.split("/");    
  popup(MS_ESHOPROOT+"/catalog/imgbyname/?filename="+path[path.length-1],'img',newImg.width,newImg.height);        
}

function showImg(image_id,width,height){
  popup(MS_ESHOPROOT+"/catalog/img/?image_id="+image_id,'img',width,height);        
}

function cartAdd(product_id){
   var count = parseInt(document.getElementById("cart"+product_id).value);
   document.getElementById("cart"+product_id).value = count+1;        
}

function cartSubstract(product_id){
   var count = parseInt(document.getElementById("cart"+product_id).value);
   if(count > 0){
    document.getElementById("cart"+product_id).value = count-1;
   }        
}

function _ge(id){
  return document.getElementById(id);
}

function searchDisplayParameters(){
  //if(_ge("srchdepartment_id").indexOf("|") !== -1){
  	window.location = MS_ESHOPROOT+"/search/?q="+_ge("srchq").value+"&pricemin="+_ge("srchpricemin").value+"&pricemax="+_ge("srchpricemax").value+"&producer_id="+_ge("srchproducer_id").value+"&department_id="+_ge("srchdepartment_id").value;  
  //}
}

function defText(field,deftext){
  if (_ge(field).value == deftext) {
		_ge(field).value='';
	} else if (_ge(field).value == '') {
		_ge(field).value=deftext;
	}
}

function resetProducers(array,value){
  var inputs = _ge('filterform').elements;
  for(var i=0; i<inputs.length;i++){
    if(inputs[i].id.indexOf('producer_') == 0 && inputs[i].checked){
    inputs[i].checked=false;
    }           
  }
  resetParameter();
}

function resetParameter(){

  var url = new String(window.location);
  var inputs = _ge('filterform').elements;
  var query_string = new Array;
  var producers = new Array;
  var gotofirstpage = false;
  
  for(var i=0; i<inputs.length;i++){
    if(inputs[i].id=="dir"){
    query_string[query_string.length] = "dir="+_ge("dir").value;
    }else if(inputs[i].id=="par"){
    query_string[query_string.length] = "par="+_ge("par").value;
    }else if(inputs[i].id.indexOf('list_') == 0){    
      if(_ge("listtype").value != inputs[i].value){
        gotofirstpage = true;
      }    
    query_string[query_string.length] = inputs[i].name+"="+_ge(inputs[i].id).value;
    }else if(inputs[i].id.indexOf('producer_') == 0 && inputs[i].checked){
    producers[producers.length] = _ge(inputs[i].id).value;
    }else if(inputs[i].id=="p"){
    query_string[query_string.length] = "p="+_ge("p").value;
    }           
  }
  
  if(producers.length > 0){
    query_string[query_string.length] = "producers="+producers.join(",");
  }
  
  if(query_string.length > 0){
    newlocation=_ge("category_url").value+"?"+query_string.join("&");    
    if(producers.length > 0 || gotofirstpage){
  		newlocation = newlocation.replace(new RegExp('p=([0-9]+)','g'),'');
    }
    window.location = newlocation;
  }

  return true;
}

function setDirection(direction){
  _ge("dir").value=direction;
  resetParameter();
}

function recountCredits(){

  if(document.getElementById("usecredits").checked){
    var credits = "y";
  }else{
    var credits = "n";
  }
  
  var url = "" + window.location;  
  var url = url.substring(0, url.indexOf('?'));
  
  document.getElementById("usecredits").value=credits;  
  window.location.replace(url+"?crdts="+credits);
  
}

function goTo(url){
  location.replace(url);
}
