// JavaScript Document

(function (){
	
	$(document).bind("ready", function () {
		
		$.ajaxSetup({type:"post", url:"ajax/", global:false});
		
		// floaters
		$("li.home-push6").bind("mouseover", function () {
			var o=$(this), e = o.children().get(1);
			$(e).css({display: "block"});
		});	
		$("li.home-push6").bind("mouseout", function () {
			var e = $(this).children().get(1);
			$(e).css({display: "none"});
		});	
		
		/*setTimeout(function () {
			$("#post-box").slideUp("slow");
		}, 1000);*/
		
		
	});
	
})($);

// show box
function showBox (b) {
	$("#submit-contact, #submit-ad").slideUp("fast");
	//$("#login-notice").html("").hide();	
	$("#"+b).slideDown("slow");	
}

// validate user
function validateUser () {
	var em, pw, fm, ln;
	fm = $("#login-box").html();
	em = $("#login-email").val();
	pw = $("#login-password").val();
	//ln = $("#login-notice").html();
	
	if(!$.validate(".login-input", "#login-notice")) return false;
	
	$(".login-input").attr("disabled","disabled");
	$("#login-notice").show().html("Please wait...");
	
	$.post("ajax/", {fn: "validateUser", e: em, p:pw}, function (r) {
		if (!r) {
			$("#login-notice").html('<span id="notice-success">Okay! please proceed</span>');
			$(".post-input, .login-input").removeAttr("disabled");
			$(".login-input").attr("disabled","disabled");
			//$("#post-box").slideDown("fast");	
			//$("#adhtml").focus();
		}
		else {
			$("#login-notice").html('<span id="notice-failure">'+r+'</span>');
			$(".login-input").removeAttr("disabled");
			setTimeout(function () {
				$("#login-notice").html("").hide();
				//$("#login-email").focus();
			}, 3000);	
		}
	});
}

function showAdBox() {
	$("#login-notice").html("").hide();								 
	$("#upload_status").html("(jpg, jpeg, gif, png, bmp) 200kb maximum");								 
	$("#submit-contact").slideUp("fast");
	$("#post-box").slideDown("slow");	
	//$("#login-notice").html("").hide();	
}

// post ad
function postAd () {
	//if(!$.validate(".login-input", "#login-notice")) return false;
	//if(!$.validate(".login-input, .post-input", "#login-notice")) return false;
	
	if(!$.validate(".login-input, .post-input", "#login-notice")) return false;
	
	var f=[], c, i, ln;	
	ln = $("#login-notice").html();
		
	adhtml___Frame.FCK.UpdateLinkedField();
	$(".login-input, .post-input, #adhtml").each(function (i, c) {
		f.push($(c).attr("name")+"="+$(c).val());
		//$(c).val("");																				
	});
	document.forms["__axuploader_form"].reset();	
	f.push("fn=postAd");
	f = f.join("&");
	
	$("#post-box").slideUp("slow");
	$("#login-notice").show().html("Please wait...");
	$(".login-input").attr("disabled","disabled");
	$.ajax({data:f, success:function (r) {
		if (!r) {
			$("#login-notice").show().html('<span id="notice-success">Your ad has been submitted for review.</span> <a href="javascript:;" onclick="showAdBox()">Create another ad</a>');
			$(".post-input, #adhtml").reset();
		}
		else {//alert(r);
			$(".post-input").reset();
			$("#login-notice").show().html('<span id="notice-failure">'+r+'</span>');
			setTimeout(function () {
				$("#login-notice").html("").hide();
				$(".login-input").removeAttr("disabled");
				//$("#post-box").slideDown("slow");	
			}, 3000);
		}
	}});
}

function uploadFile (f) {
	$("#upload_status").html("Uploading file...");
	f.form.submit();
}

function ax_uploadStatus (s) {
	s = decodeURIComponent(s);
	$("#upload_status").html(s);
	setTimeout(function () {	
		$("#upload_status").html(s);
	}, 2000);
}

/*function ax_sendUploads (c) {
	$.post("ajax/", {fn:"registerUploads", "uploads":c}, function (r) {
		if (r) {
		}
	});
}*/

// register
function register () {
	var f=[], c;
	
	if(!$.validate(".register-input", "#register-notice")) return false;
	
	// process registration
	$(".register-input").each(function (i, c) {
		f.push($(c).attr("name")+"="+$(c).val());
		//$(c).val("");																				
	});
	f.push("fn=Register");
	f = f.join("&");
	
	$("#register-box").slideUp("slow");
	$("#register-notice").show().html("Please wait...");
	$.ajax({data:f, success:function (r) {
		if (!r) {
			$("#register-notice").show().html('<span id="notice-success">Welcome to your free world! Please wait...');
			setTimeout(function () {
				//$("#submit-contact").slideUp("slow");
				$("#submit-ad").slideDown("slow");
				$("#register-box").slideDown("fast");
				//$("#register-notice").html("").hide();
				showAdBox();
			}, 2000);
		}
		else {
			$("#register-notice").show().html('<span id="notice-failure">'+r+'</span>');
			$("#register-box").slideDown("slow");	
		}
	}});
}

function showSearchBox() {
	$("#advanced-search-box").slideDown("slow");	
}