/*!
    This file is part of the Navitaire NewSkies application.
    Copyright (C) Navitaire.  All rights reserved.
*/

/*
    Dependencies:
        This file depends on other JavaScript files to be there at run time.
        
        jquery.js:
            $ is a jquery variable
        common.js:
            SKYSALES namespace is used to avoid name collisions.
        TaxAndFeeInclusiveDisplay.js
            Calls taxAndFeeInclusiveDisplayDataRequestHandler
    
    General Notes:
        This JavaScript is used with the AvailabilityInput control
        
    JsLint Status:
        Pass - JsLint Edition 2008-06-04
        
        + Strict whitespace
        + Assume a browser 
        + Disallow undefined variables
        + Disallow leading _ in identifiers
        + Disallow == and !=
        + Disallow ++ and --
        + Disallow bitwise operators
        + Disallow . in RegExp literals
        + Disallow global var
        Indentation 4
        
*/

/*extern $ jQuery window SKYSALES */

/*
    Name: 
        Class AvailabilityInput
    Param:
        None
    Return: 
        An instance of AvailabilityInput
     Functionality:
        The object that initializes the AvailabilityInput control
    Notes:
        The journeyInfoList is set down to the browser via JSON that is created in the XSLT file.
        The XSLT creates a new SKYSALES.Class.AvailabilityInput and then sets the SKYSALES.availabilityInput.journeyInfoList
        property to an array of JourneyInfo objects. It then calls SKYSALES.availabilityInput.init
        This class also attemps to call taxAndFeeInclusiveDisplayDataRequestHandler that is in TaxAndFeeInclusiveDisplay.js
    Class Hierarchy:
        AvailabilityInput
*/
if (SKYSALES.Class.AvailabilityInput === undefined)
{
    SKYSALES.Class.AvailabilityInput = function ()
    {
        var thisAvailabilityInput = this;
        
        thisAvailabilityInput.detailsLinks = null;
        thisAvailabilityInput.journeyInfoArray = [];
		
		thisAvailabilityInput.PrintScreen = function ()
        {
		alert('aaaa');
		}
        
        thisAvailabilityInput.getPriceItineraryInfo = function ()
        {
            if (undefined !== window.taxAndFeeInclusiveDisplayDataRequestHandler)
            {
                var markets = $("#selectMainBody .availabilityTable tr td[@class^='fareCol'] :radio[@checked]");
                var keys = [];
                 
                $(markets).each(function (i)
                {
                    keys[i] = $(this).val();
                });
                 
                window.taxAndFeeInclusiveDisplayDataRequestHandler(keys, markets.length);
            }
        };        
        thisAvailabilityInput.showPreselectedFares = function (keyArray)
        {
            for (var keyIndex = 0; keyIndex < keyArray.length; keyIndex += 1)
            {
                if (keyArray[keyIndex] !== null)
                {
                    $("#" + keyArray[keyIndex]).click();
                }
            }
        };        
        thisAvailabilityInput.setPreselectedFares = function ()
        {
            var fareColumnCells = $("#selectMainBody .availabilityTable tr td[@class^='fareCol']");
            var markets = fareColumnCells.find(":radio[@checked]").length;
            if (markets === 0)
            {
                $("#taxAndFeeInclusiveDivBody").remove();
            }
            else if (markets === 1)
            {
                $("#taxesAndFeesInclusiveDisplay_2").hide();
            }
           
            var marketOneLowPrice = -1;
            var marketTwoLowPrice = -1;
            var marketOneID = "";
            var marketTwoID = "";
            var price = -1;
            var marketOneHas2C = false;
            var marketTwoHas2C = false;
           
            $("td[@class^='fareCol']", $('.availabilityTable')).each(
                function ()
                {
                    var radioId = $(this).find(":radio[@name$='market1']").attr("id");
                    if (radioId)
                    {
		                    carrierCode = $(this).find('.carrierCode').text();
		                    if (carrierCode == "2C")
		                      marketOneHas2C = true;
                    }
                });
           
            //$("#selectMainBody #availabilityTable tr td[@class^='fareCol'] > input[@name$='market1']").each(function ()
            $("td[@class^='fareCol']", $('.availabilityTable')).each(
                function ()
                {
                    var radioId = $(this).find(":radio[@name$='market1']").attr("id");
                    if (radioId)
                    {
		                    carrierCode = $(this).find('.carrierCode').text();
                    	//the price is inside the div containing the class marketPrice.
		                price = $(this).find('.marketPrice').text();
		                price = parseFloat(price);
		  
                        if (!isNaN(price))
                        {
                            if ((price < marketOneLowPrice || marketOneLowPrice < 0) && (carrierCode == "2C" || (carrierCode == "2Y" && marketOneHas2C == false)))
                            {
                                marketOneLowPrice = price;
                                marketOneID = radioId;
                            }
                        }
                    }
                });
            $("#" + marketOneID).attr("checked", "checked");
           
            $("td[@class^='fareCol']", $('.availabilityTable')).each(
                function ()
                {
                    var radioId = $(this).find(":radio[@name$='market2']").attr("id");
                    if (radioId)
                    {
		                    carrierCode = $(this).find('.carrierCode').text();
		                    if (carrierCode == "2C")
		                      marketTwoHas2C = true;
                    }
                });
                
            //$("#selectMainBody #availabilityTable tr td[@class^='fareCol'] > input[@name$='market2']").each(function ()
            $("td[@class^='fareCol']", $('.availabilityTable')).each(
                function ()
                {
                    var radioId = $(this).find(":radio[@name$='market2']").attr("id");
                    if (radioId)
                    {
		                    carrierCode = $(this).find('.carrierCode').text();
                       //the price is inside the div containing the class marketPrice.
		                price = $(this).find('.marketPrice').text();
		                price = parseFloat(price);
		                if (!isNaN(price))
                        {
                            if ((price < marketTwoLowPrice || marketTwoLowPrice < 0) && (carrierCode == "2C" || (carrierCode == "2Y" && marketTwoHas2C == false)))
                            {
                                marketTwoLowPrice = price;
                                marketTwoID = radioId;
                            }
                        }
                    }
                });
            $("#" + marketTwoID).attr("checked", "checked");
            
            thisAvailabilityInput.getPriceItineraryInfo();
            
            var keyArray = [];
            
            keyArray[0] = marketOneID;
            keyArray[1] = marketTwoID;
            
            thisAvailabilityInput.showPreselectedFares(keyArray);
        };        
        thisAvailabilityInput.addGetPriceItineraryInfoEvents = function ()
        {
            $("#selectMainBody .availabilityTable tr td[@class^='fareCol'] :radio").each(
                function () 
                {
                    $(this).click(
                        function ()
                        {
                            thisAvailabilityInput.getPriceItineraryInfo();
                        });
                });
        };        
        thisAvailabilityInput.ajaxEquipmentProperties = function ()
        {
            
        };        
        thisAvailabilityInput.addEquipmentPropertiesAjaxEvent = function ()
        {
            $(this).click(thisAvailabilityInput.ajaxEquipmentProperties);
        };
        thisAvailabilityInput.addEquipmentPropertiesAjaxEvents = function ()
        {
            thisAvailabilityInput.detailsLinks.each(thisAvailabilityInput.addEquipmentPropertiesAjaxEvent);
        };        
        thisAvailabilityInput.addEvents = function ()
        {
            thisAvailabilityInput.addGetPriceItineraryInfoEvents();
            thisAvailabilityInput.addEquipmentPropertiesAjaxEvents();            
        };        
        thisAvailabilityInput.setVars = function ()
        {
            thisAvailabilityInput.detailsLinks = $('.showContent');
        };
        thisAvailabilityInput.initJourneyInfoContainers = function ()
        {
            var i = 0;
            var journeyInfoList = this.journeyInfoList;
            var journeyInfo = null;
            for (i = 0; i < journeyInfoList.length; i += 1)
            {
                journeyInfo = new SKYSALES.Class.JourneyInfo();
                journeyInfo.init(journeyInfoList[i]);
                thisAvailabilityInput.journeyInfoArray[thisAvailabilityInput.journeyInfoArray.length] = journeyInfo;
            }
        };
        thisAvailabilityInput.init = function ()
        {
            thisAvailabilityInput.initJourneyInfoContainers();
            
            thisAvailabilityInput.setPreselectedFares();
            if (undefined !== window.taxAndFeeInclusiveDisplayDataRequestHandler)
            {           
                thisAvailabilityInput.setVars();
                thisAvailabilityInput.addEvents();
            }
        };
    };
}

/*
    Name: 
        Class JourneyInfo
    Param:
        None
    Return: 
        An instance of JourneyInfo
     Functionality:
        The object that represents the journey information on the AvailabilityInput control
    Notes:
        When the user clicks a link to view more details about the flight it gets the data and shows it in a floating div.
        It uses AJAX to get data about the equipment that the journey will use.
        The AJAX response sends the equipment data in the form of a JSON object.
    Class Hierarchy:
        AvailabilityInput
*/
if (SKYSALES.Class.JourneyInfo === undefined)
{
    SKYSALES.Class.JourneyInfo = function ()
    {
        var thisJourneyInfo = this;
        
        thisJourneyInfo.equipmentInfoUri = 'EquipmentPropertiesDisplayAjax-resource.aspx';
        thisJourneyInfo.journeyIndex = -1;
        thisJourneyInfo.journeyContainerId = "";
        thisJourneyInfo.activateJourneyId = "";
        thisJourneyInfo.activateJourney = null;
        thisJourneyInfo.deactivateJourneyId = "";
        thisJourneyInfo.deactivateJourney = null;
        thisJourneyInfo.journeyContainer = null;
        thisJourneyInfo.legInfoArray = [];
        thisJourneyInfo.clientName = 'EquipmentPropertiesDisplayControlAjax';
        
        thisJourneyInfo.init = function (paramObject)
        {
            this.setSettingsByObject(paramObject);
            this.setVars();
            this.addEvents();
        };
        thisJourneyInfo.setVars = function ()
        {
            thisJourneyInfo.journeyContainer = $('#' + thisJourneyInfo.journeyContainerId);
            thisJourneyInfo.activateJourney = $('#' + thisJourneyInfo.activateJourneyId);
            thisJourneyInfo.deactivateJourney = $('#' + thisJourneyInfo.deactivateJourneyId);
        };
        thisJourneyInfo.addEvents = function ()
        {
            thisJourneyInfo.activateJourney.click(thisJourneyInfo.show);
            thisJourneyInfo.deactivateJourney.click(thisJourneyInfo.hide);
        };
        thisJourneyInfo.setSettingsByObject = function (jsonObject)
        {
            var propName = '';
            for (propName in jsonObject)
            {
                if (thisJourneyInfo.hasOwnProperty(propName))
                {
                    thisJourneyInfo[propName] = jsonObject[propName];
                }
            }
        };
        thisJourneyInfo.showWithData = function (data)
        {
            var legInfoStr = $(data).html();
            var legInfoJson = SKYSALES.Json.parse(legInfoStr);
            var legInfoHash = legInfoJson.legInfo;
            var legInfo = null;
            var prop = '';
            var propertyContainer = null;
            var propertyHtml = '';
            var equipmentPropertyArray = null;
            var i = 0;
            var equipmentProperty = null;
            for (prop in legInfoHash)
            {
                if (legInfoHash.hasOwnProperty(prop))
                {
                    propertyHtml = '';
                    legInfo = legInfoHash[prop];
                    if (legInfo.legIndex !== undefined)
                    {
                        propertyContainer = $('#journey_' + thisJourneyInfo.journeyIndex + '_' + legInfo.legIndex);
                        equipmentPropertyArray = legInfo.equipmentPropertyArray;
                        for (i = 0; i < equipmentPropertyArray.length; i += 1)
                        {
                            equipmentProperty = equipmentPropertyArray[i];
                            propertyHtml += '<div>' + equipmentProperty.name + ': ' + equipmentProperty.value + '<\/div>';
                        }
                        propertyContainer.html(propertyHtml);
                    }
                }
            }
            thisJourneyInfo.journeyContainer.show('slow');
        };
        thisJourneyInfo.show = function ()
        {
            var legInfoArray = thisJourneyInfo.legInfoArray;
            var legInfo = null;
            var postHash = {};
            var prop = '';
            var i = 0;
            var propName = thisJourneyInfo.clientName;
            for (i = 0; i < legInfoArray.length; i += 1)
            {
                legInfo = legInfoArray[i];
                for (prop in legInfo)
                {
                    if (legInfo.hasOwnProperty(prop))
                    {
                        postHash[propName + '$legInfo_' + prop + '_' + i] = legInfo[prop];
                    }
                }
            }
            //postHash['__EVENTTARGET'] = thisJourneyInfo.clientName + '$EquipmentInfo';
            $.post(thisJourneyInfo.equipmentInfoUri, postHash, thisJourneyInfo.showWithData);
        };
        thisJourneyInfo.hide = function ()
        {
            thisJourneyInfo.journeyContainer.hide();
        };
        return thisJourneyInfo;
    };
}

