/*****************************************************************************************/
/*---------------------------------------------------------------------------------------*/
/*-------------- Shit-the-bed.com, the best like the worst of your life -----------------*/
/*---------------------------------------------------------------------------------------*/
/*----------------------------- Writen by Alexandre Bram --------------------------------*/
/*****************************************************************************************/

/*****************************************************************************************/
/******************************** ALL RIGHTS RESERVED ************************************/
/*****************************************************************************************/


$(document).ready(function() {

// INIT

	$(".add-story").hide();
	$('.error').hide();
	$('#login-conteneur').hide();
	

//****************************************************************************************//
//********************************* ADD A NEW STORY **************************************//
//****************************************************************************************//
	
    $(".click-story").click(function () {
		$(".add-story").toggle();
		return false;
   	 });	
    
	$(".send").click(function () {	
		
		$('.error').hide();
		
		var nickname = $("input#nickname").val();
			if (nickname == "") {
		  $("label#nickname_error").show();
		  $("input#nickname").focus();
		  return false;
		}
		
		var story = $("textarea#story").val();
			if (story == "") {
		  $("label#story_error").show();
		  $("input#story").focus();
		  return false;
		}
		
		var category = $("select#category").val();
		var namechi = $("input#namechi").val();
		
			  Controlstory = 'nickname='+ nickname + '&story='+ story + '&category='+ category + '&namechi=' + namechi;

				$.ajax({
				  type: "POST",
				  url: "/ajax/process.php",
				  data: Controlstory,
				  success: function(data) {
					if(data == 0){
						$('#error_add_story').html("<div id='message'></div>");
						$('#message').html("<h2>Shit the bed ! A error has been occured when you have add your story, try again.</h2>")
						.append("<p>Send us a email if the problem persist.</p>")
						return false;
		  			}else{
						$('.add-story').html("<div id='message'></div>");
						$('#message').html("<h2>You're story has been add Dude !</h2>")
						.append("<p>A moderator will read your story ASAP.;-)</p>")
						return false;
				  }
				  }
			  });

      return false;
    });

//****************************************************************************************//
//********************************* ADD A NEW VIDEO **************************************//
//****************************************************************************************//

$(".send_video").click(function () {	
		
		$('.error').hide();
		
		var nickname = $("input#nickname").val();
			if (nickname == "") {
		  $("label#nickname_error").show();
		  $("input#nickname").focus();
		  return false;
		}
		
		var title_video = $("input#title_video").val();
			if (title_video == "") {
		  $("label#title_video_error").show();
		  $("input#title_video").focus();
		  return false;
		}
		
		var video_description = $("textarea#video_description").val();
			if (video_description == "") {
		  $("label#video_description_error").show();
		  $("input#video_description").focus();
		  return false;
		}
		
		var video_code = $("textarea#video_code").val();
			if (video_code == "") {
		  $("label#video_code_error").show();
		  $("input#video_code").focus();
		  return false;
		}
		
			  Controlvideo = 'nickname='+ nickname + '&title_video='+ title_video + '&video_description='+ video_description + '&video_code='+ video_code;

				$.ajax({
				  type: "POST",
				  url: "/ajax/process.php",
				  data: Controlvideo,
				  success: function(data) {
					if(data == 0){
						$('#error_add_story').html("<div id='message'></div>");
						$('#message').html("<h2>Shit the bed ! A error has been occured when you have add your video, try again.</h2>")
						.append("<p>Send us a email if the problem persist.</p>")
						return false;
		  			}else{
						$('.add-story').html("<div id='message'></div>");
						$('#message').html("<h2>You're video has been add Dude !</h2>")
						.append("<p>A moderator will watch your funny shit ASAP.;-)</p>")
						return false;
				  }
				  }
			  });

      return false;
    });


//****************************************************************************************//
//********************************** ADD A COMMENT ***************************************//
//****************************************************************************************//

$(".add-comment").click(function () {
      
	  	var comment = $("textarea#comment").val();
		if (comment == "") {
		  $("label#comment_error").show();
		  $("input#comment").focus();
		  return false;
		}
		
		var story_id = $("input#story_id").val();
		var user_id = $("input#user_id").val();
		var user_name = $("input#user_name").val();
		
		Addcomment = 'comment='+ comment + '&story_id='+ story_id + '&user_name='+ user_name + '&user_id='+ user_id;

		$.ajax({
				  type: "POST",
				  url: "/ajax/process.php",
				  data: Addcomment,
				  success: function(data) {
					if(data == 0){
						 $('#status').html("<h2>Shit the bed ! A error has been occured when you have add your comment, try again.</h2>");
						 $('#status').fadeOut(5000, function () {
						 	return false;
						 });
		  			}else{
						$('#status').html("<h2>Well done dude !</h2>");
						$('#result_add_comment').html("<div id='message'></div>");
						$('#message').fadeIn(3000, function () {
							$('#message').html(data);
						});									
						$('#status').fadeOut(3000, function () {
							return false;
						});
				   }		
				  }
		});
		
		return false;
});

//****************************************************************************************//
//*********************************** LOGIN TOGGLE ***************************************//
//****************************************************************************************//

    $(".login").click(function () {
      $('#login-conteneur').toggle();
    });


});

//****************************************************************************************//
//*********************************** LOGIN SYSTEM ***************************************//
//****************************************************************************************//

var xmlHttp;
var xmlHttpfinal;
var essay = 5;

function showHint(str){

	var username = str.username.value;
	
	var key2 = str.magic2.value;
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  document.getElementById("status").innerHTML="Your Browser doesn't support Ajax, that coulp be a security issue.";
	  return
	} 
	var url="/controleur-js.php";
	url=url+"?status=0";
	url=url+"&q="+username;
	url=url+"&m2="+key2;
	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged(){ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 

		eval(xmlHttp.responseText); 
	}
	
	
}


function loginstep2( passh, key, key2, activate){
	
	// If user doesn't exist
	if(key == "0"){
	
		document.getElementById("status").innerHTML="Sorry this user doesn't exist!"; 
		
		
	}else{
		
		if(activate == "0"){

			document.getElementById("status").innerHTML="Please activate your acompte";
			document.loginform.username.disabled = true;	
			document.loginform.password.disabled = true;	

		}else{

			var passresult = hex_hmac_md5(key, document.loginform.password.value);
			var passresult2 = hex_hmac_md5(key2, document.loginform.password.value);
			
			document.loginform.password_hash.value=passresult;
			document.loginform.password_hash_2.value=passresult2;
			
			if( passresult == passh){
				// dummy
				document.loginform.password.value="-----------------";
				document.loginform.submit();
			}else{
				essay = essay - 1;
					if(essay == 0){
						document.loginform.username.disabled = true;	
						document.loginform.password.disabled = true;	
						xmlHttp=GetXmlHttpObject();
			
						var url="/controleur-js.php";
						url=url+"?status=1";
						url=url+"&username="+document.loginform.username.value;
						
						xmlHttp.onreadystatechange=stateChanged ;
						xmlHttp.open("GET",url,true);
						xmlHttp.send(null);
					}else{
						document.getElementById("status").innerHTML="Password incorrect, you've got "+ essay + " attempt left";
					}
				return false
			
			}
			
		}
	}
}

function GetXmlHttpObject() { var xmlHttp=null; try  { xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }


//****************************************************************************************//
//*************************************** VOTE *******************************************//
//****************************************************************************************//


function votep(id){
	
				var votep = "votep";
	
		  		Addvote = 'id='+ id + '&votep='+ votep;

				$.ajax({
				  type: "POST",
				  url: "/ajax/process.php",
				  data: Addvote,
				  success: function(data) {
					if(data == 0){
						return false;
		  			}else{
						$('#votep-'+id).html(" (" + data + ")");
						return false;
				  	}
				  }
			  });
	
	
}
/* Video vote */
function votep_v(id){
	
				var votep = "votep";
	
		  		Addvote = 'id='+ id + '&votep_v='+ votep;

				$.ajax({
				  type: "POST",
				  url: "/ajax/process.php",
				  data: Addvote,
				  success: function(data) {
					if(data == 0){
						return false;
		  			}else{
						$('#votep-v-'+id).html(" (" + data + ")");
						return false;
				  	}
				  }
			  });
	
	
}


function votem(id){
	
				var votem = "votem";
	
		  		Addvote = 'id='+ id + '&votem='+ votem;

				$.ajax({
				  type: "POST",
				  url: "/ajax/process.php",
				  data: Addvote,
				  success: function(data) {
					if(data == 0){
						return false;
		  			}else{
						$('#votem-'+id).html(" (" + data + ")");
						return false;
				  	}
				  }
			  });
	
	
}

function votem_v(id){
	
				var votem = "votem";
	
		  		Addvote = 'id='+ id + '&votem_v='+ votem;

				$.ajax({
				  type: "POST",
				  url: "/ajax/process.php",
				  data: Addvote,
				  success: function(data) {
					if(data == 0){
						return false;
		  			}else{
						$('#votem-v-'+id).html(" (" + data + ")");
						return false;
				  	}
				  }
			  });
	
	
}