function FitToContent(id, maxHeight)
{
   var text = id && id.style ? id : document.getElementById(id);
   if ( !text )
      return;

   var adjustedHeight = text.clientHeight;
   if ( !maxHeight || maxHeight > adjustedHeight )
   {
      adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
      if ( maxHeight )
         adjustedHeight = Math.min(maxHeight, adjustedHeight);
      if ( adjustedHeight > text.clientHeight )
         text.style.height = adjustedHeight + "px";
   }
}


$(function(){

	$("#orderbyswitch").change(function(){
		$ord = $(this).val();
		if(($ord != null)&&($ord != undefined)&&($ord != "")) {
			document.location = "/" + $ord;
		}
	});
	
	$("#pppswitch").change(function(){
		$ord = $(this).val();
		if(($ord != null)&&($ord != undefined)&&($ord != "")) {
			document.location = "/" + $ord;
		}
	});
	
	$("#selectvendor").change(function(){
		$url = $(this).val();
		if(($url != null)&&($url != undefined)&&($url != "")) {
			document.location = $url;
		}
	});
	
	$("#sidebarvendorselect").change(function(){
		$url = $(this).val();
		if(($url != null)&&($url != undefined)&&($url != "")) {
			document.location = $url;
		}
	});
	
	$("#sidebarofferselect").change(function(){
		$url = $(this).val();
		if(($url != null)&&($url != undefined)&&($url != "")) {
			document.location = $url;
		}
	});
	
	$("#selectgroup").change(function(){
		$url = $(this).val();
		if(($url != null)&&($url != undefined)&&($url != "")) {
			document.location = $url;
		}
	});
	
	$(".productCount").change(function(){
		$productId = $(this).attr("productId");
		$productCount = $(this).val();
		if(($productId != null)&&($productId != undefined)&&($productId != "")) {
			if(($productCount != null)&&($productCount != undefined)&&($productCount != "")&&($productCount != 0)) {
				document.location = "/change/" + $productId + "/" + $productCount;
			}
		}
	});
	
	$("#zinojums").keyup(function(){
		FitToContent(this, document.documentElement.clientHeight)
	});
	
	$(".zoom").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'titlePosition' : 'over',
		'padding'		: 0,
		'hideOnContentClick':	'true'
	});	
	$("#orderaforma").bind("keypress", function(e) {
		if(e.keyCode == 13) {
			return false;	
		}
	});
	$(".input_code").bind("keypress", function(e) {
		return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
	});
	$(".input_code").bind("keyup", function() {
		$limit = $(this).attr("size");
		if($(this).val().length > $limit) {
			$(this).val($(this).val().substring(0, $limit));
		}
	});
	// ordera forma
	$("#submit").click(function(){
		$error = false;
		$lauki = $(".order").find("input");
		//console.log($lauki);
		$.each($lauki, function(){
			if($(this).attr("type") == "text") {
				if(($(this).val() == "")&&($(this).attr("obl") == "true")) {
					$(this).addClass("wrong");
					$error = true;
				} else {
					if($(this).attr("id") == "pastakods") {
						$pastakods = parseInt($(this).val());
						if(($pastakods < 1000)||($pastakods > 9999)) {
							$(this).addClass("wrong");
							$error = true;
						} else {
							$(this).removeClass("wrong");
						}
//					} else if($(this).attr("id") == "pk1") {
//						if($(this).val() != "") {
//							$pk1val = $(this).val();
//							$pk1 = new Array();
//							$pk1[1] = parseInt($pk1val[0] + $pk1val[1]);
//							$pk1[2] = parseInt($pk1val[2] + $pk1val[3]);
//							$pk1[3] = parseInt($pk1val[4] + $pk1val[5]);
//							if((($pk1[1] < 1)||($pk1[1] > 31))||(($pk1[2] < 1)||($pk1[2] > 12))||(($pk1[3] < 0)||($pk1[3] > 99))) {
//								$error = true;
//								$(this).addClass("wrong");
//							} else {
//								$(this).removeClass("wrong");
//							}
//						} else {
//							$(this).removeClass("wrong");
//						}
					} else {
						$(this).removeClass("wrong");
					}
				}
			}
			if($(this).attr("type") == "checkbox") {
				if(($(this).attr("checked") == false)&&($(this).attr("obl") == "true")) {
					$(this).parent("td").find("label").css("color", "#b60000");
					$error = true;
				} else {
					$(this).parent("td").find("label").css("color", "#000");
				}
			}
		});
		if($("#epasts").val() != "") {
			$.get("http://veroni.lv/scripts/validator.php?mode=email&data="+$("#epasts").val(), function(data){
				if(data == "error") {
					$("#epasts").addClass("wrong");
					$error = true;
				} else {
					$("#epasts").removeClass("wrong");
				}
			});
		} else {
			$("#epasts").addClass("wrong");
			$error = true;
		}
		if($error == true) {
			$(".erroralert").find("div.alert").html("Aizpildiet visus iezīmētos laukus! E-pasta adresei ir jābūt reālai!");
			$(".erroralert").show();
			return false;
		} else if($error == false) {
			$(".erroralert").hide();
			$.ajax({
				url: "/createorder",
				type: "POST",
				data: ({
					op: "order_basket",
					mode: "ajax",
					vards: $("#vards").val(),
					telefons: $("#telefons").val(),
					epasts: $("#epasts").val(),
					zinojums: $("#zinojums").val(),
					apmaksas_veids: $('input[name=apmaksas_veids]:checked').val(),
					pk1: $("#pk1").val(),
					pk2: $("#pk2").val(),
					iela: $("#iela").val(),
					pilseta: $("#pilseta").val(),
					novads: $("#novads").val(),
					pastakods: $("#pastakods").val()
				}),
				dataType: "jsonp",
				success: function(data) {
					if(data.result == "ok"){
						document.location = "/grozs/pasutits#"+data.orderid+"-"+data.key;
					}
				}
			});
			return false;
		}
	});
	
	
	function centerThis(div) { var winH = $(window).height(); var winW = $(window).width(); var centerDiv = $('#' + div); centerDiv.css('top', winH/2-centerDiv.height()/2 + $(window).scrollTop()); centerDiv.css('left', winW/2-centerDiv.width()/2 + $(window).scrollLeft()); }
	function removevideopopup() { $("#pirktpopupwrap").fadeOut("slow").hide(); $("body").unbind("click"); }
	function loadpopupfunctions(){
		centerThis("pirktpopup");
		$("body").bind("click", function(e) { if($(e.target).closest("#pirktpopup").length == 0) { removevideopopup(); return false; } });
		$(window).bind('resize scroll', function() { centerThis("pirktpopup"); });	
	}
	$pirktpogas = $(".most_wanted").find(".btn_1");
	$pirktpogas.click(function(){
		$url = $(this).attr("href") + "?mode=ajax";
		$.get($url, function(data) {
			$("#pirktprecesnosaukums").text(data.prece);
			$("#precuskaits").text(data.skaits);
			$("#precusumma").text(data.summa);
		});
		$("#pirktpopupwrap").show();
		loadpopupfunctions();
		return false;
	});
	$pirktpogas2 = $(".buy").find(".btn_1");
	$pirktpogas2.click(function(){
		$url = $(this).attr("href") + "?mode=ajax";
		$.get($url, function(data) {
			$("#pirktprecesnosaukums").text(data.prece);
			$("#precuskaits").text(data.skaits);
			$("#precusumma").text(data.summa);
		});
		$("#pirktpopupwrap").show();
		loadpopupfunctions();
		return false;
	});
	$("#pirktpopupwrap").find(".btn_1").click(function(){
		$("#pirktpopupwrap").fadeOut("slow").hide();
		return false;
	});
	$("#pirktpopupwrap").find(".btn_2").click(function(){
		document.location = "http://veroni.lv/grozs";
		return false;
	});
	
	// kontaktforma
	$(".contact_form").find("#nosutit").click(function(){
		$tips = $('.contact_form input:radio:checked').val();
		$vards = $("#vards").val();
		$telefons = $("#telefons").val();
		$epasts = $("#epasts").val();
		$zinojums = $("#zinojums").val();
		if($zinojums.length > 0) {
			$("#zinojums").css("border-color", "#D0B46B");
			$.post("/submitcontactform", {op: "add_new_message", vards: $vards, telefons: $telefons, epasts: $epasts, zinojums: $zinojums, tips: $tips}, function(data){
				if(data.result == "ok") {
					$(".contact_form").parent(".c").html("<div class=\"basket\"><div class=\"basket_check\"><p>Paldies, ka sazinājāties ar mums!<br /> Jūsu ziņa nosūtīta apstrādei!</p><p>&nbsp;</p><a href=\"/\" class=\"btn_1\"><i></i><b>DOTIES UZ VEIKALU</b><u></u></a></div></div>");
				}
			}, "json");
		} else {
			$("#zinojums").css("border-color", "red");
		}
		return false;
	});
	
	$(".to_top").click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
	
	
});