function randOrd(){
    return (Math.round(Math.random())-0.5); 
} 

$(document).ready(function(){
    /*instanciating jQuery just in case;*/
    $ = jQuery;
    
    if ($("#module-article") != null) {
        $("img[align='right']").addClass("frpl").removeAttr("align");
        $("img[align='left']").addClass("flpr").removeAttr("align");
    }

	/* secondary nav--automatically loads the right secondary nav. dependent on body id */
	/* tr: override the body id method and do it conditionally by page. */
	/* instead, grab the URL, figure out what page you're on, and assign an id based on those conditions.  */
	try {
	    /* if this isn't here, menus will not work in IE6 */
	    $("ul.sf-menu").superfish(); 
    }
    catch(e){
        /*alert("jq fail - " + e);*/
    }	
	
	var currentURLPath = location.pathname;
	var lookupURL = currentURLPath.split("/")[1];
		
	var menuTabNews = document.getElementById("nav-news-secondary-horizontal").innerHTML;
	var menuTabTopics = document.getElementById("nav-topics-secondary-horizontal").innerHTML;
	var menuTabDestinations = document.getElementById("nav-destinations-secondary-horizontal").innerHTML;
	var menuTabPartner = document.getElementById("nav-partner-secondary-horizontal").innerHTML;
	var menuTabAwards = document.getElementById("nav-awards-secondary-horizontal").innerHTML;
	var menuTabEvents = document.getElementById("nav-events-secondary-horizontal").innerHTML;
    
    
    /*this is like this since jquery wasn't working on the article page, but now since it works, we can get rid of it later and only use the jQuery calls.*/
	if ((currentURLPath.indexOf("/home.aspx") >= 0) || (lookupURL == ""))
	{
	    try {
	        $("body").attr("id", "home");
        }
        catch (e) {
            document.getElementsByTagName("body")[0].setAttribute("id","home");
        }
	}	
	else if ((menuTabNews.indexOf(lookupURL) >= 0) || (lookupURL.indexOf("newsindex.aspx") >= 0) || (lookupURL.indexOf("industry-news-categories.aspx") >= 0)) {
	    try {
	       if ($("body") == null) {
                throw "NoJQ";
           }
           else {
                $("body").attr("id", "news")
           }
	    }
        catch (e) {
            if (e == "NoJQ") {
                document.getElementsByTagName("body")[0].setAttribute("id","news");
            }            
        }
	}
	else if (menuTabTopics.indexOf(lookupURL) >= 0) {
	    try {
	       if ($("body") == null) {
                throw "NoJQ";
           }
           else {
                $("body").attr("id", "topics")
           }
	    }
        catch (e) {
            if (e == "NoJQ") {
                document.getElementsByTagName("body")[0].setAttribute("id","topics");
            }            
        }
	}
	else if ((menuTabDestinations.indexOf(lookupURL) >= 0) || (currentURLPath.indexOf("destinations.aspx") >= 0)) {
	    //$("body").attr("id", "destinations");
        try {
	       if ($("body") == null) {
                throw "NoJQ";
           }
           else {
                $("body").attr("id", "destinations")
           }
	    }
        catch (e) {
            if (e == "NoJQ") {
                document.getElementsByTagName("body")[0].setAttribute("id","destinations");
            }            
        }
       
	}
	else if ((menuTabPartner.indexOf(lookupURL) >= 0) || (currentURLPath.indexOf("partner.aspx") >= 0) || (currentURLPath.indexOf("partner.aspx") >= 0) || (currentURLPath.indexOf("partnerindex.aspx") >= 0)) {
	    $("body").attr("id", "partner");
	}
    else if (menuTabAwards.indexOf(lookupURL) >= 0) {
	    $("body").attr("id", "awards");
	}
	else if ((menuTabEvents.indexOf(lookupURL) >= 0) || (currentURLPath.indexOf("meetings-conventions-events.aspx") >= 0)) {
	    $("body").attr("id", "events");
	}
	else if (currentURLPath.indexOf("article") >= 0) {	    
	    try {
	       if ($("body") == null) {
                throw "NoJQ";
           }
           else {
                $("body").attr("id", "news")
           }
	    }
        catch (e) {
            if (e == "NoJQ") {
                document.getElementsByTagName("body")[0].setAttribute("id","news");
            }            
        }
	}
	var currentPage = "";	
		
	if (document.getElementsByTagName("body")[0].getAttribute("id") != null) {
	    var getID = document.getElementsByTagName("body")[0].getAttribute("id");		
	    currentPage = "nav-" + getID + "-secondary-horizontal";
	}		
	
	try {
	    $("ul#" + currentPage).show();
	}
	catch(e) {	    
	    if ((currentPage != "") && (document.getElementById(currentPage.toString()) != null)) {
	        document.getElementById(currentPage.toString()).style.display = "block";
	    }
	}

	/* secondary nav bg */
	try {
	    var getSecondaryNavID = $("div.container").attr("id");
	    var currentSecondaryNav = "#" + getSecondaryNavID;
	    $("#nav").find(currentSecondaryNav).addClass("current");
	
	try {
	    if (lookupURL == "newsindex.aspx")
	    {
	        $("#primary").css("border-right", "1px solid #ccc");
	    }	    	
	} 
	catch(e){
	}
	
	//add the frugal planner icon
	if (lookupURL.indexOf("frugal") >= 0) {
        $("#page-section-title").css("background", "#fff url(/images/frugal-icon.gif) no-repeat top right");
        $("#page-section-title").height("48px");
	}
	
	
	//randomize content order on partner content
	if (lookupURL.indexOf("partner") >= 0) {
	
	    var partnerModules = new Array();
	    var partnerCount = 0;
	    partnerCount = $("#primary .module").length;
	    
	    //add the dom objects to array
	    for(px = 0;px<partnerCount;px++){
	        partnerModules[px] = $("#primary .module:eq(" + px + ")");
	    }
	    
	    //randomize the partnerModules array
	    partnerModules.sort(randOrd);
	    
	    
	    //alert($("#module-destination-spotlight"));	    
	}
	
	
	
	/* greybox (overlay window) */
	
        $("a.greybox-tall").click(function(){
    	    var t = this.title || $(this).text() || this.href;
    	    GB_show(t,this.href,660,980);
    	    return false;
        });

        $("a.greybox").click(function(){            
    	    var t = this.title || $(this).text() || this.href;
    	    GB_show(t,this.href,440,980);
    	    $("#GB_window").css({top:200+"px"});
    	    return false;
        });


	/* sliders */
        $("#slider-mc-destination-guides").easySlider({
    	    prevId:"button-prev-mc-destination-guides",
    	    nextId:"button-next-mc-destination-guides"	
        });

        $("#slider-mc-destination-guides-wide").easySlider({
    	    prevId:"button-prev-mc-destination-guides-wide",
    	    nextId:"button-next-mc-destination-guides-wide"	
        });

        $("#slider-mc-city-maps").easySlider({
    	    prevId:"button-prev-mc-city-maps",
    	    nextId:"button-next-mc-city-maps"	
        });

        $("#slider-upcoming-events").easySlider({
    	    prevId:"button-prev-upcoming-events",
    	    nextId:"button-next-upcoming-events"	
        });

        $("#slider-video-more").easySlider({
    	    prevId:"button-prev-video-more",
    	    nextId:"button-next-video-more"	
        });

        $("#slider-video-library-videos").easySlider({
		    numeric:true,
		    controlsShow:true,
		    continuous:false, 
		    numericId:"controls-01",
		    prevId:"button-prev-videos",
		    nextId:"button-next-videos"	
        });

        $("#slider-video-library-slideshows").easySlider({
		    numeric:true,
		    controlsShow:true,
		    continuous:false, 
		    numericId:"controls-02",
		    prevId:"button-prev-slideshows",
		    nextId:"button-next-slideshows"	
        });

        $("#slider-video-library-webcasts").easySlider({
		    numeric:true,
		    controlsShow:true,
		    continuous:false, 
		    numericId:"controls-03",
		    prevId:"button-prev-webcasts",
		    nextId:"button-next-webcasts"	
        });

        $("#slider-video-library-podcasts").easySlider({
		    numeric:true,
		    controlsShow:true,
		    continuous:false, 
		    numericId:"controls-04",
		    prevId:"button-prev-podcasts",
		    nextId:"button-next-podcasts"	
        });
	
	/* tabs */

    var tabContainers01 = $(".tab-container-most");
    
    $("#module-most .tab-nav-most a").click(function () {
        tabContainers01.hide().filter(this.hash).show();
        
        $("#module-most .tab-nav-most a").removeClass("selected");
        $(this).addClass("selected");
        
        return false;
    }).filter(":first").click();


    var tabContainers02 = $(".tab-container-video-library");
    
    $("#module-video-library .tab-nav-video-library a").click(function () {
        tabContainers02.hide().filter(this.hash).show();
        
        $("#module-video-library .tab-nav-video-library a").removeClass("selected");
        $(this).addClass("selected");
        
        return false;
    }).filter(":first").click();

	/* destinations popup */
	
    $("#destinations-north-america").click(function() {
    	$("#destinations-container div").siblings().hide();
    	$("#destinations-north-america-list").toggle();
		return false;
    });
    $("#destinations-latin-america").click(function() {
    	$("#destinations-container div").siblings().hide();
    	$("#destinations-latin-america-list").toggle();
		return false;
    });
    $("#destinations-europe").click(function() {
    	$("#destinations-container div").siblings().hide();
    	$("#destinations-europe-list").toggle();
		return false;
    });
    $("#destinations-asia").click(function() {
    	$("#destinations-container div").siblings().hide();
    	$("#destinations-asia-list").toggle();
		return false;
    });
    $("#destinations-africa").click(function() {
     	$("#destinations-container div").siblings().hide();
	   	$("#destinations-africa-list").toggle();
		return false;
    });
    
    $("body").click(function() {
	    $(".destinations-list").fadeOut("100");
    });



	/* change font size */
	
	$(".style-s").click(function() {
		$("#primary p").css({"font-size":"11px","line-height":"15px"});
		return false;
	});
	$(".style-m").click(function() {
		$("#primary p").css({"font-size":"13px","line-height":"18px"});
		return false;
	});
	$(".style-l").click(function() {
		$("#primary p").css({"font-size":"16px","line-height":"22px"});
		return false;
	});



	/* count items */
	//$("table.library-items tr td:last").css({backgroundColor: 'yellow', fontWeight: 'bolder'});
	
	//var totalVideo = $("#slider-video-library-videos img").size();
	//$("#library-videos-count-total").html(totalVideo);
	//
	//var currentPageVideo = $("#videos .current").attr("id").slice(11,12);
	//var totalPagesVideo = $("#videos ol li").size();	
	//
	//$("#library-videos-count").html(currentPageVideo);
	




  }
  catch (e) {
    //alert(e.description);
    }


});	

$(window).load(function(){
	$('div.ad').each(function(){
		ad_height = $($(this).find('div').get(0)).height();
		$(this).css('height',(ad_height==100)?ad_height:'');
		$(this).find('div').css('position','absolute');				
	});
});


$(window).load(function(){
ad_004 = $('#ad_004');
ad_004_div = ad_004.find('div');
if (ad_004_div.length){
ad_004.height(ad_004_div[0].style.height);
}
});

