﻿var clientCachedUserName = null;
var suppressGetUserName = false;
var loginTitleId;
var failureTextId;
var defaultRegistrationUrl;
var registrationLinkId;

function modalLoginSetup(){
    var modalPopup = $find('popupLoginBehavior');              // <. this is the BehaviorID from the ModalPopupExtender
    addOnShowingEventHandler(modalPopup);
    addOnShownEventHandler(modalPopup);
    addOnHideEventHandler(modalPopup);
}
function addOnShowingEventHandler(modalPopup) {
    modalPopup.add_showing(onModalLoginShowing);
}
function addOnShownEventHandler(modalPopup) {
    modalPopup.add_shown(onModalLoginShown);
    //Sometimes Safari 3.2 doesn't have zIndex set
    document.getElementById(sLoginID).style.zIndex = "10001";
   
}
function addOnHideEventHandler(modalPopup) {
    modalPopup.add_hiding(onModalLoginHide); 
}
function onModalLoginShowing() {
    getCurrentUsersLoginName(sLogin_UserNameID);
}
function onModalLoginShown() {
    //Set focus fails becuase textbox parent visibility is off for a brief moment, so this timeout allows DOM a chance to update visibility before we set the focus.
    setTimeout('setCredentialsFocus();', 300);  
}
function onModalLoginHide() {
    $get(sLogin_UserNameID).value = '';
    $get(sLogin_PasswordID).value = '';
    $get(loginTitleId).innerHTML = '';
    $get(failureTextId).innerHTML = '&nbsp;';
    if ($get(registrationLinkId) != null) {
        $get(registrationLinkId).href = defaultRegistrationUrl;
    }
    $get(sLogin_RememberMeID).checked = true;
}
function getCurrentUsersLoginName(destControlId) {
    if (!suppressGetUserName) {
        if (clientCachedUserName == null)
            clientCachedUserName = PageMethods.GetCurrentUsersLoginName("widget", CallSuccess_GetCurrentUsersLoginName, CallFailed_GetCurrentUsersLoginName, destControlId);
        else
            CallSuccess_GetCurrentUsersLoginName(clientCachedUserName, destControlId, "GetCurrentUsersLoginName");
    }
}

//Runs on successful server callback of PageMethods.GetCurrentUsersLoginName to set the value of the username textbox
function CallSuccess_GetCurrentUsersLoginName(result, userContext, methodName) {
    var ctrl = document.getElementById(userContext);
    if(ctrl.value=='')  //<--check needed so we don't overwrite browser's auto-form fill/remebered login features.
        ctrl.value = result;
    clientCachedUserName = result;
    //setCredentialsFocus();
}

function CallFailed_GetCurrentUsersLoginName(result, userContext, methodName) {
    //setCredentialsFocus();
}
function setCredentialsFocus(){
    try {
        var userNameTextBox = document.getElementById(sLogin_UserNameID);
        if (userNameTextBox.value == '')
            userNameTextBox.focus();
        else
            document.getElementById(sLogin_PasswordID).focus();
    } catch (e) {
    }
}
//******************************************
//Immediate Execution on Dom Parse of script
//******************************************

//Add handler to onload on doc
Sys.Application.add_load(modalLoginSetup);

//widget services

var accordianId = null;

Sys.Application.add_load(setWidgetPnl);
var recipeBoxLoaded;
var recentlyViewedLoaded;
var communityHotTopicsLoaded;
var groceryListLoaded;
var newsletterPanelLoaded;


function setWidgetPnl() {

    var results = document.cookie.match('(^|;) ?' + 'widgetCurrentPanel' + '=([^;]*)(;|$)');
    var selectedPanel;

    if (results)
        selectedPanel = (unescape(results[2]));
    else
        selectedPanel = 0;


    if (accordianId != null) {
        var accordian = $get(accordianId).AccordionBehavior;
        accordian.set_FadeTransitions(false);
        accordian.set_TransitionDuration(1);
        LoadAccordtion(parseInt(selectedPanel) + 1)
        accordian.set_SelectedIndex(selectedPanel);
        accordian.set_FadeTransitions(true);
        accordian.set_TransitionDuration(250);
        accordian.add_selectedIndexChanged(onAccordionPaneChanged);
    }
}

function onAccordionPaneChanged(sender, eventArgs) {
    var selPane = sender.get_SelectedIndex() + 1;
    LoadAccordtion(selPane)
}


function LoadAccordtion(selPane) {

    if (selPane == 1 && newsletterPanelLoaded != true) {
        BettyCrocker.WidgetService.GetNewsLettersSubscriptions(location.href, OnNewsLetterSucceeded, OnNewsLetterFailed);
    }

    if (selPane == 2 && recipeBoxLoaded != true) {
        BettyCrocker.WidgetService.GetRecipeBox(location.href, OnRecipeBoxSucceeded, OnRecipeBoxFailed);
    }

    if (selPane == 3 && groceryListLoaded != true) {
        BettyCrocker.WidgetService.GetGroceryList(location.href, OnGroceryListSucceeded, OnGroceryListFailed);
    }

    if (selPane == 4 && recentlyViewedLoaded != true) {
        BettyCrocker.WidgetService.GetRecentlyViewRecipes(OnRecentlyViewedSucceeded, OnRecentlyViewedFailed);
    }

    if (selPane == 5 && communityHotTopicsLoaded != true) {
        BettyCrocker.WidgetService.GetCommunityHotTopics(OnCommunityTopicsSucceeded, OnCommunityTopicsFailed);
    }

}

function OnNewsLetterSucceeded(result) {
    newsletterPanelLoaded = true;
    document.getElementById("NewsLettersContainer").innerHTML = result;
}

function OnNewsLetterFailed(error) {
    document.getElementById("NewsLettersContainer").innerHTML = "<b style='color:red'>An Error Occured</b>";
}


function OnGroceryListSucceeded(result) {
    groceryListLoaded = true;
    document.getElementById("GroceryListContainer").innerHTML = result;
}

function OnGroceryListFailed(error) {
    document.getElementById("GroceryListContainer").innerHTML = "<b style='color:red'>An Error Occured</b>";
}

function OnCommunityTopicsSucceeded(result) {
    communityHotTopicsLoaded = true;
    document.getElementById("CommunityHotTopicsContainer").innerHTML = result;
}

function OnCommunityTopicsFailed(error) {
    document.getElementById("CommunityHotTopicsContainer").innerHTML = "<b style='color:red'>An Error Occured</b>";
}

function OnRecipeBoxSucceeded(result) {
    recipeBoxLoaded = true;
    document.getElementById("RecipeBoxListContainer").innerHTML = result;
}

function OnRecipeBoxFailed(error) {
    document.getElementById("RecipeBoxListContainer").innerHTML = "<b style='color:red'>An Error Occured</b>";
}


function OnRecentlyViewedSucceeded(result) {
    recentlyViewedLoaded = true;
    document.getElementById("RecentlyViewedRecipesContainer").innerHTML = result;
}

function OnRecentlyViewedFailed(error) {
    document.getElementById("RecentlyViewedRecipesContainer").innerHTML = "<b style='color:red'>An Error Occured</b>";
}


//Function to save last used panel index to cookie
function saveWidgetPnl(panelIndex) {
    var argv = saveWidgetPnl.arguments;
    var argc = saveWidgetPnl.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = "/"; //(argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = 'widgetCurrentPanel' + '='
			 + escape(panelIndex) +
		   ((expires == null) ? '' : ('; expires=' +
		   expires.toGMTString())) +
		   ((path == null) ? '' : ('; path=' + path)) +
		   ((domain == null) ? '' : ('; domain=' + domain)) +
		   ((secure == true) ? '; secure' : '');
}


