function tab_show( show, hide )
{
	li_show = jQuery( '#' + show );
	li_hide = jQuery( '#' + hide );
	
	li_show.addClass( 'selected' );
	li_hide.removeClass( 'selected' );
	
	id_show = li_show.attr( 'rel' );
	id_hide = li_hide.attr( 'rel' );
	
	carousel_show = jQuery('#chained_' + id_show).data('jcarousel');
	carousel_hide = jQuery('#chained_' + id_hide).data('jcarousel');

	debug1=carousel_hide;
	
	carousel_hide.stopAuto();
	jQuery( '#block-' + id_hide ).css( 'display', 'none' );
	carousel_show.startAuto();
	jQuery( '#block-' + id_show ).css( 'display', 'block' );	
}

$(function(){
	if($('a.popup-loader').length){
		$('a.popup-loader').fancybox();
	}
	
	if($('#zone6').find('.block-empty').length){
		$('#zone6').hide();
	}
	
	$('.bloc_free_content h3').wrapInner("<span/>");
	$('.bloc_free_content h3').after('<div class="cl" />')
	
	$('div.search input.search_field').addClass('blink');
	$('div.search input.search_field').val('Search...');
	$('div.search input.search_field').attr('title', 'Search...');
	//$('#zone_content div.search input.button').remove();
	
	$('.blink').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('.blink').blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	$('.item').live('click', function() {
		var href = $(this).find('a').attr('href');
		location.href= href;
		self.focus();
	});
	
	
	$('#navigation > ul > li:eq(0)').addClass('home-link');
	$('#navigation > ul > li > a').wrapInner('<span/>');
	
	$('#navigation > ul > li').hover(function(){
		$(this).find('a:eq(0)').addClass('hover');
	},function(){
		$(this).find('a:eq(0)').removeClass('hover');
	})
	
	$('#header div#topmenu div#navigation ul.navigation_niv_2').each(function(){
		$(this).find('li:last').addClass('last');
	});
	
	$('.global-food-entry').hover(function(){
		$(this).find('.img').stop(true, true).animate({
			height: '47'
		})
	},function(){
		$(this).find('.img').animate({
			height: '104'
		})
	});
	
	$('.global-food-entry').click(function(){
		window.location = $(this).find('a').attr('href');
	});

	
	$('#zone4 .breaker').remove();
	$('#zone4 .border-bottom-right').wrapInner('<ul/>');
	$('#zone4 .border-bottom-right').prepend("<h2>News</h2>");
	$('#zone4 .block-article-item').wrap('<li/>');
	$('#zone4 .block-article-item .image').append("<span class='tl'/>");
	$('#zone4 .block-article-item .image').append("<span class='tr'/>");
	$('#zone4 .block-article-item .image').append("<span class='bl'/>");
	$('#zone4 .block-article-item .image').append("<span class='br'/>");
	
	$('#zone4 .border-bottom-right').prepend('<div class="slider-control" />');
	
	$('.text .accroche').each(function(){
		var len = 200;
		if($(this).text().length>len){
			$(this).text($(this).text().substring(0, len)+"...");
		}
	});
	
	setTimeout(function() {
		var numSlides = $('#zone4 .border-bottom-right .block-article-item').length;
		var i;
		for(i=0; i<numSlides; i++){
			$('.slider-control').append('<a href="#">'+(i+1)+"</a>");
		}
		
		$('.block-article-item').live('click', function(){
			window.location = $(this).find('h3 a').attr('href');
		})
	}, 300);
	
	setTimeout(function() {
		$('#zone4 .border-bottom-right ul').jcarousel({
			auto: 3,
		    scroll: 1,
		    wrap: 'both',
		    itemFirstInCallback: mycarousel_itemFirstInCallback,
		    initCallback: mycarousel_initCallback,
		    buttonNextHTML: null,
		    buttonPrevHTML: null
		});
	}, 600);
	
	$('.video-box .img img:first').addClass('active').show();
	setInterval(function() {
		var activeImg = $('.video-box .img img.active').index();
		$('.video-box .img img.active').hide().removeClass('active');
		if((activeImg+1)==6){
			$('.video-box .img img').eq(0).fadeIn().addClass('active');
		} else {
			$('.video-box .img img').eq(activeImg+1).fadeIn().addClass('active');
		}
	}, 3000);
	
	$('#sitemap').appendTo('#page');
	
	$('#sitemap > ul > li:first').hide();
	
	$('#footer .footer-right a.sitemap-link').click(function(){
		var t = $(this);
		if($('#sitemap').is(':visible')){
			$('#sitemap').slideUp(function(){
				t.removeClass('sitemap-open');
			});
		} else {
			t.addClass('sitemap-open');
			$('#sitemap').slideDown(function(){
				$('body, html').animate({
					scrollTop: $('#sitemap').offset().top
				}, 1000);
			});
		}
		return false;
	});
	
	
	//SIDEBAR JS
	$('#zone2 .block-quicklinks:first').addClass('side-top-links');
	$('.side-top-links ul li:last').addClass('last');
	
	$('#zone2 .block-quicklinks:first').after('\
										<div class="box" id="bigger-box">\
											<div class="box-t">&nbsp;</div>\
											<div class="box-c"></div>\
											<div class="box-b">&nbsp;</div>\
										</div>');
	
	$('#zone2 .block-quicklinks:eq(1)').addClass('innerHtml').addClass('links');
	$('#zone2 .block-quicklinks:eq(2)').addClass('innerHtml').addClass('btns');
	$('#zone2 .block-quicklinks:eq(2) ul li:last').addClass('orange-btn');
	$('#zone2 .block-HtmlBloc:eq(0)').addClass('innerHtml');
	
	$('.innerHtml').appendTo('#bigger-box .box-c');
	
	$('#bigger-box .box-c').prepend('<h2>Quick links</h2>');
	
	
	$(".port-part").each(function() {
		var newAlt = "";
		
		var initLeft = $(this).position().left;
		var initTop = $(this).position().top;
		
		newAlt = "l:"+ initLeft + ";t:" + initTop;
		
		$(this).attr("alt", newAlt);
	});
	
	var mouse = function(){
		this.x = 0;
		this.y = 0;
	}
	if (!document.all) document.captureEvents(Event.MOUSEMOVE);
		
	
	// Those koeficients set the movement. k1 is global, k2 for the distance
	var k1 = 0.05;
	var k2 = 0.01;
	
	$(document).mousemove(function getMouseXY(e) {
		if (document.all) {
			mouse.x = event.clientX + document.body.scrollLeft;
			mouse.y = event.clientY + document.body.scrollTop;
		} else {  
			mouse.x = e.pageX;
			mouse.y = e.pageY;
		}  
		
		
		if (mouse.x < 0){mouse.x = 0};
		if (mouse.y < 0){mouse.y = 0};
		
		var tree = $("body")
		var screenX = tree.offset().left + tree.width()/2;
		var screenY = tree.offset().top + tree.height()/2;
		
		$(".port-part").each(function() {
			var leafPos = $(this).attr("alt");
			leafPos = {
				x: parseInt(leafPos.split(";")[0].split(":")[1]),
				y: parseInt(leafPos.split(";")[1].split(":")[1])
			};
			
			var distanceX = mouse.x - screenX;
			var distanceY = mouse.y - screenY;
			
			var d = Math.pow( Math.abs(leafPos.x + $(this).parent().offset().left - mouse.x) ,2);
			d +=    Math.pow( Math.abs(leafPos.x + $(this).parent().offset().top - mouse.x ) ,2);
			d = Math.sqrt( d );
			d = Math.pow(d, k2);
			
			var ratioX = $(this).offset().left - Math.abs(distanceX);
			var ratioY = $(this).offset().top - Math.abs(distanceY);
			
			$(this).css({
				left: leafPos.x + k1 * distanceX * $(this).attr("title")
				//top: leafPos.y + k1 * distanceY * $(this).attr("title")
			});
		});
	
		return true;
	});
	
	var minHeight = $('#zone2').height();
	$('.Layout_1').css('min-height', minHeight);
	
	$(".block-partnerFolder").css('position', 'absolute');
	$(".graphitec-tabs ul li:first").addClass("selected").show(); 
	$(".graphitec-tabs ul li:last").addClass("media"); 
	$(".block-partnerFolder:first").css('position', 'static'); 
	
	$(".graphitec-tabs ul li").click(function() {
		var t = $(this);
		var idx = $(this).index();
		$(".graphitec-tabs ul li").removeClass("selected");
		$(this).addClass("selected");
		$(".block-partnerFolder").animate({
			'opacity': 0
		}, function(){
			var activeTab = $(".block-partnerFolder").eq(idx)
			$(activeTab).css('position', 'static').animate({
				'opacity': 1
			});
			
		}).css('position', 'absolute');
		return false;
	});
	
})

function mycarousel_initCallback(carousel) {
    $('.slider-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
};
	
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
	$('.slider-control a').removeClass('active');
	$('.slider-control a').eq(idx-1).addClass('active');
};

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-3239722-5']);
  _gaq.push(['_setDomainName', 'none']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  
