$(document.body).ready(  function(){
    $('.jq-width').each( function(){
      if ( navigator.userAgent.toLowerCase().indexOf("msie") )
      {
        var jqw_max, jqw_min ;
        if ( ! parseFloat(jqw_max = $(this).css('max-width')) )
          jqw_max = '*';
        if ( ! parseFloat(jqw_min = $(this).css('min-width')) )
          jqw_min = '0';
 
        $(this).wrap("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr border=\"0\"><td border=\"0\" width=\""+jqw_max+"\"><div width=\""+jqw_min+"\">"+"</div></td></tr></table>");
      }
    })
});

function MM_findObj(n, d) { //v4.01
//  alert("find("+n+")");
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
//    alert("done");
  if(!x && d.getElementById) x=d.getElementById(n); return x;

}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function checkForm() {
  var frm = document.forms['mail'];
  var error = "";
  if(frm.elements['Name'].value==""||frm.elements['Name'].value=="Имя") error+="Пожалуйста введите имя\n";
  if(frm.elements['email'].value==""||frm.elements['email'].value=="Электронная почта") error+="Пожалуйста введите адрес email\n";
  else if(!validateEmail(frm.elements['email'].value)) error+="Некорректный адрес email!\n";
  if(frm.elements['Question'].value==""||frm.elements['Question'].value=="Вопрос") error+="Пожалуйста введите Ваш вопрос\n";
  if(error==""){
  	frm.elements['action'].value = "doPostForm";
  	return true;
  }
  else {
    alert(error);
    return false;
  }
}

function validateEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

var s_cleared = false;

function clearSearch(){
 if(s_cleared) return;
 else {
 document.forms['searchFrm'].elements['s'].value = "";
 s_cleared = true;
 }
}

function runActiveContent(what) {
	document.write(what);
}

function bookmark(){
	if (window.sidebar)
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print)
	{ 
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)
		window.external.AddFavorite(url, title);
	else
		alert("Ошибка добавления в избранное!");
	return false;
}

var name_cleared = false;
var mail_cleared = false;
var phone_cleared = false;
var question_cleared = false;

function clearName(){
  if(name_cleared) return;
  else {
    document.forms['mail'].elements['Name'].value = "";
    name_cleared = true;
  }
}

function clearMail(){
  if(mail_cleared) return;
  else {
    document.forms['mail'].elements['email'].value = "";
    mail_cleared = true;
  }
}

function clearPhone(){
  if(phone_cleared) return;
  else {
    document.forms['mail'].elements['Phone'].value = "";
    phone_cleared = true;
  }
}

function clearQuestion(){
  if(question_cleared) return;
  else {
    document.forms['mail'].elements['Question'].value = "";
    question_cleared = true;
  }
}

function linkObj(obj){
	//obj.style.cssText = "background: #4f8c0d; padding: 1px";
	obj.style.cssText = "background: #ffffff; padding: 1px";
}

function unlinkObj(obj){
	obj.style.cssText = "background: #c6c6c4; padding: 1px";
}

function doUpdateTotalCost(){
  total_rur = Math.round(total_cost*document.forms['orderFrm'].elements['rate'].value*100)/100;
  total_rur = total_rur.toString();
  total_rur = total_rur.replace(".", ",");
  ps = total_rur.indexOf(",");
  if(ps>0&&ps<total_rur.length-1) total_rur = total_rur+"0";
  else if (ps<0) total_rur = total_rur+",00";
  document.forms['orderFrm'].elements['cost_total'].value = total_cost.toString()+" руб.";
}

function decrease(what) {
  fe = document.forms['orderFrm'].elements['qty'+what];
  fp = document.forms['orderFrm'].elements['price'+what];
  if(fe.value!="0") {
    fe.value = parseInt(fe.value)-1;
    total_cost = total_cost-parseFloat(fp.value);
  }
  doUpdateTotalCost();
}

function increase(what) {
  
  fe = document.forms['orderFrm'].elements['qty'+what];
  fp = document.forms['orderFrm'].elements['price'+what];
  fe.value = parseInt(fe.value)+1;
  total_cost = total_cost+parseFloat(fp.value);
  doUpdateTotalCost();
}

function doHighlight(what, color) {
	what.style.bgColor = color;
}

function doBlur(what, color) {
	what.style.bgColor = "";
}

function checkCart(frm) {	
	var qty = parseInt(frm.elements['qty'].value);
	var mn = parseInt(frm.elements['mo'].value);	
	if(qty.toString()=="NaN") frm.elements['qty'].value = mn;
	if(qty<mn) frm.elements['qty'].value = mn;
	return true;
}

function addCart() {
	//var url = dp+'cart/index.php';
	var frm = document.forms['orderFrm'];
	
	var qty = parseInt(frm.elements['qty'].value);
	//var mn = parseInt(frm.elements['mo'].value);
	
	if(qty<0) frm.elements['qty'].value = 1;
	//if(qty<mn) frm.elements['qty'].value = mn;
	
	document.forms['orderFrm'].elements['a1'].value = "add";
	openCartWindow("!");
	//window.location=url;		
	document.forms['orderFrm'].submit();		
}

function openCartWindow(url){
  if(url=="") url = dp+'cart/';
  if(url=="!") url = "";
  window.open(url, 'cart', 'width=650, height=350, scrollbars=yes, resizable=yes');
}

function doAddToCart(id, params){
  if(total_cost==0) alert("Пожалуйста выберите хотя бы одну позицию для добавления в корзину!");
  else {
    document.forms['orderFrm'].elements['a1'].value = "add";
    openCartWindow("!");
    document.forms['orderFrm'].submit();
  }
}

function scrolling(way, id){	
	no = id-1;	
	oldid = -1;
	if(way=='-') no++;
	else no--;
	for(i=1;i<=page_limit&&i<=page_limit;i++){
		if(no<0) no=bigPhotos.length-1;
		if(no>bigPhotos.length-1) no=0;		
		if(oldid<0) oldid = no+1;
		document.getElementById('im'+i).src = smallPhotos[no];
		document.getElementById('im'+i).title = photoAlts[no];
		document.getElementById('im'+i).oldno = no+1;
		document.getElementById('a'+i).href = bigPhotos[no];
		no++;
	}
	//no = oldno;
}

var s_int = 0;
var cur_obj = 0;
var cur_dir = 0;
var old_pos = 0;
var s_step = 0;

function doStartScroll(what, dir, step) {
	if(cur_obj==0) cur_obj = document.getElementById("scroll"+what);
	if(cur_dir==0) cur_dir = dir;
	if(old_pos==0) old_pos = cur_obj.scrollLeft;
	if(step>0) s_step = step;
	else step = s_step;
	
	if(cur_obj.scrollLeft+cur_dir*step < cur_obj.scrollWidth-step) {
		cur_obj.scrollLeft+=cur_dir*step;
	}
	//alert("doScroll("+cur_obj.id+", "+cur_dir+"; sl="+cur_obj.scrollLeft+"; s_int="+s_int+"; op="+old_pos+")");
	if(cur_obj.scrollLeft<step||cur_obj.scrollLeft>cur_obj.scrollWidth-step) {
		//cur_obj.scrollLeft = 0;
		doStopScroll();
		//alert("finished!");
	}
	else {
		if(s_int==0) s_int=setInterval("doStartScroll()", 10);
		//alert("going on; s_int="+s_int);
	}
}

function doStopScroll() {
	clearInterval(s_int);
	s_int = 0;
	cur_obj = 0;
	cur_dir = 0;
	old_pos = 0;
}

function doStartScroll(what, dir, step) {
	if(cur_obj==0) cur_obj = document.getElementById("scroll"+what);
	if(cur_dir==0) cur_dir = dir;
	if(old_pos==0) old_pos = cur_obj.scrollLeft;
	if(step>0) s_step = step;
	else step = s_step;
	
	if(cur_obj.scrollLeft+cur_dir*step < cur_obj.scrollWidth-step) {
		cur_obj.scrollLeft+=cur_dir*step;
	}
	//alert("doScroll("+cur_obj.id+", "+cur_dir+"; sl="+cur_obj.scrollLeft+"; s_int="+s_int+"; op="+old_pos+")");
	if(cur_obj.scrollLeft<step||cur_obj.scrollLeft>cur_obj.scrollWidth-step) {
		//cur_obj.scrollLeft = 0;
		doStopScroll();
		//alert("finished!");
	}
	else {
		if(s_int==0) s_int=setInterval("doStartScroll()", 10);
		//alert("going on; s_int="+s_int);
	}
}

function doStopScroll() {
	clearInterval(s_int);
	s_int = 0;
	cur_obj = 0;
	cur_dir = 0;
	old_pos = 0;
}

function fixResize() {
	//alert("fixResize()");
	//alert(document.getElementById("wdth").width-33);
	document.getElementById("scroll1").style.cssText="overflow: hidden; width: "+(document.getElementById("wdth").width-33)+"px";
	//alert("done.");
}