"use strict";

var cycleRegionPhotos = function(){
	
	if(jQuery('.region_photos li').length > 3)
	{
		
		jQuery('.region_photos ul').width(jQuery('.region_photos ul').width() + 60);
		
		var first_time = true;

		jQuery.getScript('/js/jquery.async.js', function() {

			jQuery.whileAsync({
				delay: 3000,
				bulk: 0,
				test: function(){
					return true;
				},
				loop: function()
				{
					if(first_time){
						first_time = false;
					}else{
						var first = jQuery('.region_photos ul li').first();
						var first_width = first.width();		
				
						jQuery(first).clone().appendTo('.region_photos ul');
						
						jQuery('.region_photos ul').animate({
							'margin-left': '-' + first_width + 'px'
						}, 500, function() {
							// Animation complete.
							jQuery(first).remove();
							jQuery(this).css('margin-left',0);
						});
						
					}
				},
				end: function() { }
			});
		
		});
		
	}
	
};





jQuery(document).ready(function(){
				
				
	if(jQuery('a.fancybox').length > 0)
	{
		//highjack the anchors
		jQuery.getScript('/js/fancybox/fancybox/jquery.fancybox-1.3.4.pack.js', function() {
		
			// fancybox effect for image gallery
			jQuery("a.fancybox").fancybox({
				'opacity'		: true,
				'overlayShow'	: true,
				'overlayColor'	: '#fff',
				'overlayOpacity': 0.7,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'titlePosition'	: 'over',
				'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {

					if (currentArray.length < 2)
					{
						return '<span id="fancybox-title-over">' + title + '</span>';
					}
					else
					{
						return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '		</span>';
					}
				}
			});

			
		});
	}			
				
	//alert(window.location.hostname.substring(0, 2));
	if (window.location.hostname.substring(0, 3) == 'fr.')
	{
		jQuery('.altlink').attr('href', 'http://' + window.location.hostname.substring(3) + jQuery('.altlink').attr('href'));
	}
	else
	{
		jQuery('.altlink').attr('href', 'http://fr.' + window.location.hostname + jQuery('.altlink').attr('href'));
	}
	
	var originalHeight = jQuery("#main_content").height();
	jQuery("#main_content").height('100px');
	jQuery("a.top").css({'display': 'none'});
	
	jQuery("a.more").click(function(){
		jQuery("#main_content").animate({ 
			height: originalHeight
		}, 1500 );
		jQuery(this).hide();
		jQuery("a.top").css({'display': 'block'});
		return false;
	});
	jQuery("a.top").click(function(){
		jQuery("#main_content").animate({ 
			height: "100px"
		}, 1500 );
		jQuery(this).hide();
		jQuery("a.more").css({'display': 'block'});
		return false;
	});							
								
	jQuery("#primary li").hover(function(){
		//jQuery(this).addClass("hover");
		jQuery('ul:first', this).fadeIn("fast");
	},function(){
		//jQuery(this).removeClass("hover");
		jQuery('ul:first', this).fadeOut("fast");
	});
	
	jQuery('#right').css('border-bottom','none');
	
	
	// set min-height on content and right divs
	var highest = null;
	
	jQuery('#content, #right').each(function(){
		if(jQuery(this).height() > highest){
			highest = jQuery(this).height();
		}
	});
	
	jQuery('#content, #right').css('min-height', highest);
	
	
	// set min-height on left columns
	var highest = null;
	
	jQuery('#container, #left').each(function(){
		if(jQuery(this).height() > highest){
			highest = jQuery(this).height();
		}
	});
	
	jQuery('#left').css('min-height', highest);
	
	
	// cycle throught region photos
	cycleRegionPhotos();
	
	//jQuery('#right iframe').css();
	
});
