var pre_time = false;

function docall(id){
	pre_time = false;
	$.getJSON("signup.php", {call:id, val:$("#"+id).val()+""}, function (data){
		if (data.resp == 0)
			$("#disp_"+data.call).html('<span style="font-size:10px; color:green;">Available</span>');
		else
			$("#disp_"+data.call).html('<span style="font-size:10px; color:red;">Unavailable</span>');
	});
}

$(document).ready(function (){
	$(".titless").each(function (i){
		if ($(this).next(".feature_popout").text() != ""){
			$(this).bind("mouseenter", function (){
				$(this).next(".feature_popout").fadeIn("fast");
			}).bind("mouseleave", function (){
				$(this).next(".feature_popout").fadeOut("fast");
			}).prepend('<img class="q_img" src="/site_files/images/q.png" alt="" />');
		}
	});
	$(".toggle").bind("click", function (){
		if ($(this).parent().next(".tog_content").css("display") == "none")
			$(this).parent().next(".tog_content").slideDown("slow");
		else
			$(this).parent().next(".tog_content").slideUp("slow");
	});
	$("#add_file").one("click", function (){
		$(this).hide("fast")
		$("#add_file_form").show("fast");
	});
	$("#update_image").one("click", function (){
		$(this).hide("fast")
		$("#update_image_form").show("fast");
	});
	$(".del_file").one("click", function (){
		$(this).parent().html('<a href="'+$(this).attr("floc")+'&action=del_file&id='+$(this).attr("fid")+'">Yes Delete</a><br />click again to confirm');
	});
	$(".pre_check").bind("keyup", function (){
		if (pre_time != false)
			clearTimeout(pre_time);
		pre_time = setTimeout("docall('"+$(this).attr("id")+"')", 500);
	});
	$("#update").bind("click", function (){
		$("#loader").show();
		$.getJSON("/community/calls/widgets.php?callback=?", {keywords:$("#keywords").val(), type:$("#type").val(), order:$("#order").val(), dir:$("#dir").val(), page:$("#page").val()}, function (data){
			$("#loader").hide();
			$("#styles").html("");
			var num = 0;
			$.each(data.widgets, function (i, val){
				num++;
				$("#styles").append('<div style="border:1px #DDDDDD solid; margin:3px; padding:2px;"><div style="float:left;"><a href="/community/style/'+val.id+'/'+val.safe_name+'"><img src="/community/files/'+val.safe_name+'_thumb_'+val.id+'.png" /></a></div><div style="margin-left:5px; float:left; max-width:450px;"><strong><a href="/community/style/'+val.id+'/'+val.safe_name+'">'+val.name+'</a> By: <a href="/community/profile/'+val.user_id+'/'+val.safe_user_nick+'">'+val.user_nick+'</a></strong><br />'+val.descr+'<br /><strong>Widget Type: </strong>'+val.type+'<br /><strong>Created: </strong>'+val.created+' &nbsp; <strong>Updated: </strong>'+val.updated+'</div><div style="clear:both"></div></div>');
			});
			if (num == 0){
				$("#styles").append('<span style="font-size:16px;">No Results</span>');
			}
			if (data.paging.next == 1 || data.paging.pre == 1){
				$("#styles").append('<div style="width:100%"><div style="text-align:center;">Page: '+data.paging.cur+'</div>');
				if (data.paging.pre == 1)
					$("#styles").append('<span style="float:left;"><a id="pre" page="'+(data.paging.cur-1)+'">&laquo; '+(data.paging.cur-1)+'</a></span>');
				if (data.paging.next == 1)
					$("#styles").append('<span style="float:right;"><a id="next" page="'+(data.paging.cur+1)+'">&laquo; '+(data.paging.cur+1)+'</a></span>');
				$("#styles").append('<div style="clear:both"></div></div>');
			}
		});
	});
	$("#update").click();
});