/// <reference path="Required/jquery.min-vsdoc.js" />
/// <reference path="Required/jquery-ui.min.js" />

/// <reference path="fillSelect.js" />
// JavaScript Document

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//  --------------- Notification ------------------- //
//  - Michael Hollis
//  - 7/28/11
//  - Shows notification at top of screen, uses JQueryUI theme
//  ------------------------------------------------ //
function notify(msg) {
    var $alertdiv = $('<div id = "alertmsg" class="ui-state-highlight"/>');
    $alertdiv.html(msg);
    $alertdiv.bind('click', function () {
        $(this).slideUp(200);
    });
    $(document.body).prepend($alertdiv);
    $("#alertmsg").effect("drop", { direction: "up", mode: "show" }, "slow")
        .click(function () {
            $alertdiv.slideUp(200, function () {
                $("#alertmsg").remove();
            });
        });
    
    setTimeout(function () {
        $alertdiv.slideUp(200, function () {
            $("#alertmsg").remove();
        });
    }, 20000);
}

//  --------------- styleTable ------------------- //
//  - Michael Hollis
//  - 7/28/11
//  - Table styling using jqueryUI theme
//  - options: rowLinks will make the TR a link. The href for that link will be the href of 
//  - the first anchor in that TR that has the attribute data-row='true'
//  - newWindows will make the TR link open in a new window.
//  ------------------------------------------------ //
(function ($) {
    $.fn.styleTable = function (options) {
        var defaults = {
            css: 'styleTable'
        };
        options = $.extend(defaults, options);

        return this.each(function () {

            var input = $(this);
            input.addClass(options.css);
            if (options.rowLinks) {
                input.find("tbody tr").live('mouseover mouseout', function(event) {
                    if (event.type == 'mouseover') {
                        $(this).children("td").addClass("ui-state-hover");
                    } else {
                        $(this).children("td").removeClass("ui-state-hover");
                    }
                }).live('click', function(event) {
                    event.preventDefault();
                    var href = $(this).find("a[data-row='true']:first").attr("href");
                    if (href) {
                        if (options.newWindow)
                            window.open(href);
                        else
                            window.location = href;}
                }).css({ "cursor": "pointer" });
            };
            input.find("th").addClass("ui-state-default");
            input.find("td").addClass("ui-widget-content");

            input.find("tr").each(function () {
                $(this).children("td:not(:first)").addClass("first");
                $(this).children("th:not(:first)").addClass("first");
            });
        });
    };
})(jQuery);


function DropDownWithOther(propertyName) {
    ///<summary>For use when there is a dropdown with an "Other" option.</summary>
    ///<param name="PropertyName" type="String">The name of the property</param>

    //                <div class="row">
    //                @Html.LabelFor(Function(model) model.PROPERTYNAME)
    //                <div class="fields">
    //                    <select id="PROPERTYNAMEDropDown">
    //                        <option value="1">One </option>
    //                        <option value="Other">Other</option>
    //                    </select>
    //                </div>
    //            </div>
    //            <div class="row" id="PROPERTYNAMERow">
    //                @Html.LabelFor(Function(model) model.PROPERTYNAME, "PROPERTYNAME (Other)")
    //                <div class="fields">
    //                    @Html.EditorFor(Function(model) model.PROPERTYNAME)
    //                    <br /><br />@Html.ValidationMessageFor(Function(model) model.PROPERTYNAME)
    //                </div>
    //            </div>

    $("#" + propertyName + "Row").hide();
    $("#" + propertyName).val($("#" + propertyName + "DropDown").val());
    $("#" + propertyName + "DropDown").change(function () {
        if ($(this).val() == "Other") {
            $("#" + propertyName).val('');
            $("#" + propertyName + "Row").show("blind");
        } else {
            if ($("#" + propertyName + "Row").is(":visible")) {
                $("#" + propertyName + "Row").hide("blind", null, 500, function () {
                    $("#" + propertyName).val($("#" + propertyName + "DropDown").val());
                });
            } else {
                $("#" + propertyName).val($("#" + propertyName + "DropDown").val());
            }
        }
    });
}

