﻿/*global $, document, window*/

function formSelect() {
    $(".form .dropdown").sSelect();
}

function setFlexible() {
    if ($("#body").outerHeight() < $("#sidebar").outerHeight()) {
        if ($("#fixed").children().length > 0) {
            $("#flexible").height($("#sidebar").outerHeight() - $("#fixed").outerHeight() - 37);
            $("#page-products").height($("#sidebar").outerHeight() - $("#fixed").outerHeight() - 32);
        } else {
            $("#flexible").height($("#sidebar").outerHeight() - 32);
        }
    }
}

function openMenu() {
    var level = $(this).parent().parent().parent().attr("class");
    $("#products-menu ul." + level + " span.opened").removeClass("opened").parent().next().slideUp("slow");
    $(this).parent().next().slideDown("slow", setFlexible);
    $(this).addClass("opened");
    return false;
}

function openAccordion() {
    var current = $("#accordion .product-header span.opened");
    var next = $(this);
    current.removeClass("opened").parent().next().slideUp("normal", function () { next.addClass("opened").parent().next().slideDown("normal"); });
    return false;
}

/*detect IE.*/
function isIE() {
    if (navigator.appName.toLowerCase() == "microsoft internet explorer") { return true; } else { return false; }
}

/*allow number format e=event, n=negative, c=comma*/
function number(e, c, n) {
    e.stopPropagation();
    if (isIE) {
        if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode === 8) || (e.keyCode === 44 && c === true) || (e.keyCode === 109 && n === true)) { return true; } else { return false; }
    } else {
        if ((e.charCode >= 48 && e.charCode <= 57) || (e.charCode === 0) || (e.charCode === 44 && c === true) || (e.charCode === 45 && n === true)) { return true; } else { return false; }
    }
}

function searchSystemChange() {
    if ($(this).val() === "Mono sustav") {
        $(".overlay").show();
    } else {
        $(".overlay").hide();
    }
}

$(document).ready(function () {
    /*custom dropdown*/
    $("#searchbox .dropdown").sSelect();
    $(".filter-box .dropdown").sSelect();
    formSelect();
    /*labels*/
    $("label:not(:contains(':'))").append(":");
    /*cycle*/
    if ($("#footer .item").length > 4) { $("#footer .item:nth-child(4)").addClass("no-back"); $("#footerCycle").cycle({ fx: "uncover", timeout: 10000, speed: 3000 }); }
    if ($("#news .item").length > 2) { $("#newsCycle").cycle({ fx: "turnDown", timeout: 7000, speed: 2000 }); }
    /*products menu*/
    $("#products-menu span").click(openMenu);
    $("#products-menu ul").not(":has(li)").parent().children(0).children(0).remove();
    var current = $("#products-menu a[href='" + window.location.pathname + "']");
    var parent = current.parent().parent().prev();
    var root = parent.parent().parent().prev();
    root.children(0).click();
    parent.children(0).click();
    current.children(0).click();
    /*product accordion*/
    $("#accordion .product-header span").click(openAccordion);
    $("#accordion .product-header").click(function () { $(this).children().eq(0).click(); });
    /*download*/
    $(".download-section").not(":has(div)").parent().remove();
    /*scrollpane*/
    $(".scrollpane").jScrollPane({ showArrows: true, horizontalGutter: 30, verticalGutter: 30 });
    /*search*/
    $(".searchSystem").change(searchSystemChange);
});
