$(function(){

	
	$('#navigation').css({opacity: '0.9'});
	
	$('#country_select a').click(function(){
		$('#country_options').toggle();
	});
	
	$('#country_options').mouseleave(function(){
		$(this).hide();
	});
		
	$('.pagination a img').hover(function(){
		$(this).attr("src", $(this).attr("src").replace("_off.gif", "_on.gif"));
	}, function(){
		$(this).attr("src", $(this).attr("src").replace("_on.gif", "_off.gif"));
	});

	$('#navigation ul li a').hover(function(){
		$img = $(this).find('img');
		$img.attr("src", $img.attr("src").replace("_off.png", "_on.png"));
		title = $(this).attr("title");
		$('body').append('<div id="navtip">'+title+'</div>');
		$('#navtip').css({position:"absolute", top:(e.pageY - 60) + "px", left:(e.pageX - 10) + "px", opacity: "0.9"});
	}, function(){
		$img.attr("src", $img.attr("src").replace("_on.png", "_off.png"));
		$('#navtip').remove();
	});
	$('#navigation ul li a').mousemove(function(e){
		$("#navtip").css({top:(e.pageY - 60) + "px", left:(e.pageX - 10) + "px", opacity: "0.9"});
	});
	
	$('.block ul li a img').hover(function(e){	
		block = $(this).parent().parent().parent().parent().parent().index();
		if(block != '2'){ boxPos = e.pageX + 20; } else { boxPos = e.pageX - 320; }
		$('.block').find('a img').not(this).stop().animate({opacity: '0.3'}, 500);
		content = $(this).parent().parent().find('.tooltip').html();	
		$("#tooltip").html(content).css({position:"absolute", top:(e.pageY - 60) + "px", left:(boxPos) + "px", opacity: "0.8"}).hide().stop().fadeIn(100)
	}, function(){	
		$('.block').find('a img').not(this).stop().animate({opacity: '1'}, 500);
		$("#tooltip").stop().fadeOut(100);
	});	
	$('.block').mousemove(function(e){
		block = $(this).index();
		if(block != '2'){ boxPos = e.pageX + 20; } else { boxPos = e.pageX - 320; }
		$("#tooltip").css({top:(e.pageY - 60) + "px", left:(boxPos) + "px"})
	});
	
	
	evtPagination = 1;
	evtMargin = 0;
	galPagination = [];
	
	if($('#home').length == 0) {
		eventsCount = Math.ceil($('#events ul li').size() / 6);
		eventHeight = 305;
	}
	else {
		eventsCount = Math.ceil($('#events ul li').size() / 4);
		eventHeight = 203;
	}
	
	if(eventsCount < 2){
		$('#secondstage .pagination').hide();
	}
		
	$('#secondstage .pagination span:eq(1)').text(eventsCount);
	$('.less').css({opacity: 0}).addClass('disabled');
		
	$('#secondstage .pagination a').click(function(){
		if($(this).attr('class').match("disabled") == null){
			if($(this).attr('class') == 'more'){
				evtPagination++;
			}
			else {
				evtPagination--;
			}
			$.showEvtPag();
			evtMargin = (evtPagination-1) * (eventHeight + 1);
			$('#eventSlide').animate({marginTop: '-'+ evtMargin +'px'}, 250);
			$('.pagination span:eq(0)').text(evtPagination);	
		}
		return false;
	});
	
	$('.block').each(function(){
		
		galPagination.push('0');
		
		ct = $(this).find('.outerSlide ul li').size();
		$(this).find('.pagination span:eq(1)').text(Math.ceil(ct / 4));
		if(ct < 5){
			$(this).find('.pagination').hide();
		}
		$(this).find('.outerSlide ul').css({width: (ct * 80) + 'px'});
		
		$(this).find('.pagination a').click(function(){
			if($(this).attr('class').match("disabled") == null){	
				blockIndex = $(this).parent().parent().index();
				if($(this).attr('class') == 'more'){
					galPagination[blockIndex]++;
				}
				else {
					galPagination[blockIndex]--;
				}
				$(this).parent().find('span:eq(0)').text(galPagination[blockIndex] + 1);
				$(this).parent().parent().find('.outerSlide ul').animate({marginLeft: '-' + galPagination[blockIndex] * 320 + 'px'});
				$.showGalPag(blockIndex);
			}
			return false;
		});
		
	});
		
	$('.article').first().siblings().hide();
	currentArticle = 1;
	totalArticles = $('.article').size();
	
	if(totalArticles < 2){
		$('.article_info .pagination').hide();
	}
	
	$('.article').each(function(){
		$(this).find('.pagination span:eq(1)').text(totalArticles);
	});
	
	$('.article_info .pagination a').live('click', function(){
		if($(this).attr('class').match("disabled") == null){
			if($(this).attr('class') == 'more'){
				currentArticle++;
			}
			else {
				currentArticle--;
			}
			$.showArtPag();
			
			$('.article').hide();
			$('.article:eq(' + (currentArticle - 1) + ')').show();
			
			$('.article').each(function(){
				$(this).find('.pagination span:eq(0)').text(currentArticle);
			});
		}
		return false;
	});
	
	// set up social share
	//current_url = encodeURIComponent(location.href);
	current_url = location.href;
	
	
	$('.social_share').each(function(){
		current_title = $(this).siblings('h2').text();
		$(this).find('.facebook_share a').attr('href', 'http://www.facebook.com/share.php?u='+current_url+'&t='+current_title);
		$(this).find('.twitter_share a').attr('href', 'http://twitter.com/home?status=RT%20%40burn%20'+current_url);
		$(this).find('.delicious_share a').attr('href', 'http://delicious.com/post?url='+current_url+'&title='+current_title);
		$(this).find('.myspace_share a').attr('href', 'http://www.myspace.com/Modules/PostTo/Pages/?u='+current_url+'&t='+current_title);
		
		
	});
	
//	$('.facebook-but iframe').attr({ 'src': 'http://www.facebook.com/plugins/like.php?href='+current_url+'&layout=button_count&amp;show_faces=false&amp;width=95&action=like&font=lucida+grande&colorscheme=light&height=21' });
//	$('.facebook-send iframe').attr({ 'src': 'http://www.facebook.com/plugins/send.php?href='+current_url });
//	$('.facebook-but').children().attr({ 'href': current_url });
//	$('.g-plusone').children().attr({ 'href': current_url });

	
	$.showGalPag = function(blockIndex){
		galCount = Math.ceil($('.block:eq('+ blockIndex +')').find('.outerSlide ul li').size() / 4);
		if(galPagination[blockIndex] == 0){
			$('.block:eq('+ blockIndex +')').find('.less').css({opacity: 0}).addClass('disabled');
		}
		else {
			$('.block:eq('+ blockIndex +')').find('.less').css({opacity: 1}).removeClass('disabled');	
		}
		if(galPagination[blockIndex] == (galCount - 1)) {
			$('.block:eq('+ blockIndex +')').find('.more').css({opacity: 0}).addClass('disabled');
		}
		else {
			$('.block:eq('+ blockIndex +')').find('.more').css({opacity: 1}).removeClass('disabled');
		}
	}
	
	$.showEvtPag = function(){
		if(evtPagination == 1){
			$('#secondstage .pagination a:eq(0)').css({opacity: 0}).addClass('disabled');	
		}
		else {
			$('#secondstage .pagination a:eq(0)').css({opacity: 1}).removeClass('disabled');
		}
		if(evtPagination == eventsCount) {
			$('#secondstage .pagination a:eq(1)').css({opacity: 0}).addClass('disabled');
		}
		else {
			$('#secondstage .pagination a:eq(1)').css({opacity: 1}).removeClass('disabled');
		}
	}
	
	$.showArtPag = function(){
		if(currentArticle == 1){
			$('.article').each(function(){
				$(this).find('.pagination a:eq(0)').css({opacity: 0}).addClass('disabled');
			});
		}
		else {
			$('.article').each(function(){
				$(this).find('.pagination a:eq(0)').css({opacity: 1}).removeClass('disabled');
			});
		}
		if(currentArticle == totalArticles) {
			$('.article').each(function(){
				$(this).find('.pagination a:eq(1)').css({opacity: 0}).addClass('disabled');
			});
		}
		else {
			$('.article').each(function(){
				$(this).find('.pagination a:eq(1)').css({opacity: 1}).removeClass('disabled');
			});
		}
	}
	
	locale = window.location.pathname.split("/")[1];
	
	if (locale == "fr_BE" || locale == "nl_NL") {
		$('#navigation').css({height: '70px'});
		$('#product_can p').css({position: 'absolute', right: '50px', bottom: '12px', 'font-size': '10px', 'line-height': '12px'});
		$('#product_can img').css({top: '-50px'});
		$('#navigation ul').css({'padding-top': '10px'});
		$('#logo div a').css({'bottom': '13px'});
	};

});

