﻿/*  The targetID is what element on the page my popup
        is going to be postioned on...and the position is
        determined from positioningMode the in variable passed in.
        
        Positioning Ints
            Center: 1
            BottomLeft: 2
            BottomRight: 3
            TopLeft: 4
            TopRight: 5 
*/

//variables
/*popup related params*/
var AlphaId;
var SortId;
var DivIdLoading;
var DivIdData;

/*webservice related Params*/
var Search;
var EventId;
var EventType;
var PageName;
var Sort;
var LikeString;

/*initialization and caching related */
var initialized = false;
var cachedData;

function ShowPopup(/*popup related params*/popupBehaviorId, divIdLoading, divIdData, alphaId, sortId,
    /*webservice related Params*/search, eventId, eventType, pageName, sort, searchString)
{
    //Initialize the variables if for the first time
    if(initialized == false)
    {
        DivIdLoading = divIdLoading;
        DivIdData = divIdData;
        AlphaId = alphaId;
        SortId = sortId;
        
        Search = search;
        EventId = eventId;
        EventType = eventType;
        PageName = pageName;
        Sort = sort;
        LikeString = searchString;
    }
    
    //Set the Css classes
    ChangeCssClass(AlphaId, "selected");
    ChangeCssClass(SortId, "nameSelected");
    
    //get hold of the popup and show it
    var popup = $find(popupBehaviorId);
    if(popup == null)
    {
        return;
    }
    popup._onFocus();
    
    //Make the webservice request to fetch data
    FetchArtistList();
    
    //set the initialized variable here to prevent resetting of visual
    //elements in subsequent calls.. This will also result in proper caching
    //of data
    initialized = true;
}

function Alpha(searchString, alphaId)
{
   LikeString = searchString; 
    
   //Change the Css classes
   ChangeCssClass(AlphaId, "");
   AlphaId = alphaId;
   ChangeCssClass(AlphaId, "selected");
   
   //Call webservice after clearing cache
   cachedData = null;
   FetchArtistList();   
}

function SortOrder(sort, sortId)
{
    Sort = sort;
    
    //Change the Css classes
    ChangeCssClass(SortId, "nameNormal");
    SortId = sortId;
    ChangeCssClass(SortId, "nameSelected");
    
    //Call webservice after clearing cache
    cachedData = null;
    FetchArtistList();
}

function ChangeCssClass(elemId, classname)
{
    var element = $get(elemId);    
    if (element != null)
    {
        element.setAttribute("class", classname);
        element.setAttribute("className", classname);
    }
}

function FetchArtistList()
{
    if(typeof(cachedData) != "undefined" && cachedData != null &&
        initialized == true)
    {
        $get(DivIdLoading).style.display = "none";
        $get(DivIdData).style.display = "block";
        $get(DivIdData).innerHTML = cachedData;
        return;
    }
    
    $get(DivIdLoading).style.display = "block";
    $get(DivIdData).style.display = "none";
    
    Autocomplete.GetArtistNames(Search, EventId, Sort, LikeString,
        EventType, PageName, SucceededCallback, 
        FailedCallback,"Search WebService");
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededCallback(result, context, methodName)
{
    $get(DivIdLoading).style.display = "none";
    $get(DivIdData).style.display = "block";
    $get(DivIdData).innerHTML = result;
    
    //cache the result
    cachedData = result;
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function FailedCallback(result, context, methodName)
{
    $get(DivIdLoading).style.display = "none";
    $get(DivIdData).style.display = "block";
    $get(DivIdData).innerHTML = result.get_message();
}

if (typeof(Sys) != "undefined") 
{
    Sys.Application.notifyScriptLoaded();
}

