﻿jQuery.noConflict();                                       

/**
 * Called on css/js loaded
 * Things that cannot be called on DOM-ready that requires the JS and CSS to be ready
 */
jQuery(window).load(function() {
	
	TRGlobal.setupImageCaptions();
	
	//CSS initially puts the navigation off the page to do the rendering
	//we use jquery to calculate all the box sizes for shadows
	//at the very end, we place the nav where it belongs
	TRGlobal.setupTopNavigation();
	    
   
	TRGlobal.setupLeftNavigation();
	 
    
    //calculate first tab's arrow placement
    var tabFirstWidth = jQuery(".tabs li:first a span").width();
    if(tabFirstWidth > 0) {
	    var halfPoint = tabFirstWidth/2 - 4;
	    jQuery(".tabs li.first").css("background-position", ""+halfPoint+"px 19px");
    }
    
    // movie = the swf path and name
	// width = width of the swf
	// height = height of the swf
	// version = the compiled flash version of the movie
	// flashvars = query string passed to swf movie
	// params = param tags embedded into object of swf movie
	// attributes = attributes added to object tag itself 
	jQuery(".flash").each(function(i){ 

		try {
			var movie = jQuery(".movie", jQuery(this)).text();
			var width = jQuery(".width", jQuery(this)).text();
			var height = jQuery(".height", jQuery(this)).text();
			var version = jQuery(".version", jQuery(this)).text();


			var flashvars = new Object();
			jQuery(".flashvars", jQuery(this)).each(function(j) {
				var flashvar = jQuery(this).text();
				var obj = flashvar.split(" ", 2);  
				flashvars[jQuery.trim(obj[0])] = obj[1]; 
			});
		flashvars["plugins"] = "gapro-1&gapro.accountid=UA-22221761-1&gapro.trackstarts=true&gapro.trackpercentage=true&gapro.tracktime=true";

			var params = new Object();
			jQuery(".params", jQuery(this)).each(function(j) {
				var param = jQuery(this).text();
				var obj = param.split(" ", 2);  
				params[jQuery.trim(obj[0])] = obj[1]; 
			});

			var attributes = new Object();
			jQuery(".attributes", jQuery(this)).each(function(j) {
				var attribute = jQuery(this).text();
				var obj = attribute.split(" ", 2);  
				attributes[jQuery.trim(obj[0])] = obj[1]; 
			});

			swfobject.embedSWF(movie, jQuery(this)[0].id, width, height, version, "", flashvars, params, attributes);
		} catch(err) {
			alert("Unable to load flash." + err);
		}
		 
	});
     
	//ALL UPPERCASE EXCEPT THE LAST ONE 
	jQuery("#breadcrumb li:last").css("text-transform", "none");

	
});
 

/**
 * Called on DOM-ready
 */
jQuery(document).ready(function() {
	 
	
	//TITLE STUFF
	//MAKE SURE ITS THE FIRST THING TO DO...
	//IE6/7 gets confused if it isnt..
	var siteTitleHeader = jQuery("#header #mini-site-header");
    var siteTitle = jQuery("#holder-l #rootItemTitle");
    var parentTitle = jQuery("#holder-l #selectedNavTitle");
    var pageTitle = jQuery("#holder-r #pageTitlePlaceholder");
    if (siteTitleHeader.length > 0) {
          siteTitleHeader.html(siteTitle.val());
    }
    if (parentTitle.length > 0 && (siteTitleHeader.length == 0 || parentTitle.val() != siteTitle.val())) {
          jQuery("#holder-r .title").html('<h1>' + parentTitle.val() + '</h1>');
    }
    
    if(pageTitle.length > 0) {
	    if (pageTitle.val() != parentTitle.val()) {
	          pageTitle.replaceWith('<h2>' + pageTitle.val() + '</h2>');
	    } else {
	          pageTitle.replaceWith(''); 
	    }
	}
    siteTitle.replaceWith('');
    parentTitle.replaceWith('');
		 
	
	
	//WINDOWS specfic CSS overrides
	//if(navigator.appVersion.indexOf("Win")!=-1) {
	if(jQuery.browser.msie) { 
		jQuery("head").append("<link>");
	    css = jQuery("head").children(":last");
	    css.attr({
	      rel:  "stylesheet",
	      type: "text/css",
	      href: "/page/css/windows/windows.css?rand=" + new Date().getTime()
	    }); 
	    
	    //if japanese IE
	    if(window.location.href.indexOf("jp") >= 0 ) {
	    	jQuery("head").append("<link>");
		    css = jQuery("head").children(":last");
		    css.attr({
		      rel:  "stylesheet",
		      type: "text/css",
		      href: "/page/css/windows/ie-japanese.css"
		    }); 
	    }
	    
	    
	    /*
	    var deviceAgent = navigator.userAgent.toLowerCase(); 
	    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
 
	    if (agentID) { 
	    	jQuery("head").append("<link>");
		    css = jQuery("head").children(":last");
		    css.attr({
		      rel:  "stylesheet",
		      type: "text/css",
		      href: "/page/css/apple/apple.css"
		    }); 
	    }
	    */
	    
	    
	}
	
	
	try {
		
		//remove the "view=Standard" from the URL links
        jQuery("a").each(function() { 
            var displayLink = jQuery(this).html();
            var linkUrl = jQuery(this).attr("href");  
            var linkPostViewRemoved = removeParameterFromUrl(linkUrl, "view"); 
            jQuery(this).attr("href", linkPostViewRemoved);
            jQuery(this).html(displayLink);
        });

	} catch(e) {
		
	}
	
	//add back in language selector for stage/dev/local only
	var hostname = window.location.hostname;
	if(hostname.indexOf("staging.") >= 0 || hostname.indexOf("dev.") >= 0 || hostname.indexOf("local.") >= 0 || hostname.indexOf("qa.search.thomsonreuters.com") >= 0 ) {
		jQuery("#header-holder ul li.language-item").css("display", "block");

		jQuery("#header-holder ul li.contact-item").css("padding-left", "15px");
		jQuery("#header-holder ul li.contact-item").css("border-left", "#ECECEC 1px solid");
 
	} else {
		jQuery("#header-holder ul li.contact-item").css("padding-left", "0px");
		jQuery("#header-holder ul li.contact-item").css("border-left", "none");
	}
	
	
	//jQuery(document).pngFix();
	
	 
	TRGlobal.setupLightboxCarousel();

	TRGlobal.setupHeroCarousel();
	
	
	//turn off caching of ajax in jquery
    jQuery.ajaxSetup({ cache: false });
    
    TRGlobal.setupInternetExplorerFixes();

    TRGlobal.setupListStyling();
    

	 
	TRGlobal.setupDropdown();
	
	TRGlobal.setupLanguageSelector();
	

	
	  
	TRGlobal.setupTooltip();
	
	TRGlobal.setupSearch();

	 
	TRGlobal.setupLightbox();
	
	TRGlobal.setupFAQ();
	

	TRGlobal.setupFooterCarousel(); 

	
	TRGlobal.setupExternalStubMessage();
	
	jQuery(".pdf-japanese-to-english").append(" (英文) ");
	
	// center page studio dialogs when icon is clicked
	jQuery(".ps_select_component").live("click", function() {
		alert("FIRE!");
		jQuery("#ps_select_component_dialog").css("position","absolute");
		jQuery("#ps_select_component_dialog").css("top", ((jQuery(window).height() - jQuery("#ps_select_component_dialog").outerHeight()) / 2) + "px");
		jQuery("#ps_select_component_dialog").css("left", ((jQuery(window).width() - jQuery("#ps_select_component_dialog").outerWidth()) / 2) + "px");
	});
	jQuery(".ps_display_component_configuration_button").live("click", function() {
		jQuery("#ps_configure_component_dialog").css("position","absolute");
		jQuery("#ps_configure_component_dialog").css("top", ((jQuery(window).height() - jQuery("#ps_configure_component_dialog").outerHeight()) / 2) + "px");
		jQuery("#ps_configure_component_dialog").css("left", ((jQuery(window).width() - jQuery("#ps_configure_component_dialog").outerWidth()) / 2) + "px");
	});
		
});

// For the awesomeness that is IE
// For mouse over of the top level navigation
// dynamically attach a new function to jquery 
// called "hoverClass" that handles adding/removing of a class
// on mouseover/mouseout
jQuery.fn.hoverClass = function(c) {
	return this.each(function() {
		jQuery(this).hover(function() {
			jQuery(this).addClass(c);
		}, function() {
			jQuery(this).removeClass(c);
			jQuery(this).removeClass(c);
		});
	});
};


var TRGlobal = {
		/**
		 * IE specific hacks
		 */
		'setupInternetExplorerFixes' : function() {
			if (document.all) {
				jQuery("#r-cont div div:first-child div:first-child > h3:eq(0)").addClass("firstChild");
			    jQuery("#r-cont > h3:eq(0)").addClass("firstChild");
				jQuery(".tab-r-cont").each( function() { 
					jQuery(this).find("div div:first-child div:first-child > h3").eq(0).addClass("firstChild");
					jQuery(this).children("h3").eq(0).addClass("firstChild");
				});
		  
				//suckerfish alternative rollover for IE support
				jQuery("#top-nav li").hoverClass("sfHover");
				jQuery("#content #holder-l #left-navigation li").hoverClass("sfHover");			
				jQuery(".dropdown-menu li").hoverClass("sfHover");
				jQuery("#languageSelector > li").hoverClass("sfHover");
				jQuery("#l-cont-full .last_third .contact-us-column a").hoverClass("hover");
				
				//to let empty page studio layout cells collapse
				jQuery(".ps_cell").prepend("<!-- -->");
			}
		},
		
		/**
		 * Top level global navigation
		 */
		'setupTopNavigation' : function() {
			jQuery("#top-nav .selected").next().addClass("hidepipe");
			
			jQuery("#top-nav li div.list .subnav-container").each( function() {
				if (jQuery(this).find(".l-half").length == 0) {
					if (jQuery(this).find("li").length > 0) {
						jQuery(this).parent().parent().parent().addClass("has-rollover").children("a").append("<span class='rollover-arrow'></span>");
						
						jQuery(this).children("ul").eq(0).addClass("l-half");
						jQuery(this).append("<ul class='r-half'></ul>");
						
						midpoint = (jQuery(this).find("li").length / 2);
						jQuery(this).children(".r-half").append(jQuery(this).find("li").splice(midpoint + .5, jQuery(this).find("li").length));
						jQuery(this).children(".l-half").children("li").eq(midpoint - .5).addClass("last");
						jQuery(this).children(".r-half").children("li").eq(midpoint - .5).addClass("last");
						
						jQuery(this).wrapInner("<div class='nav-column-holder' />");
					} else
						jQuery(this).parent().parent().remove();
				}
			});
			
			jQuery("#top-nav li div.list").each(function(i){
				//ok, make the width, the min width of non wrapping content
				
				var h = jQuery(".shadow-border-middle", jQuery(this)).height(); 
				jQuery(this).css("height", h+9+7); 
				
//				var minimumWidth = jQuery(this).parent().width();

				//is any of the child navigation items wider than the parent item? 
//				jQuery("ul li a", this).each(function(i){ 
//					
//					//35 pixels of margin around the widths 
//					var listItemWidth = jQuery(this).width() + 35;  
//					minimumWidth = (listItemWidth > minimumWidth)? listItemWidth : minimumWidth; 
//				});
				
				var minimumWidth = 155;

				jQuery(this).css("width", (minimumWidth*2)+14+18+22+14);
				jQuery(".subnav-container", this).css("width", (minimumWidth*2)+18+22+14);
				jQuery(".nav-column-holder", this).css("width", (minimumWidth*2)+22+14);
				jQuery("ul", this).css("width", minimumWidth+14);
				
				// determine if the text has wrapped and notify css
				jQuery(this).find("ul li a").each(function() {
					if (jQuery(this).height() > 15)
						jQuery(this).addClass("two-lines");
				});
			});
			 
				
			//odd bug where it was causing the browser
			//to initially add horizontal scrollbars. 
			//fail race condition FTW
			//we hide the secondary nav initially to keep it from crying
			jQuery("#top-nav li div.list").css("display", "none");
			 
			
			//add dropshadow to last nav
			jQuery("#top-nav > li.last").hover(
				function() {
					jQuery("#top-nav > li.last .shadow-image").css("visibility","visible");
				},  
				function() {
					jQuery("#top-nav > li.last .shadow-image").css("visibility","hidden");
				}
			);
			
			// how to hide both of the separating pipes on each side of a selected
			// top level navigation item, also add the drop shadows, and centering
			// the sub navigation
			jQuery("#top-nav > li").hover(
				
				//ON TOP NAVIGATION ROLLOVER
				function() {
				// for centering navigation
			 
					var pos = jQuery("#top-nav").offset();
					var opos = jQuery(this).offset();
					
					var menuPosition = opos.left - pos.left;
					
					jQuery("#top-nav li div.list").css( {
						"left" : (-7) + "px",
						"top:45px" : 45 + "px"
					});
					
					//if theres no content in the list 
					var listSize = jQuery("div.list ul li", jQuery(this)).size();
					if(listSize < 1) {
						jQuery("div.list", jQuery(this)).remove();
					}
					
					
					
					//determine what shadows to draw on the left and right side of
					//the current rolled over nav item
					//1) if the current item has no child, the shadow
					//	cannot be drawn pass the orange line
					//2) If the current item has children, the shadow
					//	can be drawn all the way to the bottom
					//we use CSS background on the previous and next items to "draw" 
					//these shadows
					
					
					var childless = "";
					if (jQuery("a", jQuery(this)).siblings().length < 1) {
						childless = "childless-";
					}
					
					//hide/show the pipes and shadows for the top level nav
					jQuery(this).addClass(childless+"highlight");
					jQuery(this).next().addClass("hidepipe");
					 
					
					if (jQuery(this).next().hasClass("selected")) {
						jQuery(this).next().addClass(childless+"shadow-right-selected");
					} else {
						jQuery(this).next().addClass(childless+"shadow-right");
					}
					
					if(!jQuery(this).hasClass("last")) {
						if (jQuery(this).prev().hasClass("selected")) { 
								jQuery(this).prev().addClass(childless+"shadow-left-selected"); 
						} else {
							jQuery(this).prev().addClass(childless+"shadow-left");
						}
					}
					 
					
					//IE7/8 bug to display the rollover
					//removed fade-in effect because IE fails at fading in PNGs
					jQuery(this).find("div:first").show();
					//jQuery(this).find("div:first").fadeIn("fast");
				
					
					//Jquery failing at calculating size on DOM/PAGE-Ready so we 
					//redo the math here to resize it.... 
					 
					jQuery("div.list", jQuery(this)).each(function(i){ 
						
						var h = jQuery(".shadow-border-middle ul", jQuery(this)).eq(0).height();  
						jQuery(this).css("height", h+18+7+2);
						jQuery(".subnav-container", this).css("height", h+18+2);
						jQuery(".nav-column-holder", this).css("height", h+2);
					  
					 
						
						//var parentLiWidth = jQuery(this).parent().width();
						//jQuery(this).css("width", parentLiWidth+14);
						//jQuery("ul", this).css("width", parentLiWidth); 
					}); 
					 
				},
				
				//ON TOP NAVIGATION ROLLOUT
				function() {
			
					jQuery(this).removeClass("highlight");
					jQuery(this).next().removeClass("shadow-right");
					jQuery(this).next().removeClass("shadow-right-selected");
					jQuery(this).prev().removeClass("shadow-left");
					jQuery(this).prev().removeClass("shadow-left-selected");
					
					jQuery(this).removeClass("childless-highlight");
					jQuery(this).next().removeClass("childless-shadow-right");
					jQuery(this).next().removeClass("childless-shadow-right-selected");
					jQuery(this).prev().removeClass("childless-shadow-left");
					jQuery(this).prev().removeClass("childless-shadow-left-selected");
			
					if (!jQuery(this).hasClass("selected")) {
						jQuery(this).next().removeClass("hidepipe");
					}
					
					jQuery(this).find("div:first").hide();
				});
			
			 //DISPLAY THE NAVIGATION AFTER IT IS DONE RENDERING
			 jQuery("#top-nav").css("position", "relative");
			 jQuery("#top-nav").css("left", "0");
			 
			if (jQuery.browser.msie) {
				if (parseInt(jQuery.browser.version, 10) == 6) {
					jQuery("#top-nav li .rollover-arrow").css("top", "-5px");
					jQuery("#top-nav li .rollover-arrow").css("background-image", "url(/page/images/nav-rollover-arrow.jpg)");
					jQuery("#top-nav li.selected .rollover-arrow").css("background-image", "url(/page/images/nav-selected-rollover-arrow.jpg)");
					jQuery("#top-nav li.has-rollover").hover(function() {
						jQuery(this).find(".rollover-arrow").css("background-image", "url(/page/images/nav-highlighted-rollover-arrow.jpg)");
					}, function() {
						if (jQuery(this).hasClass("selected"))
							jQuery(this).find(".rollover-arrow").css("background-image", "url(/page/images/nav-selected-rollover-arrow.jpg)");
						else
							jQuery(this).find(".rollover-arrow").css("background-image", "url(/page/images/nav-rollover-arrow.jpg)");
					});
				} else
					jQuery("#top-nav li .rollover-arrow").css("top", "0px");
			}
			
		},
		
		/**
		 * The rollover caption for images
		 * This is pretty sweet, I should open source this..  
		 * @author Bao
		 */
		'setupImageCaptions' : function() {
			 
			jQuery("#content img").each(function(i){ 
				
				//get the "caption" from the alt content of an image
				var captionString = jQuery(this).attr("alt").toLowerCase();
				var idx = captionString.indexOf("caption|"); 
				 
				//if there exists the string "caption|" make it a caption on the image
				if(idx >= 0 ) {
		
					jQuery(this).hover(function() {
						var title = jQuery(this).attr("alt").substring(idx+8);
						 
						jQuery(this).after("<div id='imagecap'>"+title+"</div>");
						var opos = jQuery(this).position(); 

						jQuery("#imagecap").css("width", jQuery(this).width()-2);
						
						var y = opos.top + jQuery(this).height() - (jQuery("#imagecap").height() + 4);
						
						jQuery("#imagecap").css("top", y+2);
						jQuery("#holder-r #imagecap").css("top", y);
						jQuery("#imagecap").css("left", opos.left);
						jQuery("#imagecap").css("margin-top", jQuery(this).css("padding-top"));
						jQuery("#imagecap").css("margin-left", jQuery(this).css("margin-left"));
						
					},
					function() { 
						var t = setTimeout('jQuery("#imagecap").remove()', 10);
						jQuery("#imagecap").hover(function() {
							clearTimeout(t);
						},
						function() {
							jQuery("#imagecap").remove();
						});
					});
				}
			});
			
		},
		
		/**
		 * Left navigation
		 */
		'setupLeftNavigation' : function() {
			
			//if the rollover has only 1 column, just let it be as wide as its content
			var leftNavHoverLength = jQuery("#left-navigation li a ~ div.shadow ul").size();
		 	if(leftNavHoverLength == 1) {
		 		jQuery("#left-navigation li a ~ div.shadow ul li").css("width", "auto");
		 	}
			
			
			//rollover menu
		 	jQuery("#left-navigation li div.rollover-content").each(function(i){
				var totalLeftNavWidth = 0;
				jQuery("ul", jQuery(this)).each(function(i) {
					totalLeftNavWidth += jQuery(this).width() + 10;		
				});
				jQuery(this).css("width", totalLeftNavWidth);
			});
		 	
		 	
		
		 	
		 
		 	
		 	if(jQuery("#left-navigation li a ~ div.shadow ul li").size() < 1) {
		 		jQuery("#left-navigation li a ~ div.shadow").remove();
		 	}
		 	
			jQuery("#left-navigation li a ~ div.shadow").prev().parent().hoverClass("rollover-highlight");	
			jQuery("#left-navigation li a ~ div.shadow").prev().parent().css("padding-right","10px");
			
			//have the last selected item always be orange
			jQuery("#left-navigation li.selected:last > a").css("color", "#FF8100");
			
			//#left-navigation li:hover div.shadow, #left-navigation li.sfHover div.shadow 
		 	jQuery("#left-navigation > li:first-child").hover(
		 		function() { 
		 		 	var leftTitleOpos = jQuery("#holder-l .title").offset(); 
		 		 	var leftTitleHeight = jQuery("#holder-l .title").height();
		 		 	var yShadow = leftTitleOpos.top + leftTitleHeight + 24;
		 		 	
		 			jQuery("div.shadow", jQuery(this)).css("top",yShadow+"px");
			 	}, 
			 	function() {
			 		
			 	}
			 );
		 	
		 	
		 	
		 	
		 	
			
		
		}, 
		
		/**
		 * Dynmaic class attachment for lists styling
		 */
		'setupListStyling' : function() {
			jQuery("#top-nav li:first-child").addClass("first");
			//jQuery("#top-nav li:last-child").addClass("last");
			jQuery("#breadcrumb ul li:first-child").addClass("first");
			jQuery("#top-nav li ul li:last-child").removeClass("last");
			jQuery("#languageSelector ul li:first-child").addClass("first");
			
			
			
			jQuery("#footer #footer-carousel-list li:first-child").addClass("first");
			
			jQuery("#footer #footer-navigation ul li:first-child").addClass("first");
			jQuery("#footer #footer-navigation ul li:last-child").addClass("last");
			
			jQuery(".tabs li:first-child").addClass("first");

			jQuery("#content .meta li:first-child").addClass("first");
			
			jQuery("#content #holder-l #left-navigation li div.rollover-content li:first-child").addClass("first");
			jQuery("#content #holder-l #left-navigation li div.rollover-content ul:first-child").addClass("first");

			
			jQuery(".product-list-two-col .product:nth-child(odd)").addClass("odd");
			jQuery(".product-list-two-col .product:nth-child(even)").after("<div class='clear'></div>");
			
			 
			if(jQuery("#left-navigation > li").size() < 1) {
				jQuery("#left-navigation").css({"display":"none"});
				return;
			}
			
			
		}, 
		
		/**
		 * Init for hero carousel for home page and BU pages
		 */
		'setupHeroCarousel' : function() {
			//HACK!!!!
			//make sure we move the title above the carousel
			//Its easier to account for it in the javascript than the jsp itself
			var next = jQuery("#holder-r #hero-carousel-container").next();
			
			if(next.attr("class") == "title") {
				var content = next.text();
				next.remove();
				jQuery("#holder-r #hero-carousel-container").before("<div class='title'><h1>"+content+"</h1></div>");
				
			}
			 
			//account for zero...
			//dont display anything, and dont initialize jquery carousel
			if(jQuery("#hero-carousel li").size() < 1) {
				jQuery("#hero-carousel-container").css({"display":"none"});
				return;
			}
			
			
			// hero carousel
			jQuery('#hero-carousel').jcarousel( {
				wrap : 'both',
				initCallback : initHeroHandler,
				scroll : 1, 
				itemVisibleInCallback : visibleHeroHandler,
				buttonNextHTML : null,
				buttonPrevHTML : null
			});
			
			
			var visibleCarouselThumbs = 3;
			if(jQuery("#hero-carousel li").size() < 3) {
				visibleCarouselThumbs = jQuery("#hero-carousel li").size();
			}

			// associated thumbnail carousel for hero
			jQuery('#hero-thumb-carousel').jcarousel( {
				scroll : 1,
				initCallback : initHeroThumbHandler,
				itemVisibleInCallback : visibleHeroThumbHandler, 
				buttonNextHTML : null,
				buttonPrevHTML : null,
				visible : visibleCarouselThumbs
			});
			 
			//if we have two slides
			if(visibleCarouselThumbs == 2 ){
				//jQuery('.jcarousel-control').css({"padding-left":"20px"});
				jQuery('#hero-carousel-container.home-carousel .jcarousel-control .thumb-slide').css({"width":"200px"});
				jQuery('.home-carousel #hero-thumb-carousel').css({"width":"200px"});
				jQuery('.home-carousel .jcarousel-control .jcarousel-item').css({"width":"90px"});
				
				//hide the left button by default
				jQuery('.jcarousel-control .hero-right-btn').addClass("disabled");	
			}
			 

			//if we have 1 slide
			//hide the control and make the content take up the full height 
			if(visibleCarouselThumbs < 2) {
				jQuery('.jcarousel-control').css({"top":"-1000px"});
				jQuery("#hero-carousel-container #hero-carousel .hero-content").css({"height":"268px"}); 
				jQuery("#hero-carousel-container.home-carousel #hero-carousel .hero-content").css({"height":"378px"}); 
			}
			
			//hide the left button by default
			jQuery('.jcarousel-control .hero-left-btn').addClass("disabled");	
		},
		
		'setupDropdown' : function() {
			//dangerous operation assuming HTML structure is valid for the dropdown menu class
		    //calls a function based on the name of the id of the UL
		    /* EXAMPLE */
		    /*
				<ul class="dropdown-menu" id="monthSelector">
					<li class="head-link"> <span>Jan</span> 
						<ul>
							<li><a href="#">Feb</a></li>
							<li><a href="#">Mar</a></li>
							<li><a href="#">Apr</a></li>
						</ul>
					</li>
				</ul>
		    */
			jQuery(".dropdown-menu a").click(function () {

				try {

					//get the span to change the text
					var parentSpan = jQuery(this).parent().parent().parent().children("span");

					//get the id of the UL to call a function
					var parentId = jQuery(this).parent().parent().parent().parent();
		 
					//change the span to the selected text
					parentSpan.get(0).innerHTML = jQuery(this).html();

					//call a function based off of the id, and pass in the selected text as the parameter
					try {
						if(parentId.get(0).id) {
							eval(parentId.get(0).id + "('" + jQuery(this).html() + "', '"+jQuery(this).attr("rel")+"');");
						}
					} catch(e) {
						
					}
					
				} catch(err) {

				}
				
				//hide after clicked
				jQuery(".dropdown-menu ul").addClass("clicked");
				
				return false;
				
				
			});
			
			jQuery(".dropdown-menu").hover(function() {
				
			}, 
			function() {
				jQuery(".dropdown-menu ul").removeClass("clicked");
			});
		},
		
		/**
		 * The language selector dropdown on the global header
		 */
		'setupLanguageSelector' : function() {
			
			jQuery(".de-DE").hide();
			jQuery(".fr-FR").hide();
			
		 
		},
		
		/**
		 * The lightbox slideshow carousel
		 */
		'setupLightboxCarousel' : function() {
			jQuery('.lightbox-carousel-box ul li a[rel="slide"]').colorbox({photo:true});
			
			if(jQuery("'.lightbox-carousel li").size() < 1) {
				return;
			}
			
			
			jQuery('.lightbox-carousel').jcarousel( {
				scroll : 1,
				initCallback : initLightboxCarouselHandler,
				itemVisibleInCallback : visibleLightboxCarouselHandler, 
				buttonNextHTML : null,
				buttonPrevHTML : null,
				visible : 1
			});
		},
		
		/**
		 * The carousel in the footer
		 */
		'setupFooterCarousel' : function() {
			//make sure to not let the footer carousel attempt to initialize if there
			//is no content 
			if(jQuery("#footer-carousel-list li").size() < 1) {
				jQuery("#footer-holder").hide();
				return;
			}
			
			jQuery("#footer-carousel-list").jcarousel({ 
				   scroll: 1, 
				   initCallback: footerCarouselInitCallback,
				   itemVisibleInCallback: footerCarouselVisibleCallback,
				    // This tells jCarousel NOT to autobuild prev/next buttons 
				   buttonNextHTML: null, 
				   buttonPrevHTML: null 
			   });
			 jQuery('#footer-carousel-prev').addClass("disabled");
		},
		
		/**
		 * Mouseover tool tips
		 * With a class "tooltip", will convert the title tag into text
		 * the text in the title tag is parsed as: TITLE|CONTENT
		 * Used in the carousels
		 * There is some super crazy logic going on
		 * with how to place the tooltip to the upper right corner of the box
		 * and to get the arrow to be on the bottom of the box...
		 */
		'setupTooltip' : function() {
			jQuery('.tooltip').each(function(i) {
				var title = jQuery(this).attr("title");
				title.replace(/<[^>]+>/ig,""); 
				jQuery(this).attr("title", title);
				
			});
			
			var xx = 0;
			var yy = 0;
			
			jQuery(".hero-thumb").hover(
				function() {
					xx = jQuery(this).offset().left;
					yy = jQuery(this).offset().top; 
				},
				
				function() {
					
				} 
			); 
			var homeThumb = (jQuery(".home-carousel .hero-thumb").size() > 0) ?true:false;
			var eventListingThumb = (jQuery("#event-listing-banner .hero-thumb").size() > 0) ?true:false;
			
			var onCluetipShow = function(ct, c){
				jQuery("#cluetip").css("position","absolute"); 
				  
				
				if(!homeThumb && !eventListingThumb && !(navigator.userAgent.indexOf("NT 6.0") > 0 && jQuery.browser.msie) ) {

					jQuery("#cluetip").css("left",xx-210);
					jQuery('#cluetip-arrows').addClass("cluetip-arrow-right");
				} else {
					jQuery("#cluetip").css("left",xx+45);
					jQuery('#cluetip-arrows').removeClass("cluetip-arrow-right");
 
				}
				 
				 
				 
				
				var h = jQuery("#cluetip").height();   
				var y = yy + 15 - h;
				 
				
				jQuery("#cluetip").css("top",y);
		  
				jQuery("#cluetip").removeClass("clue-left-default");
				jQuery("#cluetip").removeClass("clue-right-default");
				jQuery("#cluetip").removeClass("clue-bottom-default"); 
				
				jQuery("#cluetip").addClass("clue-top-default"); 
				
			};
			 
			jQuery('.tooltip').cluetip({splitTitle: '|', arrows:true, width:"232px", onShow:onCluetipShow, dropShadowSteps:5});
			//http://plugins.learningjquery.com/cluetip/#options
		},
		
		/**
		 * Insert a stub message on top of the page. Doesn't display until "showStubMessage()" is called.
		 * This should be called only once on a page.
		 * @param title
		 * @param message
		 * @return
		 */
		'writeStubMessage' : function(title, message) {
			var text = "<div id='stub-message'>"+
			"<div id='stub-content'>"+
			"<h6>"+title+"</h6>"+
			"<p>"+message+"</p>"+
			"</div>"+
			"<div id='stub-footer'>"+
			"<p> <a href='#' class='gray-button' onclick=\"jQuery('#stub-message').slideUp();\">Close</a> </p>"+
			"</div>"+
			"</div>";
			
			jQuery("#main").before(text);
		},
		
		/**
		 * Displays the stub message, if it exists.
		 * @return
		 */
		'showStubMessage' : function() {
			jQuery('#stub-message').slideDown();
		},
		
		/**
		 * Hides the stub message if it exists.
		 * @return
		 */
		'hideStubMessage' : function() {
			jQuery('#stub-message').slideUp();
		},
		
		/**
		 * Pop open a colorbox with messaging
		 * @param title
		 * @param message
		 * @return
		 */
		'showPopupMessage' : function(title, message, handler) {
			jQuery.fn.colorbox({  html:"<h3>"+title+"</h3><p>"+message+"</p>", onComplete:handler}); 
		},
		
		/**
		 * Opens a colorbox message via a URL
		 * @param page - internal URL (cant be external URL because of XSS)
		 * @return
		 */
		'showPopupMessageByPage' : function() {
			jQuery.fn.colorbox({ href:page, width:"680px"}); 
		},
		
		/**
		 * The searchbox in the header
		 */
		'setupSearch' : function() {
			//clear the search field the first time they click on it
			var searchClicked = false;
			jQuery("#searchfield").click(function() {
				if(!searchClicked) {
					searchClicked = true;
					jQuery(this).attr("value","");
				}
			});
		},
		
		/**
		 * Attach a class="lightbox" to a link
		 * and makes the link popup open in a lightbox
		 */
		'setupLightbox' : function() {
			jQuery('.lightbox').colorbox({photo:true});
		},
		
		
		/**
		 * The animation effect for FAQs datatables
		 */
		'setupFAQ' : function() { 
			jQuery(".faq dt").click(function() {
				jQuery(this).next().slideToggle("fast");	 
			});
		},
		
		'setupExternalStubMessage' : function() {
			var msg = jQuery(".external-stub-message"); 
			jQuery(".external-stub-message").remove();
			jQuery("#main").before(msg);
			
			
			
			var browserCenterHeight = jQuery(window).height() / 2;
			jQuery(".external-stub-message").css("top",browserCenterHeight-100);
			
			var browserCenterWidth = jQuery(window).width() / 2; 
			jQuery(".external-stub-message").css("left",browserCenterWidth-157);
			
			jQuery(".external-on").click(function() { 
				jQuery(".external-stub-message").hide(); 
				var rel = jQuery(this).attr('rel');
				

				jQuery("#main").before("<div class='external-overlay'></div>");
				
				if(jQuery.browser.msie) {
					jQuery("#external-stub-message-" + rel).show();

				} else {
					jQuery("#external-stub-message-" + rel).fadeIn();
				}
				return false;
				
			});
			
			jQuery(".external-stub-accept").click(function() { 
				jQuery(".external-stub-message").hide(); 
				jQuery(".external-overlay").hide();
				return true;
				
			});
			jQuery(".external-stub-cancel").click(function() { 

				jQuery(".external-overlay").hide();
				if(jQuery.browser.msie) {
					jQuery(".external-stub-message").hide(); 
				} else {
					jQuery(".external-stub-message").fadeOut();
				}
				return false;
				
			});
		}
		
		/*
		
		'ZZZ' : function() {
			
		}
		*/
		
		
};
 


function footerCarouselInitCallback(carousel) {

 
	jQuery("#footer-carousel-list li:nth-child(1) div").addClass("first");
	
	
	jQuery('#footer-carousel-prev').addClass("disabled");
	jQuery('#footer-carousel-next').addClass("disabled");
 
	jQuery('#footer-carousel-next').bind('click', function() {
		
		if(!jQuery(this).hasClass("disabled")) {
			carousel.next();
		}
		
		
		return false;
	});

	jQuery('#footer-carousel-prev').bind('click', function() {
		if(!jQuery(this).hasClass("disabled")) {
			carousel.prev();
		}
		return false;
	});
};


/**
 * Called when the display of a footer carousel change.
 * 
 * @param carousel
 * @param item
 * @param idx
 * @param state
 * @return
 */
function footerCarouselVisibleCallback(carousel, item, idx, state) {

	var firstSlideIndex = carousel.first;
	
	jQuery("#footer-carousel-list li div").removeClass("first");
	
	var link = jQuery("#footer-carousel-list li:nth-child("+firstSlideIndex+") div").addClass("first");
	
	 
	if(idx == 1 || firstSlideIndex == 1) {
		jQuery('#footer-carousel-prev').addClass("disabled");	
	} else {
		jQuery('#footer-carousel-prev').removeClass("disabled");	
	}
	
	//RIGHT BUTTON
	if(carousel.size() == idx) {
		jQuery('#footer-carousel-next').addClass("disabled");	
	} else {
		jQuery('#footer-carousel-next').removeClass("disabled");	
	}

}

 
/**
 * Called when the display of a hero carousel change.
 * 
 * @param carousel
 * @param item
 * @param idx
 * @param state
 * @return
 */
function visibleHeroHandler(carousel, item, idx, state) {
	/*
	jQuery('#hero-carousel img').css( {
		display : 'none'
	});
	jQuery('#hero-carousel img').fadeIn("slow");
	*/
	 
	

	jQuery('.jcarousel-control a.hero-thumb').removeClass("selected");
	jQuery('.jcarousel-control a.t' + idx).addClass("selected");
}

/**
 * Called when the thumbnails of a hero carousel change display.
 * @param carousel
 * @param item
 * @param idx
 * @param state
 * @return
 */
function visibleHeroThumbHandler(carousel, item, idx, state) {
	 
	var firstSlideIndex = carousel.first;
	
	//LEFT BUTTON
	if(idx == 1  || firstSlideIndex == 1) {
		jQuery('.jcarousel-control .hero-left-btn').addClass("disabled");	
	} else {
		jQuery('.jcarousel-control .hero-left-btn').removeClass("disabled");	
	}
	
	//RIGHT BUTTON
	if(carousel.size() == idx) {
		jQuery('.jcarousel-control .hero-right-btn').addClass("disabled");	
	} else {
		jQuery('.jcarousel-control .hero-right-btn').removeClass("disabled");	
	}
	
 
}


/**
 * Initialize the hero carousel
 * @param carousel
 * @return
 */
function initHeroHandler(carousel) {
	/*
	jQuery('#hero-carousel img').css( {
		display : 'none'
	});
	jQuery('#hero-carousel img').fadeIn("slow");
	*/

	jQuery('.jcarousel-control a.hero-thumb').bind('click', function() {

		var link = jQuery(this);
		rel = link.attr('rel');

		carousel.scroll(jQuery.jcarousel.intval(rel));
		return false;
	});


	 

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});

}

/**
 * Initialize the thumbnails of the hero carousel
 * @param carousel
 * @return
 */
function initHeroThumbHandler(carousel) {
	
	//carousel.imagesInitialized = false;
	
	jQuery('.jcarousel-control .hero-left-btn').addClass("disabled");	
	jQuery('.jcarousel-control .hero-left-btn').bind('click', function() {
		
		if(!jQuery(this).hasClass("disabled")) {
			carousel.prev();
		}
 
		return false;
	});

	jQuery('.jcarousel-control .hero-right-btn').bind('click', function() {

		if(!jQuery(this).hasClass("disabled")) {
			carousel.next();
		}
 
		return false;
	});
}

/**
 * Initialize the thumbnails of the lightbox carousel
 * @param carousel
 * @return
 */
function initLightboxCarouselHandler(carousel) {
	
	//carousel.imagesInitialized = false;
	
	
	if(carousel.size() < 2) {
		jQuery('.lightbox-carousel-left-btn').hide();
		jQuery('.lightbox-carousel-right-btn').hide();
	}
	
	jQuery('.lightbox-carousel-left-btn span').addClass("disabled");
	jQuery('.lightbox-carousel-left-btn').bind('click', function() {
	 
		if(!jQuery("span", this).hasClass("disabled")) {
			carousel.prev();
		}
		return false;
	});

	jQuery('.lightbox-carousel-right-btn').bind('click', function() {

		if(!jQuery("span", this).hasClass("disabled")) {
			carousel.next();
		}
		return false;
	});
	
	
	jQuery('.lightbox-carousel-zoom').bind('click', function() {
		  
		 var selectedIndex = carousel.first;
 
		//pretend to click the lightbox... fire off the event dispatcher for click...
		jQuery(".lightbox-carousel li:nth-child("+selectedIndex+") a[rel='slide']").click();
 
	 
		return false;
	});
}

/**
 * Called when the display of a lightbox carousel change.
 * 
 * @param carousel
 * @param item
 * @param idx
 * @param state
 * @return
 */
function visibleLightboxCarouselHandler(carousel, item, idx, state) {
	 
	 
	if(idx >= carousel.size()) {
		jQuery('.lightbox-carousel-right-btn span').addClass("disabled");
	} else {
		jQuery('.lightbox-carousel-right-btn span').removeClass("disabled");
	}
	
	if(idx <= 1) {
		jQuery('.lightbox-carousel-left-btn span').addClass("disabled");
	} else {
		jQuery('.lightbox-carousel-left-btn span').removeClass("disabled");
	}
	
	
 
}

function languageSelector(value, rel) {
	  
	//break out if they selected the first one...
	if(rel == "") {
		return;
	}
	
	var language = rel;
 
	var url = window.location.toString();
	var hash = window.location.hash;
	if (hash == null)
		hash = "";
	
	var jsonURL = "/language.action";
	if (hash == "")
		jsonURL += "?itemId=" + url;
	else
		jsonURL += "?itemId=" + url.substring(0, url.indexOf("#"));
	jsonURL += "&languageCode=" + language;
	
	//var loadingHandler = new Object();
//	loadingHandler.complete = function() {

		jQuery.getJSON( jsonURL, null, function(json) {
			
			if(json.successful) {
				if (json.translated)
					window.location = removeParameterFromUrl(json.response, "view") + hash;
				else
					window.location = removeParameterFromUrl(json.response, "view");
			} else {
				TRGlobal.showPopupMessage('', json.response);
				//jQuery("#language-selector-dropdown").val(currentLanguageIndex);
			}
		});
	//};
	//jQuery.fn.colorbox({ href:"http://thomsonreuters.com/page/images/loading.gif", width:"100px", height:"100px", onComplete:loadingHandler.complete}); 
	//jQuery.fn.colorbox({ href:"/static/languageLoader.jsp", width:"200px", height:"100px", onComplete:loadingHandler.complete}); 
	
}




/**
 * Remove query strings from a given url string.
 * @param url
 * @param parameter
 * @return
 */
function removeParameterFromUrl( url, parameter ) {

	
    if( typeof url == "undefined" || url == null || url == "" ) return url;
	
    if( typeof parameter == "undefined" || parameter == null || parameter == "" ) throw new Error( "parameter is required" );

    url = url.replace( new RegExp( "\\b" + parameter + "=[^&;]+[&;]?", "gi" ), "" ); "$1" ;

    // remove any leftover crud
    url = url.replace( /[&;]$/, "" );

    //remove floating ? if its the only thing left of the query string if we removed all of the params
    url = (url.lastIndexOf("?") == url.length-1)? url.substring(0, url.length-1) : url;

    return url;
}

/**
 * Manually select the top nav selected state
 * @param idx
 * @return
 */
function selectTopNavByIndex(idx){
	jQuery("#top-nav > li").removeClass("selected");
	jQuery("#top-nav > li:eq("+idx+")").addClass("selected");
	jQuery("#top-nav > li.selected").next().addClass("hidepipe");
}

