﻿var showSelectorIds = new Array();

//ctor
function ShowSelector(SelectedShowType, SelectionMode, PrimaryDropDownId, SecondaryDropDownId,MyName,ShowPageMode)
{
    this.selectedShowType = SelectedShowType;
    this.selectionMode = SelectionMode;
    this.primaryDropDownId = PrimaryDropDownId;
    this.secondaryDropDownId = SecondaryDropDownId;
    this.myName = MyName;
    this.showPageMode = ShowPageMode;
        
    this.nonSelectBuffer = false;
    this.showTypeSelectedBuffer = false;
    
    this.initialised = false;
    
    if (this.selectionMode=="Season")
    {
        this.enableDesignerDropDownWithoutPrimarySelection = true;
        //this.primaryDefaultText = "Select a season...";
        this.primaryDefaultText = "Выберите сезон...";
    }
    else if (this.selectionMode=="City")
    {
        this.enableDesignerDropDownWithoutPrimarySelection = false;
        //this.primaryDefaultText = "Select a city...";
        this.primaryDefaultText = "Выберите город...";
    }
}

ShowSelector.prototype.initialise = function(){
    this.initialised = true;
    
    this.primaryDropDown = $get(this.primaryDropDownId);
    this.secondaryDropDown = $get(this.secondaryDropDownId);
    
    this.primaryDropDownS = $find(this.primaryDropDownId);
    if (this.secondaryDropDownId!="")
    {
        this.secondaryDropDownS = $find(this.secondaryDropDownId);
    }
    
    
    if (this.primaryDropDownS==undefined || (this.secondaryDropDownId!="" && this.secondaryDropDownS==undefined))
    {
        //initialisation was not successful, try again later
        this.initialised=false;
        window.setTimeout(this.myName + ".initialise()",800);
        return;    
    }
    
    //
    //initialised, therefore we can work with the objects
    //
    if (this.enablePrimaryDropDownWithoutPrimarySelection==false && this.secondaryDropDownId!="")
    {
        //disable secondary drop down till primary dd has a selection
        this.secondaryDropDownS.disable();
    }
    
    this.primaryDropDown.oself = this;
    this.secondaryDropDown.oself = this;
    
    if (this.selectionMode=="Season")
    {
         //this.ResetDesignersDD();
         if (this.selectedShowType!="" && this.selectedShowType!=" ")
         {
             this.primaryDropDownS.set_text(this.primaryDropDownS.get_selectedItem().get_text() + " (" + this.selectedShowType + ")");
             this.ResetDesignersDD();
         }
         else this.primaryDropDownS.set_text(this.primaryDefaultText);
     }
     else if (this.selectionMode=="City")
     {
        this.primaryDropDownS.set_text(this.primaryDefaultText);
     }
     
     //enable primary drop down
     this.primaryDropDownS.enable();
};

function ShowSelector_initialiseAll()
{
    for (var i = 0;i<showSelectorIds.length;i++)
        showSelectorIds[i].initialise();
}

ShowSelector.prototype.SeasonsDD_Loaded = function(sender)
 {
    
    
 };
 
 ShowSelector.prototype.SeasonDD_Opening = function(combo)
 {
    //combo._element.oself.selectedShowType="";
 };
 
 ShowSelector.prototype.SeasonDD_Closing = function(sender, eventArgs)
 {
    if (sender._element.oself.nonselectBuffer==true)
        eventArgs.set_cancel(true);
    else
    {
        eventArgs.set_cancel(false);    
    }
 
 };
 
 ShowSelector.prototype.selectShowType = function(showType)
 {
    this.selectedShowType = showType;
    this.showTypeSelectedBuffer = true;
    window.setTimeout(this.myName + ".showTypeSelectedBuffer=false",2000);
    this.SeasonDD_SetText();
 };
 
 ShowSelector.prototype.NonSelectableItemClick = function(prototypeObj)
 {
    if (prototypeObj.showTypeSelectedBuffer==false)
    {
        prototypeObj.nonSelectBuffer = true;
        window.setTimeout(prototypeObj.myName + ".nonSelectBuffer=false",1000);
    }
 };
 ShowSelector.prototype.SeasonDD_SelectedIndexChanging = function(sender, eventArgs)
 {
    if (sender._element.oself.nonSelectBuffer == true)
        eventArgs.set_cancel(true);
     else
     {
        sender._element.oself.LoadDesignersDD(sender,eventArgs);
        eventArgs.set_cancel(false);
     }
 };
 ShowSelector.prototype.SeasonDD_SelectedIndexChanged = function(sender, eventArgs)
 {
    sender._element.oself.SeasonDD_SetText();//set_text(sender.get_selectedItem().get_text() + " (" + sender._element.oself.selectedShowType + ")");               
 };
 ShowSelector.prototype.SeasonDD_SetText = function()
 {
    var seasonsCombo = this.primaryDropDownS;
    if (seasonsCombo.get_selectedItem() != null)
        seasonsCombo.set_text(seasonsCombo.get_selectedItem().get_text() + " (" + this.selectedShowType + ")");               
 };
ShowSelector.prototype.LoadDesignersDD = function(combo, eventarqs)
{    
    var designersCombo = combo._element.oself.secondaryDropDownS;
    combo._element.oself.ResetDesignersDD();
};
ShowSelector.prototype.ResetDesignersDD = function()
{
    this.secondaryDropDownS.enable();
    this.secondaryDropDownS.clearItems();
    //this.secondaryDropDownS.set_text("Select a designer...");
    this.secondaryDropDownS.set_text("Выберите дизайнера...");
};
ShowSelector.prototype.DesignerDD_HandleIndexChange = function(sender, args)
{
//                    var item = args.get_item();
//                    window.location.href=item.attributes["L"];
};
ShowSelector.prototype.DesignerDD_Loaded = function(comboBox)
{
    
};
ShowSelector.prototype.DesignerDD_HandleClose = function(comboBox)
{
    var designersCombo = comboBox._element.oself.secondaryDropDownS;
    //designersCombo.set_text("Select a designer...");
    designersCombo.set_text("Выберите дизайнера...");
};
ShowSelector.prototype.DesignerDD_HandleOpen = function(comboBox, args)
{            
        if (comboBox._element.oself.selectionMode!="Season" && comboBox._element.oself.primaryDropDownS.get_text()==comboBox._element.oself.primaryDefaultText)
        {
            //cancel if nothing selected in primary drop down
            args.set_cancel(true);
            var comb = comboBox._element.oself.primaryDropDown;
            //comb.style.border="solid 3px red";
            //window.setTimeout(comboBox._element.oself.myName + ".HideSeasonWarning()",1000);
        }
        else
        {
            var designersCombo = comboBox._element.oself.secondaryDropDownS; 
            designersCombo.set_text("");
        }
};
ShowSelector.prototype.DesignerDD_HandleRequestStart = function(sender, args)
{
    args.get_context()["PageMode"]=sender._element.oself.showPageMode;
    
    if (sender._element.oself.selectionMode=="Season")
    {
        var seasonCombo = sender._element.oself.primaryDropDownS;
        var selectedSeasonItem = seasonCombo.get_selectedItem();
        var seasonId;
        if (selectedSeasonItem!=null)
            seasonId = selectedSeasonItem.get_value();
        else
            seasonId = -1;
        args.get_context()["SeasonId"] = seasonId;
        args.get_context()["ShowType"] = sender._element.oself.selectedShowType;
    }
    else if (sender._element.oself.selectionMode=="City")
    {
        var cityCombo = sender._element.oself.primaryDropDownS;
        var selectedCityItem = cityCombo.get_selectedItem();
        var cityName = selectedCityItem.get_value();
        args.get_context()["CityName"] = cityName;
    }
};
ShowSelector.prototype.HideSeasonWarning = function()
{
   var comb =  this.primaryDropDown;
   comb.style.border="solid 3px transparent";
};

$addHandler(window,"load",ShowSelector_initialiseAll);