var tabSelectedClassName = ' selected';
var tabIdArray = new Array('bookFlightTab', 'myTripsTab', 'checkInTab', 'flightStatusTab');
var widgetIdArray = new Array('bookFlightWidget', 'myTripsWidget', 'checkInWidget', 'flightStatusWidget');
var offerIdArray = new Array('offer1', 'offer2', 'offer3');
var currentOfferIndex = 0;
var selectedTab;
       
function showSubNav(subNavId) {
    var subNav = document.getElementById(subNavId);
    if(subNav)
        subNav.style.display = 'block';
}

function hideSubNav(subNavId) {
    var subNav = document.getElementById(subNavId);
    if(subNav)
        subNav.style.display = 'none';
}

function tabClick(tab, index) {
    if(tab.className.indexOf(tabSelectedClassName) == -1)
    {
        unselectAllTabs();
        hideAllWidgets();
        selectTab(index)
        showWidget(index);
        tab.className = tab.className + tabSelectedClassName;
    }
}
        
function unselectAllTabs() {
    for(var i = 0; i<tabIdArray.length; i++)
    {
        var tab = document.getElementById(tabIdArray[i]);
        if(tab)
            tab.className = tab.className.replace(tabSelectedClassName,'');
    }                        
}

function hideAllWidgets() {
    for(var i = 0; i<widgetIdArray.length; i++)
    {
        var widget = document.getElementById(widgetIdArray[i]);
        if(widget)
            widget.style.display = 'none';
    }          
}

function selectTab(tabIndex) {
    var tab = document.getElementById(widgetIdArray[tabIndex]);
    if(selectedTab)
        selectedTab.className = selectedTab.className.replace(tabSelectedClassName, "");
    if(tab)
    {
        selectedTab = tab;
        tab.className = tab.className + tabSelectedClassName;
    }
}

function showWidget(widgetIndex) {
    var widget = document.getElementById(widgetIdArray[widgetIndex]);
    if(widget)
        widget.style.display = 'block';
}
       
function toggleKids() {
    var kidsLink = document.getElementById('kids');
    var kidPassengers = document.getElementById('kidPassengers');
    var infantPassengers = document.getElementById('infantPassengers');
    
    if(kidsLink && kidPassengers && infantPassengers)
    {
        kidsLink.style.display = 'none';
        kidPassengers.style.display = 'block';
        infantPassengers.style.display = 'block';
    }
}
        
function togglePromotion() {
    var promotionCodeWrapper = document.getElementById('promotionCodeWrapper');
    promotionCodeWrapper.style.display = 'none';
    var promotionForm = document.getElementById('promotion');
    promotionForm.style.display = 'block';
}

function scrollOffers(direction) {
    if(direction > 0 && offerIdArray[currentOfferIndex + 1])
    {
        currentOfferIndex++;
    }
    
    if(direction < 0)
    {
        if(currentOfferIndex > 0 && offerIdArray[currentOfferIndex - 1])
        {
            currentOfferIndex--;
        }
    }
    
    displayOffer();
    updateOfferCount();
}

function displayOffer() {

    for(var i = 0; i<offerIdArray.length; i++)
    {
        var offer = document.getElementById(offerIdArray[i]);
        if(offer)
            offer.style.display = 'none';
    }
    var offer = document.getElementById(offerIdArray[currentOfferIndex]);
    if(offer)
            offer.style.display = 'block';
}

function updateOfferCount() {
    var offerCount = document.getElementById('offerCount');
    if(offerCount)
    {
        offerCount.innerHTML = currentOfferIndex + 1 + ' of ' + offerIdArray.length;
    }
} 

function showCitiesPopup(textBox) {
    document.getElementById('citiesPopup').style.display = '';
    var posArray = findPos(textBox);
    //alert('textBox.offsetTop = ' + textBox.offsetTop + ', textBox.offsetHeight = ' + textBox.offsetHeight);
    //alert('textBox.X = ' + textBox.X + ', textBox.Y = ' + textBox.Y);
    document.getElementById('citiesPopup').style.left = (posArray[0]) + 'px';
    document.getElementById('citiesPopup').style.top = (posArray[1] + textBox.offsetHeight) + 'px';
} 

function hideCitiesPopup() {
    document.getElementById('citiesPopup').style.display = 'none';
}

function showLanguagePopup() {
    $find('languagePopupMPE').show(); 
} 

function hideLanguagePopup() {
    $find('languagePopupMPE').hide(); 
}

function showLoginPopup() {
    $find('loginPopupMPE').show(); 
} 

function hideLoginPopup() {
    $find('loginPopupMPE').hide(); 
}

function hideLeaveDateCalendarExtender() {
    var cal = $find('leaveDateCalendarExtender');
    if(cal._isOpen == true)
    {
        cal.hide();
    } 
} 

function hideReturnDateCalendarExtender() {
    var cal = $find('returnDateCalendarExtender');
    if(cal._isOpen == true)
    {
        cal.hide();
    }
}

function hideSevereWeatherNotification() {
    $get('severeWeatherNotification').style.display = 'none';
    $get('severeWeatherNotification').style.visibility = 'hidden';
}

 /* js for switching of views */
function showMyTripsView1() {
    document.getElementById('myTripsView1').style.display = 'block';
    document.getElementById('myTripsView2').style.display = 'none';
}

function showMyTripsView2() {
    document.getElementById('myTripsView1').style.display = 'none';
    document.getElementById('myTripsView2').style.display = 'block';
}

function setupNavigation() {
    if (document.getElementById) {
        navRoot = document.getElementById("navigation");
        
        for (i=0; i < navRoot.childNodes.length; i++) {
	        node = navRoot.childNodes[i];
	        
	        if (node.nodeName == "LI") {
		        
		        node.onmouseover = function() {
			        this.className += " over";
			        var previousLI = getPreviousLI(this);
	                if(previousLI)
	                {
                        previousLI.className += " previousOver";
	                }
		        }
		        node.onmouseout = function() {
		            this.className = this.className.replace("over", "");
		            var previousLI = getPreviousLI(this);
	                if(previousLI)
	                {
		                previousLI.className = previousLI.className.replace("previousOver", "");
	                }
	            }
	            
	            for(j=0; j < node.childNodes.length; j++)
	            {
	                if(node.childNodes[j].nodeName == "UL")
	                {
	                    node.childNodes[j].onmouseover = function() {
	                        this.parentNode.className += " over";
	                    }
	                    node.childNodes[j].onmouseout = function() {
	                        this.parentNode.className = this.parentNode.className.replace("over", "");
	                    }
	                }
	            }
            }
        }
    }
}

function getPreviousLI(node) {
    var x = node.previousSibling;
    if(x)
    {
        if(x.nodeName == "LI")
            return x;
        else
            return getPreviousLI(x);
    }
}