﻿//common
String.prototype.startsWith = function (t, i) {
    if (i == false) {
        return
        (t == this.substring(0, t.length));
    } else {
        return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
    } 
}

String.prototype.endsWith = function (t, i) {
    if (i == false) {
        return (t
== this.substring(this.length - t.length));
    } else {
        return
        (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
    } 
} 


//nav
$(document).ready(function () {
    $('#mainnavigation a.nopage').click(function () {
        var li = $(this).parent();
        var ul = li.find('ul:first');
        if (ul.is(':visible')) {
            ul.slideUp();
        }
        else {
            ul.slideDown();
        }
        return false;
    });
});

//links
$(document).ready(function () {
    $('a').each(function () {
        if ($(this).attr('href')) {
            if (($(this).attr('href').startsWith('http://')) && ($(this).attr('rel') != 'l'))
                $(this).attr('target', '_blank');
            else if ($(this).attr('href').indexOf('/share/?t=') >= 0)
                $(this).attr('target', '_blank');
        }
    });
});

//adresar search
var adresarSearchTimer;
$(document).ready(function () {
    $('#adresarForm #q').keyup(function () {
        if ($(this).val().length >= 3) {
            window.clearTimeout(adresarSearchTimer);
            adresarSearchTimer = window.setTimeout('adresarSearch()', 500);
            $('.typemore').fadeOut();
        } else {
            $('.typemore').fadeIn();
        }
    });

});
function adresarSearch() {
    $.getJSON('/onama/sindikati/', { q: $('#adresarForm #q').val() }, function (data) {
        if (data.ok == 1) {
            if ($('#rezultati').data('hash') != data.hash) {
                $('h2.naslov').hide();
                $('h2.naslovsearch').show();
                $('#rezultati').fadeOut(function() { $(this).html(data.html).fadeIn(); });
                $('#rezultati').data('hash', data.hash);
            }
        }
    });
}


//tables
$(document).ready(function () {
    $('.table2 .meta').each(function () {
        var t = $(this);
        if (t.text().startsWith('-')) {
            t.addClass(t.hasClass('negBetter') ? 'pos' : 'neg');
        }
        else if (t.text() != '') {
            t.addClass(t.hasClass('negBetter') ? 'neg' : 'pos');
            t.text('+' + t.text());
        }
    });

});

//period
$(document).ready(function () {
    $('.periodMjesecOd').change(function () {
        var mjesecOd = $('.periodMjesecOd').val();
        var mjesecDo = $('.periodMjesecDo').val();
        if (mjesecOd >= mjesecDo) {
            $('.periodMjesecDo').val(mjesecOd);
        }
    });

    $('.periodMjesecDo').change(function () {
        var mjesecOd = $('.periodMjesecOd').val();
        var mjesecDo = $('.periodMjesecDo').val();
        if (mjesecOd >= mjesecDo) {
            $('.periodMjesecOd').val(mjesecDo);
        }
    });

    $('.periodPokazi').click(function () {
        var url = location.href.split('?')[0];
        document.location = url + '?od=' + $('.periodMjesecOd').val() + '&do=' + $('.periodMjesecDo').val();
    });

});
