var timer;

jQuery.preloadImages = function(){
		jQuery.each(arguments, function (e){jQuery("<img>").attr("src", this);});
	}
	
/*ПОКАЗАТЬ ЦЕНЫ*/
function ShowPrice(pr_id){
	$.getJSON("/ajax/showprice/", 
				  {'pr_id':pr_id, 'PHPSESSID':$('head').data('sessid')},
				  function (result){
						if(result.count>0){
								for(var counter=0; counter<result.count; counter++){
										$("#art_"+result.id[counter]).html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '+result.price[counter]+' Р &nbsp;&nbsp;');
									}
							}
					}
				  );
	}
	
/*ДОБАВЛЕНИЕ В КОРЗИНУ*/
function putToCart(id, type){	
	hideDivMsg();
	$("#atc_inf_div_img").attr("src", pic_load);
	
	var el = document.getElementById("p" + id);
	
	if(type == 1){
			//295
			if(detectIE6()) $("#atc_inf_div").css("left", getAbsolutePosition(el).x  - 295 + "px").css("top", getAbsolutePosition(el).y  - 198 + "px");
			else $("#atc_inf_div").css("left", getAbsolutePosition(el).x - 30 + "px").css("top", getAbsolutePosition(el).y  - 198 + "px");
		}		
	else if(type == 2){
			$("#atc_inf_div_img").css("padding-top", 80 + "px");
			$("#atc_inf_div").css("width", 300 + "px").css("height", 310 + "px").css("left", getAbsolutePosition( el.parentNode.parentNode.parentNode ).x - 15 + "px").css("top", getAbsolutePosition( el.parentNode.parentNode.parentNode ).y  - 150 + "px");
		}
		
	$("#atc_inf_div").css("display", "block");	
	setTimeout('putToCartEnd("'+id+'")',500);
}

function putToCartEnd(id){
	$.get("/ajax/puttocart/", 
				  {'pr_id':id, 'PHPSESSID':$('head').data('sessid')},
				  function (result){	
						$("#atc_inf_div_img").attr("src", pic_ok);
						$("#cart").html(result);
						timer = window.setTimeout("hideDivMsg()", 2000);
					}
			);
}

function hideDivMsg(){	
	if (timer) window.clearTimeout(timer);
	$("#atc_inf_div").css("display","none");
}

/*ДОБАВЛЕНИЕ В КОРЗИНУ СРАВНЕНИЯ*/
function putToSCart(id){
	$.get("/ajax/puttoscart/", 
				  {'pr_id':id, 'PHPSESSID':$('head').data('sessid')},
				  function (result){
						$("#scart").html(unescape(result));
						$("#s"+id).html("Добавлен к&nbsp;сравнению")
								  .css("border","0px").css("cursor","text").css("color","#F6871F")
                                  .attr("title","");
					}
			  );			  
}		

/*СОСТОЯНИЕ КОРЗИНЫ*/
function showCartInfo(){
	$("#cart_info_content").html("<br><center><img src=\"/images/bg_l.gif\"></center>");
	$("#cart_info").animate({height:"show"},300,
					function(){
							$.get("/ajax/showcartinfo/",
									 {'PHPSESSID':$('head').data('sessid')},
									 function (result){
											$("#cart_info_content").animate({height:"hide"},300, function(){ $("#cart_info_content").html(result).animate({height:"show"},500);});
										}
								);
						}
					);
}

function PollResponse(poll){
	var elem = $('#response > input:radio[name=response]:checked');
	var response = $("#response");
	if(elem.val()){	
		response.slideUp(700,
			function(){
				response.html("<center><img src=\"/images/bg_l.gif\"></center>").slideDown(500, 
					function(){
						response.height('100%');
						$.get("/ajax/pollresult/", {'poll':poll, 'response':elem.val(), 'PHPSESSID':$('head').data('sessid')},
							function (result){
								response.slideUp(500, 
									function(){		
										response.height('');
										response.html(unescape(result)).slideDown(1000, function() {response.height('100%');});																				
									}
								)						
							}
						);
					}
				);
			}
		);				
	}
	else{
		var poll_err = $('#poll_err');
		poll_err.slideDown(300, function(){poll_err.height(poll_err.height());});
	}
}

/*ОТОБРАЖЕНИЕ ИНФОРМАЦИИ О СПОСОБАХ ДОСТАВКИ*/
function CDCart(elem){
	var id = elem.value;	
	$("#cd_cart_l").html(' <img src="/images/bg_ls.gif" alt="Идет загрузка.." style="vertical-align: middle;">');
	$("#cd_cart").slideUp(500,
		function(){
			$("#cd_cart").html(" "); 					

			$.get("/ajax/cdcart/",
					{'type':id, 'PHPSESSID':$('head').data('sessid')},
						function(result){
								$("#cd_cart").html(unescape(result)).slideDown(500);	
								$("#cd_cart_l").html(' ');
							}
				 );
		});
}	
	
/*ОТОБРАЖЕНИЕ ИНФОРМАЦИИ О СПОСОБАХ ОПЛАТЫ*/
function CPCart(elem){
	var id = elem.value;		
	$("#cp_cart_l").html(" <img src=\"/images/bg_ls.gif\" alt=\"Идет загрузка..\" style=\"vertical-align: middle;\">");		
	$("#cp_cart").slideUp(500, 
		function(){
			$("#cp_cart").html(" "); 					
	
			$.get("/ajax/cpcart/",
					{'type':id, 'PHPSESSID':$('head').data('sessid')},
						function(result){
								$("#cp_cart_l").html(' ');
								$("#cp_cart").html(unescape(result)).slideDown(500, function(){ShowOrgDiv();});																
							}
				 );
		});
}

/*ОТОБРАЖЕНИЕ БЛОКА ДЛЯ ЮР ЛИЦ ПРИ ОФОРЛЕНИИ ЗАКАЗА*/
function ShowOrgDiv(){
	if($("#p_type").val() == 2) $("#org_div").slideDown(500);
	else $("#org_div").slideUp(500);
}	

/*ОТОБРАЖЕНИЕ КАРТИНОК*/
function doPic(imgName){
		$("#m_img").fadeOut(300, function(){$(this).attr("src","/img/catalog/"+imgName).fadeIn(300);});						
	}
	
function doCompPic(id, imgName){
		$("#"+id).fadeOut(300, function(){$(this).attr("src","http://shop.v-lazer.com/img/catalog/"+imgName).fadeIn(300);});						
	}

/*ДОП. ФУНКЦИИ*/	
function getAbsolutePosition(el) {	
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}	
	
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) 
		{
			limitField.value = limitField.value.substring(0, limitNum);
		}
	else 
		{
			limitCount.value = limitNum - limitField.value.length;
	    }
	}	
	
function detectIE6()
	{
		var browser = navigator.appName;
		if (browser == "Microsoft Internet Explorer")
			{
				var b_version = navigator.appVersion;
				var re = /\MSIE\s+(\d\.\d\b)/;
				var res = b_version.match(re);
				
				if (res[1] <= 6)
					{
						return true; 
					}
			}	
		return false;
	}	