/*
 * File: Destinations.js
 *
 * Supported browsers:
 *          - DOM compiliant browsers.
 *            Old IE4 , Netscape 5 are not supported.
 *            >IE6, Mozilla, > Firefox 1.5, > Opera 7
 *
 * @date 06.03.2007
 * @autor perkon / AI Create / http://www.aicreate.com /
 *
 * <script SRC="js/Destinations.js"></script>
 * @dependencies
 *      <script SRC="js/JSUtils/JSEvents.js" ></script>
 *          <script SRC="js/JSUtils/DOMUtils.js"></script>
 *              <script SRC="js/JSUtils/browser.js"></script>
 */

Destinations.fromComboId = "ft";
Destinations.toComboId = "tt";

function Destinations(fromId, toId)
{
    var self = this;
    this.fromId = fromId;
    this.toId = toId;
    this.destData = window.DestData;
    JSEvents.AttachEvent(window, "load", function(evt) { self.OnLoad(evt); }, false, false)
}

Destinations.prototype.OnLoad = function(evt)
{
    var self = this;

    this.fromCombo = GetElement(Destinations.fromComboId);
    this.toCombo = GetElement(Destinations.toComboId);
    if (!this.fromCombo || (!this.toCombo))
        return;

    this.fromFirstItem = this.fromCombo.options[0];// this.AddComboItem(this.fromCombo, 0, "Loading");
    this.toFirstItem = this.toCombo.options[0]; //this.AddComboItem(this.toCombo, 0, "Loading");
    var dest;
    var isSelected;
    var item;
    var selected;
    
    for(var i = 0; i < this.destData.length; i++)
    {
        if (dest != this.destData[i].dest_from_id)
        {
            if (this.fromId == this.destData[i].dest_from_id)
            {
                selected = true;
                isSelected = true;
            }
            else
            {
                selected = false;
            }
            item = this.AddComboItem(this.fromCombo, this.destData[i].dest_from_id, this.destData[i].from_name, selected);
            //item.openInFullScreen = this.destData[i].fullscreen;
            dest = this.destData[i].dest_from_id;
        }
    }

    this.fromFirstItem.text = "Please choose";
    this.toFirstItem.text = "Please choose";

    if (isSelected)
    {
        this.OnFromComboChange(this.toId);
    }

    JSEvents.AttachEvent(this.fromCombo, "change", function(evt) {self.OnFromComboChange(0);}, false, false);
}
Destinations.prototype.AddComboItem = function(combo, id, name, selected)
{
    var item = new Option(name, id, null, selected);
    combo.options.add(item);
    return item;
}
Destinations.prototype.OnFromComboChange = function(defToId)
{
    this.toFirstItem.text = "Loading";
    this.toCombo.length = 1;

    if (this.fromCombo.selectedIndex > 0)
    {
        var option = this.fromCombo.options[this.fromCombo.selectedIndex];
        var dest;
        //var selectedItem;
        var selected;
        for(var i = 0; i< this.destData.length; i++)
        {
            if (dest != this.destData[i].dest_to_id && (option.value == this.destData[i].dest_from_id))
            {
                if (this.destData[i].dest_to_id == defToId)
                {
                    selected = true;
                }
                else
                {
                    selected = false;
                }
                var item = this.AddComboItem(this.toCombo, this.destData[i].dest_to_id, this.destData[i].to_name, selected);
                dest = this.destData[i].dest_to_id;
                /*if (this.destData[i].dest_to_id == defToId)
                {
                    selectedItem = item;
                }*/
            }
        }

    }

    this.toFirstItem.text = "Please Choose";

}
Destinations.prototype.Go = function()
{
    var ft = this.fromCombo.options[this.fromCombo.selectedIndex].value;
    var tt = this.toCombo.options[this.toCombo.selectedIndex].value;
    if ((ft > 0)&&(tt > 0))
    {
        document.location.href = "?t=journey/show&ft=" + ft + "&tt=" + tt;
    }
}
Destinations.prototype.BookNow = function()
{
    var ft = this.fromCombo.options[this.fromCombo.selectedIndex].value;
    var tt = this.toCombo.options[this.toCombo.selectedIndex].value;
    if ((ft > 0)&&(tt > 0))
    {
        for(i = 0; i < this.destData.length; i++)
        {
           if (ft == this.destData[i].dest_from_id && tt == this.destData[i].dest_to_id)
           {
           		if (this.destData[i].fullscreen)
           		{
           			document.location.href = this.destData[i].book_link;
           		}
           		else
           		{
                	window.open(this.destData[i].book_link,'pop','scrollbars=yes, status=yes, menubar=no, toolbar=no, resizable=yes, height=' + (screen.width > 800 ? 800 : 450) + ', width=620');
               	}
                break;
           }
        }
    }
}

