/*
* Placeholder plugin for jQuery
* ---
* Copyright 2010, Daniel Stocks (http://webcloud.se)
* Released under the MIT, BSD, and GPL Licenses.
*/

(function(b){function d(a){this.input=a;a.attr("type")=="password"&&this.handlePassword();b(a[0].form).submit(function(){if(a.hasClass("placeholder")&&a[0].value==a.attr("placeholder"))a[0].value=""})}d.prototype={show:function(a){if(this.input[0].value===""||a&&this.valueIsPlaceholder()){if(this.isPassword)try{this.input[0].setAttribute("type","text")}catch(b){this.input.before(this.fakePassword.show()).hide()}this.input.addClass("placeholder");this.input[0].value=this.input.attr("placeholder")}},
hide:function(){if(this.valueIsPlaceholder()&&this.input.hasClass("placeholder")&&(this.input.removeClass("placeholder"),this.input[0].value="",this.isPassword)){try{this.input[0].setAttribute("type","password")}catch(a){}this.input.show();this.input[0].focus()}},valueIsPlaceholder:function(){return this.input[0].value==this.input.attr("placeholder")},handlePassword:function(){var a=this.input;a.attr("realType","password");this.isPassword=!0;if(b.browser.msie&&a[0].outerHTML){var c=b(a[0].outerHTML.replace(/type=(['"])?password\1/gi,
"type=$1text$1"));this.fakePassword=c.val(a.attr("placeholder")).addClass("placeholder").focus(function(){a.trigger("focus");b(this).hide()});b(a[0].form).submit(function(){c.remove();a.show()})}}};var e=!!("placeholder"in document.createElement("input"));b.fn.placeholder=function(){return e?this:this.each(function(){var a=b(this),c=new d(a);c.show(!0);a.focus(function(){c.hide()});a.blur(function(){c.show(!1)});b.browser.msie&&(b(window).load(function(){a.val()&&a.removeClass("placeholder");c.show(!0)}),
a.focus(function(){if(this.value==""){var a=this.createTextRange();a.collapse(!0);a.moveStart("character",0);a.select()}}))})}})(jQuery);
function showPopup(title, body){
	$(".modal_window_container").html('<div class="modal_loader" style="margin:10px;"><img src="images/loader.gif" alt="loading" />&nbsp;Loading...</div>');
	$(".modal_loader").hide();
	var html = '<div class="modal_window_header"><h2>'+title+'</h2></div><div class="modal_window_body">'+body+'</div><div class="modal_window_footer"><input type="button" value="Close" class="modal_window_close" /></div>';
	$(".modal_window_container").html(html);
	$(".modal_window_container").fadeIn("fast");
}
function hidePages(){
	$(".top_stats_container").find(".slide_d_c").each(function(){
		$(this).hide();
	});
}
$(document).ready(function(){
	var www = "http://www.ciber.fr/";//change it to the scripts full url path
	var loader = $(".ajax_loader_top_bar");
	$('input[placeholder], textarea[placeholder]').placeholder();
	$(".modal_window_close").live("click", function(){
		$(this).parents(".modal_window_container").fadeOut("fast");
	});
	$(".question_container").hover(
		function(){
			$(".question_footer", this).css("border-bottom", "1px solid #1E6E00");
			$(".question_footer", this).addClass("q_footer_hover");
		},
		function(){
			$(".question_footer", this).css("border-bottom", "1px solid #A1A1A1");
			$(".question_footer", this).removeClass("q_footer_hover");
		}
	);
	$(".show_hide_answer_form").toggle(
		function(){
			$(".answer_form_inner").slideUp();
			$(this).text("show");
		},
		function(){
			$(".answer_form_inner").slideDown();
			$(this).text("hide");
		}
	);
	$(".popup_login_box").click(function(){
		if (!$(".user_action_modal_box").is(":visible")){
			$(this).find("span").removeClass("drop_down_d_active").addClass("drop_down_u_active");
			var top = 40;
			var offset = $(this).offset();
			var left = offset.left - 162;
			$(this).addClass("active_login_modal_box");
			$(".user_action_modal_box").css({
				"left": left,
				"top": top
			});
			$(".user_action_modal_box").show();
		}else{
			$(this).find("span").removeClass("drop_down_u_active").addClass("drop_down_d_active");
			$(".user_action_modal_box").hide();
		}
		return false;
	});
	$(".sort_questions_container").click(function(){
		if (!$(".sort_questions_selection").is(":visible")){
			var call_id = $(this).attr("id");
			//if call_id is 2 , we are on search page
			var width = $(this).width();
			var top;
			if (call_id == 2){
				var offset = $(".sort_questions_container").offset();
				top = offset.top - 196;
				var left = "738px";
			}else{
				top = "26px";
				var left = "10px";
			}
			$(this).css("border-radius", "0px");
			$(".sort_questions_selection").css({
				"min-width" : width + 9,
				"top" : top,
				"left" : left
			});$(".sort_questions_selection").fadeIn("fast");
		}else{
			$(".sort_questions_selection").fadeOut("fast");
			$(this).css({
				"border-radius" : "0 0 5px 5px"
			});
		}
		return false;
	});
	$(".popup_loggedin_box").click(function(){
		if (!$(".user_action_modal_box").is(":visible")){
			var width = $(this).parent().width();
			$(".user_action_modal_box").css("width", width);
			$(this).find("span").removeClass("drop_down_d_active").addClass("drop_down_u_active");
			var top = 40;
			var offset = $(this).offset();
			var left = offset.left;
			$(this).addClass("active_login_modal_box");
			$(".user_action_modal_box").css({
				"left": left,
				"top": top
			});
			$(".user_action_modal_box").show();
		}else{
			$(this).find("span").removeClass("drop_down_u_active").addClass("drop_down_d_active");
			$(".user_action_modal_box").hide();
		}
		return false;
	});
	$("body").click(function(){
		$(".popup_login_box, .popup_loggedin_box").find("span").removeClass("drop_down_u_active").addClass("drop_down_d_active");
		$(".popup_login_box, .popup_loggedin_box").removeClass("active_login_modal_box");
		$(".user_action_modal_box").hide();
		$(".sort_questions_selection").fadeOut("fast");
		$(".sort_questions_container").css({
			"border-radius" : "0 0 5px 5px"
		});
	});
	$(".user_action_modal_box").click(function(e){
		e.stopPropagation();
	});
	$(window).resize(function(){
		if ($(".user_action_modal_box").is(":visible")){
			var top = 40;
			var offset = $(".popup_login_box, .popup_loggedin_box").offset();
			var left = offset.left - 162;
			if ($(".user_action_modal_box").attr("id") == "ll")
				left = offset.left;
			$(".popup_login_box, .popup_loggedin_box").addClass("active_login_modal_box");
			$(".user_action_modal_box").css({
				"left": left,
				"top": top
			});
			$(".user_action_modal_box").show();
		}
	});
	$("#register_btn").click(function(){
		var username = $("#username").val();
		var password = $("#password").val();
		var retypepassword = $("#retypepassword").val();
		var email = $("#email").val();
		var captcha = $("#captcha").val();
		var ajax_request = "register";
		var c_ca = $("#c_ca").val();
		var a_a = $('#a_a:checked').val();
		var loader = $(this).parent().find(".loader");
		loader.css("display", "inline-block");
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				username: username,
				password: password,
				retypepassword: retypepassword,
				email: email,
				captcha: captcha,
				c_ca: c_ca,
				a_a: a_a,
				request: ajax_request
			},
			success: function(data){
				$(".reg_error").html(data.message).show();
				if (data.error == true){
					if (data.captcha != "")
						$(".captcha").html(data.captcha);
				}else{
					if (a_a != "on"){
						window.location.href = "login.php";
					}
				}
				loader.hide();
			}
		});
	});
	if($('#markItUp').length >0 ){
		$('#markItUp').markItUp(mySettings);
		$(".post_q_body").keyup(function(){
			var body = $(this).val();
			$.ajax({
				type: "POST",
				url: www+"ajax/ajax.php",
				dataType: 'json',
				data: {
					body: body,
					request: 'preview_q_body'
				},
				success: function(data){
					$(".preview_question_body").html(data.message).show();
				}
			});
		});
	}
	function removeVoteClasses(me){
		me.parent().parent().find(".up_vote_btn").removeClass("voted_up");
		me.parent().parent().find(".down_vote_btn").removeClass("voted_down");
	}
	$(".up_vote_btn, .down_vote_btn").live('click', function(){
		var data = $(this).attr("id");
		var qid = $(this).parent().find("#q_id").val();
		var aid = $(this).parent().find("#a_id").val();
		var cid = $(this).parent().find("#c_id").val();
		var split = data.split(":");
		var req = split[0];
		var type = split[1];
		if (req == "q"){
			var request = "question_vote";
			var qid = $(this).parent().parent().next().find(".question_view_content").find("#q_id").val();
			var aid = $(this).parent().parent().next().find(".question_view_content").find("#a_id").val();
			var cid = $(this).parent().parent().next().find(".question_view_content").find("#c_id").val();
		}else if (req == "a"){
			var request = "answer_vote";
		}else if (req == "c"){
			var request = "comment_vote";
		}
		me = $(this);
		loader.show();
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				request: request,
				type: type,
				qid: qid,
				aid: aid,
				cid: cid
			},
			success: function(data){
				showPopup('Vote', data.message);
				setTimeout(function(){ $(".modal_window_container").fadeOut("fast"); }, 800);
				if (data.voted == true){
					if (data.vote == 1){
						removeVoteClasses(me);
						me.addClass("voted_up");
					}else if (data.vote == 0){
						removeVoteClasses(me);
						me.addClass("voted_down");
					}
				}
				loader.hide();
			}
		});
	});
	$(".flag_item").live('click', function(){
		var item_type = $(this).attr("id");
		var qid = $(this).parent().find("#q_id").val();
		var aid = $(this).parent().find("#a_id").val();
		var cid = $(this).parent().find("#c_id").val();
		var request = "flag_item";
		loader.show();
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				request: request,
				item_type: item_type,
				qid: qid,
				aid: aid,
				cid: cid
			},
			success: function(data){
				showPopup('Flag', data.message);
				setTimeout(function(){ $(".modal_window_container").fadeOut("fast"); }, 800);
				loader.hide();
			}
		});
		return false;
	});
	$(".answer_btn").click(function(){
		var body = $(this).parent().find(".answer_body");
		var qid = $(this).parent().find("#q_id").val();
		var request = "add_answer";
		loader.show();
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				request: request,
				body: body.val(),
				qid: qid
			},
			success: function(data){
				if (data.error == false){
					body.val('');
					$(".new_answer_form_container").before(data.append);
				}else{
					showPopup('Add an Answer', data.message);
					setTimeout(function(){ $(".modal_window_container").fadeOut("fast"); }, 1000);
				}
				loader.hide();
			}
		});
	});
	$('.answer_parent_container').live('mouseover',function(){
		$(".toggle_comment_form", this).show();
	});
	$('.answer_parent_container').live('mouseout',function(){
		$(".toggle_comment_form", this).hide();
	});
	$(".toggle_comment_form").live('click', function(){
		if ($(this).parents(".answer_parent_container").find(".answer_comment_form").is(":visible")){
			$(this).parents(".answer_parent_container").find(".answer_comment_form").hide();
		}else
			$(this).parents(".answer_parent_container").find(".answer_comment_form").show();
			$(this).parents(".answer_parent_container").find(".answer_comment_form").find("#answer_comment_body").focus();
	});
	$("#submit_answer_comment").live('click', function(){
		var body = $(this).parent().find("#answer_comment_body");
		var qid = $(this).parent().find("#q_id").val();
		var aid = $(this).parent().find("#a_id").val();
		var request = "add_comment";
		var me = $(this);
		loader.show();
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				request: request,
				body: body.val(),
				aid: aid,
				qid: qid
			},
			success: function(data){
				if (data.error == false){
					body.val('');
					$(data.append).hide().prependTo(me.parent().prev()).fadeIn();
				}else{
					showPopup('Add a Comment', data.message);
					setTimeout(function(){ $(".modal_window_container").fadeOut("fast"); }, 1000);
				}
				loader.hide();
			}
		});
	});
	$(".mas_btn").click(function(){
		var qid = $(this).parent().find("#q_id").val();
		var type = $(this).attr("rel");
		var request = 's_u_question';
		var me = $(this);
		loader.show();
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				request: request,
				type: type,
				qid: qid
			},
			success: function(data){
				if (data.error == false){
					me.text(data.replace.label);
					me.attr("rel", data.replace.rel);
				}
				showPopup('Question Solve State', data.message);
				setTimeout(function(){ $(".modal_window_container").fadeOut("fast"); }, 1000);
				loader.hide();
			}
		});
		return false;
	});
	$(".show_all_answer_comments").click(function(){
		var aid = $(this).parent().find(".comment_o_container").find("#a_id").val();
		var qid = $(this).parent().find(".comment_o_container").find("#q_id").val();
		var request = 's_a_answer_comments';
		var me = $(this);
		loader.show();
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				request: request,
				aid: aid,
				qid: qid
			},
			success: function(data){
				me.parent().html(data.message);
				loader.hide();
			}
		});
		return false;
	});
	$(".delete_item").live('click', function(){
		if (confirm("Are you sure you want to delete this item?You cant undo this.")){
			var type = $(this).attr("id");
			var qid = $(this).parent().find("#q_id").val();
			var aid = $(this).parent().find("#a_id").val();
			var request = "delete_item";
			var me = $(this);
			loader.show();
			$.ajax({
				type: "POST",
				url: www+"ajax/ajax.php",
				dataType: 'json',
				data: {
					request: request,
					type: type,
					qid: qid,
					aid: aid
				},
				success: function(data){
					if (data.error == false){
						if (data.deleted == true){
							if (type == "q"){
								window.location.href = www;
							}else if (type == "a"){
								$(".answer_parent_container").each(function(){
									var id = $(this).find(".question_view_content").find(".body_footer_q").find("#a_id").val();
									if (id == aid){
										$(this).fadeOut("fast");
									}
								});
							}
						}
					}else{
						showPopup('Question Solve State', data.message);
						setTimeout(function(){ $(".modal_window_container").fadeOut("fast"); }, 1000);
					}
					loader.hide();
				}
			});
		}
		return false;
	});
	var cancel_edit_body;
	$(".q_answer_edit_item").live('click', function(){
		var body = $(this).parent().parent().parent().find(".question_v_body").html();
		cancel_edit_body = body;
		body = body.replace(/<br>/gi, '\r');
		$(this).parent().parent().parent().find(".question_v_body").html('<textarea class="edit_answer_body">'+body+'</textarea><a href="#" class="link cancel_q_answer_edit">or Cancel</a> <input type="button" value="Edit" class="answer_b_edit_submit" /><br /><br />');
		$(this).parent().hide();
		return false;
	});
	$(".answer_b_edit_submit").live('click', function(){
		var body = $(this).parent().find(".edit_answer_body").val();
		var qid = $(this).parent().next().next().find("#q_id").val();
		var aid = $(this).parent().next().next().find("#a_id").val();
		var request = "edit_question_answer";
		var me = $(this);
		loader.show();
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				request: request,
				body: body,
				qid: qid,
				aid: aid
			},
			success: function(data){
				showPopup('Question Solve State', data.message);
				setTimeout(function(){ $(".modal_window_container").fadeOut("fast"); }, 1000);
				if (data.error == false){
					me.parent().next().next().find(".flag_item").after('<span class="edit_action">&nbsp;&bull;&nbsp;<a class="wh_button q_answer_edit_item" href="#">edit</a></span>');
					me.parent().html(data.replace);
				}
				loader.hide();
			}
		});
		return false;
	});
	$(".cancel_q_answer_edit").live('click', function(){
		var body2 = $(this).parent().find(".edit_answer_body").val();
		$(this).parent().next().next().find(".flag_item").after('<span class="edit_action">&nbsp;&bull;&nbsp;<a class="wh_button q_answer_edit_item" href="#">edit</a></span>');
		$(this).parent().parent().find(".question_v_body").html(cancel_edit_body);
		return false;
	});
	$(".load_more_u_p_questions").click(function(){
		var user_total_qs = 0;
		var user_id = $(this).find("#user_id").val();
		$(".u_questions_outer_list").find(".user_profile_questions_list").each(function(){
			user_total_qs++;
		});
		var request = "load_more_user_p_questions";
		var me = $(this);
		loader.show();
		$.ajax({
			type: "POST",
			url: www+"ajax/ajax.php",
			dataType: 'json',
			data: {
				request: request,
				user_total_qs: user_total_qs,
				user_id: user_id
			},
			success: function(data){
				if (data.finished == true)
					me.remove();
				if (data.error == false){
					$(".u_questions_outer_list").append(data.message);
				}
				loader.hide();
			}
		});
	});
	var next = 0;
	var prev = 3;
	var done = false;
	$(".navig_left_rh, .navig_right_rh").click(function(){
		var go = $(this).attr("id");
		if (go == "next"){
			$(".top_stats_container").find(".slide_d_c").each(function(){
				if (next == 0 && done == false){
					hidePages();
					$(".s_r_title").html("Top Voted Questions");
					$(".highest_voted_questions_r").fadeIn("fast");
					prev = 0;
					done = true;
				}else if (next == 1 && done == false){
					hidePages();
					$(".s_r_title").html("Top Viewed Questions");
					$(".highest_viewed_questions_r").fadeIn("fast");
					prev = 1;
					done = true;
				}else if (next == 2 && done == false){
					hidePages();
					$(".s_r_title").html("Newest Users");
					$(".newest_users_st_r").fadeIn("fast");
					prev = 2;
					done = true;
				}else if (next == 3 && done == false){
					hidePages();
					$(".s_r_title").html("Top Contributors");
					$(".top_contributors_r").fadeIn("fast");
					prev = 3;
					done = true;
				}
			});
			next++;
			if (next >= 4){
				next = 0;
			}
			done = false;
		}else{
			$(".top_stats_container").find(".slide_d_c").each(function(){
				if (prev == 0 && done == false){
					hidePages();
					$(".s_r_title").html("Top Contributors");
					$(".top_contributors_r").fadeIn("fast");
					next = 0;
					done = true;
				}else if (prev == 1 && done == false){
					hidePages();
					$(".s_r_title").html("Top Voted Questions");
					$(".highest_voted_questions_r").fadeIn("fast");
					next = 1;
					done = true;
				}else if (prev == 2 && done == false){
					hidePages();
					$(".s_r_title").html("Top Viewed Questions");
					$(".highest_viewed_questions_r").fadeIn("fast");
					next = 2;
					done = true;
				}else if (prev == 3 && done == false){
					hidePages();
					$(".s_r_title").html("Newest Users");
					$(".newest_users_st_r").fadeIn("fast");
					next = 3;
					done = true;
				}
			});
			prev--;
			if (prev < 0){
				prev = 3;
			}
			done = false;
		}
	});
	$(".showhide").click(function(){
		var next = $(this).next();
		if (!next.is(":visible")){
			next.slideDown("fast");
			$(this).css({
				"border-bottom" : "1px solid #bebebe",
				"border-radius" : "5px 5px 0 0"
			});
		}else{
			next.slideUp("fast", function(){
				$(".showhide").css({
					"border-bottom" : "none",
					"border-radius" : "5px"
				});
			});
		}
	});
});
