﻿/// <reference path="E:\Source\Intermex@WEB\Intermex.Web\Scripts\jquery-1.4.1-vsdoc.js"/>
var latinica;


function initExternalLinks() {
    $("#menu ul").hide();
    $("#menu li a").click(function() {
        $(this).next().slideToggle("normal");
    });
}

function LoadURL(url) {
    window.open(url, "_blank", "", "");
}

function AddBookmark() {
    var BookmarkURL = 'http://www.propisionline.com'
    var BookmarkTitle = 'Propisi Online'
    // If the browser is Internet Explorer
    if (document.all) {
        // Add to Favorites (Internet Explorer)
        window.external.AddFavorite(BookmarkURL, BookmarkTitle);
    }
    else {
        // Add to Bookmarks (Mozilla Firefox)
        window.sidebar.addPanel(BookmarkTitle, BookmarkURL, '');
    }
}


$(document).ready(function() {
    initExternalLinks();
});



jQuery.fn.Cirylic = function() {
    for (node = 0; node < this.length; node++) {
        var latin = this[node].data;

        if (latin != undefined) {
            latin = latin.replace(/nj/g, 'њ');
            latin = latin.replace(/dž/g, 'џ');
            latin = latin.replace(/lj/g, 'љ');

            latin = latin.replace(/DŽ/g, 'Џ');
            latin = latin.replace(/LJ/g, 'Љ');
            latin = latin.replace(/NJ/g, 'Њ');
            latin = latin.replace(/Dž/g, 'Џ');
            latin = latin.replace(/Lj/g, 'Љ');
            latin = latin.replace(/Nj/g, 'Њ');

            latin = latin.replace(/a/g, 'а');
            latin = latin.replace(/b/g, 'б');
            latin = latin.replace(/v/g, 'в');
            latin = latin.replace(/g/g, 'г');
            latin = latin.replace(/d/g, 'д');
            latin = latin.replace(/đ/g, 'ђ');
            latin = latin.replace(/e/g, 'е');
            latin = latin.replace(/ž/g, 'ж');
            latin = latin.replace(/z/g, 'з');
            latin = latin.replace(/i/g, 'и');
            latin = latin.replace(/j/g, 'ј');
            latin = latin.replace(/k/g, 'к');
            latin = latin.replace(/l/g, 'л');
            latin = latin.replace(/m/g, 'м');
            latin = latin.replace(/n/g, 'н');
            latin = latin.replace(/o/g, 'о');
            latin = latin.replace(/p/g, 'п');
            latin = latin.replace(/r/g, 'р');
            latin = latin.replace(/s/g, 'с');
            latin = latin.replace(/t/g, 'т');
            latin = latin.replace(/ć/g, 'ћ');
            latin = latin.replace(/u/g, 'у');
            latin = latin.replace(/f/g, 'ф');
            latin = latin.replace(/h/g, 'х');
            latin = latin.replace(/c/g, 'ц');
            latin = latin.replace(/č/g, 'ч');
            latin = latin.replace(/š/g, 'ш');

            latin = latin.replace(/A/g, 'А');
            latin = latin.replace(/B/g, 'Б');
            latin = latin.replace(/V/g, 'В');
            latin = latin.replace(/G/g, 'Г');
            latin = latin.replace(/D/g, 'Д');
            latin = latin.replace(/Đ/g, 'Ђ');
            latin = latin.replace(/E/g, 'Е');
            latin = latin.replace(/Ž/g, 'Ж');
            latin = latin.replace(/Z/g, 'З');
            latin = latin.replace(/I/g, 'И');
            latin = latin.replace(/J/g, 'Ј');
            latin = latin.replace(/K/g, 'К');
            latin = latin.replace(/L/g, 'Л');
            latin = latin.replace(/M/g, 'М');
            latin = latin.replace(/N/g, 'Н');
            latin = latin.replace(/O/g, 'О');
            latin = latin.replace(/P/g, 'П');
            latin = latin.replace(/R/g, 'Р');
            latin = latin.replace(/S/g, 'С');
            latin = latin.replace(/T/g, 'Т');
            latin = latin.replace(/Ć/g, 'Ћ');
            latin = latin.replace(/U/g, 'У');
            latin = latin.replace(/F/g, 'Ф');
            latin = latin.replace(/H/g, 'Х');
            latin = latin.replace(/C/g, 'Ц');
            latin = latin.replace(/Č/g, 'Ч');
            latin = latin.replace(/Š/g, 'Ш');

            this[node].data = latin;
        }
    }
}

jQuery.fn.textNodes = function() {
    var ret = [];

    (function(el) {
        if (!el) return;
        if ((el.nodeType == 3) || (el.nodeName == "BR"))
            ret.push(el);
        else
            for (var i = 0; i < el.childNodes.length; ++i)
            arguments.callee(el.childNodes[i]);
    })(this[0]);
    return $(ret);
}


function ToCirylic(elem) {
    if ($('#Pismo').text() == 'Ćirilica') {
        latinica = $(elem).html();    
        $(elem).textNodes().Cirylic();
        $('#Pismo').text('Latinica');
    }
    else {
        //window.location.reload(false);
        $(elem).html(latinica);    
        $('#Pismo').text('Ćirilica');
    }
    };


function IndOkShowAdvancedSearch() {
    $("#Search").hide();
    $("#Application").hide();
    $("#AdvancedSearch").show();
    $.cookie("searchtype", "advanced");
}

function IndOkShowStandardSearch() {
    $("#AdvancedSearch").hide();
    $("#Application").show();
    $("#Search").show();
    $.cookie("searchtype", "standard");
}


function UpdateIndexList() {
    var book = $.cookie("book");
    $.ajax(
{
    type: "GET",
    contentType: "application/json; charset=utf=8",
    url: "/Practice/GetBookIndex/" + book,
    data: "",
    dataType: "json",
    success: function(data) {
        if (data.length > 0) {
            var options = '';
            for (s in data) {
                var state = data[s];
                options += "<option value='" + state.Value + "'>" + state.Text + "</option>";
            }
            $("#index").html(options);
            var index = $.cookie("index");
            if (index == null)
                index = 0;
            $("#index").val(index);

        }
    }
});
};

function PracticeSetup() {

    var book = $.cookie("book");
    if (book != null)
        UpdateIndexList();

    $("select#book").change(function() {
        $.cookie("book", $(this).attr("value"));
        $.cookie("index", null);
        UpdateIndexList();
    });

    $("select#index").change(function() {
        $.cookie("index", $(this).attr("value"));
    });

};

function UpdateSectionGroupList() {
    var group = $.cookie("section");
    $.ajax(
{
    type: "GET",
    contentType: "application/json; charset=utf=8",
    url: "/IndOk/GetSectionList/?group=" + group,
    data: "",
    dataType: "json",
    success: function(data) {
        if (data.length > 0) {
            var options = '';
            for (s in data) {
                var state = data[s];
                options += "<option value='" + state.Value + "'>" + state.Text + "</option>";
            }
            $("#sectionGroup").html(options);
            var sectionGroup = $.cookie("sectionGroup");
            if (sectionGroup == null)
                sectionGroup = 0;
            $("#sectionGroup").val(sectionGroup);
        }
    }
});
};

function UpdateSectionSubGroupList() {
    var group = $.cookie("sectionGroup");
    $.ajax(
{
    type: "GET",
    contentType: "application/json; charset=utf=8",
    url: "/IndOk/GetSectionList/?group=" + group,
    data: "",
    dataType: "json",
    success: function(data) {
        if (data.length > 0) {
            var options = '';
            for (s in data) {
                var state = data[s];
                options += "<option value='" + state.Value + "'>" + state.Text + "</option>";
            }
            $("#sectionSubGroup").html(options);
            var sectionSubGroup = $.cookie("sectionSubGroup");
            if (sectionSubGroup == null)
                sectionSubGroup = 0;
            $("#sectionSubGroup").val(sectionSubGroup);

        }
    }
});
};


function UpdateYearList() {
    var journal = $.cookie("journal");
    $.ajax(
{
    type: "GET",
    contentType: "application/json; charset=utf=8",
    url: "/IndOk/GetYearList/?journal=" + journal,
    data: "",
    dataType: "json",
    success: function(data) {
        if (data.length > 0) {
            var options = '';
            for (s in data) {
                var state = data[s];
                options += "<option value='" + state.Value + "'>" + state.Text + "</option>";
            }
            $("#year").html(options);
            var year = $.cookie("year");
            if (year == null)
                year = 0;
            $("#year").val(year);
        }
    }
});
};

function UpdateNumberList() {
    var journal = $.cookie("journal");
    var year = $.cookie("year");
    $.ajax(
{
    type: "GET",
    contentType: "application/json; charset=utf=8",
    url: "/IndOk/GetNumberList/?journal=" + journal + "&year=" + year,
    data: "",
    dataType: "json",
    success: function(data) {
        if (data.length > 0) {
            var options = '';
            for (s in data) {
                var state = data[s];
                options += "<option value='" + state.Value + "'>" + state.Text + "</option>";
            }
            $("#number").html(options);
            var number = $.cookie("number");
            if (number == null)
                number = 0;
            $("#number").val(number);

        }
    }
});
};

function IndOkAdvancedSearchSetup() {
    var searchtype = $.cookie("searchtype");
    if ((searchtype == null) | (searchtype == "standard"))
        IndOkShowStandardSearch();
    if (searchtype == "advanced")
        IndOkShowAdvancedSearch();

    var opcija = $.cookie("option");
    if (opcija == "Keyword") {
        $("#Sections").hide();
        $("#Keywords").show();
        $('input[name="option"]')[0].checked = true;
    }
    else if (opcija == "Section") {
        $("#Keywords").hide();
        $("#Sections").show();
        $('input[name="option"]')[1].checked = true;
    };


    $("#rbKeyword").click(function() {
        var opcija = $(this).attr("value");
        $.cookie("option", opcija);
        if (opcija == "Keyword") {
            $("#Sections").hide();
            $("#Keywords").show();
        }
        else if (opcija == "Section") {
            $("#Keywords").hide();
            $("#Sections").show();
        };
    });


    $("#rbSection").click(function() {
        var opcija = $(this).attr("value");
        $.cookie("option", opcija);
        if (opcija == "Keyword") {
            $("#Sections").hide();
            $("#Keywords").show();
        }
        else if (opcija == "Section") {
            $("#Keywords").hide();
            $("#Sections").show();
        };
    });

    var journal = $.cookie("journal");
    if (journal != null)
        UpdateYearList();
    var year = $.cookie("year");
    if (year != null)
        UpdateNumberList();

    $("select#journal").change(function() {
        $.cookie("journal", $(this).attr("value"));
        $.cookie("year", null);
        $.cookie("number", null);
        $("#year").val(0);
        $("#number").val(0);
        UpdateYearList();
        UpdateNumberList();
    });

    $("select#year").change(function() {
        $.cookie("year", $(this).attr("value"));
        $.cookie("number", null);
        $("#number").val(0);
        UpdateNumberList();
    });

    $("select#number").change(function() {
        $.cookie("number", $(this).attr("value"));
    });


    var section = $.cookie("section");
    if (section != null)
        UpdateSectionGroupList();
    var sectionSubGroup = $.cookie("sectionSubGroup");
    if (sectionSubGroup != null)
        UpdateSectionSubGroupList();


    $("select#section").change(function() {
        if ($("#section > option:selected").attr("value") == 0) {
            $("#sectionGroup").html("<option value='0'>Sve</option>");
            $("#sectionSubGroup").html("<option value='0'>Sve</option>");
            $.cookie("section", null);
            $.cookie("sectionGroup", null);
            $.cookie("sectionSubGroup", null);
        }
        else {
            $.cookie("section", $(this).attr("value"));
            $.cookie("sectionGroup", null);
            $.cookie("sectionSubGroup", null);
            $("#sectionGroup").val(0);
            $("#sectionSubGroup").val(0);
            UpdateSectionGroupList();
            UpdateSectionSubGroupList();
        }
    });

    $("select#sectionGroup").change(function() {
        if ($("#sectionGroup > option:selected").attr("value") == 0) {
            $.cookie("sectionGroup", null);
            $.cookie("sectionSubGroup", null);
            $("#sectionSubGroup").html("<option value='0'>Sve</option>");
        }
        else {
            $.cookie("sectionGroup", $(this).attr("value"));
            $.cookie("sectionSubGroup", null);
            $("#sectionSubGroup").val(0);
            UpdateSectionSubGroupList();
        }
    });

    $("select#sectionSubGroup").change(function() {
        $.cookie("sectionSubGroup", $(this).attr("value"));
    });

};




(function($) {
    var printAreaCount = 0;

    $.fn.printArea = function() {
        var ele = $(this);

        var idPrefix = "printArea_";

        removePrintArea(idPrefix + printAreaCount);

        printAreaCount++;

        var iframeId = idPrefix + printAreaCount;
        var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';

        iframe = document.createElement('IFRAME');

        $(iframe).attr({ style: iframeStyle,
            id: iframeId
        });

        document.body.appendChild(iframe);

        var doc = iframe.contentWindow.document;

        $(document).find("link")
                .filter(function() {
                    return $(this).attr("rel").toLowerCase() == "stylesheet";
                })
                .each(function() {
                    doc.write('<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >');
                });

        doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html() + '</div>');
        doc.close();

        var frameWindow = iframe.contentWindow;
        frameWindow.close();
        frameWindow.focus();
        frameWindow.print();
    }

    var removePrintArea = function(id) {
        $("iframe#" + id).remove();
    };

})(jQuery);



///////////////////////////////////////
// Stapanje propisa
//
function LegislationPrint() {
    $('div#Legislation').printArea();
    return false;
}


///////////////////////////////////////
// Stapanje Ugovora
//
function ContractPrint() {
    $('div#Contract').printArea();
    return false;
}


///////////////////////////////////////
// Stapanje Misljenja
//
function OpinionPrint() {
    $('div#Opinion').printArea();
    return false;
}

///////////////////////////////////////
// Stapanje Misljenja
//
function DecisionPrint() {
    $('div#Decision').printArea();
    return false;
}
