/*addthis configs*/

var addthis_config = {
     ui_language: "fr",
	 ui_header_color: "#ffffff",
	 services_exclude: 'print',
     ui_header_background: "#440000"

}
/*use my addthis email template*/
var addthis_share = { email_template: "virile" }

/*
footer drop Down
*/

$(document).ready(function(){    
	var saison =$("#dropdownSeason div:first").attr("title")
		if($(".selection .dropdownbutton").text() != saison&& saison != ""){
			
			$(".selection .dropdownbutton").html(saison)
			saison = saison.replace(" ","")
			saison = saison.replace("S","s")
			bgImage = "/library/images/background/bg_"+saison+".gif"
			$("#saisons .bg").css("backgroundImage",  'url('+bgImage+')')
		}
	$("a").live("click",function() {
		// Act on the event
		$.validationEngine.closePrompt(".formError",true)
	});
	$('.regenerateCaptcha').live("click",function () {
		$.ajax({url: '/misc/ajaxregeneratecaptcha/',type: 'get',
			   success: function (data) {
							var response = eval("("+data+")");
							if (1 == response.error)$('#captchabox').html(response.box);
						}
			 });
	});
	$(".selecter").click(function() {
		// Act on the event
		$(this).parent().find(".selecter").removeClass("selected")
		$(this).addClass("selected")
		return false;
	});
    $("div.dropDown").hover(function () {
      $(this).children().eq(1).not(":animated").slideDown("fast");
    }, function() {
		$(this).children().eq(1).slideUp("fast");
	});
	
	
  $("div.dropDown2").hover(function () {
      $(this).children().eq(0).not(":animated").slideDown("fast");
    }, function() {
		$(this).children().eq(0).slideUp("fast");
	});
/*show commenting system accordion*/


$(".playerbloc .comments").click(function(){
	  $("div.commentSys").slideToggle("slow")
	  $(this).toggleClass("active");
	  FB.XFBML.Host.parseDomTree();
	});
$(".playerbloc .comments").hover(function() {
	// Stuff to do when the mouse enters the element;
	 $("#linkComment").html("Bienvenue aux dames")
}, function() {
	// Stuff to do when the mouse leaves the element;
	 $("#linkComment").html("Commentaires de vrai mâle")
});	
	
	

$("#lire").click(function(){
	  $("div.commentSys").slideToggle("slow")
	  $(".playerbloc .comments").toggleClass("active");
	  FB.XFBML.Host.parseDomTree();
	});

/*modal box popup init*/
	$("a[rel^='prettyPopin']").prettyPopin({
			modal : false, /* true/false */
			width : false, /* false/integer */
			height: false, /* false/integer */
			opacity: 0.5, /* value from 0 to 1 */
			animationSpeed: 'fast', /* slow/medium/fast/integer */
			followScroll: true, /* true/false */
			loader_path: '/library/images/prettyPopin/loader.gif', /* path to your loading image */
			callback: function(){} /* callback called when closing the popin */
		});
	$("#header #email2").click(function() {
		// Act on the event
		$(this).val("")
	});
/* Rating */	
	$(".playerbloc #rating_href").click(function(){
		var id = $(this).attr("rel")
		$.ajax({url: '/misc/ajaxsavevote/video/' + id,type: 'get',
			   success: function (data) {
							var response = eval("("+data+")");
							if (1 == response.error) $('#rating_total').html(response.vote);
						}
			 });
	});
	
	var windowWidth = $(window).width()	
	var totalWidthMax =1110
	var totalWidth = 150
	var padMax = 30
	
	
 	if(windowWidth < 1110 && windowWidth > 960){
 	
	 		currentsmallWidth = totalWidthMax- windowWidth - totalWidth
	 		currentsmallWidth -= (currentsmallWidth* 2) 
	 		var padding = (padMax / totalWidth * currentsmallWidth)/2 
	 		
			padding += "px"
	 		windowWidth += "px"

	 		$("#siteWrap").css("width",windowWidth)
	 		$(".boxcarroussel").css("paddingRight",padding)
 			$(".boxcarroussel").css("marginRight",padding)
 	}
 	if(windowWidth > 1110){
		$(".boxcarroussel").css("paddingRight","30px")
		$(".boxcarroussel").css("marginRight","30px")
	}
	if(windowWidth < 960){
		$("#siteWrap").css("width","960px")
		$(".boxcarroussel").css("paddingRight","5px")
	}		
	$(window).resize(function(){
		windowWidth = $(window).width()
	
	 	if(windowWidth < 1110 && windowWidth > 960){
	 		currentsmallWidth = totalWidthMax- windowWidth - totalWidth
	 		currentsmallWidth -= (currentsmallWidth* 2)
	 		var padding = (padMax / totalWidth * currentsmallWidth)/2
	 	
			padding += "px"
	 		windowWidth += "px"

	 		$("#siteWrap").css("width",windowWidth)
	 		$(".boxcarroussel").css("paddingRight",padding)
	 		$(".boxcarroussel").css("marginRight",padding)
	 	}
	 		if(windowWidth > 1110){
	 			$("#siteWrap").css("width","1110px")
	 			var pad = padMax + "px"
	 			$(".boxcarroussel").css("paddingRight",pad)
	 			$(".boxcarroussel").css("marginRight",pad)
	 		}
	});


  });

function blameComment (elem) {
	/* Signal comment*/
	var id = $(elem).attr("rel")
		$.ajax({url: '/misc/ajaxsignalcomment/comment/' + id,type: 'get',
			   success: function (data) {
							var response = eval("("+data+")");
							if (1 == response.error){
								$('#totalComments').html(parseInt($('#totalComments').html())-1);
								$.each( ['#commentH1_','#commentH2_', '#commentFlag_'], function(index, item){
									$(item + id).fadeOut("normal");
								});
							}
						}
			 });
}

function submitComment(formid) {
	$.ajax({url: '/misc/ajaxsavecomment',type: 'post',data: $(formid).serialize(),
		   success: function (data) {
						var response = eval("("+data+")");
						$('#errorLayer').html('');
						if (response.error == 1) {
							$('#commentListDL').prepend(response.box);
							$('#commentaire_raw').hide();
							$('#commentaire_fb').hide();
							$('#totalComments').html(parseInt($('#totalComments').html())+1);
							clear_form_elements('#commentaire_raw');
							clear_form_elements('#commentaire_fb');
							var divOffset = $('#commentListDL').offset().top ;
							FB.XFBML.Host.parseDomTree();
							if (response.publishFB == 1) {
								var attachment = {'media':[{'type':'image','src':unescape(response.thumbnailURL),'href':unescape(response.videoURL)}],
								                  'caption':'Contrat d\'gars - '+response.videoTitle,
								                  'description':response.videoDescription}; 
								var actionLinks = [{"text": "Voir le site de Contrat d'gars", "href": unescape(response.siteURL)}];
								FB.Connect.streamPublish(response.commentText, attachment, actionLinks, null, 'Montre à tous tes chums que t\'es un vrai mâle.', function(){$('html,body').animate({scrollTop: '+=-' + divOffset + 'px'}, 1000)});
							} else {
								$('html,body').animate({scrollTop: '+=-' + divOffset + 'px'}, 1000);
							}
							
						} else {
							$('#errorLayer').html('Erreur!  Impossible de sauver ton commentaire');
						}
					}
		 });
}
function reloadVideoCommentListing(videoID, page) {
	$.ajax({url: '/misc/ajaxreloadvideocommentslist/video/' + videoID + '/page/' + page,type: 'get',
		   success: function (data) {
						var response = eval("("+data+")");
						if (1 == response.error) {
							$('#videoCommentListing').html(response.box);
							FB.XFBML.Host.parseDomTree();
						}
					}
		 });
}

$("#sortByRating").click(function(){
	  reloadVideoListing('videoListing_slice_of_life', 1, 'rating');
	  $(this).toggleClass("selected");
	});

function submitBioMail(formid) {
	$.ajax({url: '/bio/ajaxsendmail',type: 'post',data: $(formid).serialize(),
		   success: function (data) {
						var response = eval("("+data+")");
						if (response.error == 1) {
							$('#formBio').html(response.html);
						} else {
							$('#errorLayerBio').html(response.error);
						}
					}
		 });
}

function update_comment_box() {
	$('#commentaire_raw').hide();
	$('#commentaire_fb').show();
	$('#fbuid').val(FB.Facebook.apiClient.get_session().uid);
	$('.fconnect').html('<fb:profile-pic uid=\'loggedinuser\' facebook-logo=\'true\'></fb:profile-pic>');
	FB.XFBML.Host.parseDomTree();
}

function fbLogout(){
	FB.Connect.logout();
	jQuery('span#fbLogin').show();
	jQuery('span#loggedin').empty();
	jQuery('span#logged').text('0');
}

function clear_form_elements(ele) {
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}
