/* Utility.js */
$(document).ready(function()
{
    $("#modal").jqm({
        closeClass: "close",
        onShow: function (hash) {
            var modal = $(hash.w);
            var left = ($(window).width() / 2) - (modal.width() / 2);
            var top = ($(window).height() / 2) - (modal.height() / 2);
            modal.css("top", top).css("left", left);
            if (callbackFunction != null) {
                modal.fadeIn(500, callbackFunction);
            }
            else {
                modal.fadeIn(500);
            }
        }
    });
    $(".dropdown dt a").live("click", function() {
        var dropdown = $(this).parent().parent();
        if ($("dd ul", dropdown).is(":visible")) {
            $(".dropdown dd ul").hide();
        } else {
            $(".dropdown dd ul").hide();
            $("dd ul", dropdown).toggle();
        }
    });

    $(".dropdown dd ul li a").live("click", function() {
        var text = $(this).html();
        //$(".dropdown dt a span").html(text);
        $(".dropdown dd ul").hide();
    //$("#result").html("Selected value is: " + getSelectedValue("sample"));
    });

    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (!$clicked.parents().hasClass("dropdown"))
            $(".dropdown dd ul").hide();
        if ($("#submenu").is(":visible"))
        {
            if (!$clicked.parents().hasClass("submenu") && !$clicked.parents().hasClass("selected") &&
                !$clicked.hasClass("submenu") && !$clicked.hasClass("selected") &&
                !$clicked.parents().hasClass("submenubottom") &&
                !$clicked.hasClass("submenubottom") &&
		!$clicked.parents().hasClass("goog-tooltip") &&
		!$clicked.hasClass("goog-tooltip")) {
                $("#submenu").fadeOut(250);
                $('.selected').removeClass('selected');
            }
        }
    });
    $(document).bind('mousemove', function(e)
    {
        var $clicked = $(e.target);
        if ($("#submenu").is(":visible"))
        {
            if (!$clicked.parents().hasClass("submenu") && !$clicked.parents().hasClass("selected") &&
                !$clicked.hasClass("submenu") && !$clicked.hasClass("selected") &&
                !$clicked.parents().hasClass("submenubottom") &&
                !$clicked.hasClass("submenubottom") &&
		!$clicked.parents().hasClass("goog-tooltip") &&
		!$clicked.hasClass("goog-tooltip")) {
                $("#submenu").fadeOut(250);
                $('.selected').removeClass('selected');
            }
        }
    });
    $(".dropdown ul").corner("5px");
    $("#dock > div:first-child").corner("5px TL");
    $("#dock > div:last-child").corner("5px TR");
});

function ValidateEmpty(element, ok) {
    var failed = element.val() == "";
    return SetValidationClass(element, ok, failed);
}

function ValidateDropDown(element, ok) {
    var failed = element.val() == "0" || element.val() == null;
    return SetValidationClass(element, ok, failed);
}

function ValidateOrder(element1, element2, ok) {
    var element1Value = parseInt(element1.val());
    var element2Value = parseInt(element2.val());
    var failed = element1Value >= element2Value;
    SetValidationClass(element1, ok, failed);
    return SetValidationClass(element2, ok, failed);
}

function SetValidationClass(element, ok, failed) {
    if (failed) {
        element.addClass("error");
        return false;
    } else {
        element.removeClass("error");
        return ok;
    }
}

function SetFocus(html) {
    if (html == null) {
        $("input:first").focus();
    } else if (typeof (html) == "object") {
        $("input:first", html.get_updateTarget()).focus();
    } else {
        $("input:first", html).focus();
    }
}

function GetText(html) {
    var text;
    if (typeof (html) == "object") {
        text = html.get_data();
    } else {
        text = html;
    }
    return text;
}

var callbackFunction;

function ShowSuccessModal(html, callback) {
    CloseModal();
    if (IsFunction(callback)) {
        callbackFunction = callback;
    }
    var text = GetText(html);
    $("#successModal").html(text);
    $("#successModal").jqmShow();
}

function ShowModal(html, callback) {
    CloseModal();
    if (IsFunction(callback)) {
        callbackFunction = callback;
    }
    var text = GetText(html);
    $("#modalcontent").html(text);
    $("#modal").jqmShow();
}

function CloseModal(callback) {
    callbackFunction = null;
    $("#modal").jqmHide();
    $("#modalcontent").html("");
    //$.modal.close();
    if (IsFunction(callback))
        callback();
}

function CloseSuccessModal(callback) {
    callbackFunction = null;
    $("#successModal").jqmHide();
    //$.modal.close();
    if (IsFunction(callback))
        callback();
}

function IsFunction(method) {
    return method != null && typeof (method) == "function";
}

function Remove(element, callback) {
    var text = GetText(element);
    $(text).fadeOut(500, function() {
        $(text).remove();
        if (IsFunction(callback))
            callback();
    });
}

function FadeIn(element, html, callback) {
    var text = GetText(html);
    var fadeInDiv = $(element);
    fadeInDiv.stop(false, true);
    if (fadeInDiv.is(":visible")) {
        fadeInDiv.fadeOut(500, function() {
            fadeInDiv.html(text);
            if (IsFunction(callback))
                fadeInDiv.fadeIn(500, callback);
            else
                fadeInDiv.fadeIn(500);
        });
    } else {
        fadeInDiv.html(text);
        if (IsFunction(callback))
            fadeInDiv.fadeIn(500, callback);
        else
            fadeInDiv.fadeIn(500);
    }
}

function ErrorOccurred(html) {
    var text = GetText(html);
    var startIndex = text.indexOf("<title>") + 7;
    var endIndex = text.indexOf("</title>");
    var length = endIndex - startIndex;
    text = text.substring(startIndex, endIndex);
    var div = $("#main").children(":first");
    var errorDiv = $("<div>").css({
        "color": "Red",
        "fontWeight": "bold",
        "display": "none"
    }).html(text).insertBefore(div).fadeIn(500);
    CloseModal();
    setTimeout(function() {
        errorDiv.fadeOut(500, function() {
            errorDiv.remove();
        });
    }, 5000);
}

function SetSuccessfulMessage(html) {
    var text = GetText(html);
    CloseModal();
    CloseSuccessModal();
    ShowSuccessModal(html, function() {
        setTimeout(function() {
            CloseSuccessModal();
        }, 1500);
    });
//    var div = $("#main").children(":first");
//    var errorDiv = $("<div>").css({ "color": "Green", "fontWeight": "bold", "display": "none" }).html(text).insertBefore(div).fadeIn(500);
//    setTimeout(function() {
//        errorDiv.fadeOut(500, function() {
//            errorDiv.remove();
//        });
//    }, 5000);
}

function GoEdit() {
    var url = document.URL;
    if (url.indexOf("#") != -1) {
        url = url.substring(0, url.indexOf("#")) + "?e=t";
    } else if (url.indexOf("?") != -1) {
        url += "&e=t";
    } else {
        url += "?e=t";
    }
    document.location = url;
}

function RemoveEdit() {
    var url = document.URL;
    var index = url.indexOf("?e=t");
    if (index == -1) {
        index = url.indexOf("&e=t");
    }
    url = url.substring(0, index);
    document.location = url;
}

function PinpointTableLocation(table) {
    $(table).delegate('td', 'mouseover mouseleave', function(e) {
        if (e.type == 'mouseover') {
            $(this).parent().addClass("pinpointhover");
        //$(this).parent().parent().parent().children("colgroup").eq($(this).index()).children("tr").not(":first").addClass("pinpointhover");
        }
        else {
            $(this).parent().removeClass("pinpointhover");
        //$(this).parent().parent().parent().children("colgroup").eq($(this).index()).children("tr").not(":first").removeClass("pinpointhover");
        }
    });
}

function ReadMoreAtLineList(element, lineCount) {
    var $this = $(element);
    var lineHeight = $this.css("lineHeight").substring(0, $this.css("lineHeight").indexOf("px"));
    if (($("li", $this).length * lineHeight) > (lineHeight * lineCount)) {
        $this.data('height', ($("li", $this).length * lineHeight))
        .css({
            overflow: 'hidden',
            height: (lineHeight * lineCount) + "px"
        });
        var readLess = $('<div class="more" style="display: none;">View Less</div>');
        var readMore = $('<div class="more">Read More...</div>').css("float", "right");
        readMore.click(function() {
            $this.animate({
                height: $this.data('height')
            }, 500, function() {
                $this.css("display", "inline");
                $this.next().next().show();
            });
            $(this).hide();
        });
        readLess.click(function() {
            $this.css("display", "block");
            $this.animate({
                height: (lineHeight * lineCount) + "px"
            }, 500, function() {
                $this.next().show();
            });
            $(this).hide();
        });
        $this.children(0).after(readLess)
        .after(readMore);
        $this.after(readLess)
        .after(readMore);
    }
}

function ReadMoreAtLine(element, lineCount) {
    var $this = $(element);
    var lineHeight = $this.css("lineHeight").substring(0, $this.css("lineHeight").indexOf("px"));
    if ($this.height() > (lineHeight * lineCount)) {
        $this.data('height', $this.height())
        .css({
            overflow: 'hidden',
            height: (lineHeight * lineCount) + "px"
        });
        var readLess = $('<div class="more" style="display: none;">View Less</div>');
        var readMore = $('<div class="more">Read More...</div>').css("width", $this.width());
        $this.after(readLess)
        .after(readMore);

        $this.next().click(function() {
            $this.animate({
                height: $this.data('height')
            }, 500, function() {
                $this.css("display", "inline");
                $this.next().next().show();
            });
            $(this).hide();
        });
        $this.next().next().click(function() {
            $this.css("display", "block");
            $this.animate({
                height: (lineHeight * lineCount) + "px"
            }, 500, function() {
                $this.next().show();
            });
            $(this).hide();
        });
    }
}

$.fn.SetSelectOptions = function(data) {
    return this.each(function() {
        var list = this;
        $.each(data, function(index, itemData) {
            var output = [];

            $.each(data, function(key, itemData)
            {
                if (itemData.Selected) {
                    output.push('<option value="'+ itemData.Value +'" selected="selected">'+ itemData.Text +'</option>');
                } else {
                    output.push('<option value="'+ itemData.Value +'">'+ itemData.Text +'</option>');
                }
            });

            $(list).html(output.join(''));
        });
    });
}
