﻿var origionalStoreAndEventLocatorTextBoxValue = null;
var modalCloseDelegate = null;

function MakeHitboxRequest(control, campaignCode, hbxcc, hbxpn, hbxan, image)
{
    // Placeholder function for old links with the MakeHitboxRequest.
    // So that those function calls don't give JavaScript errors.
}

function DisplayModalMessageFromArticle(articleName)
{
    var messageDiv = new Element('div');
    
    new Ajax.Updater(messageDiv, '/Company/Globals/Services/RPCUtilities.ashx?method=ArticleContent&articleName=' + articleName);
	    
	modalCloseDelegate = DisplayModalWindow(messageDiv, null);    
	
	return false; 
}

function DisplayModalMessageFromArticleWithClose(articleName, closedDelegate)
{
    var messageDiv = new Element('div');
    
    new Ajax.Updater(messageDiv, '/Company/Globals/Services/RPCUtilities.ashx?method=ArticleContent&articleName=' + articleName);
	    
	modalCloseDelegate = DisplayModalWindow(messageDiv, closedDelegate);    
	
	return false; 
}

function CloseModalMessageFromArticle()
{
    if(modalCloseDelegate != null)
    {
        modalCloseDelegate();
    }
}

function FocusStoreAndEventLocatorTextBox(event, control) 
{
    if (origionalStoreAndEventLocatorTextBoxValue == null) 
    {
        origionalStoreAndEventLocatorTextBoxValue = control.value;
    }

    if (control.value == origionalStoreAndEventLocatorTextBoxValue) 
    {
        control.value = '';
        control.style.color = '#000000';
    }
}

function UnFocusStoreAndEventLocatorTextBox(event, control) 
{
    if (control.value == '') 
    {
        control.value = origionalStoreAndEventLocatorTextBoxValue;
        control.style.color = '#666666';
    }
}

function LocatorKeyPress(event) 
{
    if(event.keyCode == Event.KEY_RETURN)
    {
        Event.stop(event);
        
        LocatorGo();
    }
}

function LocatorGo()
{
    var sLocatorTextBox = $(ClientIDs.StoreAndEventLocatorSearch).value; 
    var mode = ($(ClientIDs.Store).checked) ? "buy" : "play";
    var url = "http://ww2.wizards.com/StoreAndEventLocator/Default.aspx?link=true&ReturnParamMode=" + mode + "&ReturnParamMapSearch=" + sLocatorTextBox + "&ReturnParamLastDirectionsQuery=null";

    window.open(url, '', '');
  
    return false;
}

function UpdateProgressBar(bar, percent)
{
    if(progressBarTimeOutID != null)
    {
        clearTimeout(progressBarTimeOutID);
    }
        
    var percentToUpdate = percent;
    
    if(percentToUpdate < 0)
    {
        percentToUpdate = 0;
    } 
    else if(percentToUpdate > 100)
    {
        percentToUpdate = 100;
    }
    
    bar.currentPercent = percentToUpdate;
    bar.style.width = percentToUpdate + '%';
    bar.cont = false;
}

function UpdateProgressBarWait(bar, start, percent, delay, whenComplete)
{
    if(bar.cont == null || bar.cont)
    {
        var nextWidth = start + percent;
        
        if(nextWidth < 100 && nextWidth > 0)
        {
            bar.currentPercent = nextWidth;
            bar.style.width = nextWidth + '%';
            
            progressBarTimeOutID = setTimeout(function() { UpdateProgressBarWait(bar, nextWidth, percent, delay, whenComplete); }, delay);
        }
        else if(whenComplete == null)
        {
            nextWidth = percent;
            
            bar.currentPercent = nextWidth;
            bar.style.width = nextWidth + '%';
            
            progressBarTimeOutID = setTimeout(function() { UpdateProgressBarWait(bar, nextWidth, percent, delay, whenComplete); }, delay);
        } 
        else 
        {
            bar.cont = false;
            
            if(whenComplete != null)
            {
                whenComplete();
            }
        }        
    }
}
