// JavaScript Document
function mh(obj){
	var ob=document.getElementById(obj);
	ob.className="active";
}
function mh1(obj){
	var ob=document.getElementById(obj);
	ob.className="a";
}

function show_bdiv(){
var	minus;
if(navigator.appName=='Microsoft Internet Explorer'){
	minus=300;
}else if(navigator.appName=='Opera'){
	minus=320;
}else if(navigator.appName=='Netscape'){
	minus=44;
}

var to=document.getElementById("wrapper").clientHeight-minus;
adv='<div id="bottom-shadow" style="top:'+to+'px;">&nbsp;</div>';
return adv;
}

function resize(){
	if(navigator.appName!='Netscape'){
		var div=show_bdiv();document.write(div);
	}else{
		document.getElementById("content").style.marginTop='-12px';
	}				  
}

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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openWindow(theURL,winName,features, myWidth, myHeight, isCenter) { 
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  }
  var pw=window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
  pw.print();
}

function add2cart(id){
	//document.cartForm.goodid.value=id;
	$('#basket').html('');
	$.post('/js/basket.php', {'goodid':id}, function(data){ $('#basket').html(data)});
	//document.cartForm.submit();
}

function add2cart1(id){
	var size_field=document.getElementById("good_size");
	//document.cartForm.goodid.value=id;
	//document.cartForm.goodsize.value=
	var size=size_field.options[size_field.selectedIndex].value;
	$.post('/js/basket.php', {'goodid':id, 'size':size}, function(data){ $('#basket').html(data)})
	//document.cartForm.submit();
}

function add2favor(id){
	$.post('/js/fav.php', {'goodidFav':id}, function(data){ $('#favorits').html(data)})
	/*document.cartFormFav.goodidFav.value=id;
	document.cartFormFav.submit();*/
}

function delfavor(id){
	document.cartFormFav.goodidFavDel.value=id;
	document.cartFormFav.submit();
}

function getBrowserHeight(){
	if (window.innerHeight){
		return window.innerHeight;}  
	else if (document.documentElement && document.documentElement.clientHeight != 0){
		return document.documentElement.clientHeight;    }
	else if (document.body){return document.body.clientHeight;}      
		return 0;
}

function getBodyScrollTop(){
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}


function hideDialog(){
	$("#dialog").hide('fast', function(){$("#dialog_fon").fadeOut(250);});//css('display', 'none');
	$(window).unbind("resize", moveDialog);
	$(window).unbind("scroll", moveDialog);
}

function moveDialog(){
	var	lef=document.body.clientWidth/2-200+document.body.scrollLeft;
	var	to=$(window).height()/2-100 + $(document).scrollTop();;
	$("#dialog").css('left', lef);
	$("#dialog").css('top', to);
}

function showDialog(txt, mode){
	var button="";
	switch(mode){
		case "favor": button="<input type=submit id=submit value=' Продолжить ' onclick='hideDialog()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=submit id=submit value=' Закладки ' onclick='hideDialog();document.location.href=\"/favorits/\"'>"; break;	
		case "shop": button="<input type=submit id=submit value=' Продолжить ' onclick='hideDialog()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=submit id=submit value=' Корзина ' onclick='hideDialog();document.location.href=\"/catalog/order/\"'>"; break;	
		case "alert": button="<input type=submit id=submit value='   OK   ' onclick='hideDialog()'>"; break;	
		case "confirm": button="<input type=submit id=submit value='   OK   ' onclick='dialogOK();hideDialog()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=submit id=submit value='OK' onclick='dialogCencel();hideDialog()'>"; break;
		default: button=''; break;	
	}
	var	lef=document.body.clientWidth/2-200;
	var	to=$(window).height()/2-100 + $(document).scrollTop(); 
	//(getBrowserHeight()+getBodyScrollTop())
	$("<div id='dialog_fon' style='position:absolute; z-index:99; width:100%; height:"+$(document).height()+"px; left:0px; top:0px; display:none; filter:alpha(opacity=60);background-color:#000000; moz-opacity:0.6; opacity:0.6'></div>").appendTo("body");
	$("<div id='dialog' style='position:absolute; z-index:100; display:none; left:"+lef+"px;'><div class='dialog_top'><div><img src='/images/delgray.gif' onclick='hideDialog()' style='cursor:pointer; margin:7px 9px 0 0;' title='Закрыть'></div></div><div id='dialog_text'></div><br><br><center><div id='dialog_buttons'></div></center><div class='dialog_bottom'><div></div></div></div>").appendTo("body");
	$("#dialog_fon").fadeIn(250, function(){
		$("#dialog_text").html(txt);
		$("#dialog_buttons").html(button);
		$("#dialog").css('top', to);
		$("#dialog").show('fast');
		//$("#dialog").draggable({'cursor':'move'});
	})
	$(window).bind("resize", moveDialog);
	$(window).bind("scroll", moveDialog);
}

function checkEmail(form_name,em_fld){
	if(em_fld=="") em_fld="email";
	
	var email=eval('document.'+form_name+'.'+em_fld);
	var reg = new RegExp("[0-9a-z_]+@[0-9a-z_^.]+\\.[a-z]", 'i');
	if (!reg.test(email.value)) {alert('email неверен'); return false;}
	else return true;
}

function checkNumber(obj){
	valid="1234567890- ()";
	tmp="";
	for(i=0;(i<obj.value.length);i++)  {
		if (valid.indexOf(obj.value.charAt(i))!=-1)  {
			tmp=tmp+obj.value.charAt(i);};
		};  
	obj.value=tmp;	  
}

// Example:

// writeCookie("myCookie", "my name", 24);

// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.

function writeCookie(name, value, hours){
  var expire = "";
  if(hours != null){
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + value + expire;
}

// Example:

// alert( readCookie("myCookie") );

function readCookie(name){
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0){ 
    offset = document.cookie.indexOf(search);
    if (offset != -1){ 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = document.cookie.substring(offset, end)
    }
  }
  return cookieValue;
}

function logout(){
	document.logFrm.submit();
}

function setSize(){
	var d=document.getElementById("cartlink");
	var s=document.getElementById("good_size");
	if(s.options[s.selectedIndex].value!=""){
		d.style.visibility="visible";
	}else{
		d.style.visibility="hidden";
	}
}

function checkCoupon(){
	var f=document.couponForm;
	var err='';
	var er=0;
	if(f.name.value.length==0){
		err+="Укажите Ваше имя\n";
		er++;
	}
	/*if(f.second_name.value.length==0){
		err+="Укажите Вашу фамилию\n";	
		er++;
	}
	if(f.third_name.value.length==0){
		err+="Укажите Ваше отчество\n";	
		er++;
	}*/
	if(er>0){
		alert(err);
		return false;
	}else{
		return true;	
	}
}

function showdiv(obj){
a=document.getElementById(obj);
if (a.style.display=='none')
{
	a.style.display='block';
	document.cookie = ""+obj+"=Y; expires=Thu, 31 Dec 2020 23:59:59 GMT; path=/;";
}else{
	a.style.display='none';
	document.cookie = ""+obj+"=N; expires=Thu, 31 Dec 2020 23:59:59 GMT; path=/;";
}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function printThis(){
	MM_openBrWindow('/print.php?path=<?=$path?>','','scrollbars=yes,resizable=yes,width=800,height=600')
}

var pg_ffwrong = "red";
var pg_padding = "5";
var pg_filenamewithjs = "1";
var pg_dhtml_border = "gray";
var pg_openjs_background = "white";
var pg_disableclick = "1";
var pg_use_code = "1";
var pg_show_title_in_dhtml = "1";
var pg_show_description_in_dhtml = "1";
var ponygallery_image = "Фото";
var ponygallery_of = "из";
var ponygallery_close = "Закрыть";
var ponygallery_prev = " Назад";
var ponygallery_next = "Дальше";
var ponygallery_press_esc = "(Esc)";
var resizeJsImage = "1";
function opendhtml(id, cat, imgsource, imgtitle, imgtext, imgwidth, imgheight) {
  imgwidth = parseInt(imgwidth);
  imgheight = parseInt(imgheight);
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		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;
	}
	var yScroll, xScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	var fullHeight=(document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
	//Create wrap layer
	var bodyObj = document.getElementsByTagName('BODY')[0];
  if(!document.getElementById("bigImgWrap")) {
    divObjWrapper = document.createElement("div");
    divObjWrapper.setAttribute("id", "bigImgWrap");
    bodyObj.appendChild(divObjWrapper);
  } else {
    divObjWrapper = document.getElementById("bigImgWrap");
  }
  with(divObjWrapper.style){
	position='absolute'; 
	backgroundColor='#000000'; 
	top='0px'; 
	left='0px'; 
	zIndex='200'; 
	width='100%';
	height=fullHeight+'px'; 
	filter='alpha(opacity=60)'; 
	mozOpacity='0.6';	
	opacity='0.6';
	visibility='visible';
  }

  if(resizeJsImage==1) {
   if((imgwidth+3*pg_padding)>windowWidth) {
     imgheight = (imgheight * (windowWidth-2*pg_padding))/imgwidth;
     imgwidth = windowWidth-2*pg_padding;
   }
   if((imgheight+2*pg_padding+80)>windowHeight) {
     imgwidth = (imgwidth * (windowHeight-2*pg_padding-80))/imgheight;
     imgheight = windowHeight-2*pg_padding-80;
   }
  }
  var postop =(windowHeight/2)-(imgheight/2)+yScroll+document.body.style.padding-10;
  var posleft =(windowWidth/2)-(imgwidth/2)+xScroll+document.body.style.padding;
  if(postop >= 30) { 
   postop = postop-30;
  }
 // var bodyObj = document.getElementsByTagName('BODY')[0];
  if(!document.getElementById("pg_photocontainer")) {
    divObjContainer = document.createElement("div");
    divObjContainer.setAttribute("id", "pg_photocontainer");
    bodyObj.appendChild(divObjContainer);
  } else {
    divObjContainer = document.getElementById("pg_photocontainer");
  }

  var closeimg = new Image();
  closeimg.src = "/images/close.gif";

  var dhtmltext, dhtmltext2="";

  divObjContainer.style.display = "block";
  dhtmltext  = "<div class=\"pg_photocontainer\" style=\"top:"+postop+"px; left:"+posleft+"px; position: absolute; display:block;z-index:99999;\">";
  dhtmltext += "<div class=\"photoborder\" style=\"background-color: "+pg_openjs_background+"; padding: "+pg_padding+"px; border: solid 1px "+pg_dhtml_border+";\">";
  dhtmltext += "<img onclick=\"pg_photocontainershut()\" style=\"cursor:pointer;border: solid 1px #000;width:"+imgwidth+"px;height:"+imgheight+"px;\" src=\""+imgsource+"\" alt=\""+imgtitle+"\" width=\""+imgwidth+"px\" height=\""+imgheight+"px\" class=\"pngfile\" \/>";
  dhtmltext += "<div id='arrows' style='text-align:center'></div><img onclick=\"pg_photocontainershut()\" style=\"cursor:pointer;position:absolute;bottom:"+pg_padding+"px;right:"+pg_padding+"px;width:"+closeimg.width+"px;height:"+closeimg.height+"px;\" src=\""+closeimg.src+"\" title=\"Закрыть\" id=\"dhtml_close\" class=\"pngfile\" />";  
  dhtmltext += "<br /><div id=\"pony_dhtml_imgtext\" style=\"color:#000000; margin-top:"+pg_padding+"px;text-align: justify; width:"+imgwidth+"px;\">&nbsp;<br />&nbsp;</div>";
  //dhtmltext += "<\/div></div>";
  divObjContainer.innerHTML = dhtmltext;
     document.getElementById("pony_dhtml_imgtext").style.width2 = document.getElementById("pony_dhtml_imgtext").style.width-document.getElementById("dhtml_close").style.width;
  if (pg_show_title_in_dhtml==1) {
   dhtmltext2 += "<strong>"+imgtitle+"</strong><br />";
  }
  if (pg_show_description_in_dhtml==1) {
   dhtmltext2 += imgtext;
  }
  dhtmltext2 += "<\/div></div>";
  if (dhtmltext2!="") {
   document.getElementById("pony_dhtml_imgtext").innerHTML = dhtmltext2;
  }

  if (pg_disableclick==1) { 
    divObjContainer.oncontextmenu = function(){return false;}
  }
  getNextPrev(id, cat);
}

var submitExist=0;
var divExist=false;
function setTimeSubmit(time){
	submitExist=time;
}
function getTimeSubmit(){
	return submitExist;
}

function prepAjaxFrm(obj){
	
	//$('.ajaxFrm').submit(function(){ 
		var now = parseInt(new Date().getTime());
		var submitTime=getTimeSubmit();
		if(submitTime==0 || (now-submitTime)>=3000){
			setTimeSubmit(now);
			var formId=$(obj).attr('id');
			$("*").css('cursor', 'wait'); 
			if(divExist==false){ 
				$('#'+formId).before('<div id="'+formId+'_answer"><img src="/images/loading.gif" align="absmiddle">&nbsp;Пожалуйста, подождите...</div>');
				divExist=true;
			}else{
				//$("#faq_form_answer").removeClass("error_answer good_answer");
				$('#'+formId+'_answer').html('<div id="'+formId+'_answer"><img src="/images/loading.gif" align="absmiddle">&nbsp;Пожалуйста, подождите...</div>');
			} 
			$.post('/modules/'+formId+'.php', $('#'+formId).serialize(), 
				function(data){ 
					$('#'+formId+'_answer').html(data).effect("pulsate", { times:2 }, 500); 
					$("*").css('cursor', 'default');
					$("a").css('cursor', 'pointer');
				}
			);
		}
		return false;
	//});
}

function getSelect(start, end){
	var out='';
	for(var i=start; i<=end; i++){
		out+='<option value="'+i+'">'+i+'</option>';
	}
	return out;
}

$(document).ready(
	function(){
		$('.roller').hover(
			function(){ 
				$(this).attr('src', $(this).attr('src').replace('.jpg', '')+'c.jpg')
			},
			function(){
				$(this).attr('src', $(this).attr('src').replace('c.jpg', '')+'.jpg')
			}
		)
		//prepAjaxFrm();
	}
);
