//JASON POST
$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};
function Action(caller, data, undefined){
//increase open requests
var n = caller.attr("action");
//check for data input
var d = caller.attr("data");

if(d != undefined){
var data = $("#"+d).val();

}
var data = {"data": data};
	nAction(n,data);

}

function nAction(n,data){
//var realurl = "/index.php/ajax/"+n+"/";
var realurl = "/"+n+"/";
	$.postJSON(realurl, data, function(data){errorHandler(data);});

}

function errorHandler(data,nofail,caller,profit){
	$.each(data.data, function(index, value){

	var hyson_payload = value[2];
	var hyson_marker = value[1];

		switch(value[0]){
		case "news_html":
			add_news(hyson_marker, hyson_payload);
		break;
		case "news_comment":
			add_comments(hyson_marker, hyson_payload);
		break;
		case "is_live":
			isLive(hyson_payload);
		break;
		case "is_live_o":
			isLiveOwned(hyson_payload);
		break;
		case "hud" :

			addhud(hyson_marker, hyson_payload,".");

		break;
			case "hudappend" :

			addhud(hyson_marker, hyson_payload,".", true);

		break;

		case "hudID":
			addhud(hyson_marker, hyson_payload, "#");

		break;
		}

	});
}

function load_into(load, target){
	$(target).load(load);
}

function add_news(marker,payload){
	$("#news_body_text"+marker).empty();
	$("#news_body_text"+marker).hide();
	$("#news_body_text"+marker).append(payload);
	$("#news_body_text"+marker).show(520);
	var images = "";
	$("#news_body_text"+marker).children().each(function(item, value, undef){
		var sauce = value.src;
		if(sauce != undef && false){
			//only do jpgs
			if((sauce.substr(-3) == "jpg" || sauce.substr(-3) == "png"|| sauce.substr(-3) == "gif") && value.width > 20){

			var div = $('<div></div>').addClass('fun');


				var i = new Image();
				i.src = sauce;
			$(i).addClass("funimage");
			//calculate not worst dimension
			if(value.width > value.height){
				i.height = 90;
			} else {
				i.width = 90;
			}
			div.append(i);
			$("#news_body_text"+marker).append(div);
				$(value).remove();
			}

		}

	});

}

function add_comments(marker,payload){
		$("#news_comments"+marker).empty();
	$("#news_comments"+marker).hide();

		$(".news_comments"+marker).empty();
	$(".news_comments"+marker).hide();

	$(".news_comments"+marker).append(payload);

	$("#news_comments"+marker).append(payload);

	//init TinyMCE

$("#news_comments"+marker).show();
$(".news_comments"+marker).show();

}


function addhud(hyson_marker,hyson_payload, what, append, undef){
if(append == undef){
append = false;
}
if(append == false){
$(what+hyson_marker).empty();
}
$(what+hyson_marker).append(hyson_payload);


$(what+hyson_marker +" .auto-dialog").dialog({
	modal:false,
	width: "100%",
	position: "top"
});

}
