$(document).ready(function() {
	if ($.browser.webkit) {
		$("body").addClass("webkit");
	} else if ($.browser.mozilla) {
		$("body").addClass("mozilla");
	} else if ($.browser.opera) {
		$("body").addClass("opera");
	} else if ($.browser.msie) {
		$("body").addClass("ie");
	}
	
	$("a.ajax, a.datagrid-ajax").live("click", function (event) {
	    event.preventDefault();
	    $.get(this.href);
	});

	$("form.ajax, form.datagrid").live("submit", function(e) {
		$(this).ajaxSubmit();
		return false;
    });
	
	$("form.ajax :submit, form.datagrid :submit").live("click", function(e) {
		$(this).ajaxSubmit();
		return false;
    });
		
	$("input.datepicker").livequery(function() {
		$(this).datepicker({duration: 'fast'});
	});

	$("#widgetSentiment a.vote").live('click', function(e) {
		var type = $("#widgetSentiment li.selected a").attr("type");
		debug($.cookie("vote-" + type));
		if ($.cookie("vote-" + type)) {
			e.preventDefault();
			alert('Dnes ste už hlasovali!');
		}
	});
	
	$("div.calendar select").live('change', function(e) {
		e.preventDefault();
		var link = $(this).parents('div.calendar').attr('link');
		var year = $(this).parents('div.calendar').find("select[name='year'] :selected").val();
		var month = $(this).parents('div.calendar').find("select[name='month'] :selected").val();
		
		link = link.replace('_YEAR_', parseInt(year)).replace('_MONTH_', parseInt(month));
	    $.get(link);
	});
	
    $('a[href*=#]').bind("click", function(e) {
        e.preventDefault();
        var target = $(this).attr("href");
        $('html,body').animate({ scrollTop: $(target).offset().top }, 500, function() {
        	location.hash = target;
        });

    });
});


function debug(field) {
	if (window.console && window.console.log) {
		window.console.log(field);
	}
}

String.prototype.numberFormat = function(decimals, dec_point, thousands_sep) {
    var n = this, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
 
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
String.prototype.toFloatNumber = function() {
	var n = this;
    return parseFloat(n.replace(' ', '').replace(',', '.'));
}
Number.prototype.toStringNumber = function() {
	var n = new String(this);
    return n.replace('.', ',');
}
