﻿/// <reference path="jquery.min-vsdoc.js" />

function defaultInit() {
    $(".tipAnchor").hover(
        function() {
            $(this).toggleClass("tipHovering");
        }, function() {
            $(this).toggleClass("tipHovering");
        }
    );

    $(".tipAnchorBig").hover(
        function() {
            $(this).toggleClass("tipHovering");
        }, function() {
            $(this).toggleClass("tipHovering");
        }
    );
}

function resultsInit() {
    $("#clearResultFilter").bind("click", function(e) { clearSearchFilter(); });
    $(".schoolFavorite").bind("click", function(e) { toggleSchoolSelection(this); });
    $(".resultTip").hover(
        function() {
            $(this).toggleClass("resultTipHover");
        }, function() {
            $(this).toggleClass("resultTipHover");
        }
    );
}

function clearSearchFilter() {
    $("[ID$='_txtSearchName']").val('');
    $("[ID$='_ddlSearchState']").val('');
    $("[ID$='_chkSearchYellowRibbon']").attr('checked',false);
    $("[ID$='_chkSearchCreditForMilService']").attr('checked', false);
    $("[ID$='_chkSearchExamsForCredit']").attr('checked', false);
    $("[ID$='_chkSearchOnline']").attr('checked', false);
    $("[ID$='_chkSearchOnCampus']").attr('checked', false);
    //$("[ID$='_chkSearchOnBase']").attr('checked', false);
    $("[ID$='_chkSearchAssociates']").attr('checked', false);
    $("[ID$='_chkSearchBachelors']").attr('checked', false);
    $("[ID$='_chkSearchMasters']").attr('checked', false);
    $("[ID$='_chkSearchDoctorate']").attr('checked', false);
    $("[ID$='_chkSearchCertificate']").attr('checked', false);
}

function onSelectedCountRetrieved(count) {
    updateSelectedSchoolsCount(count);
    PageMethods.GetSelectedSchoolList(onSelectedListRetrieved, onError);
}

function onSelectedProfileCountRetrieved2(count) {
    var lblCount = $(".profileSchoolCount")
    if (count > 0) {
        if (count == 1) {
            lblCount.html("Complete request for " + count + " selected school")
        } else {
            lblCount.html("Complete request for " + count + " selected schools")
        }
    } else {
        lblCount.html("")
    }
}

function onSelectedListRetrieved(list) {
    clearResultsCheckboxes();
    if ((list !== '') && (list !== null)) {
        var schools = list.split(",");
        var i = 0;
        while (i < schools.length) {
            var id = 'chk-' + schools[i];
            $("[ID$='" + id + "']").attr("checked", true);
            i++;       
        }        
    }    
}

function clearResultsCheckboxes() {
    $("#resultsContainer [type='checkbox']").each(function() {
        $(this).attr("checked", false);
    });
}

function refreshSelectedSchoolList(count) {
    PageMethods.GetSelectedCount(onSelectedCountRetrieved, onError);
}

function toggleSchoolSelection(item) {
    var $item = $(item);
    if ($item.attr("checked") === true) {
        PageMethods.AddSchoolToList($item.attr("id"), onListUpdatedSuccess, onError);
    } else {
        PageMethods.RemoveSchoolFromList($item.attr("id"), onListUpdatedSuccess, onError);
    }
}

function onListUpdatedSuccess(result) {
    updateSelectedSchoolsCount(result);
}

function onError(error) { alert(error.get_message()); }

function updateSelectedSchoolsCount(count) {
    if (count > 0) {
        if (count === 1) {
            $("#selectedSchoolCount").html(count.toString() + " school");
            $("#selectedSchoolsContainer [ID$='btnCheckout']").removeClass("nodisplay");
        } else {
            $("#selectedSchoolCount").html(count.toString() + " schools");
            $("#selectedSchoolsContainer [ID$='btnCheckout']").removeClass("nodisplay");
        }
    } else {
        $("#selectedSchoolCount").html("0 schools");
        $("#selectedSchoolsContainer [ID$='btnCheckout']").addClass("nodisplay");
    }
}

function profileInit() {
    $("[ID$='profileToggleFavorite']").bind("click", function(e) { toggleProfileFavorite(); });
}

function toggleProfileFavorite() {
    alert("here");
    var $button = $("[ID$='profileToggleFavorite']");
    var $unitid = $("[ID$='hidunitid']");
    var $btnRequest = $("[ID*='hypRequest']");
    var $divRequest = $("[ID*='divRequest']");
    if ($button.hasClass("profileAddFavorite")) {
        $button.removeClass("profileAddFavorite").addClass("profileRemoveFavorite");
        $button.html("Remove from my Schools List");
        $btnRequest.hide();
        $divRequest.show("slow");
        PageMethods.AddSchoolToList($unitid.val(), null, onError);
    } else {
        $button.removeClass("profileRemoveFavorite").addClass("profileAddFavorite");
        $button.html("Add to my Schools List");
        $btnRequest.show();
        $divRequest.hide("slow");
        PageMethods.RemoveSchoolFromList($unitid.val(), null, onError);
    }
}

function overviewInit() {
    $(".overviewReadMore").bind("click", function() {
        var $readMore = $(this);
        $readMore.parent().children(".overviewContent").slideToggle("slow", null);
        $readMore.toggleClass("nodisplay");
        return false;
    });
}

function benefitInit() {
    $(".benefitProgramName").bind("click", function() {
        var $name = $(this);
        $name.parent().children(".benefitProgramMenu").slideToggle("slow", null);
        return false;
    });
    $(".benefitHideMenu").bind("click", function() {
        var $name = $(this);
        $name.parent().slideToggle("slow", null);
        return false;
    });
    $(".benefitProgramMenuItem").hover(function() {
            $(this).addClass("benefitProgramMenuItemHover");
        }, function() {
            $(this).removeClass("benefitProgramMenuItemHover");
    });
    $(".benefitWhat").bind("click", function() {
        var $this = $(this);
        var $item = $this.parent();
        $item.children(".benefitProgramWhoContent").slideUp("slow", null);
        $item.children(".benefitProgramHowMuchContent").slideUp("slow", null);
        $item.children(".benefitProgramMenuItem").removeClass("benefitProgramMenuItemSelected");
        $this.addClass("benefitProgramMenuItemSelected");
        $item.children(".benefitProgramWhatContent").slideDown("slow", null);
        return false;
    });
    $(".benefitWho").bind("click", function() {
        var $this = $(this);
        var $item = $(this).parent();
        $item.children(".benefitProgramWhatContent").slideUp("slow", null);
        $item.children(".benefitProgramHowMuchContent").slideUp("slow", null);
        $item.children(".benefitProgramMenuItem").removeClass("benefitProgramMenuItemSelected");
        $this.addClass("benefitProgramMenuItemSelected");
        $item.children(".benefitProgramWhoContent").slideDown("slow", null);
        return false;
    });
    $(".benefitHowMuch").bind("click", function() {
        var $this = $(this);
        var $item = $(this).parent();
        $item.children(".benefitProgramWhatContent").slideUp("slow", null);
        $item.children(".benefitProgramWhoContent").slideUp("slow", null);
        $item.children(".benefitProgramMenuItem").removeClass("benefitProgramMenuItemSelected");
        $this.addClass("benefitProgramMenuItemSelected");
        $item.children(".benefitProgramHowMuchContent").slideDown("slow", null);
        return false;
    });
}

function explainInit() {
    $("#rotEmailLink").bind("click", function() {
        $("#rotShare").css("display", "none");
        $("#rotEmail").slideToggle("slow", null);
    });
    $("#rotShareLink").bind("click", function() {
        $("#rotEmail").css("display", "none");
        $("#rotShare").slideToggle("slow", null);
    });
}

function adviceContentInit() {
    var $hidden = $(".contentHidden");
    $hidden.before("<div class='contentContinueReading' onClick='showAdviceContent(); return false;'>Continue Reading...</div>");
}

function showAdviceContent() {
    $(".contentContinueReading").remove();
    $(".contentHidden").show();
    reloadCSS();
}

// from: http://msdn.microsoft.com/en-us/library/ms537509(VS.85).aspx
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function reloadCSS() {
    ver = getInternetExplorerVersion();
    if (ver != -1 && ver < 9) {
        var i, a, s;
        a = document.getElementsByTagName('link');
        for (i = 0; i < a.length; i++) {
            s = a[i];
            if (s.rel.toLowerCase().indexOf('stylesheet') >= 0 && s.href) {
                var h = s.href.replace(/(&|%5C?)forceReload=\d+/, '');
                s.href = h + (h.indexOf('?') >= 0 ? '&' : '?') + 'forceReload=' + (new Date().valueOf())
            }
        };
    }
        }
