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);
