// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

jQuery(document).ready(function() {
  setDatePickers();
  changeTaxable();
  setHoovers();
  setAutoCompleters();
  setAddItem();
  setTimeout("$('#news-box').fadeIn(1000)", 1000);
});


function setAddItem() {
  jQuery('#add_item').click(function() {jQuery.get("/items/add", function(html) {
      jQuery('table#items').append(html)});
      return false;});
}

function setHoovers() {
  jQuery('tr.to-hilite').mouseover(function () {$(this).addClass('hilite')});
  jQuery('tr.to-hilite').mouseout(function () {$(this).removeClass('hilite')});
  jQuery('tr.to-hilite').click(function () {$(this).toggleClass('perm-hilite')});

}

function setDatePickers() {
    $.datepicker.regional['cs'] = {
      closeText: 'Zavřít',
      prevText: '&#x3c;Dříve',
      nextText: 'Později&#x3e;',
      currentText: 'Nyní',
      monthNames: ['leden','únor','březen','duben','květen','červen',
        'červenec','srpen','září','říjen','listopad','prosinec'],
      monthNamesShort: ['led','úno','bře','dub','kvě','čer',
       'čvc','srp','zář','říj','lis','pro'],
      dayNames: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek', 'sobota'],
      dayNamesShort: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
      dayNamesMin: ['ne','po','út','st','čt','pá','so'],
      firstDay: 1,
      isRTL: false};
  $.datepicker.setDefaults($.datepicker.regional['cs']);
  jQuery('.datepicker').datepicker({ dateFormat: "yy-mm-dd", changeYear: false, showOn: 'both', buttonImage: '/images/calendar.gif', buttonImageOnly: true });
}

function setAutoCompleters() {
  var reg = /^.* \[(\d+)\]/;
  jQuery("#invoice_company_name").suggest("/companies/suggest",{
    onSelect: function () {
                if (this.value != '') {
                  var value = this.value.match(reg)[1];
                  jQuery('#invoice_company_id').val(value);}
              }
    });
}



function changeTaxable() {
  jQuery('#invoice_printed_on').change(function() {
    var printedOn = jQuery("#invoice_printed_on").val();
    jQuery("#invoice_taxable_payment_on").val(Date.parse(printedOn).toString('yyyy-MM-dd'))
    jQuery("#invoice_collectable_on").val(Date.parse(printedOn).add(14).days().toString('yyyy-MM-dd'));
  })
 
}

function pad(x) { return (x<0||x>9?"":"0") + x; }

$(function(){

    // initializing jQuery-tools Tooltip
    $("#tooltipForm").tooltip({
        // place tooltip on the right edge 
        position: ['top', 'center'], 
        // a little tweaking of the position 
        offset: [-2, 10], 
        // custom opacity setting 
        opacity: 0.7
    });
    $(".tooltipTrigger").tooltip({
        // place tooltip on the right edge 
        position: ['bottom', 'center'], 
        // a little tweaking of the position 
        offset: [12, 10], 
        // custom opacity setting 
        opacity: 0.8
    });
    
	// Accordion
	$("#accordion").accordion({ header: "h3" });

	// Tabs
	$('#tabs').tabs();


	// Dialog			
	$('#dialog').dialog({
		autoOpen: false,
		width: 600,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	
	// Dialog Link
	$('#dialog_link').click(function(){
		$('#dialog').dialog('open');
		return false;
	});

	// Slider
	$('#slider').slider({
		range: true,
		values: [17, 67]
	});
	
	// Progressbar
	$("#progressbar").progressbar({
		value: 20 
	});
	
	//hover states on the static widgets
	$('.fg-button, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
				
});
