var travelport = window.travelport || {};
travelport.web=travelport.web || {}
travelport.util={
    //utility functions
}

travelport.web.common={
    init:function(){
                
    var CMN=travelport.web.common;
    CMN._mainNavEffects();
    CMN._homeTips();
    CMN._sizeRequirements();
    CMN.industrySelect();
    CMN.businessSelect();
    CMN._sizeSeperator();
    CMN._fixHeight();
    CMN._prepButtons();
    CMN._cleanLIs();
    CMN._selectTab();
},

//private methods
    
    _mainNavEffects:function(){ //Rollover effects for main nav and drop nav	    
        if($("body").hasClass('regional')) {
            var dropItems = $("#nav_main li ul.nav_drop")
            for(i=0; i<dropItems.length; i++) {
                var thisDropItem = $(dropItems[i]);
                var w = thisDropItem.parents("li").width()
                thisDropItem.width(w);
            }
        }
        $("#nav_main>li").hover(		                        
	        function(){ 
                var idx = $(this).parent().children().index(this);
                var bgPos = (((-36) * idx) - 36) + "px";
                $(this).parent().css("background-position", "0 " + bgPos );                
	            $(">ul", this).slideDown("fast");
	        }, 
	        function() {
	            $(">ul", this).slideUp(100); 
	            $(this).parent().css("background-position","0 0");
	        } 	        
        );	    	 	    	    	    	    
    },
    _homeTips:function(){ //Tooltip like elements on the homepage and category pages
        var maxHeight = 0;
    
        //Get the Maximum rendered tooltip so they are all sized uniformly                
        $('#nav_sub li div.content').each( function() {
                    var elementTotalHeight = $('p',this).height() + $('h1',this).height()
                    if ( elementTotalHeight > maxHeight )                    
                        maxHeight = elementTotalHeight;
                 } );
        
        //The additional 25 pixels is take into account the padding for the header and the paragraph
        $('#nav_sub li div div.content').css({"height": ( maxHeight + 25 )+ "px"});            
    
	    $("#nav_sub li a").mouseover(function(){
	        var container = $(this);
                        	        	        		        
	        $('>div', $(this).parent() ).each( function() {
	            $(this).css( { "left": ( container.position().left + 9 ) + "px", 
	                           "top": "-" +  ( $(this).height() - 3 ) + "px", 
	                           "visibility" : "visible" });
	            $('div',this).css( {"visibility" : "visible" });
	            
	            
	        } );		        
		    
	    });

	    $("#nav_sub li a").mouseout(function(){
		    $("#nav_sub li  div").css("visibility", "hidden");
	    });

        maxHeight = 0;
        $('#spotlight li div.content').each( function() {
                    var elementTotalHeight = $('p',this).height() + $('h1',this).height()
                    if ( elementTotalHeight > maxHeight )
                        maxHeight = elementTotalHeight;
                 } );
        $('#spotlight li div div.content').css({"height": ( maxHeight + 25 )+ "px"});
                        	    	    
	    $("#spotlight li a").mouseover(function(){
	        var spotlight = $(this); 
	        
            $('>div', $(this).parent() ).each( function() {                
	            $(this).css( { "left": spotlight.position().left - 3 +( ( spotlight.width() - $(this).width() ) / 2 )+ "px", 
	                           "top": "-" + ( $(this).height() - spotlight.height() - 23 ) + "px", 
	                           "visibility" : "visible" });
	            $('div',this).css( {"visibility" : "visible" });
            } );
            
	    });
	    	    
	     $("#spotlight li a").mouseout(function(){
                $("#spotlight li div").css("visibility", "hidden");
            } );

    },

    _sizeRequirements:function() { //Turn Off Drop Shadow if browser width gets too small,otherwise there will be a scrollbar at 1024x768
        var isException = false;
                    
        function optimizeDimensions( force ) {
            var container = $('#page_container');
                            
            if ( container.height() < $(window).height() ) {
                container.css("height",$(window).height() );
            } 
            if ( $(window).width() < ( container.width() + 18 ) ) {
                if ( force ) {
                    container.css("padding","0");
                } else {
                    $('#page_container').animate( { "paddingLeft" : "0px", "paddingRight": "0px" }, { "duration": 300, "queue" : "false" }, function() { container.css("padding","0 0"); } ); 
                }
            } else {                
                container.css("padding","0 9px");
            }                
        }
        
        //This is to avoid background distraction from the content in the third column if one exists
        if ( $('#features') != null ) {
            if ( ( $('#content_main').height() - $('#features').height() ) < 130 ) {
                $('#features').css("height", ( $('#features').height() + 130) + "px" );
            }
        }
        
        optimizeDimensions( true );
        
        $(window).resize( function() { optimizeDimensions( false ); } );
    },
    industrySelect:function(){	
	    $("#industryLinkBtn").click(function(e){
	        e.target.preventDefault;	        
	        var thisSelect = document.getElementById("industryLink");
	        document.location=thisSelect.value;
	    });
    },
    businessSelect:function(){	
	    $("#businessLinkBtn").click(function(e){
	        e.target.preventDefault;
	        var thisSelect = document.getElementById("businessLink");
	        document.location=thisSelect.value;
	    });
	    
    },
    _sizeSeperator:function(){
        var height = Math.max( $('#highlights #newsroom').height(),$('#highlights #facts').height());        
        height = Math.max( $('#highlights #categorynewsroom').height(),height);

        height = height - 80;        
        if ( height < 0 ) {
            height = 0;
        }        
        $('.vertical-seperator').html( "<div class='top'></div><div class='spacer'></div><div class='bottom'></div>");
        $('.vertical-seperator .spacer').css( {'height':height+'px'});
    },
    _fixHeight:function() {
        var content = $("#content");
        var contentH = content.height();
        if(contentH <= 430) {
            content.height(500);
        }
    },
    _prepButtons:function() {
        $('a.btn_go').each(function(tag) {
            if(tag && typeof(tag.click == 'undefined')) {
                tag.click = function() {
                    var result = true;
                    if(tag.onclick) result = tag.onclick();
                    if(typeof(result) == 'undefined' || result) {
                        alert('enter');
                        eval(tag.href);
                    }
                }
            }
        });
    },
    _prepCarousel:function() {
        jQuery('#item-carousel').jcarousel({
            vertical: true,
            scroll: 1
            });
        },
    _cleanLIs:function() {
        var aLists = $(".three_col #content_main ul.related");
        for(i=0; i<aLists.length; i++) {
            var thisList = aLists[i];
            var theseLI = $(thisList).children("li:even");
            $(theseLI).css("clear","left");
        }
        var lis = $("#language_search .language_opts ul li");
        var w = 0;
        for(i=0; i<lis.length; i++) {
            var thisLi = $(lis[i]);
            w = w + 20 + thisLi.width();
        }
        var wUl = w + 170;
        $("#language_search").width(wUl);
        $("#language_search .language_opts ul").width(w);
        $("#language_search").css("visibility","visible");
        $("#content_main .langLinks li:last").css({"border":"none", "padding":"0 0 0 10px"});
    },
    _selectTab:function() {
        if($(".nav_tabs").length > 0) {
            var tabIdx = document.location.hash.replace("#","");
            $(".nav_tabs").tabs("select",tabIdx);
            var subNav = $("#nav_main li .nav_drop li a");
            subNav.click(function(e) {
                subNavURL = e.target.href;
                var tabs = $(".nav_tabs li a");
                for(i=0; i<tabs.length; i++) {
                    if(subNavURL.indexOf(tabs[i]) != -1) {
                        e.preventDefault();
                        var tabURL = $(tabs[i]).attr("href");
                        tabURL = tabURL.replace("#","");
                        $(".nav_tabs").tabs("select",tabURL);
                    }
                }
            });
        }
    }
}

$(document).ready(travelport.web.common.init);


/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var loadedobjects=""
var rootdomain="http://"+window.location.hostname

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
page_request.open('GET', url, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}


/***********************************************
* Advanced Gallery script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice must stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var tickspeed=30000 //ticker speed in miliseconds (2000=2 seconds)
var displaymode="auto" //displaymode ("auto" or "manual"). No need to modify as form at the bottom will control it, unless you wish to remove form.

if (document.getElementById){
document.write('<style type="text/css">\n')
document.write('.gallerycontent{display:none;}\n')
document.write('</style>\n')
}

var selectedDiv=0
var totalDivs=0

function getElementbyClass(classname){
partscollect=new Array()
var inc=0
var alltags=document.all? document.all.tags("DIV") : document.getElementsByTagName("*")
for (i=0; i<alltags.length; i++){
if (alltags[i].className==classname)
partscollect[inc++]=alltags[i]
}
}

function contractall(){
var inc=0
while (partscollect[inc]){
partscollect[inc].style.display="none"
inc++
}
}

function expandone(){
var selectedDivObj=partscollect[selectedDiv]
contractall()
selectedDivObj.style.display="block"
if (document.gallerycontrol)
temp.options[selectedDiv].selected=true
selectedDiv=(selectedDiv<totalDivs-1)? selectedDiv+1 : 0
if (displaymode=="auto")
autocontrolvar=setTimeout("expandone()",tickspeed)
}

function populatemenu(){
temp=document.gallerycontrol.menu
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<totalDivs;i++){
var thesubject=partscollect[i].getAttribute("subject")
thesubject=(thesubject=="" || thesubject==null)? "HTML Content "+(i+1) : thesubject
temp.options[i]=new Option(thesubject,"")
}
temp.options[0].selected=true
}

function manualcontrol(menuobj){
if (displaymode=="manual"){
selectedDiv=menuobj
expandone()
}
}

function preparemode(themode){
displaymode=themode
if (typeof autocontrolvar!="undefined")
clearTimeout(autocontrolvar)
if (themode=="auto"){
document.gallerycontrol.menu.disabled=true
autocontrolvar=setTimeout("expandone()",tickspeed)
}
else
document.gallerycontrol.menu.disabled=false
}


function startgallery(){
if (document.getElementById("controldiv")) //if it exists
document.getElementById("controldiv").style.display="block"
getElementbyClass("gallerycontent")
totalDivs=partscollect.length
if (document.gallerycontrol){
populatemenu()
if (document.gallerycontrol.mode){
for (i=0; i<document.gallerycontrol.mode.length; i++){
if (document.gallerycontrol.mode[i].checked)
displaymode=document.gallerycontrol.mode[i].value
}
}
}
if (displaymode=="auto" && document.gallerycontrol)
document.gallerycontrol.menu.disabled=true
expandone()
}

if (window.addEventListener)
window.addEventListener("load", startgallery, false)
else if (window.attachEvent)
window.attachEvent("onload", startgallery)
else if (document.getElementById)
window.onload=startgallery

