var clientListStep=150;
var clientsStep=1;


$(document).ready(function () {
   
	if(initPrettyPhoto)
	{
		$("a[rel^='prettyPhoto']").prettyPhoto();
	}

	
	if(document.getElementById("clientsList"))
	{
		animateClients();
	}
	
	
	if(document.getElementById("dialog-message"))
	{
		$("#dialog-message").dialog({
				modal: true,
				buttons: {
					Ok: function() {
						document.location=base_url;
						$(this).dialog('close');						
					}
				}
			});
	}
	
	if(document.getElementById("dialog-form"))
	{
		$("#dialog-form").dialog({
				autoOpen: false,
				height: 180,
				width: 380,
				modal: true,
				buttons: {
					'Добавить в корзину': function() {
						
						updateCart($('#complectId').attr("value"), $('#dialogAmount').attr("value"));
						$(this).dialog('close');
					},
					'Отмена': function() {
						$(this).dialog('close');
					}
				},
				close: function() {
					$('#dialogAmount').attr("value",1);
				}
		});
	}
	
	
	if(document.getElementById("dialog-form2"))
	{
		$("#dialog-form2").dialog({
				autoOpen: false,
				height: 180,
				width: 380,
				modal: true,
				buttons: {
					'Изменить количество': function() {
						$("#amountLink"+$('#complectId').attr("value")).html($('#dialogAmount').attr("value"));
						updateCart($('#complectId').attr("value"), $('#dialogAmount').attr("value"));
						$(this).dialog('close');
					},
					'Отмена': function() {
						$(this).dialog('close');
					}
				},
				close: function() {
					
				}
		});
	}
	
	
});





function selectMenu(id)
{
	currentClass=$("#catalogItem"+id).attr("class");
	
	if(currentClass!='activeLi')
	{	
		$(".activeLi ul").slideUp(400);
		$(".activeLi").attr("class","");
		
		$("#catalogItem"+id).attr("class","activeLi");
		$("#catalogItem"+id+" ul").slideDown(400);	
	}
}







function animateClients()
{
	if(clientsStep<=totalClients)
	{
		$("#clientsList").delay(1000).animate({ marginLeft: -clientListStep*clientsStep }, 500, 
				function() {
					clientsStep++;
					animateClients();
				}
		);
	}
	else
	{
		$("#clientsList").css("margin-left", "150px");
		clientsStep=0;
		$("#clientsList").animate({ marginLeft: -clientListStep*clientsStep }, 500, 
				function() {
					clientsStep++;
					animateClients();
				}
		);	}
}


function hlTooltip(id, state)
{
	if(state)
	{
		$("#tooltip"+id).show();	
	}
	else
	{
		$("#tooltip"+id).hide();
	}
}

function checkSubscribe(def)
{
	if($("#subcribeEmail").attr("value")==def)
	{
		$("#subcribeEmail").attr("value","");	
	}

}

function checkSubscribeEmpty(def)
{
	if($("#subcribeEmail").attr("value")=="")
	{
		$("#subcribeEmail").attr("value",def);	
	}

}



function scheckSubscribe(def)
{
	if($("#searchQuery").attr("value")==def)
	{
		$("#searchQuery").attr("value","");	
	}

}

function scheckSubscribeEmpty(def)
{
	if($("#searchQuery").attr("value")=="")
	{
		$("#searchQuery").attr("value",def);	
	}

}


function subscribe(def)
{
	var email=$("#subcribeEmail").attr("value");
	if(def!=email)
	{
		var requestUrl2=base_url+"ajax/subscribe/"+email+"/";
		$.ajax({
		   url: requestUrl2,
		   success: function(msg){
				switch(parseInt(msg))
				{
					case 0: alert("Скорее всего вы ввели неправильный email-адрес.  Попробуйте ещё раз."); break;
					case 1: $("#subscribeCont").html("Спасибо, ваш email-адрес добавлен в базу данных."); break;
					case 2: alert("Такой email-адрес уже есть в базе данных"); break;
					default: alert("Скорее всего вы ввели неправильный email-адрес. Попробуйте ещё раз.");
				}
			}
		});
	}
	else
	{
		alert("Введите нормальный email! :)");	
	}
}



//Функция подсветки пунктов меню
function menu_hl(uid, state, submenu)
{

	menuItem=$("#"+submenu+"menudiv_"+uid);
	linkItem=$("#"+submenu+"menulink_"+uid);
	menuClass=menuItem.attr("class");	

	if(menuClass!=submenu+"menu_link_active")
	{
		if(state)
		{
			menuItem.attr("class",submenu+"menu_link_highlighted");
			linkItem.attr("class",submenu+"menu_link_highlighted");
			switch(uid)
			{
				case '0': $("#menu_left").css("background-position","0px -51px"); break;
				case '1000':	$("#menu_right").css("background-position","0px -51px"); break;
			}
		}
		else
		{
			menuItem.attr("class",submenu+"menu_link_passive");
			linkItem.attr("class",submenu+"menu_link_passive");
			$("#menu_right").css("background-position","0px 0px");	
			if(firstMenu==false)
			{
				$("#menu_left").css("background-position","0px 0px");
			}
		}
	}
}


function orderButtonHl(obj, state)
{
	if(state)
	{
		$(obj).css("background-position","0px -27px");
	}
	else
	{
		$(obj).css("background-position","0px 0px");	
	}
}


function sendForm()
{
	$('#orderForm').submit();	
}


function hlDelo(num)
{
	$("#currentDelo").html(listOfWorks[num]);
	left=num*7;
	$("#currentDelo").css("left", left+"px");
	//$("#currentDelo").animate({"left": left+"px"}, 200);
}

function hlMark(state)
{
	$('#sama_marka').animate({opacity: state}, 500);	
}

function mailbox_open(state)
{
	if(state)
	{
		$("#mailbox").css("background-position", "0px -50px");
	}
	else
	{
		$("#mailbox").css("background-position", "0px 0px");
	}
}

function send_mail()
{
	sendLetter=1;
	
	index=$("#form_index").attr("value");
	name=$("#form_name").attr("value");
	email=$("#form_email").attr("value");
	msg=$("#form_msg").attr("value");
	
	if(parseInt(index.length)==0){ sendLetter=0;}
	if(parseInt(name.length)==0){ sendLetter=0;}
	if(parseInt(email.length)==0){ sendLetter=0;}
	if(parseInt(msg.length)==0){ sendLetter=0;}
	
	if(sendLetter)
	{
		$("#form_mailbox").submit();
	}
	else
	{
		alert("Заполните пожалуйста все поля отмеченные звёздочкой");	
	}
}

function hlTr(obj, state)
{
	
	if(state)
	{
		$(obj).attr("class", "activeTr");	
	}
	else
	{
		$(obj).attr("class", "passiveTr");
	}
}

function hlTrash(id, state)
{
	if(state)
	{
		$("#trash"+id).css("background-position", "0px -15px");
	}
	else
	{
		$("#trash"+id).css("background-position", "0px 0px");	
	}
}


//------------------------------------ Cart --------------------------------------//

function openDialog(id, name, code)
{
	$('#dialogItemName').html(name);
	$('#dialogCode').html(code);
	$('#complectId').attr("value",id);
	$('#dialog-form').dialog('open');	
}

function openDialog2(id, name, code)
{
	$('#dialogItemName').html(name);
	$('#dialogCode').html(code);
	$('#complectId').attr("value",id);
	$('#dialogAmount').attr("value", parseInt($("#amountLink"+id).html()));
	$('#dialog-form2').dialog('open');	
}

function updateCart(id, num)
{
	
	var requestUrl=base_url+"ajax/update/"+id+"/"+num+"/";
	
	$.ajax({
	   url: requestUrl,
	   success: function(msg){
			$("#itemsAmount").html(msg);
			var tovarov="товаров";
			var amo=parseInt(msg);
			
			if(amo>20 || amo<5)
			{
				switch(msg.substr(msg.length-1,1))
				{
					case '1': tovarov="товар";  break;
					case '2':
					case '3':
					case '4':  tovarov="товара"; break;							
				}
			} 
			
			$("#tovarov").html(tovarov);
		}
	});
	
}

function removeCart(id)
{
	var requestUrl=base_url+"ajax/remove/"+id+"/";
	
	$.ajax({
	   url: requestUrl,
	   success: function(msg){
			$("#itemsAmount").html(msg);
			var tovarov="товаров";
			var amo=parseInt(msg);
			
			if(amo>20 || amo<5)
			{
				switch(msg.substr(msg.length-1,1))
				{
					case '1': tovarov="товар";  break;
					case '2':
					case '3':
					case '4':  tovarov="товара"; break;							
				}
			} 
			
			$("#tovarov").html(tovarov);
			
			$("#itemTr"+id).remove();
			$("#lineTr"+id).remove();
			
		}
	});
}



