﻿if ( !window.Controls )
    window.Controls = {};

Controls.TabNavigator = function( owner )
{
    this.owner = owner;
}

Controls.TabNavigator.prototype.owner = null;
Controls.TabNavigator.prototype.tabs = [];
Controls.TabNavigator.prototype.selectedIndex = -1;

Controls.TabNavigator.prototype.addTab = function( checkbox, canvas )
{
    var item = new SLx.CheckBox ( checkbox );
    item._wireTriggers();
    item.add_Checked( Silverlight.createDelegate ( this , this.onSelectionChanged ));
    this.tabs[this.tabs.length] = { name: checkbox.Name, item: item, checkbox: checkbox, canvas: canvas };
}
Controls.TabNavigator.prototype.onSelectionChanged = function(sender, eventArgs)
{
    for ( var i=0; i<this.tabs.length; i++ )
    {
        if ( this.tabs[i].name != sender.get_visual().Name )
        {
            this.tabs[i].item.set_isChecked (false);
            this.tabs[i].canvas.Visibility = "Collapsed";
            this.tabs[i].canvas["Canvas.ZIndex"] = 10;
        }
    }
    this.showNamedTab( sender.get_visual().Name );
}
Controls.TabNavigator.prototype.showTab = function( index )
{
    var visibility = false;
    if ( index == 0 )
        visibility = true;
    
    if ( index == 0 )
        this.owner.control.content.root.FindName("map_button").Visibility = "Visible";
    else
        this.owner.control.content.root.FindName("map_button").Visibility = "Collapsed";
    
    if ( index == 1 )
    {
        if (!this.owner.detailsVisible)
            this.owner.control.content.root.FindName("slide_basket_down").begin();
        this.owner.control.content.root.FindName("slide_itinerary_down").Seek("00:00:00");
        this.owner.control.content.root.FindName("slide_itinerary_down").Stop();
    }
    else if ( index == 2 )
    {
        if (!this.owner.detailsVisible)
            this.owner.control.content.root.FindName("slide_basket_up").begin();
        this.owner.control.content.root.FindName("slide_itinerary_down").begin();
    }
    else
    {
        if (!this.owner.detailsVisible)
            this.owner.control.content.root.FindName("slide_basket_up").begin();
    }
        
    for (var i=0; i<this.owner.flightSearchTextBoxes.length; i++)
    {
        this.owner.flightSearchTextBoxes[i].set_inputVisible( visibility );
        this.owner.flightSearchTextBoxes[i].updateLayout();
    }
    this.tabs[ index ].canvas.Visibility = "Visible";
    this.tabs[ index ].canvas["Canvas.ZIndex"] = 20;
    this.selectedIndex = index;
}
Controls.TabNavigator.prototype.showNamedTab = function( name )
{
    this.showTab( this.findTabIndex( name ) );
}
Controls.TabNavigator.prototype.findTabIndex = function( name )
{
    for ( var i=0; i<this.tabs.length; i++ )
        if ( this.tabs[i].name == name )
            return i;
    return -1;
}
Controls.TabNavigator.prototype.getItemByIndex = function( index )
{
    return this.tabs[index].item;
}
Controls.TabNavigator.prototype.getSelectedIndex = function()
{
    return this.selectedIndex;
}