// response msgs
function response_msg(el, status, msg){
	if($(".response_msg").length > 0){
		$(".response_msg").empty().html(unescape(msg));
	} else {
		$(el).prepend('<div class="response_msg">' + unescape(msg) + '</div>');
	}
}
function msg(status, msg){
	$("#obvestilo").stop(clearQueue, gotoEnd).empty().addClass(status)
		.html(unescape(msg))
		.show(trajanje_animacije)
		.animate({opacity: 1.0}, fadeout_time)
		.fadeOut(trajanje_animacije);
}
$(document).ready(function() {
	$("#top_banner").cycle({
		fx: 'fade',
		speed:  2500
	});
	$("#tekst a").filter(function() {
	    return this.hostname && this.hostname !== location.hostname;
	}).after(" <img src=\"/images/icons/external.png\" alt=\"external link\" class=\"ikonca_external\">");
	$("#tekst a").each(function(){
		var link = $(this);
		if(!link.hasClass("ikonca_dl")){
			if (this.hostname && this.hostname == location.hostname) {
				var file = link.attr("href");
				var ext = file.substr(file.lastIndexOf(".")+1).toLowerCase();
				if(/^(?:pdf|xls|doc)$/.test(ext))
					link.before("<img src=\"/images/icons/" + ext + ".png\" widht=\"16\" height=\"16\" alt=\"ikonca\" class=\"ikonca_dl\"/> ") 
			}
		}
	});
	/*$(window).scroll(function(){ 
		(window.pageYOffset > 300) ? $(".top").stop(clearQueue, gotoEnd).fadeIn("slow") : $(".top").stop(clearQueue, gotoEnd).fadeOut("slow");
	});*/
	$("#contact").ajaxForm({
		dataType: "json",
		success: function(res){
			response_msg("#contact", res.status, res.msg);
			if(res.status == "success"){
				$("#contact fieldset").remove();
			}
			if(res.mark){;
				$.each(res.mark, function(){
					$("label[for=" + this + "],#" + this).addClass("missing");
				});
				setTimeout(function(){
					$("label").removeClass("missing");
				},5000);
			}
		}
	});
	$("#newsletter").ajaxForm({
		dataType: "json",
		success: function(res){
			response_msg("#newsletter", res.status, res.msg);
			if(res.status == "success"){
				$("#newsletter :input").attr("disabled", true);
			}
			if(res.mark){;
				$.each(res.mark, function(){
					$("#" + this).addClass("missing");
				});
			}
		}
	});
	$(".add_item").click(function(){
		var del = $(this).attr("id").split("-");
		$.ajax({
			type:"POST",
			url:"/script/shop.php?action=add_item",
			data:{"nsl": del[0], "product_id": del[1]},
			dataType:"json",
			success: function(res){
				msg(res.status, res.msg);
				if(res.status == "success"){
					$("#shopping_cart a span").html(res.count);
				}
			}
		});
	});
	$("#update_cart").ajaxForm({
		dataType: "json",
		success: function(res){
			if(res){
				location.reload();
			}
			if(res.mark){
				$.each(res.mark, function(){
					$("label[for=" + this + "],#" + this).addClass("missing");
				});
				setTimeout(function(){
					$("label").removeClass("missing");
				},5000);
				$(".msg").empty().html("Prosim izpolnite manjkajoča polja.");
			}
		}
	});
	$("a.remove_item").click(function(){
		var del = $(this).attr("rel").split(",");
		$.ajax({
			type:"POST",
			url:"/script/shop.php?action=remove_item",
			data:{"product_id": del[0], "nsl": del[1]},
			dataType:"json",
			success: function(){
				location.reload();
			}
		});
	});
	$(".show_user_data").click(function(){
		$("#user_data").slideDown("slow");
	});
	$("#user_data").ajaxForm({
		dataType: "json",
		success: function(res){
			response_msg("#user_data", res.status, res.msg);
			if (res.status == "success") {
				$("form #user_data :input, form #user_data :textarea, form #user_data :select").attr("disabled", true).val("");
				$("form #user_data").remove();
				
				$("form #update_cart :input").attr("disabled", true).val("");
				$("form #update_cart").remove();
				$("#shopping_cart a span").html("0");
				$("#update_cart, #user_data fieldset").remove();
			}
			if(res.mark){
				$.each(res.mark, function(){
					$("label[for=" + this + "],#" + this).addClass("missing");
				});
				setTimeout(function(){
					$("label").removeClass("missing");
				},5000);
				
			}
		}
	});
	$(":input").keyup(function(){
		var polje = $(this);
		if(polje.val().length > 1)
			polje.removeClass("missing");
	});
	$('#calcat').cycle({
		fx: 'scrollLeft',
		containerResize: 1,
		height:       'auto'
	});
	/* Feratel */
	function setArrivalDate(value, type){
                 var currDay = window.document.frmQuickFind.SrchFromDay.value;
                 var currMonth = window.document.frmQuickFind.SrchFromMonth.value;
                 var currYear = window.document.frmQuickFind.SrchFromYear.value;

                 if(currDay.length==1){
                         currDay = '0'+currDay;
                 }
                 if(currMonth.length==1){
                         currMonth = '0'+currMonth;
                 }
                 if((type==1 || type==2) && value.length==1){
                         value = '0'+value;
                 }

                 if(type==0) { //called if date does not change or on automatic date reset
                         currDateString = new String(currYear+''+currMonth+''+currDay);
                         window.document.frmQuickFind.qfdArrivalDate.value = currDateString;
                 }
                 else{
                         if(type==1) { //day to change
                                 currDateString = new String(currYear+''+currMonth+''+value);
                         }
                         if(type==2) {
                                 currDateString = new String(currYear+''+value+''+currDay);
                         }
                         if(type==3) {
                                 currDateString = new String(value+''+currMonth+''+currDay);
                         }

                         var searchDate = new Date(currYear, currMonth-1, currDay);
                         var check = checkSearchDate(searchDate);
                         if(check == false){
                                 setCurrentDay();
                         }
                         else{
                                 window.document.frmQuickFind.qfdArrivalDate.value = currDateString;
                         }
                 }
                 //alert(window.document.frmQuickFind.qfdArrivalDate.value);
         }
         function checkSearchDate(searchDate) {
                  var currDate = new Date();

                  if(searchDate.getYear() < currDate.getYear()){
                         alert("Leto iskanja < aktualno leto! Datum nespremenjen...");
                         return false;
                  }
                  if(searchDate.getYear() == currDate.getYear() && searchDate.getMonth() < currDate.getMonth()){
                         alert("Mesec iskanja < aktualen mesec! Datum nespremenjen...");
                         return false;
                  }
                  if(searchDate.getYear() == currDate.getYear() && searchDate.getMonth() == currDate.getMonth() &&
                         searchDate.getDate() < currDate.getDate()){
                         alert("Dan iskanja < aktualen dan! Datum nespremenjen...");
                         return false;
                  }

                  //alert("Datum iskanja OK");
                  return true;
         }
		function validateForm(form) {
                  var d = parseInt( form.SrchFromDay.value )
                  var m = parseInt( form.SrchFromMonth.value )
                  var j = parseInt( form.SrchFromYear.value )

                  if ( !checkDate(d,m,j) ) {
                  alert("Date incorrect!") ;
                  form.SrchFromDay.focus();
                  return false;
                  }
                  }

                  function checkDate( d, m, j ) {
                  var dt = new Date( j, m-1, d )
                  if ( dt.getDate() != d || dt.getMonth()+1 != m ) return false;
                  return true;
                  }

                  function setCurrentDay() {
                  //
                  // Set Quickfinder date to current day
                  //
                  var today = new Date();
                  var today_day = today.getDate();
                  var today_month = today.getMonth();
                  var today_year = today.getFullYear();

                  var currDateString = new String(today_year+''+(today_month+1)+''+today_day);

                  window.document.frmQuickFind.qfdArrivalDate.value = currDateString;
                  window.document.frmQuickFind.SrchFromDay.options[today_day-1].selected = true;
                  window.document.frmQuickFind.SrchFromMonth.options[today_month].selected = true;
                  window.document.frmQuickFind.SrchFromYear.options[0].value = today_year;
                  window.document.frmQuickFind.SrchFromYear.options[0].text = today_year;
                  setArrivalDate(0,0);
                  }

                  setCurrentDay();
});
