// Buttons Yes/No
//----------------------
var btn_thumb = function(){
	$('.film_list .btn_thumb, .social_results .btn_thumb, .moviePage .actionsBlock .btn_thumb').click(function(){
		if ($(this).hasClass('btn_on')){
			 $(this).removeClass("btn_on");
		} else {
			$(this).parent('.btn_thumbs').find('.btn_thumb').removeClass("btn_on");
			$(this).addClass("btn_on");
		}
	});
}

// Buttons Yes/No Speednoter
//----------------------
var btn_thumb_sn = function(){
	$('.w-interest .button').click(function(){
		if ($(this).hasClass('btn06')){
			if ($(this).hasClass('btn06_on')){
				 $(this).removeClass("btn06_on");
			} else {
				$(this).addClass("btn06_on");			
			}
			$(this).parent('.w-interest').find('.btn07').removeClass("btn07_on");
		} 
		else if ($(this).hasClass('btn07')) {
			if ($(this).hasClass('btn07_on')){
				 $(this).removeClass("btn07_on");
			} else {
				$(this).addClass("btn07_on");
			}
			$(this).parent('.w-interest').find('.btn06').removeClass("btn06_on");			
		}
		
	});
}



//Filters
//--------
var toggle_filters = function(){
	$('.filters_trigger ul li').toggleClass('current');
	$('.filters_trigger ul li').toggleClass('current');
	$('body').toggleClass('open');
}


// Filters on/off
//------------------

var btn_filter = function(){
	
		// trigger filter col
	/*$('.filters_trigger > span, .filters_trigger #footer_search i' ).click(function(){	
		toggle_filters();
	});*/

	$('.column_button' ).click(function(){	
		toggle_filters();
		$(this).toggleClass("btn_search_on");
		setTimeout("centerFilmListBloc()",600);
	});
		
	$('.filters_trigger ul li span').click(function(){
		if (!$(this).parent().hasClass('current')){
			$(this).parent().find('li').removeClass('current').end();
			$(this).parent().find('li').addClass('current');
			toggle_filters();
		}
	});
		
}


/*var btn_filter = function(){
	$('.filters_trigger ul li span').click(function(){
		if (!$(this).parent().hasClass('current')){
			$(this).parents('.filters_trigger ul').find('li').removeClass('current');
			$(this).parent('.filters_trigger ul li').addClass('current');
			toggle_filters();
		}
	});
	
	$('.filters_trigger form i').click(function(){
			$(this).parents('.filters_trigger ul').find('li').removeClass('current');
			$(this).parent('.filters_trigger ul li').addClass('current');
			toggle_filters();
	});
	
	$('.filters_trigger > span').click(function(){
		$(this).parent('.filters_trigger').find('ul li').toggleClass('current');
			toggle_filters();
	});
}

*/

// Filters on/off
//------------------
var btns_filter = function(){
	
	$('.filters_trigger > span').click(function(){
		$(this).parent('.filters_trigger').find('ul li').toggleClass('current');
			toggle_filters();
	});
}



// Manage profiles
//------------------
var manage_profiles = function(){

	$('.add_profiles span').click(function(){	
		$('.manage_profiles > div ').hide();
		$('.connect_profiles').show();
	});
	
	$('.logout' ).click(function(){	
		$('.manage_profiles > div ').hide();
		$('.deconnect_profiles').show();
	});
	
	$('.deconnect' ).click(function(){	
		$('.deconnect_profiles').hide();
	});
		
	
}



//ACCORDION


$('.adCntnr div.acco2:eq(0)').find('div.collapse').hide().end()
	.find('div.expand').click(function() {
$(this).toggleClass('openAd').siblings().removeClass('openAd').end()
	.next('div.collapse').slideToggle().siblings('div.collapse:visible').slideUp();
	return false;
});


/*
var initCollapsed = function(){
	$('.collapse').addClass('invisibility');
}
initCollapsed();



$('.adCntnr div.acco2 .expand').click(function() {	
	
	$(this).
		toggleClass('openAd').
		siblings().
		removeClass('openAd').
		end().
		next('.collapse').
		hide().
		removeClass('invisibility').		
		slideToggle().		
		siblings('.collapse:visible').
		slideUp();	
	return false;		
});
*/


//MODAL (http://www.ericmmartin.com/projects/simplemodal/)
/*
$('#basic').click(function (e) {
	$('#basic-modal-content').modal({overlayClose: true});
	return false;
});
*/

$("body:not('.profile') .facettes").mCustomScrollbar({
	advanced:{
	    updateOnContentResize: true
	},
	autoDraggerLength:false,
	scrollButtons:{
		enable:false
	}
});

$('#basic2').click(function (e) {
	$('#basic-modal-content2').modal({
		containerCss:{
		height:320,
		width:690
		},
		overlayClose: true
	});
	return false;
});


$('.filters_search input[type="text"]').click(function () {
	$(this).parent().find('.search_dropdown').toggle();
});

$('.search_dropdown ul li a').click(function () {
	$(this).parents('.search_dropdown').hide();
	return false;
});



var openNodal = function(){
	$('#basic-modal-content3').modal({
		containerCss:{
		height:460,
		width:900
		},
		overlayClose: true
	});
	
	$('.simplemodal-close').hide();
	
	$('.btn_see ').click(function(){
		$('.simplemodal-close').trigger('click');
	});
}, modifMovieModal = function(){
	$('#modif-movie-modal').modal({
		containerCss:{
		height:268,
		width:672
		},
		overlayClose: true
	});
	
	$('.btn_see ').click(function(){
		$('.simplemodal-close').trigger('click');
	});
}, addMovieModal = function(){
	$('#add-movie-modal').modal({
		containerCss:{
		height:268,
		width:672
		},
		overlayClose: true
	});
	
	$('.btn_see ').click(function(){
		$('.simplemodal-close').trigger('click');
	});
}, fullVitrineModal = function(){
	$('#full-vitrine-modal').modal({
		containerCss:{
		height:160,
		width:590
		},
		overlayClose: true
	});
	
	$('.btn_see ').click(function(){
		$('.simplemodal-close').trigger('click');
	});
}, commentModal = function(){
	$('#comment-modal').modal({
		containerCss:{
		height:238,
		width:672
		},
		overlayClose: true
	});
	
	$('.btn_see ').click(function(){
		$('.simplemodal-close').trigger('click');
	});
}, allBadgesModal = function(){
	$('#allBadges-modal').modal({
		containerCss:{
		height:540,
		width:622
		},
		overlayClose: true
	});
	
	$('.btn_see ').click(function(){
		$('.simplemodal-close').trigger('click');
	});
}, newBadgesModal = function(){
	$('#newBadges-modal').modal({
		containerCss:{
		height:268,
		width:612
		},
		overlayClose: true
	});
	
	$('.btn_see, .valid_badge ').click(function(){
		$('.simplemodal-close').trigger('click');
	});
}

$('.film_list .btn_thumbs .btn_yes').click(function(){
	$('#interest-modal-content').modal({
		containerCss:{
		height:310,
		width:590
		},
		overlayClose: true
	});
});
$('.film_list .btn_thumbs .btn_no').click(function(){
	$('#not-interest-modal-content').modal({
		containerCss:{
		height:310,
		width:590
		},
		overlayClose: true
	});
});

//Center film list block
var centerFilmListBloc = function(){

	var wArea = $(".results").width(),
		w = wArea % $(".film_list li").outerWidth();

	$(".results > ul").stop(true,true).width(wArea - w);

}, mediaQueriesTest = function(){
	var windowWidth= window.innerWidth;

	if(windowWidth < 1352){
		$(".other_container").insertAfter(".container_table_row .like_container:eq(0)");
	} else{
		$(".other_container").insertAfter(".container_table");
	}
}, synopsisDotDotDot = function(){
	var heightSynop = $(".synopsis").height(),
		synopContent = $(".synopsis").triggerHandler("originalContent");

	$(".synopsis").dotdotdot({
        watch: "window",
        callback: function() {
			$(".sinopSee").removeClass("allText");
			$(".sinopSee").addClass("dotDoted");
			$(".sinopSee span").text("+");
        }
    });
    $(".dotDoted").live('click',function() {
	    $(".synopsis").trigger("destroy");
	    $(".synopsis").height("auto");
	    $(".sinopSee").addClass("allText");
	    $(".sinopSee").removeClass("dotDoted");
	    $(".sinopSee span").text("-");
	});
    $(".allText").live('click',function() {
    	$(".synopsis").height(heightSynop);
	    $(".synopsis").dotdotdot({
	        watch: "window",
	        callback: function() {
				$(".sinopSee").addClass("dotDoted");
				$(".sinopSee").removeClass("allText");
				$(".sinopSee span").text("+");
	        }
	    });
	});

}

//Speednoter Animation
var activateSeeRecommandations = function(){
	$(".stillToRate").animate({"opacity":"0"},300);
	$(".stillToRate").animate({width: 'toggle'},500);
	$(".btn_see").animate({"opacity":"1"},1500);

	/* $('.my_reco').attr({
		href: 'new_link...',
		title: '...'
	}); */
}, youhouNumber = function(){
	$(".stillToRate span").addClass("youhou");
	setTimeout(function() {
		$(".stillToRate span").removeClass("youhou");
	}, 300);//the same time like ".stillToRate span" CSS transition animation
}

//profile - hide message tuto
var closeMsgTuto = function(){
	$('.msg_box i.close').click(function(){
		$(this).parent().animate({"opacity":"0"},300,function(){
			$(this).slideUp();
		});
	});
}
$(function() {

	$(window).resize(function(){
		centerFilmListBloc();
		mediaQueriesTest();
	});
	mediaQueriesTest();
	centerFilmListBloc();
	//socialGrid();
	// manageyes/no buttons
	if($('.film_list .btn_thumb').length){
		 btn_thumb();
	}
	if($('.social_results .btn_thumb').length){
		 btn_thumb();
	}
	if($('.moviePage .actionsBlock .btn_thumb').length){
		 btn_thumb();
	}
	
	// manageyes/no buttons speednoter
	if($('.speednoter').length){
		 btn_thumb_sn();
	}
	
	//
	if($('.msg_box i.close').length){
		 closeMsgTuto();
	}

	// trigger filter col
	btn_filter();
	
	manage_profiles();

		/* fiche film */
	$('.movie_vitrine').not(".vitrineFull").click(function (e) {
		addMovieModal();
		return false;
	});
	$('.vitrineFull').click(function (e) {
		fullVitrineModal();
		return false;
	});
		/* vitrine */
	$('.edit_btn').click(function (e) {
		modifMovieModal();
		return false;
	});
	$('.vitr_list .overlay_area .desc').click(function (e) {
		commentModal();
		return false;
	});
	$('.moreBadges').click(function (e) {
		allBadgesModal();
		return false;
	});
	$('nav .notification').click(function (e) {
		newBadgesModal();
		return false;
	});
	
		/* */
	$('.simple_modal_trigger').click(function (e) {
		$('#simple_modal').modal({
			containerCss:{
			height:320,
			width:690
			},
			overlayClose: true
		});
		return false;
	});
	
	$('#basic').click(function() {
		$("#basic-modal-content").modal({
			containerCss:{
				height:440,
				width:690
				},
			overlayClose: true,
			onShow : function() {
				$(".star_names").
					mCustomScrollbar({
						autoDraggerLength:false,
						scrollButtons:{
							enable:false
						}
					});
				}
			});
		return false;
	});

	//TOOLTIP INFO VIGNETTE
	var show_tooltip_info = function(elem){

		var clWidth = $("body").width(),//WIDTH AREA
			$thisEl = elem.parent().parent().parent(),// target LI
			$thisElDiv = $thisEl.children("div"),// target LI DIV
			elWidth = elem.parent().find(".tooltip_info").outerWidth() + elem.parent().find(".arrow_tooltip").outerWidth() ,//WIDTH 1 ELEMENT
			thisElPos = $thisEl.offset();//POSITION ELEMENT

		//move on the left IF the last line element
		if ((clWidth - thisElPos.left) < elWidth*2) {
			$thisEl.find(".tooltip_info, .arrow_tooltip").addClass("posLeft");
		} else {
			$thisEl.find(".tooltip_info, .arrow_tooltip").removeClass("posLeft");
		};

		//tooltip vetical alignement
		var vignHeight = $thisElDiv.outerHeight(),
			tooltipHeight = $thisEl.find(".tooltip_info").outerHeight(),
			toAddToTop = Math.round((vignHeight - tooltipHeight)/2);
			
			
			if(toAddToTop > -3){
				toAddToTop = -3;
			}
			var toAddToTopArrow = 13 - toAddToTop;

		$thisEl.find(".tooltip_info").css({"top":toAddToTop+"px"});
		$thisEl.find(".arrow_tooltip").css({"top":toAddToTopArrow+"px"});
		
	}
	//SHOW TOOLTIP
	$(".info_btn").hover(function(){
		show_tooltip_info($(this));
	},function(){
		show_tooltip_info($(this));
	});

	//JCAROUSEL
	if ( $('.carousels .js_jcar').length ) {
		$('.carousels .js_jcar').each(function(){
			$(this).jcarousel();
		});
	}

});