﻿/// <reference path="jquery.min.vsdoc.js" />


function loginPageInit() {
    $(".tabStrip div").hover(function() {
        $(this).addClass("tabStripHover");
    }, function() {
        $(this).removeClass("tabStripHover");
    });

    $("#InfoPowerTab").bind("click", function(e) { showTab('InfoPowerTab', 'InfoPower'); showPowerTab('InfoPowerCase1Tab','InfoPowerCase1'); });
    $("#InfoWhyTab").bind("click", function(e) { showTab('InfoWhyTab', 'InfoWhy'); });
    $("#InfoCredibilityTab").bind("click", function(e) { showTab('InfoCredibilityTab', 'InfoCredibility'); });
    $("#InfoTimelineTab").bind("click", function(e) { showTab('InfoTimelineTab', 'InfoTimeline'); });
    showTab('InfoWhyTab', 'InfoWhy');

    $("#InfoPower span").hover(function() {
        $(this).addClass("powerTabHover");
    }, function() {
        $(this).removeClass("powerTabHover");
    });
    $("#InfoPowerCase1Tab").bind("click", function(e) { showPowerTab('InfoPowerCase1Tab', 'InfoPowerCase1'); });
    $("#InfoPowerCase2Tab").bind("click", function(e) { showPowerTab('InfoPowerCase2Tab', 'InfoPowerCase2'); });
    $("#InfoPowerCase3Tab").bind("click", function(e) { showPowerTab('InfoPowerCase3Tab', 'InfoPowerCase3'); });
    $("#InfoPowerCase4Tab").bind("click", function(e) { showPowerTab('InfoPowerCase4Tab', 'InfoPowerCase4'); });

}

function showPowerTab(tabName, tabContent) {
    var $tab = $("#" + tabName);
    $tab.parent().children("span").removeClass("selectedPowerTab");
    $tab.addClass("selectedPowerTab");
    var $tabContent = $("#" + tabContent);
    $tabContent.parent().children("div").addClass("nodisplay");
    $tabContent.removeClass("nodisplay");
}

function showTab(tabName, tabContent) {
    var $tab = $("#" + tabName);
    $tab.parent().children().removeClass("selectedTab");
    $tab.addClass("selectedTab");
    var $tabContent = $("#" + tabContent);
    $tabContent.parent().children().addClass("nodisplay");
    $tabContent.removeClass("nodisplay");
}

function toggleSurveyHelp() {
    var $surveyHelpText = $("#surveyHelpText");
    $surveyHelpText.slideToggle("600");
    if ($surveyHelpText.data('opened') === null) {
        $("#surveyHelpInfoBox").animate({ width: "550px" }, 600);
        $surveyHelpText.data('opened', 'open');
    }
    if ($surveyHelpText.data('opened') === 'open') {
        $("#surveyHelpInfoBox").animate({ width: "220px" }, 600);
        $surveyHelpText.data('opened', 'closed');
    } else {
        $("#surveyHelpInfoBox").animate({ width: "550px" }, 600);
        $surveyHelpText.data('opened', 'open');
    }
}

    function resultsInit() {
        $("#clearResultFilter").bind("click", function(e) { clearSearchFilter(); });
        $(".companyFavorite").bind("click", function(e) { toggleCompanySelection(this); });
        checkCheckAll();
    }

    function clearSearchFilter() {
    }

    function onSelectedCountRetrieved(count) {
        updateSelectedCompaniesCount(count);
        PageMethods.GetSelectedCompanyList(onSelectedListRetrieved, onError);
    }

    function onSelectedListRetrieved(list) {
        clearResultsCheckboxes();
        if ((list !== '') && (list !== null)) {
            var companies = list.split(",");
            var i = 0;
            while (i < companies.length) {
                var id = 'chk-' + companies[i];
                var span = $("[ClientID$='" + id + "']");
                var chk = span.children()[0];
                $(chk).attr("checked", true);
                i++;
            }
        }
    }

    function clearResultsCheckboxes() {
        $('input[id *= chkSelect]').each(function() {
            $(this).checked = false;
        });
    }

    function refreshSelectedCompanyList(count) {
        PageMethods.GetSelectedCount(onSelectedCountRetrieved, onError);
    }

    function toggleCompanySelection(item) {
        var $item = $(item);
        if ($item.attr("checked") === true) {
            PageMethods.AddCompanyToList($item.parent().attr("ClientID"), onListUpdatedSuccess, onError);
            //PageMethods.AddCompanyFromList($item.attr("ID"), onListUpdatedSuccess, onError);
        } else {
            PageMethods.RemoveCompanyFromList($item.parent().attr("ClientID"), onListUpdatedSuccess, onError);
            //PageMethods.RemoveCompanyFromList($item.attr("ID"), onListUpdatedSuccess, onError);
        }
        checkCheckAll();
    }

    function checkCheckAll() {
        var chk = true;
        $('input[id *= chkSelect]').each(function() {
            chk = chk && this.checked;
        });
        $('input[id *= cbSelect]').each(function() {
            $(this).attr("checked", chk);
        });
    }

    function onListUpdatedSuccess(result) {
        updateSelectedCompaniesCount(result);
    }

    function onError(error) {
        alert(error.get_message()); 
    }

    function updateSelectedCompaniesCount(count) {
        var b = $("input[ID$='btnCheckout']")
        if (count > 0) {
            b.removeAttr('disabled');
            if (count === 1) {
                $("#selectedFranchiseCount").html(count.toString() + " franchise");
                $("#selectedFranchiseContainer [ID$='btnCheckout']").removeClass("nodisplay");
            } else {
                $("#selectedFranchiseCount").html(count.toString() + " franchises");
                $("#selectedFranchiseContainer [ID$='btnCheckout']").removeClass("nodisplay");
            }
        } else {
            b.attr('disabled', 'disabled');
            $("#selectedFranchiseCount").html("0 franchises");
            $("#selectedFranchiseContainer [ID$='btnCheckout']").addClass("nodisplay");
        }
    }
    
    function profileInit() {
        $("[ID$='profileToggleFavorite']").bind("click", function(e) { toggleProfileFavorite(); });
    }

    function toggleProfileFavorite() {
        var $button = $("[ID$='profileToggleFavorite']");
        var $id = $("[ID$='hidid']");
        var $btnRequest = $("[ID*='hypRequest']");
        var $divRequest = $("[ID*='divRequest']");
        if ($button.hasClass("profileAddFavorite")) {
            $button.removeClass("profileAddFavorite").addClass("profileRemoveFavorite");
            $button.html("Remove from My Franchise List");
            $btnRequest.hide();
            $divRequest.show("slow");
            PageMethods.AddCompanyToList("-" + $id.val(), onSelectedProfileCountRetrieved, onError);
        } else {
            $button.removeClass("profileRemoveFavorite").addClass("profileAddFavorite");
            $button.html("Add to My Franchise List");
            $btnRequest.show();
            $divRequest.hide("slow");
            PageMethods.RemoveCompanyFromList("-" + $id.val(), onSelectedProfileCountRetrieved, onError);
        }
    }

    function onSelectedProfileCountRetrieved(count) {
        var lblCount = $(".profileFranchiseCount")
        if (count > 0) {
            if (count == 1) {
                lblCount.html("Complete request for " + count + " selected franchise")
            } else {
                lblCount.html("Complete request for " + count + " selected franchises")
            }
        } else {
            lblCount.html("")
        }
    }

    function checkall(chk) {
        val = $(chk).attr("checked");
        companies = ''
        $('input[id *= chkSelect]').each(function() {
            this.checked = val;
            if (companies.length > 0) {
                companies += ','
            }
            //alert($(this).parent().attr("ClientID"));
            companies += $(this).parent().attr("ClientID");
        });
        $('input[id *= cbSelect]').each(function() {
            this.checked = val;
        });
        if (val) {
            PageMethods.AddCompaniesToList(companies, onListUpdatedSuccess, onError);
        } else {
            PageMethods.RemoveCompaniesFromList(companies, onListUpdatedSuccess, onError);
        }
    }

