// JavaScript Document
$(function(){

	$.fn.toggleFade = function(speed, callback) {
		 return this.animate({opacity: 'toggle'}, speed, callback);
	
	}; 

	$('a[rel*=external]').click(function() {
		window.open(this.href);
		return false;
	}); 
	

	$("a.change-country").click(function(){
			$("#countries").toggleFade('fast', function() {
   			if ($.browser.msie)  
					this.style.removeAttribute('filter');
			});
		return false;
	});

	$('a').each(function() {
		var domain = document.domain;
		domain = domain.replace(/www./, "");
		var href= $(this).attr('href');
		if (undefined != href) {
			if ( (href.match(/^http/)) && (! href.match(domain)) ) {
				$(this).click(function() {
 					if(typeof _gaq== 'object') { 
						_gaq.push(['_trackPageview','/external/' + href]);
						_gaq.push(['t2._trackPageview','/external/' + href]);
					}
				});
			} else if (href.match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|dmg|wma|mov|avi|wmv|mp3)$/)) {
				var linktext = $(this).html();
				var extension = href.split('.').pop().toUpperCase();
				var linktextnew = linktext + " <span class='accessibility'>(" + extension + " Document)</span>"; 
				$(this).html(linktextnew);
				$(this).click(function() {
					if(typeof _gaq== 'object') { 
						_gaq.push(['_trackPageview','/downloads/' + href]);
						_gaq.push(['t2._trackPageview','/downloads/' + href]);
					}
				window.open(this.href);
				return false;
				});
			}	
		}	
	});

	$("#countries").mouseenter(function(){
      $(this).data('in', true);
			$("#countries").customFadeIn('fast');
	}).mouseleave(function(){
        $(this).data('in', false);
       setTimeout(hideMenu, 1000);
	});

	function hideMenu(){
		if (!$('#countries').data('in'))
			{
				$("#countries").customFadeOut('fast');
				$('#countries').data('hidden', true);
			}
	}

		//all hover and click logic for buttons
		$(".button:not(.ui-state-disabled)")
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		)
		.mousedown(function(){
			$(this).addClass("ui-state-active");	
		})
		.mouseout(function(){
				$(this).removeClass("ui-state-active");
		});

	
	(function($) {
		$.fn.imageswap = function(options) {
	
			var opts = $.extend({}, $.fn.imageswap.defaults, options);
		 // iterate and reformat each matched element
			return this.each(function() {
				obj = $(this);
				obj.find("a:first").addClass("selected");
				var captiondiv = obj.find(opts.caption);
				var imagediv = obj.find(opts.image);
				var links = obj.find("a");
				
				var hrefs = new Array();
				$(links).each(function(){
					hrefs.push($(this).attr('href'));
				})
	
				
				$.fn.imageswap.preloadImages(hrefs);
				links.click(function(){
					var imageSource = $(this).attr("href");
					var captionSource = $(this).attr("title");
					$("a").removeClass("selected");
					$(this).addClass("selected");
					$.fn.imageswap.image(imageSource,imagediv,captionSource,captiondiv);
					//$.fn.imageswap.caption(captionSource,captiondiv);
					return false;
				});
	
			});
		};
	
		$.fn.imageswap.preloadImages = function(links){
			for(var i = 0; i<links.length; i++)
			{
				$("<img>").attr("src", links[i]);
			}
		}
	
		// define image swap function
		$.fn.imageswap.image= function(src,div,text,caption) {
				var img = $(div).find("img");
				$(img).fadeOut("normal").remove();
				$(caption).hide();
				var image = new Image();
				 $(image).load(function(){
				 $(this).hide();
				 $(div).append(this);
				 $(this).fadeIn("fast", function() {
						$.fn.imageswap.caption(text,caption);		
			  	});              
				});    
				$(image).attr("src", src);
		};
		
		// define caption swap function
		 $.fn.imageswap.caption= function(text,div) {
				$(div).html(text);
			  $(div).show();
		};
		
		// plugin defaults - can be changed by
		$.fn.imageswap.defaults = {
			image: 'div.picturebox',
			caption: 'div.caption'
		};
	
	// end of closure
	})(jQuery);
	
	(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

});