﻿    $.fn.exlusiveCheckboxes = function () {
        var chks = $(this).find("input:checkbox").andSelf().filter("input:checkbox");
        chks.change(function () {
            if (this.checked) {
                for (var i = 0; i < chks.length; i++) {
                    var c = chks.get(i);
                    if (c != this) c.checked = false;
                }
            }
        });
    };

var highlightedRow;
function Highlight(tr) {
    if (highlightedRow != null)
        highlightedRow.css("background-color", "transparent");

    highlightedRow = tr;
    tr.css("background-color", "#eee");
}

function mergeMergableColumns() {
    $("table:has(td.x-col-merge)").each(function (ix, table) {
        table = $(table);
        var currentContent = "";
        var mergeIndex = 0;
        // group them
        table.find(".x-col-merge").each(function (ix, row) {
            row = $(row);
            var content = row.html();
            if (content != currentContent) {
                currentContent = content;
                mergeIndex++;
            }
            row.attr("x-merge-index", mergeIndex);
        });

        // merge them
        for (var i = 0; i <= mergeIndex; i++) {
            var groupElemenets = table.find("[x-merge-index=" + i + "]");
            groupElemenets.last().closest("tr").addClass("x-merge-separator");

            if (groupElemenets.length < 2) continue;

            groupElemenets.first().attr("rowspan", groupElemenets.length);
            groupElemenets.not(":first").remove();
        }
    });
}

// =================  ROUNDED CORNERS ====================
//========================================================

// This method sets rounded and leftrounded css classes to 
// elements which we want to have round corner style.
//
// This enables curvey corner to round them.
// 
// As curvey corner component does not support selecting nested css classes (like jquery)
// here we set a general class to our desired classes by selecting them using jquery.
function setRoundToBoxes() {
    $(".group-body").addClass("Omodule");
    $(".inner-tab-menu .item a").addClass("s-rounded");
}

function MakeRounded(cssClass) {
    var modules = $("." + cssClass);
    modules.each(function (index, element) {
        var module = $(element);
        if (!module.hasClass("edge-container")) {
            module.addClass("edge-container");
            module.append($("<div class='edge edge-tl' />"));
            module.append($("<div class='edge edge-tr' />"));
            module.append($("<div class='edge edge-bl' />"));
            module.append($("<div class='edge edge-br' />"));
        }
    });
}

// This function runs curvey corner with specific round width values.
function ApplyRoundCorners() {
    setRoundToBoxes();

    $(".module").addClass("rounded");
    $(".Omodule").addClass("rounded");
    if ($.browser.msie && $.browser.version == "6.0") {
        RoundCorners(7, 7, 7, 7, ".rounded");
    }
    else {
        MakeRounded("rounded");
        //MakeRounded("module");
        //MakeRounded("Omodule");
    }

    RoundCorners(5, 5, 5, 5, ".s-rounded");
}

// Run curvey corner for a specified css class with given corner widths.
function RoundCorners(tl, tr, bl, br, cls) {
    curvyCorners({ tl: { radius: tl }, tr: { radius: tr }, bl: { radius: bl }, br: { radius: br }, antiAlias: true }, cls);
}

function ScrollableTables() {
    var tableContainers = $("div.scrollable-grid");
    tableContainers.each(function (index, eltableContainer) {
        var table = $($("table", eltableContainer).get(0));
        var container = $(eltableContainer);

        var headers = $("tr.header-row", table);
        headers.each(function (i, header) {
            var headerDiv = $("<div style='fixed-header'/>");
            var headerWidth = header.offsetWidth;
            var allChildren = $(header).children("th td");
            allChildren.each(function (index, el) {
                var jel = $(el);
                var display = (index == allChildren.length - 1) ? "inline" : "inline-block";
                var html = "<span style='display:" + display + ";width:" + el.offsetWidth + "px'>" + jel.html() + "</span>";
                jel.html("");
                jel.width(el.offsetWidth);
                headerDiv.append(html);
            });
            headerDiv.insertBefore(container);
        });
        headers.css("visibility", "hidden");

        var footers = $("tr.pager", table);
        footers.each(function (i, footer) {
            var footerDiv = $("<div/>");
            var allChildren = $(footer).children("th td");
            allChildren.each(function (index, el) {
                var jel = $(el);
                var display = (index == allChildren.length - 1) ? "inline" : "inline-block";
                var html = "<span style='display:" + display + ";width:" + jel.width() + "px'>" + jel.html() + "</span>";
                footerDiv.append(html);
            });
            footerDiv.insertAfter(container);
        });
        footers.css("visibility", "hidden");

    });
}

// =================  REFINE DISPLAY INLINE SYSTEM ====================
//=====================================================================
// Our policy in this project is not to use float left to align objects next to each
// other.
// In order to acheive this we use display: inline and inline-block.
//
// Problem: When you set display inline-block to a DIV element in all browsers it works fine
// but in IE7- it does not.
//
// Solution: We need to avoid setting inline-block to DIVs and instead to its Non-Div parent
// but we know most of times its parent is not a non-div.
// so in this piece of jquery codes we will create a SPAN parent for the div with a specified css class.

function DoCorrection() {
    //$(".candidate-list .scrollable-grid").addClass("nice-scroll");
    //$(".client-list .scrollable-grid").addClass("nice-scroll");

    wrapAllElements(".view-body .item .value", "value");
    correctFormItemLabels(".form .item");
    correctFormItemLabels(".view-body .item");

    // loading on sorting
    $(".grid th a").click(function () { $(this).closest("th").addClass("loading"); });
    $(".job-list .grid td span a").click(function () { $(this).closest("span").addClass("loading"); });
}

function correctIE6InputStyles() {
}

function correctFormItemLabels(parentClass) {
    $(parentClass + " .label").each(function (index, element) {
        var jelement = $(element);
        if ($("label", jelement).length == 0) {
            var html = jelement.html();
            jelement.html("<label>" + html + "</label>");
        }
    });
}

function wrapAllElements(cssClass, targetClass) {
    var allElements = $(cssClass);
    allElements.wrap('<span class="' + targetClass + '" />');
    allElements.removeClass(targetClass);
}

Sys.Application.add_load(function () {

    $(".exclusive-checkboxes").each(function () { $(this).exlusiveCheckboxes(); });

    mergeMergableColumns();

    // use CSS3 instead for better performance
    //ApplyRoundCorners();

    DoCorrection();

    //MakeCheckBoxesNice();
    $("#rss-feed").rotator({ ms: 7000 });

    //alert(doesBrowserSupportCSS3());
});

function doesBrowserSupportCSS3() {
    var version = parseFloat($.browser.version);
    if ($.browser.safari) return true;
    if ($.browser.msie && version >= 9) return true;
    if ($.browser.mozilla && version >= 3.5) return true;
    if ($.browser.opera && version >= 10.53) return true;

    return false;
}

function MakeCheckBoxesNice() {
    $(":checkbox").each(function (index, checkbox) {
        checkbox = $(checkbox);
        if (checkbox.parent().is("span") == false) checkbox.wrap($("<span />"));
        checkbox.parent().addClass("nice-checkbox");
        checkbox.change(function () { setTimeout(niceCheckBox_reviewAll, 100); });
    });
    niceCheckBox_reviewAll();
}
function niceCheckBox_reviewAll() {
    $(":checkbox").each(function (index, checkbox) {
        niceCheckBox_reviewAppearance(checkbox);
    });
}
function niceCheckBox_reviewAppearance(input) {
    var parentEl = $(input).parent();
    if (input.checked) { if (parentEl.is(".checked") == false) parentEl.addClass("checked"); }
    else parentEl.removeClass("checked");
}


$.fn.toggleSlide = function (s) {
    this.each(function (index, el) {
        el = $(el);
        if (el.is(":visible")) el.slideUp(s);
        else el.slideDown(s);
    });
}
