$(document).ready(function(){
	//$(".floating_columns .column").equalHeights();
	//$(".izracunstroskov").attr('contentEditable', true);
	width = $(".ajax_loader").width();
	height = $(".ajax_loader").height();
	$(".overlay").hide();
	$(".overlay").css({ opacity: 0.75 });
	$(".ajax_loader").hide();
	$(".ajax_loader").css({marginLeft:-width/2,marginTop:-height/2});
	
	$(".ajax_loader, .overlay").ajaxStart(function() {
		$(this).show();
	});
	$(".ajax_loader, .overlay").ajaxStop(function() {
		$(this).stop().hide();
	});
	
	$(".uredi").live("click", function(self){

		$(this).parent().parent().attr('contentEditable', true);
		$(this).parent().parent().css('border', 'dashed 1px red');
		
		
		$(this).parent().attr('contentEditable', false);
	
		$(this).parent().html("<a href='#' class='shrani'>Shrani</a>");
	});
	
		//shrani spremembe
		$(".shrani").live("click", function(){ 
		var a = {};
		$(this).parent().parent().children("td").each(function(i, l){
			a[i] = $(l).text();
		});
		
		
		$.ajax({
		  url: "../scripts/shrani.php",
		  data: a,
		  type: 'post',
		  success: function(data) {
			alert(data);
		  }
		});
		});
		
		//zbrisi vrstico
		$(".zbrisi").live("click", function(){ 
		var a = {};
		$(this).parent().parent().children("td").each(function(i, l){
			a[i] = $(l).text();
		});
		
		$.ajax({
		  url: "../scripts/zbrisi.php",
		  data: a,
		  type: 'post',
		  success: function(data) {
			alert(data);
		  }
		});
		izpis();
	});
	//shrani spremembo
	$(".shraniNalog").live("click",function(){
		var a={};
		a[0]=$("#datum").val();
		a[1]=$("#pot").val();
		a[2]=$("#km").val();
		a[3]=$("#serija").val();
		//alert(a[0]+" "+a[1]+" "+a[2]+" "+a[3]);
		$.ajax({
		  url: "../scripts/dodajNalog.php",
		  data: a,
		  type: 'post',
		  success: function(data) {
			//alert(data);
		  }
		});
		izpis();
		
	});
	/*$(".datepicker").datepicker({
		altField: "#alternate",
		altFormat: "DD, d MM, yy"
	});*/
	$(".datepicker").each(function(){
		//console.log(this);
		//$(this).after($alter = $(this).clone().attr("name",$(this).attr("name")+"-mr").attr("type",'hidden'));
		$(this).datepicker({
			altField: "#alternate",
			//altField: $alter[0],
			altFormat: "yy-mm-dd"
		});
	});
});
$(function(){
	$('.datum').live('click', function() {
		$(this).datepicker({showOn:'focus'}).focus();
		});
	});

function daysInMonth(iMonth, iYear) {		//source: http://snippets.dzone.com/posts/show/2099
	return 32 - new Date(iYear, iMonth, 32).getDate();
}
function roundNumber(number, digits) {
	var multiple = Math.pow(10, digits);
	var rndedNum = Math.round(number * multiple) / multiple;
	return rndedNum;
}

function timeDiff(start,stop,precission) {
	var diff =
		new Date( '01/01/2009 ' + stop ) -
		new Date( '01/01/2009 ' + start );

	diff_hours = roundNumber((diff/1000/60/60),1);
	//console.log(diff_hours);
	return diff_hours;
}

/* jQuery plugins*/
(function($) {
    $.fn.toggleDisabled = function(){
        return this.each(function(){
            this.disabled = !this.disabled;
        });
    };
})(jQuery);

function openForm(which) {
	$(which).show();
	return false;
}
function izpis(){
		var serija=document.getElementById("izpis").value;
		if (window.XMLHttpRequest){	
		xmlhttp=new XMLHttpRequest();
		}
		else{	
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				document.getElementById("data-preview").innerHTML=xmlhttp.responseText;
			}
		}
		xmlhttp.open("GET","../scripts/izpis.php?serija="+serija, false);
		xmlhttp.send();
}
