//
//function promoBoxOnBefore(){
//	var promoBox = $(this).parents('.promoBox');
//	var promoboxPageNum = promoBox.find('.promoboxPageNum');
//	promoboxPageNum.animate({'opacity':0});
//	
//}
//
//function promoBoxOnAfter(){
//	var promoBox = $(this).parents('.promoBox');
//	var itemNum = $(this).siblings().size() +1;
//	var promoboxPageNum = promoBox.find('.promoboxPageNum');
//	var pageNum = parseInt(promoBox.find('.promoboxPager .activeSlide').text());
//	if (pageNum > itemNum || isNaN(pageNum)){
//		pageNum = 1;
//	}
//	promoboxPageNum.text(pageNum);
//	promoboxPageNum.animate({'opacity':1});
//}
//
//(function($){
//	$(function(){
//		promoBox();
//		loginBox();
//		tabContent();
//		picto();
//		profileBox();
//		searchBox();
//		
//		
//		$('#main').parallax({
//			'elements':[
//				{
//					'selector': '#main',
//					'properties': {
//						'x': { 'left': {'initial':0, 'multiplier':0.01, 'invert':true} },
//						'y': { 'top': {'initial':0, 'multiplier':0.01, 'invert':true} }
//					}
//				},
//				{
//					'selector': 'section#logo',
//					'properties': {
//						'x': { 'left': {'initial':30, 'multiplier':0.02, 'invert':true} },
//						'y': { 'top': {'initial':0, 'multiplier':0.02, 'invert':true} }
//					}
//				},
//				{
//					'selector': 'section#login',
//					'properties': {
//						'x': { 'left': {'initial':785, 'multiplier':0.04, 'invert':false} },
//						'y': { 'top' : {'initial':120, 'multiplier':0.04, 'invert':false} }
//					}
//				},
//				{
//					'selector': 'section#appli',
//					'properties': {
//						'x': { 'left': {'initial':800, 'multiplier':0.01, 'invert':false} }
//					}
//				},
//				{
//					'selector': 'section#picto',
//					'properties': {
//						'x': { 'left': {'initial':750, 'multiplier':0.02, 'invert':false} }
//					}
//				},
//				{
//					'selector': 'section#facebook',
//					'properties': {
//						'x': { 'right': {'initial':65, 'multiplier':0.08, 'invert':true} }
//					}
//				},
//				{
//					'selector': 'section#news',
//					'properties': {
//						'x': { 'left': {'initial':40, 'multiplier':0.04, 'invert':true} },
//						'y': { 'top': {'initial':465, 'multiplier':0.02, 'invert':false} }
//					}
//				}
//			]
//		});
//		
//		if ($('.faqList').length > 0)
//			$('.faqList').jScrollPane({showArrows:true, hijackInternalLinks:true});
//		
//		if ($('.newsList').length > 0)
//			$('.newsList').jScrollPane({showArrows:true});
//		
//		if ($('.friendList').length > 0)
//			$('.friendList').jScrollPane({showArrows:true});
//		
//		function promoBox(){			
//			var $promoBox = $('.promoBox');
//			var $promoBoxCycle = $('.items',$promoBox);
//			
//			if ( $("li",$promoBoxCycle).size() > 1 ) {
//				//Add page Number
//				$promoBox.prepend('<div class="promoboxPageNum">1</div>');
//				// Add pager
//				$promoBox.append('<div class="promoboxPager"></div>');
//				
//				// Init Cycle
//				$promoBoxCycle.cycle({ 
//					fx: 'scrollLeft', // animation
//					speed: 1000, // animation speed
//					timeout: 5000, // timeout between cycle
//					pager: '.promoboxPager',
//					pagerEvent: 'click', 
//					pauseOnPagerHover: true,
//					before:promoBoxOnBefore,
//					after:promoBoxOnAfter
//				});			
//			}			
//		}
//		
//		function loginBox(){
//			var open = false;
//			$('#login > a.button').click(function(e){
//				e.preventDefault();
//				if (!open){
//					$('body').append('<div class="loginBackground"></div>');
//					$('#login').find('section.connection').show().animate({'width': 860, 'height': 400}, 200, 'linear',function(){
//						open=!open;
//					});
//				} else {
//					$('#login').find('section.connection').animate({'width': 0, 'height': 0}, 200, 'linear',function(){
//						open=!open;
//						$(this).hide();
//						$('body .loginBackground').remove();
//					});
//				}
//			});
//		}
//		
//		function tabContent(){
//			if ($('.tabSystem').length > 0){
//				//Create tabs
//				var tabList = '<ul id="tabContentSwitcher">';
//				var idx = 1;
//				$('.tabContent').each(function(){
//					var tabID = 'tabContent'+idx;
//					$(this).attr('id',tabID);
//					var title = $(this).prev();
//					var titleLink = '<li><a href="#'+tabID+'">'+title.text()+'</a></li>';
//					title.remove();
//					tabList += titleLink;
//					idx++;
//				});
//				tabList += '</ul>';
//				$('.tabSystem').prepend(tabList);
//				//Activate first tab
//				$('.tabSystem').find('a:first').addClass('active');
//				$('.tabItem:first').show();
//				//Switch tabs
//				$('#tabContentSwitcher').find('a').click(function(e){
//					e.preventDefault();
//					var href = $(this).attr('href');
//					//Bulletproof ID (to prevent IE7 bug)
//					var startIdx = href.indexOf('#');
//					var targetID = href.slice(startIdx, href.length);
//					$('.tabContent:visible').parents('.tabItem').hide();
//					$(targetID).parents('.tabItem').show();
//					$('.tabSystem a').removeClass('active');
//					$(this).addClass('active');
//					//Reinit pictoList scrollbar
//					var thisPictoList = $(targetID).parents('.tabItem').find('.pictoList');
//					if (thisPictoList.length > 0){
//						thisPictoList.data('jsp').reinitialise();
//					}
//				});
//			}
//		}
//		
//		function picto(){
//			if ($('.pictoList').length > 0){
//				//Add scroller
//				$('.pictoList').jScrollPane({showArrows: true});
//				//Prevent click on inactive items
//				$('.pictoList').find('.exchange').click(function(e){
//					if (!$(this).parent().hasClass('active')){
//						e.preventDefault();
//					}
//				});
//			}
//		}
//		
//		function profileBox(){
//			$('.myprofile').click(function(){
//				$(this).colorbox({inline:true, width:530, opacity:0.6, href:$('#profileBox').html(), onComplete:function(){
//					$.colorbox.resize();
//				}});
//			});
//			//Lang choice
//			$('.profileBoxContent').find('.radioBtn').live('click',function(e){
//				if (!$(this).hasClass('active')){
//					var langValue = $(this).text().toLowerCase();
//					$(this).parents('form').find('input[name=lang]').val(langValue);
//					$(this).siblings().removeClass('active');
//					$(this).addClass('active');
//				}
//			});
//		}
//		
//		function searchBox(){
//			$('label.include').each(function(){
//				//Move label into field
//				searchLabel = $(this);
//				var labelTxt = searchLabel.text();
//				var searchInput = searchLabel.next('input');
//				searchInput.val(labelTxt).data('label_txt',labelTxt);
//				searchLabel.remove();
//				//Empty/fill field
//				searchInput.focusin(function() {
//					//jc
//					if ($(this).attr('id')==='form_subscription_repasswordtmp'){
//						$("#form_subscription_repasswordtmp").hide();
//						$("#form_subscription_repassword").show().focus();
//						
//					}else if ($(this).attr('id')==='form_subscription_passwordtmp'){
//						$("#form_subscription_passwordtmp").hide();
//						$("#form_subscription_password").show().focus();
//						
//					}else if ($(this).attr('id')==='form_connection_passwordtmp'){
//						$("#form_connection_passwordtmp").hide();
//						$("#form_connection_password").show().focus();
//						//jc end
//					}else if ($(this).val() == $(this).data('label_txt')) {
//						
//						$(this).val('');
//						
//						
//					}
//				}).focusout(function() {
//					
//					$(this).val($.trim($(this).val()));
//					if ($.trim($(this).val()) == '') {
//						
//						
//							
//							
//								$(this).val($(this).data('label_txt'));
//							
//						
//					}
//				});
//			});			
//		}
//	});
//}(jQuery));
(function($){
    $.fn.shuffle = function(){
        return this.each(function(){
            var items = $(this).children();

            return (items.length)
                ? $(this).html($.shuffle(items,$(this)))
            : this;
        });
    }

    $.fn.validate = function(){
        var res = false;
        this.each(function(){
            var arr = $('div#cboxLoadedContent div.profileBoxContent div.unsubscribe_hidden div.registration div.captcha_wrap ul.sortable').children();
           // log(arr[0].innerHTML+' '+arr[1].innerHTML+' '+arr[2].innerHTML+' '+arr[3].innerHTML+' '+arr[4].innerHTML+' '+arr[5].innerHTML);
            res =    ((arr[0].innerHTML=="1")&&
                (arr[1].innerHTML=="2")&&
                (arr[2].innerHTML=="3")&&
                (arr[3].innerHTML=="4")&&
                (arr[4].innerHTML=="5")&&
                (arr[5].innerHTML=="6"));
        });
        return res;
    }

    $.shuffle = function(arr,obj){
        for(
        var j, x, i = arr.length; i;
        j = parseInt(Math.random() * i),
        x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
        if(arr[0].innerHTML=="1") obj.html($.shuffle(arr,obj))
        else return arr;
    }

})(jQuery);

function promoBoxOnBefore(){
	var promoBox = $(this).parents('.promoBox');
	var promoboxPageNum = promoBox.find('.promoboxPageNum');
	promoboxPageNum.animate({'opacity':0});
	
}

function promoBoxOnAfter(){
	var promoBox = $(this).parents('.promoBox');
	var itemNum = $(this).siblings().size() +1;
	var promoboxPageNum = promoBox.find('.promoboxPageNum');
	var pageNum = parseInt(promoBox.find('.promoboxPager .activeSlide').text());
	if (pageNum > itemNum || isNaN(pageNum)){
		pageNum = 1;
	}
	
	promoboxPageNum.text(pageNum);
	promoboxPageNum.animate({'opacity':1});
}

(function($){
	$(function(){
		promoBox();
		loginBox();
		tabContent();
		picto();
		profileBox();
		searchBox();
		
		if ($('.faqList').length > 0)
			$('.faqList').jScrollPane({showArrows:true, hijackInternalLinks:true});
		
		if ($('.newsList').length > 0)
			$('.newsList').jScrollPane({showArrows:true});
		
		if ($('.content.friend.list .friendList').length > 0){
			$('section#mainContent section.content div.tabSystem ul#tabContentSwitcher li a').click(function(e){
				$('.content.friend.list .friendList').each(function(){
					//console.log($(this).html());
					$(this).jScrollPane({showArrows:true});
				});
			});
			$('.content.friend.list .friendList').each(function(){
				//console.log($(this).html());
				$(this).jScrollPane({showArrows:true});
			});
			
		}
		
		
		if ($('.friendDetails').length > 0)
			$('.friendDetails').jScrollPane({showArrows:true});
			
		$('.friendPictoList .scrollable').scrollable({prev:'a.prev.picto',next:'a.next.picto',disabledClass:'disab'});
		
		$('a.checkbox').click(function(){
			fakeCheckbox();
		});
		function fakeCheckbox(){
			$('a.checkbox').toggleClass('checked');
			if( $('input#form_subscription_terms').is(':checked') ){
				$('input#form_subscription_terms').attr('checked', false);
			} else {
				$('input#form_subscription_terms').attr('checked', true);
			};
		}
		
		function promoBox(){			
			var $promoBox = $('.promoBox');
			var $promoBoxCycle = $('.items',$promoBox);
			
			if ( $("li",$promoBoxCycle).size() > 1 ) {
				//Add page Number
				$promoBox.prepend('<div class="promoboxPageNum">1</div>');
				// Add pager
				$promoBox.append('<div class="promoboxPager"></div>');
				// Init Cycle
				$promoBoxCycle.cycle({ 
					fx: 'scrollLeft', // animation
					speed: 1000, // animation speed
					timeout: 7000, // timeout between cycle
					pager: '.promoboxPager',
					pagerEvent: 'click', 
					pauseOnPagerHover: true,
					before:promoBoxOnBefore,
					after:promoBoxOnAfter
				});			
			}			
		}
		
		function loginBox(){
			//var open = ;
			//log(open)
			$('.displayLogin').click(function(e){
				e.preventDefault();
				
				//log($('#login').find('section.connection').is(':visible'));
				if (!$('#login').find('section.connection').is(':visible')){
					$('.loginBackground').show();
					if ( $.browser.msie && $.browser.version==='7.0') { 
						$('.loginBackground').hide();
					}
					$('#login').find('section.connection').show().animate({'width': 860, 'height': 400}, 200, 'linear',function(){
						//open=!open;
					});
				} else {
					
					$('#login').find('section.connection').animate({'width': 0, 'height': 0}, 200, 'linear',function(){
						//open=!open;
						$(this).hide();
						$('.loginBackground').hide();
						$("#cont_login").animate({'left': 0}, 500, 'linear',function ( ){});
					});
				}

			});

		}
		
		function tabContent(){
			if ($('.tabSystem').length > 0){
				//Create tabs
				var tabList = '<ul id="tabContentSwitcher">';
				var idx = 1;
				$('.tabContent').each(function(){
					var tabID = 'tabContent'+idx;
					$(this).attr('id',tabID);
					var title = $(this).prev();
					var titleLink = '<li><a href="#'+tabID+'">'+title.html()+'</a></li>';
					title.remove();
					tabList += titleLink;
					idx++;
				});
				tabList += '</ul>';
				$('.tabSystem').prepend(tabList);
				//Activate first tab
				$('.tabSystem').find('a:first').addClass('active');
				$('.tabItem:first').show();
				//Switch tabs
				$('#tabContentSwitcher').find('a').click(function(e){
					e.preventDefault();
					var href = $(this).attr('href');
					//Bulletproof ID (to prevent IE7 bug)
					var startIdx = href.indexOf('#');
					var targetID = href.slice(startIdx, href.length);
					$('.tabContent:visible').parents('.tabItem').hide();
					$(targetID).parents('.tabItem').show();
					$('.tabSystem a').removeClass('active');
					$(this).addClass('active');
					//Reinit pictoList scrollbar
					var thisPictoList = $(targetID).parents('.tabItem').find('.pictoList');
					if (thisPictoList.length > 0){
						thisPictoList.data('jsp').reinitialise();
					}
				});
			}
		}
		
		function picto(){
			if ($('.pictoList').length > 0){
				//Add scroller
				$('.pictoList:first').jScrollPane({showArrows: true});
				//Prevent click on inactive items

			}
		}
		
		function profileBox(){
			$('a.myprofile').click(function(e){
				e.preventDefault();
				$(this).colorbox({inline:true,scrolling:false, width:550, height: 580, opacity:0.6, href:$('#profileBox').html(), onComplete:function(){
					$.colorbox.resize();
					
					
					$( ".sortable" ).sortable();
					$( ".sortable" ).disableSelection();
					$('.sortable').shuffle();
					
				}});
			});
			//Language switcher
			$('.profileBoxContent').find('.language a').live('click',function(e){
				if( !$(this).hasClass('active') ){
					var langValue = $(this).text().toLowerCase();
					$(this).parents('form').find('input#form_myprofile_language').val(langValue);
					$(this).siblings().removeClass('active');
					$(this).addClass('active');
				}
			});
		}
		
		function searchBox(){
			$('label.include').each(function(){
				//Move label into field
				searchLabel = $(this);
				var labelTxt = searchLabel.text();
				var searchInput = searchLabel.next('input');
				searchInput.val(labelTxt).data('label_txt',labelTxt);
				searchLabel.remove();
				//Empty/fill field
				searchInput.focusin(function() {
					//jc
					if ($(this).attr('id')==='form_subscription_repasswordtmp'){
						$("#form_subscription_repasswordtmp").hide();
						$("#form_subscription_repassword").show().focus();
						
					}else if ($(this).attr('id')==='form_subscription_passwordtmp'){
						$("#form_subscription_passwordtmp").hide();
						$("#form_subscription_password").show().focus();
						
					}else if ($(this).attr('id')==='form_connection_passwordtmp'){
						$("#form_connection_passwordtmp").hide();
						$("#form_connection_password").show().focus();
						//jc end
					}else if ($(this).val() == $(this).data('label_txt')) {
						
						$(this).val('');
						
						
					}
				}).focusout(function() {
					
					$(this).val($.trim($(this).val()));
					if ($.trim($(this).val()) == '') {
						
						
							
							
								$(this).val($(this).data('label_txt'));
							
						
					}
				});
			});			
		}
		var ppage=$('#header').attr('role');
		if (ppage==='gift|index'){
			$('#main').parallax({
				'elements':[
//					{
//						'selector': '#main',
//						'properties': {
//							'x': { 'left': {'initial':0, 'multiplier':0.01, 'invert':true} },
//							'y': { 'top': {'initial':0, 'multiplier':0.01, 'invert':true} }
//						}
//					},
					{
						'selector': 'section#logo',
						'properties': {
							'x': { 'left': {'initial':30, 'multiplier':0.02, 'invert':true} }
						}
					},
					{
						'selector': 'section#login',
						'properties': {
							'x': { 'left': {'initial':785, 'multiplier':0.04, 'invert':false} },
							'y': { 'top' : {'initial':120, 'multiplier':0.04, 'invert':false} }
						}
					},
					{
						'selector': 'section#appli',
						'properties': {
							'x': { 'left': {'initial':800, 'multiplier':0.01, 'invert':false} }
						}
					},
					{
						'selector': 'section#picto',
						'properties': {
							'x': { 'left': {'initial':750, 'multiplier':0.02, 'invert':false} }
						}
					},
					{
						'selector': 'section#facebook',
						'properties': {
							'x': { 'right': {'initial':65, 'multiplier':0.08, 'invert':true} }
						}
					},
					{
						'selector': 'section#news',
						'properties': {
							'x': { 'left': {'initial':40, 'multiplier':0.04, 'invert':true} },
							'y': { 'top': {'initial':465, 'multiplier':0.02, 'invert':false} }
						}
					}
				]
			});
		}else{
		$('#main').parallax({
			'elements':[
//				{
//					'selector': '#main',
//					'properties': {
//						'x': { 'left': {'initial':0, 'multiplier':0.01, 'invert':true} },
//						'y': { 'top': {'initial':0, 'multiplier':0.01, 'invert':true} }
//					}
//				},
				{
					'selector': 'section#logo',
					'properties': {
						'x': { 'left': {'initial':30, 'multiplier':0.02, 'invert':true} },
						'y': { 'top': {'initial':0, 'multiplier':0.02, 'invert':true} }
					}
				},
				{
					'selector': 'section#login',
					'properties': {
						'x': { 'left': {'initial':785, 'multiplier':0.04, 'invert':false} },
						'y': { 'top' : {'initial':120, 'multiplier':0.04, 'invert':false} }
					}
				},
				{
					'selector': 'section#appli',
					'properties': {
						'x': { 'left': {'initial':800, 'multiplier':0.01, 'invert':false} }
					}
				},
				{
					'selector': 'section#picto',
					'properties': {
						'x': { 'left': {'initial':750, 'multiplier':0.02, 'invert':false} }
					}
				},
				{
					'selector': 'section#facebook',
					'properties': {
						'x': { 'right': {'initial':65, 'multiplier':0.08, 'invert':true} }
					}
				},
				{
					'selector': 'section#news',
					'properties': {
						'x': { 'left': {'initial':40, 'multiplier':0.04, 'invert':true} },
						'y': { 'top': {'initial':465, 'multiplier':0.02, 'invert':false} }
					}
				}
			]
		});
		}
	});
}(jQuery));
