﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="relationalUtility.debug.js" />

Type.registerNamespace('Website.Behavior');



// ===================================================================== Persistent Toggle



// persistentToggle_click
//
// Handles the Click event of _PersistentToggle links
//
Website.Behavior.persistentToggle_click = function (evt, context) {
    var target = $get(this.id.replace(new RegExp('(.*?)_Toggle'), '$1_Target'));
    if (target.className.indexOf("Hidden") >= 0) {
        // target is hidden - show it
        Website.Behavior.persistentToggleShow(this, target);
        Relational.Utility.setCookie(Website.Behavior.persistentToggleCookieName(target.id), "Visible", Relational.Utility.daysInFuture(999));
    }
    else {
        // target is visible - hide it
        Website.Behavior.persistentToggleHide(this, target);
        Relational.Utility.setCookie(Website.Behavior.persistentToggleCookieName(target.id), "Hidden", Relational.Utility.daysInFuture(999));
    }         
    // Cancel link click from bubbling
    return Relational.Utility.eventCancel(evt);
} 

Website.Behavior.persistentToggleCookieName = function (targetId) {
    return Relational.Utility.currentPage() + targetId + "Visibility"
}

// persistentToggleShow
//
// Shows the target of a persistentToggle switch
//
Website.Behavior.persistentToggleShow = function (toggle, target) {
    toggle.setAttribute('title', toggle.toolTipValues[1]);
    Relational.Utility.changeClassName(toggle,"Off","On");
    Relational.Utility.changeClassName(target,"Hidden","Visible");
}

// persistentToggleHide
//
// Hides the target of a persistentToggle switch
//
Website.Behavior.persistentToggleHide = function (toggle, target) {
    toggle.setAttribute('title', toggle.toolTipValues[0]);
    Relational.Utility.changeClassName(toggle,"On","Off");
    Relational.Utility.changeClassName(target,"Visible","Hidden");
}

// persistentToggle_initialize
//
// Initializes the state of any persistentToggle objects
//
Website.Behavior.persistentToggle_initialize = function () {
    var toggles = Relational.Utility.getElementsBySelector("a._PersistentToggle");
    for (var i = 0; i < toggles.length; i++) {
        var toggle = toggles[i];
        toggle.toolTipValues = toggle.getAttribute("title").split(/\|/) // title attribute should contain two tooltips separated by a bar, e.g. title="Show|Hide"
        var target = $get(toggle.id.replace(new RegExp('(.*?)_Toggle'), '$1_Target'));
        var cookieValue = Relational.Utility.getCookie(Website.Behavior.persistentToggleCookieName(target.id));
	    if (cookieValue == "Visible"){
            Website.Behavior.persistentToggleShow(toggle, target);
	    } else {
            Website.Behavior.persistentToggleHide(toggle, target);
	    }
    }        
}


// ===================================================================== Column Chooser

Website.Behavior.hideColumnChooser= function()
{
    Relational.Utility.changeClassName(this, "Hover", "NoHover");
}
    
Website.Behavior.showColumnChooser= function()
{
    Relational.Utility.changeClassName(this, "NoHover", "Hover");
}

// ===================================================================== Issue Images

var issueCaptionFlash 

Website.Behavior.issueImageSwitch_mouseover = function(evt, context)
{
    /// <summary>
    /// Handles the mouseover event of links in the issue menu. Changes the masthead image and caption to display the selected item.
    /// </summary>
    var issueId = evt.target.id;
    Website.Behavior.showIssueImage(issueId, true)
}

Website.Behavior.showIssueImage= function(issueId, flashCaption)
{
    var issueImageContainer = $get("_IssueImageContainer");
    if (issueImageContainer) {
        var issueImages = issueImageContainer.getElementsByTagName('IMG');
        // Hide all images
        for (var i=0; i<issueImages .length; i++) {
            Relational.Utility.changeClassName(issueImages [i], 'Visible', 'Hidden');
       } 
       // Show the desired image
       var targetIssueImage = $get("_IssueImage" + issueId);
       Relational.Utility.changeClassName(targetIssueImage, 'Hidden', 'Visible');
    }
    
    var issueCaptionContainer = $get("_IssueCaptionContainer");
    if (issueCaptionContainer) {
        var issueCaptions = issueCaptionContainer.getElementsByTagName('p');
        // Hide all captions
        for (var i=0; i<issueCaptions.length; i++) {
            Relational.Utility.changeClassName(issueCaptions [i], 'Visible', 'Hidden');
       } 
       // Show the desired caption
       var targetIssueCaption = $get("_IssueCaption" + issueId);
       Relational.Utility.changeClassName(targetIssueCaption, 'Hidden', 'Visible');
       if (flashCaption==true) {
           // Flash the container
           if (typeof(issueCaptionFlash) == 'undefined') {
                // instantiate a new animation
               issueCaptionFlash = new AjaxControlToolkit.Animation.ColorAnimation(issueCaptionContainer, 1, 35, "style", "backgroundColor", "#CFC6C0",  "#E8E0DA");
           }
           if (issueCaptionFlash.get_isPlaying() == true) {
                // the animation is already playing, so stop & start it over (need this check or multiple animations fight each other, making the div flicker)
               issueCaptionFlash.stop();
           }
           issueCaptionFlash.play();
        }
    }
}    


// =====================================================================


Website.Behavior.selectOnFocus_Focus = function(evt, context)
{
    this.select();
}

Website.Behavior.popupLink_click = function(evt, context)
{
    var sFeatures = String.format("height={0},width={1},location=no,menubar=no,status=no,toolbar=no,scrollbars=yes", context.windowHeight, context.windowWidth);            
    window.open(this.href,"IgxPopup", sFeatures);
    // Cancel link click from bubbling
    return Relational.Utility.eventCancel(evt);
}


Website.Behavior.clickByProxy_click = function () {
    /// <summary>
    /// Handles the click event of _clickByProxy divs
    /// </summary>
    Relational.Utility.clickInside(this,'_ClickTarget');
}

// waitButton_click
//
// Handles the click event of WaitButtonLinks
//
Website.Behavior.waitButton_click = function () {
    var waitButton = this;
    if (waitButton) {
        if (waitButton.tagName=='SPAN') waitButton=waitButton.parentNode;
        if(Relational.Utility.isPageValid(waitButton) == true) {
            // build DummyButton
            var dummyButton = document.createElement('a');
            dummyButton.className = waitButton.className + " DummyButton"; // Keep same CSS classes as original link
            dummyButton.style.cssText = waitButton.style.cssText; // Keep any styles set manually on the page
            dummyButton.innerHTML="<span></span><img src='../images/dingbats/spinner.tiny.2.gif' />Please wait...";
            waitButton.parentNode.insertBefore(dummyButton,waitButton); 
            // hide WaitButton
            Relational.Utility.changeClassName(waitButton,'Button','Hidden');
        }
    } 
}


// formField_focus and formField_blur
//
// Handles the Focus and Blur event of FormField inputs
//
Website.Behavior.formField_focus = function () {
  Relational.Utility.changeClassName(this,'NoFocus','Focus');
} 
Website.Behavior.formField_blur = function () {
  Relational.Utility.changeClassName(this,'Focus','NoFocus');
} 


// clearOnFirstFocus_focus
//
// Handles the Focus event of _clearOnFirstFocus inputs
//
Website.Behavior.clearOnFirstFocus_focus = function () {
    if (this.alreadyClearedOnce==true) {
        // do nothing - don't want to clear the user's input if they've already started typing
    }  
    else {
        this.value='';
        this.alreadyClearedOnce = true;
    } 
} 


// showHideOnClick_click
//
// Handles the Click event of _showOnClick links
//
Website.Behavior.showHideOnClick_click = function (evt, context) {
    // Show
    var showTarget = $get(this.id.replace(new RegExp('ShowHideOnClickTrigger_(.*?)'), 'ShowTarget_$1'));
    Relational.Utility.changeClassName(showTarget,"Hidden","Visible");
    // Hide button
    Relational.Utility.changeClassName(this,"Visible","Hidden");
    // Cancel link click from bubbling
    return Relational.Utility.eventCancel(evt);
} 

// showHideOnClick_click
//
// Handles the Click event of _showHideOnClick links
//
Website.Behavior.showHideOnClick_click = function (evt, context) {
    // Show
    var showTarget = $get(this.id.replace(new RegExp('ShowHideOnClickTrigger_(.*?)'), 'ShowTarget_$1'));
    Relational.Utility.changeClassName(showTarget,"Hidden","Visible");
    // Hide 
    var hideTarget = $get(this.id.replace(new RegExp('ShowHideOnClickTrigger_(.*?)'), 'HideTarget_$1'));
    Relational.Utility.changeClassName(hideTarget,"Visible","Hidden");
    // Cancel link click from bubbling
    return Relational.Utility.eventCancel(evt);
} 

// hideShowOnClick_click
//
// Handles the Click event of _hideShowOnClick links
//
Website.Behavior.hideShowOnClick_click = function (evt, context) {
    // Show
    var showTarget = $get(this.id.replace(new RegExp('HideShowOnClickTrigger_(.*?)'), 'ShowTarget_$1'));
    Relational.Utility.changeClassName(showTarget,"Visible","Hidden");
    // Hide 
    var hideTarget = $get(this.id.replace(new RegExp('HideShowOnClickTrigger_(.*?)'), 'HideTarget_$1'));
    Relational.Utility.changeClassName(hideTarget,"Hidden","Visible");
    // Cancel link click from bubbling
    return Relational.Utility.eventCancel(evt);
} 

// ratingButton_click
//
// Handles the click event of _ratingButton links (used by Controls/Rating.ascx)
//
Website.Behavior.ratingButton_click= function (evt, context) {
    // find the ValueHolder textbox (hint: it's in the same naming container as the buttons, and its ClientID ends in "ValueHolder") 
    var valueHolder = $get(this.id.replace(new RegExp('(.*?)Rating.$'), '$1ValueHolder')); // e.g. if our button is ctl00$ctl06$Rating1, then find $ctl00$ctl06$ValueHolder
    // stash the clicked vlaue in the textbox
    valueHolder.innerText = this.innerText; 
    // turn all links off
    var links = this.parentNode.getElementsByTagName('A');
    for (var i=0; i<links.length; i++) {
        Relational.Utility.changeClassName(links[i], 'On', 'Off');
   } 
    // Turn this link on 
    Relational.Utility.changeClassName(this, 'Off', 'On');
    // Cancel link click from bubbling
    return Relational.Utility.eventCancel(evt);
}


// ===================================================================== Rule definitions

	
Website.Behavior.publicRules =
{
// Rules that are needed only by the public site (don't run on admin site for performance reasons)

    // Yellow fade behavior
    'div._YellowFade' : function(element){
        //AjaxControlToolkit.Animation.ColorAnimation.play(target, duration, fps, property, propertyKey, startValue, endValue);
        AjaxControlToolkit.Animation.ColorAnimation.play(element, .7, 25, "style", "backgroundColor", "#F2EEEA",  "#FFFFFF");
    },

    // Wire up _clickByProxy divs to click through to the a.ClickTarget element
    'div._ClickByProxy' : function(element){
        $addHandler(element, 'click', Website.Behavior.clickByProxy_click);
    },

    // Wire up issue image switcher
    'a._IssueImageSwitch' : function(element){
        var context = {"element" : element};
        $addHandler(element, 'mouseover', Function.createCallback(Website.Behavior.issueImageSwitch_mouseover, context));
    },

    // Show/hide on click
    'a._ShowHideOnClick' : function(element){
        var context = {'element':element};
        $addHandler(element, 'click', Function.createCallback(Website.Behavior.showHideOnClick_click, context));
    },
    
    // Hide/Show on click
    'a._HideShowOnClick' : function(element){
        var context = {'element':element};
        $addHandler(element, 'click', Function.createCallback(Website.Behavior.hideShowOnClick_click, context));
    },
    
//    // Rating button behavior
//    'div.RatingButtons a' : function(element){
//        var context = null;
//        $addHandler(element, 'click', Function.createCallback(Website.Behavior.ratingButton_click, context));
//    },
//    
    // Add/remove focus styling on form fields
    'input.FormField' : function(element){
        $addHandler(element, 'focus', Website.Behavior.formField_focus);
        $addHandler(element, 'blur', Website.Behavior.formField_blur);
    }

}

Website.Behavior.globalRules =
{
// Rules that are needed on both the admin and public sites

    'input._SelectOnFocus' : function(element){
        $addHandler(element, 'focus', Website.Behavior.selectOnFocus_Focus);
    },

    // Popup link behavior
    'a._PopupLink' : function(element){
        var context = {"windowWidth" : 400, "windowHeight" : 600 };
        $addHandler(element, 'click', Function.createCallback(Website.Behavior.popupLink_click, context));
    },

    // Popup link behavior (bigger view port)
    'a._PopupLinkLarge' : function(element){
        var context = {"windowWidth" : 800, "windowHeight" : 600 };
        $addHandler(element, 'click', Function.createCallback(Website.Behavior.popupLink_click, context));
    },

    // Wait button behavior
    'a._WaitButton' : function(element){
        $addHandler(element, 'click', Website.Behavior.waitButton_click);
    },

    // Persistent toggle
    'a._PersistentToggle' : function(element){
        var context = {"element" : element};
        //if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()==false) {
            $addHandler(element, 'click', Function.createCallback(Website.Behavior.persistentToggle_click, context));
        //}
    },

    // Wire up column chooser
    'div._ColumnChooserContainer' : function(element){
        $addHandler(element, 'mouseover', Website.Behavior.showColumnChooser);
        $addHandler(element, 'mouseout', Website.Behavior.hideColumnChooser);
    }

}


// ===================================================================== Initialization

Website.Behavior.initialize = function(isAdminPage) {
    // This needs to be called at the bottom of every root master page.
    if (isAdminPage != true || typeof(isAdminPage)==undefined){
        Relational.Utility.Rules.register(Website.Behavior.publicRules)
    }
    Relational.Utility.Rules.register(Website.Behavior.globalRules);
    Relational.Utility.Rules.apply();
    Website.Behavior.persistentToggle_initialize();
}


// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


