//if (top!=self) top.location.href=self.location.href;

$(document).ready(function(){

//============= prihlasovanie =============
	$('#dialog-result').dialog({
		autoOpen: true,
		hide: 'slide',
		modal: true,
		width: 350,
		buttons: {
			"Ok": function() { 
			$(this).dialog("close"); 
			} 
		}
	});
	
	$('#dialog-error').dialog({
		autoOpen: false,
		hide: 'slide',
		modal: true,
		width: 350,
		buttons: {
			"Ok": function() { 
			$(this).dialog("close"); 
			} 
		}
	});
//============= prihlasovanie =============
	$('#dialog-login').dialog({
		autoOpen: false,
		hide: 'slide',
		modal: true,
		width: 400,
		buttons: {
			"Ok": function() { 
			$(this).dialog("close"); 
			} 
		}
	});
	$("#button_login").click(function()
	{
		$.post("php/ajax_login.php",{ username:$('#username').val(),passwd:$('#passwd').val() } ,function(data)
		{
			if(data=='yes')
			{
				$("#login_form").submit();
			}
			else
			{
				$("#dialog-login").dialog("open");
				
			}
		});
		return false;
	});
	
	//========== registracia =============
	
	$("#button_register").click(function()
	{
		if ($("#l_agree").is(":checked"))
		{	
			var field_list = ['l_nick','l_passwd', 'l_passwd_confirm', 'l_name', 'l_surname', 'l_street', 'l_town', 'l_state@select', 'l_post_code', 'l_mobile'];
			var mail_list = ['l_email'];
			return checkMyForm('#new_user',field_list,mail_list);
		}
		else
		{
			$("#dialog-error > p").html("Zabudli ste potvrdiť, že súhlasíte s pravidlami stránky!");
			$('#dialog-error').dialog("open");
			return false;
		}
	});
	
	//========== objednavka bez registracie =============
	
	$("#button_order").click(function()
	{
		var field_list = ['l_name', 'l_surname', 'l_street', 'l_town', 'l_state@select', 'l_post_code', 'l_mobile'];
		var mail_list = ['l_email'];
		return checkMyForm('#order_form',field_list,mail_list);
	});
	
	//========== nakupny kosik =============
	
	$("#button_cart_send").click(function()
	{
		var _this_state = $('#l_deliver_state').val();
		console.log(_this_state);
		if ($('#l_pay_id').val() == 4 && _this_state != 'Česko' && _this_state != 'ČR' && _this_state != 'Česká republika' && _this_state != 'Cesko' && _this_state != 'CR' && _this_state != 'Ceska republika') {
			$("#dialog-error > p").html("Prevodným príkazom možno uhrádzať iba zásielky s dodacou adresou v Českej republike! Pre zaslanie na Slovensko zvoľte iný druh platby.");
			$('#dialog-error').dialog("open");
			return false;
		}
		else if ($("#l_agree").not(":checked").length > 0)
		{
			$("#dialog-error > p").html("Zabudli ste potvrdiť, že súhlasíte s obchodnými podmienkami!");
			$('#dialog-error').dialog("open");
			return false;
		}
		else
		{
			return true;
		}
	});
	
	//========== update profil =============
	
	$("#button_edit_settings").click(function()
	{
		var field_list = ['l_nick', 'l_name', 'l_surname', 'l_street', 'l_town', 'l_state@select', 'l_post_code', 'l_mobile'];
		var mail_list = ['l_email'];
		return checkMyForm('#edit_settings',field_list,mail_list);
	});
	
	//========== kontaktny formular =============
	
	$("#button_send_contact").click(function()
	{
		var field_list = ['l_name','l_text@textarea'];
		var mail_list = ['l_email'];
		return checkMyForm('#contact_form',field_list,mail_list);
	});
	
	//========== vyhladavanie produktov =============
	
	$("#button_search_product").click(function()
	{
		if ($("#sstring").val().length<3) 
		{
			$("#dialog-error > p").html("Hľadaný výraz musí mať minimálne 3 znaky!");
			$('#dialog-error').dialog("open");
			return false;
		}
		else
		{
			var field_list = ['sstring'];
			var mail_list = [];
			return checkMyForm('#search_by_name',field_list,mail_list);
		}
	});
	
	$("#button_new_passwd").click(function()
	{
		if ($("#f_mail").val()=="@") $("#f_mail").val("");
		var field_list = ['f_user'];
		var mail_list = ['f_mail'];
		return checkMyForm('#send_new_passwd',field_list,mail_list);
	});

});

// ---------- functions --------------- //

//Cookies handle functions //
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
//Cookies handle functions //

//kontrola vyplnenia forulara
function checkMyForm (form_name,fields,mails) 
{
	
	var formValidator = new ValidatorClass();
	formValidator.init({ 
		form: form_name,
		errorColors: {
			isRequired: '#E20065',
		    isEmail:    '#cd0a0a',
		    isNumeric:  'green',
		    isInteger:  'blue',
		    hasRegex:   'lightgray'
		},
		isRequired:        fields, 
		isEmail:           mails,
		allowNullNumbers:   true,
		requiredFieldMsg:   'Nezadali ste všetky povinné údaje!',
		emailFieldMsg:      'Zadaná e-mailová adresa nie je platná.'
	});
	error = formValidator.validate();

    if (formValidator.isEmpty(error)) {
    	return true;
	} else {
		$("#dialog-error > p").html(error);
		$('#dialog-error').dialog("open");
		return false;
		//return false;
	}
}
//ikonka zobrazit obrazky
function getGallery(obj)
{
	$(obj).click(function() {
		var obj_id = $(obj).attr("name");
		$("#thumb_img_"+obj_id).trigger("click");
		return false;
  	});
}
			
function addToCart(product)
{
	var pro_id = $(product).attr("name");
	$("#dialog-add-to-cart").load("php/ajax_add_to_cart.php?product="+pro_id, function(res) {
		$('#dialog-add-to-cart').dialog('open');
	});
}

//var def_pan_height = $("#left").height();
function resizePanel()
{
	//alert($("#left").height());
	//$("#left").height(window.def_pan_height);
	var doc_height = $("#content-all").height(); //alert(doc_height);
	var panel_height = $("#left").height(); //alert(panel_height);
	var foot_height = 0;	
	if (doc_height>panel_height)
	{
		//alert(doc_height+" "+panel_height);
		var difference = doc_height-panel_height;
		if (difference<248)
		{
			//window.status = "1 - "+doc_height+" - "+panel_height;
			$("div[class^='product_panel-all']:last").height(doc_height);
		}
		else
		{
			//window.status = "2 - "+doc_height+" - "+panel_height;
			$("#left").height(doc_height-30);
		}
	}
	else
	{
		var def_height = $("div[class^='product_panel-all']:last").height();
		if (def_height!=null)
		{
			var difference = panel_height-doc_height;
			var new_height = def_height + difference + 20;
			//window.status = "3 - "+difference+" "+def_height+" "+new_height;
			$("div[class^='product_panel-all']:last").height(new_height);
		}
		else
		{
			var def_height = $("div[id^='text_panel-all']:last").height();
			var difference = panel_height-doc_height;
			var new_height = def_height + difference + 20;
			//window.status = "4 - "+difference+" "+def_height+" "+new_height;
			$("div[id^='text_panel-all']:last").height(new_height);
		}
			
	}
}

//otvaranie / zatvaranie menu
function ShowHide(i,resizep) {
  var old_menu_height = 0;
  $("ul[id^='categories']").each(function(index) {
    old_menu_height += $(this).height()+100;
  });
  old_menu_height += 460;
  
  var obj = $("#"+i);
  var obj2 = $("#arrowImg"+i);
  //alert($(obj).css("display"));
  if ($(obj).css("display") == 'none') {
    $(obj).css("display","block");
	$(obj2).attr("class","catar2");
	//alert(i+" - show");
  } else if ($(obj).css("display") == 'block') {
    $(obj).css("display","none");
	$(obj2).attr("class","catar1");
	//alert(i+" - hide");
  }
  if (resizep==1) {
	 var new_menu_height = 0;
     $("ul[id^='categories']").each(function(index) {
       new_menu_height += $(this).height()+100;
     });
  	 new_menu_height += 460;

  	 var panel_height = $("#left").height();
	 var doc_height = $("#content-all").height();
	 
	 if (doc_height<new_menu_height)
	 {
		 var difference = new_menu_height-doc_height;
		 var def_height = $("div[class^='product_panel-all']:last").height();
		 var new_height = def_height+difference+20;
		 $("div[class^='product_panel-all']:last").height(new_height);
		 //window.status = "5 - "+doc_height+" "+new_menu_height+" "+" ";
	 }
	 else if (doc_height>new_menu_height)
	 {
		 var difference = doc_height-new_menu_height;
		 var def_height = $("div[class^='product_panel-all']:last").height();
		 var new_height = (def_height-difference+20>360) ? def_height-difference+20 : 360;
		 $("div[class^='product_panel-all']:last").height(new_height);
		 //window.status = "6 - "+doc_height+" "+new_menu_height+" "+" ";
	 }
  }
}

