function sleep(timeout) {
        var loop = true;
        var current = new Date();
        var now;
        var cTimestamp = current.getTime();
        while(loop) {
                now = new Date();
                nTimestamp = now.getTime();
                if(nTimestamp - cTimestamp > timeout) {
                        loop = false;
                }
        }
}
function AjaxLogin() {
//	console.log($("#headerloginform").serialize());
	$.post('/user.php?sub=login&ajax=1',$("#headerloginform").serialize(), function(data) {
//		console.log(data);
		  if (data.status=='ok') {
			  window.location='/';
		  } else {
			  alert(data.msg);
		  }
	},'json');
	return false;
}
function AddItemToCart(id_item) {
	$.getJSON("/catalog/cart/additem/"+id_item+"/ajax/",
		function(data){
			LoadCartLastItem();
			//console.log(data);
			if (data.msg) {
				$('#cart_count_place').text(data.count);
				$('#cart_count_place_str').text(' '+str_num125(data.count,'товар', 'товара', 'товаров'))
				$('#cart_summ_place').text(data.summ);
				//alert(data.msg);
				$('#cart_item_preview .itemName a:first').html(data.item_name);
				$('#cart_item_preview .itemName a:first').attr('href',data.item_url);
				$('#cart_item_preview .itemName a:first').attr('href',data.item_url);
				$('#cart_item_preview img:first').attr('src',data.item_img_url);
				$('#cart_empty').fadeOut();
				$('#cart_full').fadeIn();
				$('#cart_item_preview .itemPrice:first').html(data.item_price);
				$('#cart_item_preview').slideDown( 400, function(){
					sleep(1000);$('#cart_item_preview').slideUp( 400); 
					} 
				);
			}
		}
	);
}
function AddItemToCartNum(id_item,num) {
	$.getJSON("/catalog/cart/additem/"+id_item+((num>0)?"/count/"+num:'')+"/ajax/",
		function(data){
			LoadCartLastItem();
			//console.log(data);
			if (data.msg) {
				$('#cart_count_place').text(data.count);
				$('#cart_summ_place').text(data.summ);
				$('#cart_count_place_str').text(' '+str_num125(data.count,'товар', 'товара', 'товаров'))
				//alert(data.msg);
				$('#cart_item_preview .itemName a:first').html(data.item_name);
				$('#cart_item_preview .itemName a:first').attr('href',data.item_url);
				$('#cart_item_preview .itemName a:first').attr('href',data.item_url);
				$('#cart_item_preview img:first').attr('src',data.item_img_url);
				$('#cart_empty').fadeOut();
				$('#cart_full').fadeIn();
				$('#cart_item_preview .itemPrice:first').html(data.item_price);
				$('#cart_item_preview').slideDown( 400, function(){
					sleep(1000);
					$('#cart_item_preview').slideUp( 400); 
					} 
				);
			}
		}
	);
}
function ItemWish(id_item) {
	if ($('#button_wish_add').hasClass('addwish')) {
		AddWish(id_item,function(){
			$('#button_wish_add').removeClass('addwish').html('Убрать из списка желаний');
		});
	} else {
		RemoveWish(id_item,function(){
			$('#button_wish_add').addClass('addwish').html('Добавить в список желаний');
		});
	}
}
function WishListRemove(id_item) {
	RemoveWish(id_item,function(){
		var url=window.location;
		window.location=url;
	});
	
}
function AddWish(id_item,calback) {
	$.getJSON("/user.php?sub=wishes&act=add", {id_item:id_item},calback);
}
function RemoveWish(id_item,calback) {
	$.getJSON("/user.php?sub=wishes&act=delete", {id_item:id_item},calback);
}
function update_cat_links(id_cat){
	$('#cat_box'+id_cat+' a.cat_url').click(function(){
		var url=$(this).attr('href');
		if ($('#cat_'+id_cat+'_price_from').val()) {
			url+='price_from/'+$('#cat_'+id_cat+'_price_from').val()+'/';
		}
		if ($('#cat_'+id_cat+'_price_to').val()) {
			url+='price_to/'+$('#cat_'+id_cat+'_price_to').val()+'/';
		}
		window.location=url;
		return false;
	});
}
function Slider (div_id) {
	var _this=this;
	this.place=$('#'+div_id);
	this.left_index=0;
	var i =0;
	this.place.find('ul li').each(function(){
		$(this).css('left',(i*295)+'px');
		i++;
	});
	this.items_count=i;
	this.place.find('a.left_arrow').click(function(){
		_this.Left();
		return false;
	});
	this.place.find('a.right_arrow').click(function(){
		_this.Right();
		return false;
	});
}
Slider.prototype.Left = function() {
	var index1,index2,index3;
	this.left_index--;
	index1=this.left_index;
	if (index1<0) {
		index1=this.items_count-1;
		this.left_index=this.items_count-1;
	}
	index2=index1+1;
	if (index2>=this.items_count) {
		index2=0;
	}
	index3=index2+1;
	if (index3>=this.items_count) {
		index3=0;
	}
	var i=0;
	this.place.find('ul li').each(function(){
		if (i==index1) {
			$(this).css('left','0');
			$(this).show();
		} else if (i==index2){
			$(this).css('left','295px');
			$(this).show();
		} else if (i==index3){
			$(this).css('left','590px');
			$(this).show();
		} else{
			$(this).hide();
		}
		i++;
	});
}
Slider.prototype.Right = function() {
	var index1,index2,index3;
	this.left_index++;
	index1=this.left_index;
	if (index1>=this.items_count) {
		index1=0;
		this.left_index=0;
	}
	index2=index1+1;
	if (index2>=this.items_count) {
		index2=0;
	}
	index3=index2+1;
	if (index3>=this.items_count) {
		index3=0;
	}
	var i=0;
	this.place.find('ul li').each(function(){
		if (i==index1) {
			$(this).css('left','0');
			$(this).show();
		} else if (i==index2){
			$(this).css('left','295px');
			$(this).show();
		} else if (i==index3){
			$(this).css('left','590px');
			$(this).show();
		} else{
			$(this).hide();
		}
		i++;
	});
}
function LoadCartLastItem() {
	$('#cart_last_item').load('/catalog/cart/act/getlastitem/');
}
function ShowCartLastItem() {
	if ($('#cart_count_place').html()>0)
		$('#cart_last_item').show();
}
function HideCartLastItem() {
	$('#cart_last_item').hide();
}
$(document).ready(function(){
	$("#more_button").click(function(){
		$("#more_button").hide();
		$("#close_button").show();
		$("#short_text").hide();
		$("#more_text").slideDown();
		return false;
	});
	$("#close_button").click(function(){
		$("#close_button").hide();
		$("#more_button").show();
		$("#more_text").hide();
		$("#short_text").show();
		return false;
	});
	$('#cart_info').hover(ShowCartLastItem,HideCartLastItem);
	LoadCartLastItem();
	$('#callback_url').click(
		function () {
			$('#callback').fadeIn();
			$('#callback_url').fadeOut(); 
			return false;
		}
	);
	$('#subscribe_url').click(SubscribeShow);
	$('#subscribe_exit').click(SubscribeExit);
	$('#subscribe .subscribe_on').click(SubscribeOn);
	$('#subscribe .subscribe_off').click(SubscribeOff);
});
function SubscribeShow() {
	$('#auth_shadow').show();
	$('#subscribe').show(); 
	return false;
}
function SubscribeExit() {
	$('#auth_shadow').hide();
	$('#subscribe').hide();
	return false;
}
function SubscribeOn() {
//	console.log('SubscribeOn');
	$.post('/?mod=subscription&sub=subscribe&ajax=1',{email:$('#subscribe input').val(),lists:[1]}, function(data) {
		alert(data.msg);
//		console.log(data);
	},'json');
//	$('#auth_shadow').hide();
//	$('#subscribe').hide();
	return false;
}
function SubscribeOff() {
//	console.log('SubscribeOff');
	$.post('/?mod=subscription&sub=unsubscribe&ajax=1',{email:$('#subscribe input').val(),lists:[1]}, function(data) {
		alert(data.msg);
//		console.log(data);
	},'json');
//	$('#auth_shadow').hide();
//	$('#subscribe').hide();
	return false;
}
function CallMe() {
	var phone=$('#call_me_phone').val();
	if (!phone || phone=='Телефон') {
		alert('Укажите телефон');
		$('#call_me_phone').focus();
		return;
	}
	//*
	$.getJSON("/ajax.php?act=callme",{phone:phone}, function(data){
		$('#callback').fadeOut();
		$('#callback_url').fadeIn(); 
		alert(data.msg);
	});
	/**/
}
function str_num125 (num,str1,str2,str5)
{
	if (num > 4 && num < 21)
		return str5;
	num=num%10;
	if (num == 1)
		return str1;
	if (num > 1 && num < 5)
		return str2;
	return str5;
}

function getAbsoluteCoords(oElement) {
	var oResult = {
		iTop  : 0,
		iLeft : 0
		};
	while(oElement) {
		oResult.iTop += oElement.offsetTop;
		oResult.iLeft += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return oResult;
}

addPizza = false;

function AddToCart(iNum,iType,iPizzaId,iPrice) {
	if(!addPizza) {
		addPizza = true;
		
		var oPizzaBlock = document.getElementById('flying_pizza');
		var oPizzaImg = document.getElementById('flying_pizza_img');
		
		var oBox = document.getElementById('cart_full');
		
		if ($('#cart_full').css('display') == "none") var oBox = document.getElementById('cart_empty');
		
		
		var oElem = document.getElementById('pizza_'+iNum);
		
		
		console.log(oElem);
		
		var sImg = $(oElem).attr('src');
		
		var aElemCoords = getAbsoluteCoords(oElem);
		var aBoxCoords = getAbsoluteCoords(oBox);
		

		var iSide1Width = Math.abs( parseInt(aBoxCoords.iLeft - aElemCoords.iLeft) );
		var iSide2Width = Math.abs( parseInt(aElemCoords.iTop - aBoxCoords.iTop) );
		
		var iSpeedLine = Math.round(Math.sqrt( iSide1Width*iSide1Width + iSide2Width*iSide2Width ));
		
		$(oPizzaBlock).css('left', aElemCoords.iLeft);
		$(oPizzaBlock).css('top', aElemCoords.iTop);
		$(oPizzaBlock).css('width', $(oElem).attr('width'));
		$(oPizzaBlock).css('height', $(oElem).attr('height'));
		
		$(oPizzaImg).attr('src', sImg);
		$(oPizzaImg).css('width', $(oElem).attr('width'));
		$(oPizzaImg).css('height', $(oElem).attr('height'));
		
		$(oPizzaBlock).toggleClass('hidden');
		$(oElem).toggleClass('hidden');
		
		$(oPizzaBlock).animate(
			{
				left: aBoxCoords.iLeft + 23,
				top: aBoxCoords.iTop + 23,
				width: 40,
				height: 40
			},
			500
		);
		$(oPizzaImg).animate(
			{
				width: 40,
				height: 40
			},
			500,
			function() {
				$(oPizzaBlock).toggleClass('hidden');
				$(oElem).toggleClass('hidden');
				addPizza = false;
			}
		);
	}
}
