
function eanim()
{
	$(".main-error, .main-success").animate({opacity: "0.9"}, 800).delay(3000).animate({opacity: "0"}, 800);
	$(".error, .success, .error-centered, .success-centered").animate({opacity: "1"}, 800);
}

function setOverlayHeight()
{	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 

	$(".overlay-bg").css({"height" : arrayPageSize[1]});
}

$(document).ready(function() {

	setOverlayHeight();
	
	$("input[name=reg_form_surname],input[name=contact_form_surname], input[name=log_form_email], input[name=fpass_form_email], input[name=npass_form_key], input[name=validation_form_key], input[name=upload_form2_answer1], input[name=password_form_oldpassword], input[name=guess_form_answer]").focus();
	eanim();
	
	$("form").submit(function() {
		$("button", this).attr("disabled", "disabled");
	});

	$("form.upload-form").submit(function() {
		$(".loading").show();
	});
	
	$(".sponsor, #sponsor-overlay-close, #sponsor-overlay-bg").click(function() {
		$("#sponsor-overlay, #sponsor-overlay-bg").toggle();
	});
				
	$(".comments-lnk-next").live("click",function() {
		$(".loading").show();
		var id=$(".current-pic-id").html();
		var start=$(".comments-first").html();
		$(".comments-content").animate({opacity: "0"}, 200).load("ajax/a_comments.php?action=next&start="+start+"&pic_id="+id, function(){$(this).animate({opacity: "1"}, 200);$(".loading").hide();});
	});
	
	$(".comments-lnk-previous").live("click", function() {
		$(".loading").show();
		var id=$(".current-pic-id").html();
		var start=$(".comments-first").html();
		$(".comments-content").animate({opacity: "0"}, 200).load("ajax/a_comments.php?action=previous&start="+start+"&pic_id="+id, function(){$(this).animate({opacity: "1"}, 200); $(".loading").hide();});
	});
	
	$(".bookmarks-title, .bookmarks-close, .ub-bm").click(
		function () {
			$(".bookmarks").toggle();
			$.ajax({type: "POST", url: "ajax/a_bookmarks_state.php"});
		}
	);
			
	$(".single-photo .bm-icon").live("click",function() {
		$(".loading").show();
		var id=$(".current-pic-id").html();
		$.post("ajax/a_bookmarks.php?action=add&pic_id="+id, function(result){
			$(".bookmarks .bookmarks-container").load("ajax/a_bookmarks.php?action=load");
			$(".mainmsg-container").html(result);
			eanim();								
			$(".loading").hide();				
		});			
	});
	
	$(".photoset .bm-icon").live("click",function() {
		$(".loading").show();
		var id=$(".bm-id", this).html();
		$.post("ajax/a_bookmarks.php?action=add&pic_id="+id, function(result){
			$(".bookmarks .bookmarks-container").load("ajax/a_bookmarks.php?action=load");
			$(".mainmsg-container").html(result);
			eanim();								
			$(".loading").hide();				
		});			
	});

	$(".bookmarks .bookmarks-icon").live("click", function() {
		$(".loading").show();
		var id=$(".bookmarks-id", this).html();
		$.post("ajax/a_bookmarks.php?action=delete&pic_id="+id, function(result){
			$(".bookmarks .bookmarks-container").load("ajax/a_bookmarks.php?action=load");
			$(".mainmsg-container").html(result);
			eanim();			
			$(".loading").hide();	
		});			
	});

	$(".single-photo .flag-icon").click(function() {
		$(".loading").show();
		var id=$(".current-pic-id").html();
		$.post("ajax/a_flag.php?pic_id="+id, function(result){
			$(".mainmsg-container").html(result);
			eanim();										
			$(".loading").hide();					
		});
	});

	$(".comment .comment-flag").live("click", function() {
		$(".loading").show();
		var id=$(".comment-id", this).html();
		$.post("ajax/a_flag_comment.php?com_id="+id, function(result){
			$(".mainmsg-container").html(result);
			eanim();										
			$(".loading").hide();					
		});
	});

	//suppression notification
	$(".notification-delete").live("click", function() {
		$(".loading").show();
		var id=$(".notification-id", this).html();
		var action=$(".notification-action", this).html();
		var mode=$(".notification-mode", this).html();
		var page="";
		if(mode != "home"){
			var page="&page="+$(".pages-nb").attr("id");
		}
		$.post("ajax/a_notification.php?delete&pic_id="+id+"&action="+action+"&mode="+mode+page, function(result){
			if(result != 0){
				$(".notification").html(result);
				$(".notification-count").load("ajax/a_notification.php?count");
			}							
			$(".loading").hide();				
		});	
	});
	
	//remise en ligne notification
	$(".notification-postagain").live("click", function() {
		$(".loading").show();
		var id=$(".notification-id", this).html();
		var action=$(".notification-action", this).html();
		var mode=$(".notification-mode", this).html();
		var page="";
		if(mode != "home"){
			var page="&page="+$(".pages-nb").attr("id");
		}
		$.post("ajax/a_notification.php?postagain&pic_id="+id+"&action="+action+"&mode="+mode+page, function(result){
			if(result == 1 || result == 2)
			{
				var msg_id = result;
			}
			else
			{
				var msg_id = 0;
				//ne rafraichit le contenu que si pas d'erreur
				$(".notification").html(result);
				$(".notification-count").load("ajax/a_notification.php?count");	
			}			
			$.post("ajax/a_notification.php?getmsg&id="+msg_id, function(msg){
				$(".mainmsg-container").html(msg);
				eanim();	
			});	
			$(".loading").hide();
		});
	});
	
	//GA events
	$(".single-photo .bm-icon").live("click",function() {
		_gaq.push(["_trackEvent", "Bookmarks", "Add"]);
	});
	$(".ppic").live("click",function() {
		_gaq.push(["_trackEvent", "SinglePic", "Previous"]);
	});
	$(".npic").live("click",function() {
		_gaq.push(["_trackEvent", "SinglePic", "Next"]);
	});
	$(".bookmarks .bookmarks-icon").live("click", function() {
		_gaq.push(["_trackEvent", "Bookmarks", "Delete"]);
	});
	$(".comments-lnk-previous").live("click", function() {
		_gaq.push(["_trackEvent", "Comments", "Previous"]);
	});
	$(".comments-lnk-next").live("click", function() {
		_gaq.push(["_trackEvent", "Comments", "Next"]);
	});
	$(".sponsor").live("click",function() {
		_gaq.push(["_trackEvent", "Sponsor_overlay", "Show"]);
	});
	$(".ub-bm").live("click",function() {
		_gaq.push(["_trackEvent", "Bookmarks", "Open"]);
	});
	$(".notification-postagain").live("click",function() {
		_gaq.push(["_trackEvent", "Notification", "PostAgain"]);
	});
	$(".notification-delete").live("click",function() {
		_gaq.push(["_trackEvent", "Notification", "Delete"]);
	});
			
	$("#ub-fb-login").click(function() {
		FB.login(function(response) {
			if (response.authResponse) {
				var loc = "http://www.phozz.com/registration";
				window.location.replace(loc);
			} else {
			};
		}, {scope: 'email'});
	});	
	$("#account-fb-login").click(function() {
		FB.login(function(response) {
			if (response.authResponse) {
				var loc = "http://www.phozz.com/account-linkfb";
				window.location.replace(loc);
			} else {
			};
		}, {scope: 'email'});
	});
	$("#fb_regshare").click(function() {
		FB.ui(
		  {
			method: 'feed',
			name: 'Phozz - Quizz photos communautaire',
			link: 'http://www.phozz.com',
			picture: 'http://www.phozz.com/res/logoapi.png',
			caption: 'Envie de découvrir de nouveaux horizons, le lieu de vos prochaines vacances, de tester votre culture générale, venez vite sur Phozz !',
			description: 'Des centaines de photos insolites sont à découvrir chaque jour. Postez les vôtres et défiez vos amis !'
		  },
		  function(response) {
			if (response && response.post_id) {			  
				$(".mainmsg-container").html("<div class=\"main-success\">Le message a été posté sur votre mur Facebook</div>");
				eanim();
			} else {
				$(".mainmsg-container").html("<div class=\"main-error\">Erreur : le message n'a pas pu être posté sur Facebook</div>");
				eanim();
			}
		  }
		);
	});
	
	//GPLUS
	window.___gcfg = {lang: "fr"};
	$(function() {
		var po = document.createElement("script"); 
		po.type = "text/javascript"; 
		po.async = true;
		po.src = "https://apis.google.com/js/plusone.js";
		var s = document.getElementsByTagName("script")[0]; 
		s.parentNode.insertBefore(po, s);
	});
	
	//FACEBOOK
	$(function(d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) {return;}
		js = d.createElement(s); js.id = id;
		js.src = "//connect.facebook.net/fr_FR/all.js#xfbml=1&appId='.FACEBOOK_APPID.'";
		fjs.parentNode.insertBefore(js, fjs);
	}(document, "script", "facebook-jssdk"));
});

window.fbAsyncInit = function() {
    FB.init({appId:'206391006062592',status:true,cookie:true,xfbml:true,oauth:true});
};



var RecaptchaOptions = {theme : "white", lang : "fr", tabindex : 8};
