jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// top & bottom menu //
function mainmenu(menuId, selectedClass) {
    $("#"+menuId+" ul").css({display: "none"}); // Opera Fix //
    $("#"+menuId+" li").hover(function() {
            $(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(300);
            $(this).find("a:first").addClass(selectedClass); // IE6 fix //
        }, function() {
            $(this).find('ul:first').css({visibility: "hidden"});
            $(this).find("a:first").removeClass(selectedClass); // IE6 fix //
        }
    );
}

// category hover IE fix //
function categFade() {
    $("ul.categ li").hover(function() {
        $(this).addClass('categHover', 1000);
    }, function() {
        $(this).removeClass('categHover', 1500);
    });
}

// articles / recipes listing fix //
function listings() {
    var el     = $(".article_excerpt");
    if(el.length > 0) {
        if(el.length == 1) {
            $(".article_excerpt:first").addClass('first');
        } else {
            $(".article_excerpt:first").addClass('first');
            $(".article_excerpt:last").addClass('last');
        }
    }
    if(el.length < 100) {
        $(".pagination").css({display: "none"});
    }
}
function banners() {
    $("a.banner").click(function() {
        var id  = $(this).attr('rel');
        var href = $(this).attr('title');
        if(id > 0) {
            $.ajax({
                type: "POST",
                url: "ajaxReq.php?type=bannerClick",
                data: "id="+id,
                success: function() {
                    window.open(href);
                    return false;
                }
            });
        }        
        return false;
    });
}
/*
function voteUp(id) {
    $.ajax({
        type: "POST",
        url: "ajaxReq.php?type=vote",
        data: "helpfull=yes&id="+id,
        success: function() {
            $.cookie('voted'+id, 'yes', {expires:30});
        }
    });
    return false;
}
function voteDown(id) {
    $.ajax({
        type: "POST",
        url: "ajaxReq.php?type=vote",
        data: "helpfull=no&id="+id,
        success: function() {
            $.cookie('voted'+id, 'no', {expires:30});
        }
    });
    return false;
}*/
function toTop() {
    if($(".article").height() > $(window).height()) {
        $(".toTop").css('display','inline');
        $(".toTop").click(function() {
            $('html, body').animate({scrollTop:0}, 'slow');
            return false;
        });
    }
}
function checkNotice() {
    if($.cookie('ieNotice') == 'closed') {
        $("#ieNotice").css('display','none');
    }
}
$("#jsNotice").ready(function() {
    $("#jsNotice").css('display','none');
});
$("#ieNotice").ready(function() {
    checkNotice();
    $("#closeMessage").click(function() {
        $.cookie('ieNotice', 'closed', { expires: 7 });
        $("#ieNotice").fadeOut('slow').css('display','none');
        return false;
    });
});
$(document).ready(function() {
    $("#subnav_artikli").append('<div id="subnav_artikli_bottom"></div>');
    $("#subnav_recepti").append('<div id="subnav_recepti_bottom"></div>');
    $("#subnav_artikli li ul").append('<div id="subsub_bottom"></div>');
    $("#subnav_artikli1").append('<div id="subnav_artikli_bottom1"></div>');
    $("#subnav_recepti1").append('<div id="subnav_recepti_bottom1"></div>');
    $("#subnav_artikli1 li ul").append('<div id="subsub_bottom1"></div>');
	mainmenu('main_nav', 'active');
    mainmenu('foot_nav', 'active');
    categFade();
    listings();
    $.preloadCssImages();
    toTop();
    banners();
    $(".vote").css('display','block');
    if($.cookie('voted'+$(".vote a:first").attr('href')) == 'yes' || $.cookie('voted'+$(".vote a:first").attr('href')) == 'no') {
        $("a.plus").addClass('plusDisabled').attr('title', 'Vać ste glasali na ovom artiklu. Hvala');
        $("a.minus").addClass('minusDisabled').attr('title', 'Vać ste glasali na ovom artiklu. Hvala');
    }
    var click = false;
    $(".vote a").click(function() {
        if($.cookie('voted'+$(this).attr('href')) != 'yes' && $.cookie('voted'+$(this).attr('href')) != 'no' && !click) {
            click = true;
            var clicked = this;
            $.ajax({
                type: "POST",
                url: "/ajaxReq.php?type=vote",
                data: "helpfull="+$(clicked).attr('rel')+"&id="+$(clicked).attr('href'),
                success: function() {
                    $.cookie('voted'+$(clicked).attr('href'), $(clicked).attr('rel'), {expires:30});
                    $("a.plus").addClass('plusDisabled').attr('title', 'Vać ste glasali na ovom artiklu. Hvala');
                    $("a.minus").addClass('minusDisabled').attr('title', 'Vać ste glasali na ovom artiklu. Hvala');
                    if($(clicked).attr('rel') == 'yes') {
                        var no = $(".voteResGreen span").html();
                        $(".voteResGreen span").html(++no);
                    } else {
                        var no = $(".voteResRed span").html();
                        $(".voteResRed span").html(++no);
                    }
                }
            });
        }
        return false;
    });
});