$(document).ready(function(){
	
	//Clone images and text for slider
	for (i=1;i<=2;i++){
		$('#slider .wrap ul.images li').clone().appendTo('#slider .wrap ul.images');
		$('#slider .wrap .text span').clone().appendTo('#slider .wrap .text');
	}
	
	$('ul li:last-child').addClass('last');
	$('.menu-top li:first-child').addClass('noborder');
	
	//Update complementary width for main title
	update_title_width();	
	$(window).resize(update_title_width);
	
	//Top menu functions
	$('#bar-top ul.menu-top li').hover(
	function(){
		$(this).next().toggleClass('noborder');
		//$(this).animate({'backgroundColor': '#00b0ec'},150);
	},
	function(){
		$(this).next().toggleClass('noborder');
		//$(this).animate({'backgroundColor': '#de6a10'},150,function(){  });
	}
	);
	
	//Slide up/down the submenu
	$('#bar-top li').hover(
	function(){
		$(this).find('.sub').slideToggle(200);
	},
	function(){
		$(this).find('.sub').slideToggle(200);
	});	
	
	
	
	//Set up shadow structure	
	$('.shadow').each(function(){
		$(this).wrapInner('<span class="first" />');	
		$(this).append('<span class="second">'+$(this).text()+'</span>');
		$(this).wrapInner('<span class="wrapInner"  />');		
	});
	
	//Make sure shadow has small opacity (for IE)
	$('.shadow span.second').css({
					'opacity':0.2				
					
	});
	
	
	//Slider functions	
	
	//Scroll in the beginning
	$('#slider .wrap').scrollLeft(first_offset);	
	//Start interval
	var sliderIntVal = window.setInterval("slider_move()",5000);
	
	//On hover pause slider
	$('#slider').hover(
	function(){
		window.clearInterval(sliderIntVal);		
	},
	function(){
		sliderIntVal = setInterval("slider_move()",5000);
	});
	
	
	//Campswitcher
	$('#campswitcher .tab').click(function(){
		var index = $(this).index();
		
		$(this).siblings().removeClass('selected');
		$(this).addClass('selected');
		
		$('#campswitcher .item.selected').removeClass('selected');
		$('#campswitcher .item:nth-child('+(index+1)+')').addClass('selected');
		
	});
	
	//Side navigation hover effect
	
	$('.side-nav ul li').hover(
		function(){$(this).animate({'paddingLeft': 19},100);},
		function(){$(this).animate({'paddingLeft': 16},100);});
	
	//Button hover effect 	
	$('a.button.blue,.donate').hover(
		function(){	$(this).animate({'backgroundColor': '#008dde'},150);},
		function(){	$(this).animate({'backgroundColor': '#00baef'},150);});
	$('a.button.orange').hover(
		function(){	$(this).animate({'backgroundColor': '#e86d1b'},150);},
		function(){	$(this).animate({'backgroundColor': '#e88a1b'},150);});

	//Menu hover effect
	$('#menu-main li').hover(
		function(){	$(this).animate({'backgroundColor': '#050505'},250);},
		function(){	$(this).animate({'backgroundColor': '#242424'},250);});
	
	$('#footer ul li').hover(
		function(){	$(this).find('a').animate({'backgroundColor': '#008dde'},150);},
		function(){	$(this).find('a').animate({'backgroundColor': '#242424'},150);});
	
});

function update_title_width(){
	
	var width =($(window).width() - $('#slider .title').width())/2
	//if ($.browser.msie) width = width-10;
		
	$('#slider .title-bg-complement').css('width',width);
}


var init_count = $('#slider .wrap').find('ul.images li').size();

//Slider functions
var first_offset = 320;
function slider_move(){
	var slider = $('#slider .wrap')
	var images = slider.find('ul.images li');
	var texts = slider.find('.text span');
	var offset = images.first().width();
	
	//if (images.size() > init_count) return false;
	
	
	slider.animate({
	   'scrollLeft'	:	slider.scrollLeft()+offset
	}, 1000, function() {
		   
			//Copy first image to the end and remove it from beginning
			images.first().clone().appendTo(images.parent());
			images.first().remove();
			
			//Copy first text ot end
			texts.first().clone().appendTo(texts.parent());
			
			
			//Get width of first and second text
			var first_width = texts.first().width();
			var second_width = texts.first().next().width();
			
			//Get target width (to keep the shift smooth)
			var target_width = first_width+second_width-offset;
	
			//If positive, set it to next text							
			if(target_width >= 0) {
				var next = texts.first().next();
					next.width(first_width+second_width-offset);
			}
			//If negative, add it (subtract) to the second next text
			else {
				var next = texts.first().next().next();								
				next.width(next.width()+target_width);
				//Clone and remove the next text
				texts.first().next().clone().appendTo(texts.parent());
				texts.first().next().remove();	
				
			}							
			//Remove first text
			texts.first().remove();
			//Reset width of moved text
			slider.find('.text span').not('span:first-child').css('width','auto');
			
			//Scroll back to first position
			slider.scrollLeft(first_offset);
	});
	
	
}
