
function initPage() {
	//nahradene jquery - prepisoval sa nam JS
}
$(document).ready(function () {
	showBackToTopIcon();
	cookieOnlyOne();
	$("#ajax_loading").bind("ajaxSend", function(){
         $(this).show();
   }).bind("ajaxComplete", function(){
         $(this).hide();
   });
});

function showBackToTopIcon() {
	var back_icon = $("#backToTop");
	var content = $("#wrapper");
	var nasobic = 1.5;
	
	if(!back_icon || !content)
		return false;
	
	if( (content.offsetHeight) > (nasobic * screen.height)) {
		$("#backToTop").css('display','block');
	} else {
		$("#backToTop").css('display','none');
	}
}

function checkDateForm( d, m, y ) {
	return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate();
}

function waitForLoading_begin() {
	$("#ajax_loading").show();
}
function waitForLoading_end() {
	$("#ajax_loading").hide();
}
function parseFloatToPrice(price) {
	//return parseFloat(price).toFixed(2).replace('.',',');
	return $().number_format(price, {
   numberOfDecimals:2,
   decimalSeparator: ',',
   thousandSeparator: ' '
	 });
}
function parsePriceToFloat(price) {
	return parseFloat(price.replace(',','.').replace(' ',''));
}
function cookieOnlyOne() {
	var class_cookie = "cookie-onlyone";
	$("."+class_cookie).each(function () {
		if($(this).attr('id')) {
			cookie_name = ""+class_cookie+$(this).attr('id');
		} else {
			cookie_name = class_cookie;
		}
		if($.cookie(cookie_name)) {
			$(this).css("display","none").css("visibility","hidden");
		} else {
			//$(this).show();//ked ju este nemame definovanu, tak zobrazime
			$.cookie(cookie_name,'true',{ expires: 1 });
		}
	});
}

function showDostupnostInPopup(link) {
	//vyskladame si linku a parametre do pola
	var path = '/ajax_dostupnost.php';
	var param = $.url(link).param();
	var ajax_call = null;
	if(!path || !param) {
		return false;
	}
	
	//zrusime stary dialog ak bol
	$("#ajax_dialog").dialog("destroy");
	
	//spravime novy dialog
	$("#ajax_dialog").dialog({
  	autoOpen:true,
		width: 820,
		height: 300,
		modal: true,
		closeOnEscape: true,
		open: function(event, ui) {
			var info = getLoadingHtml("Overujeme dostupnosť kapacít.");
			$("#ajax_dialog").html(info);
		},
		beforeClose: function(event, ui) {
			try {
				ajax_call.abort();
			} catch (e) {
				//return true;
			}
		},
		buttons: {
			'Zavrieť': function() {
				$(this).dialog('close');
			}
		}
	});	
	ajax_call = $.ajax({
		type: "GET",
	  url: path,
	  data: param,
	  success: function(data) {
	    $("#ajax_dialog").html(data);
	    refreshDostupnostPopup();
	  }
	});
	return true;
}
function refreshDostupnostPopup() {
	var table_width = $("#ajax_dialog .dostupnost-container").innerWidth();
	$("#ajax_dialog").dialog("option", "width", table_width+60 );
	var table_height = $("#ajax_dialog .dostupnost-container").innerHeight();	
	$("#ajax_dialog").dialog("option", "height", table_height+120 );
}

function getLoadingHtml(text) {
	var html = $("#loading_text").html();
	if(html.length > 0) {
		if(text.length > 0) {
			html = html.replace('Loading...',text);
		}
		return html;
	}
	return '';
}

