/* Tabulky */
//plugin na zebra tabulky
jQuery.fn.zebraTable = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('table')) {
            $('tbody tr:even',self).removeClass('even').addClass('odd');
            $('tbody tr:odd',self).removeClass('odd').addClass('even');
        }
    });
    return this;
};
//plugin na highlight tabulky
jQuery.fn.highlightTable = function () {
    this.each(function () {
        var self = $(this);
        if (self.is('table')) {
            $('tbody tr',self).unbind('mouseover.highlight').bind('mouseover.highlight',function () {
                $('>td',this).addClass('highlight');
            });
            $('tbody tr',self).unbind('mouseout.highlight').bind('mouseout.highlight',function () {
                $('>td',this).removeClass('highlight');
            });
        }
    });
    return this;
};
jQuery.fn.ajaxloader = function (task) {
    if(task == 'remove'){
        $("div.ajax-loader",this).remove();
    }else{
        var al = $('<div class="ajax-loader"></div>');
        al.css({
            height: $(this).outerHeight(),
            width: $(this).outerWidth(),
            opacity: 0.7,
            top: 0,
            left: 0
        });
        $(this).append(al);
    }
};
$(function() {
    ajaxSearch();
    if(window.location.hash){
        var offset = window.location.hash.match(/offset=(\d+)/);
        if(offset[1]){
            getSearchPage(offset[1]);
        };
    }
    $('table.zebra').zebraTable();
    acRegions();
    ajaxForm();
    $('#job-search-tab').click(function () {
            if(!$(this).is('.active')){
                hideTabs(this);
                $('#job-search').addClass('active');
            }
    });
    $('#job-register-tab').click(function () {
            if(!$(this).is('.active')){
                hideTabs(this);
                $('#job-register').addClass('active');
            }
    });
    $('#job-order-tab').click(function () {
            if(!$(this).is('.active')){
                hideTabs(this);
                $('#job-order').addClass('active');
            }
    });
    $('#jobs div.job-register form').submit(function () {
           form = $(this);
           if($('input[name=type]',form).val() == 'order'){
               var dialog = $('<div class="dlg"></div>');
                dialog.html('<div class="report"><span class="ico ico_INFO">&nbsp;</span>Záväzná objednávka možná iba pre partnerov s platnou obchodnou zmluvou alebo partnerov, s ktorými sme spolupracovali v posledných 12 mesiacoch</div>');
                dialog.dialog({
                        title: 'Upozornenie',
                        modal: true,
                        position: 'center',
                        resizable: true,
                        buttons: { 
                            "Odoslať": function() { $(this).dialog("close"); sendForm(form)},
                            "Zrušiť": function() { $(this).dialog("close");}
                        }
                });
           }else{
               sendForm(form);
           }
           return false;
    });
});

function enableTableScripts() {
    $('table.highlight').highlightTable();
    $('#body table.jobs tr').css('cursor','pointer');
    $('#body table.jobs').click(function(e) {
        var target = e.target; // e.target je element, na ktory sa kliklo
        if (target.nodeName == 'TD') {
            window.location = $(target).parent('tr').find('a.more').attr('href');
        }
    });
}

function ajaxSearch() {
    $("span.next a, span.prev a","#job-search.tab").click(function (e) {e.preventDefault();
        jobsPager($(this).attr('href'));
        window.location.hash = '#offset='+$(this).attr('rel');
    });
    $("span.page select","#job-search.tab").removeAttr('onchange').change(function () {
        getSearchPage($(this).val());
    });
    enableTableScripts();
}
function getSearchPage(offset) {
    jobsPager(LANGROOT + '/?offset=' + offset);
    window.location.hash = '#offset='+offset;
}
function jobsPager(url) {
    $.ajax({
        url: url,
        beforeSend: function () { $('#job-search').ajaxloader();},
        complete: function () { $('#job-search').ajaxloader('remove');},
        success: function (data) {
            $('#job-search').html(data);
            ajaxSearch();
            $('table.zebra','#job-search').zebraTable();
        }
    });
}
function ajaxForm() {
    $('input.datepicker').each(function () {
       $(this).datepicker({
            minDate: new Date()
       });            
    });
}
function sendForm(form) {
    $.ajax({
        url: form.attr('action'),
        type: 'POST',
        data: form.serialize(),
        beforeSend: function () { form.closest('div.job-register').ajaxloader();},
        complete: function () { form.closest('div.job-register').ajaxloader('remove');},
        success: function (data) {
            $('table.form',form).html(data);
            $('div.reports','div.job-register').each(function () {
                var dialog = $('<div class="dlg"></div>');
                dialog.dialog({
                        title: 'Správa',
                        modal: true,
                        position: 'center',
                        resizable: true,
                        buttons: { "Ok": function() { $(this).dialog("close");}}
                });
                $(this).show().appendTo(dialog);
            })   
            ajaxForm();
        }
    });
}
function hideTabs(element) {
    $(element).closest('ul.tabs').find('li.active').removeClass('active');
    $(element).closest('div.tabs').find('div.tab').removeClass('active');
    $(element).removeClass('hover').addClass('active');
}
function acRegions(){
    $.getJSON(LANGROOT + '/jobs/get-regions',function (json) {
        $('#location').autocomplete({
            minLength: 0,
            source: json,
            select: function(e,ui) {
                $("#location").val(ui.item.label);
                $("#locationId").val(ui.item.value);
                return false;
            }
        });
    });
    $("#location").change(function () {
            if(!$(this).val()){
                $("#locationId").val('');
            }
    });
}


