
var draftTimer = 0;
var retFormRevClick = false;

$(document).ready(function(){
	
	$("#new-news FORM input[type=text], #new-news FORM textarea").focus(function() {
		$(this).addClass("styled-on");
	}).blur(function() {
		$(this).removeClass("styled-on");
	});
	
	$("#new-news :input").change(function() {
		
		confirm_exit = true;
		$.cookie('unsavedSess', '1', {path:'/'}); //record for Resetti badge
		
		//start draft timer (if not yet started)
		//auto save draft after 3 minutes, update the timer every minute after that
		if( !draftTimer && $("#savedraftbutton").val() ) {
			draftTimer = 1;
			setInterval("saveDraft('check')", 60000);
		}
	});
	
	//category switches
	$("input[name='in[category]']").click(function() {
		$(this).closest("dt").next("dd").show().siblings("dd").hide();
	});
	
	$(".selection ul > li > a").click(function(){
		$(this).parent().addClass("on").siblings("li").removeClass("on");
	});
	
	//change content type
	chContType = function(){
		$("#stexttoolbox").hide();
		$("#inp-text").css("height", "34px");
		if(ntype == "text" || ntype == "quote") $("#inp-text").css("height", "115px");
		$("#inp-type").val(ntype);
		$("#formtable").removeClass().addClass("type-"+ntype);
		$(".forms").hide();
		$(".form-"+ntype).show();
		$("#selconttype a[href='#"+ntype+"']").parent().addClass("on").siblings("li").removeClass("on");
	};
	var ntype = $("#inp-type").val();
	
	//if there's a hash on pageload, check if it relates to the form, and if so change the form
	if(window.location.hash){
		var ntypes = ["text", "quote", "link", "image", "video", "audio"];
		if(jQuery.inArray(window.location.hash.slice(1), ntypes)){
			ntype = window.location.hash.slice(1);
		}
	}
	
	chContType();
	$("#selconttype a").click(function(){
		ntype = this.hash.slice(1);//$(this).attr("href").replace("#", "");
		chContType();
	});
	
	$("#ttype a").click(function(Ev){
		Ev.preventDefault();
		var v = $(this).attr("href").replace("#", "");
		$("#in-ttype").val(v);
		$("#choosettype").hide();
		$(".ttype").hide();
		var ttype = $("input[name='in[text_type]']").val();
		$("#ttype-"+ttype).show();
	});
	
	$("#ttype-review a").click(function(Ev){
		Ev.preventDefault();
		var v = $(this).attr("href").replace("#", "");
		$("#in-rating").val(v);
		if(v == "custom") { $(this).children('input').focus(); }
	});
	
	var starrating = 1;
	var srpos = 0;
	$("#star-rating > span > span").hover(function(){
		starrating = $(this).html();
		starrating = parseInt(starrating);
		srpos = starrating * 16;
		$("#star-rating > span").css("background-position", "0 -"+srpos+"px");
	}, function(){
		//revert to current vale (Set on click)
		currating = $("input[name='in[scale_value]']").val();
		currating = parseInt(currating);
		srpos = currating * 16;
		$("#star-rating > span").css("background-position", "0 -"+srpos+"px");
	}).click(function(){
		$("input[name='in[scale_value]']").val(starrating);
	});
	
	$("#new-news .example-link").click(function(e) {
		e.preventDefault();
		$(this).toggleClass("arrow-toggle-on").siblings(".example").slideToggle();
	});
	
	$("#inp-text").focus(function(){
		if(ntype == "text"){
			$(this).css("height", "512px");
			$("#form-text-toolbox").show();
		} else {
			$("#stexttoolbox").slideDown(300);
			$(this).css("height", "115px");
		}
	}).blur(function(){ 
		if(ntype == "text" && $(this).val() == "") $(this).css("height", "115px");
	});
	
	//album list onchange -> populate track list
	$(".selectalbum").change(function(){
		$(this).next().show().children('select').html('<option value="">Loading track list&hellip;</option>');
		var albumid = $(this).val();
		if(!albumid) {
			$(this).next().hide().children('select').html('');
			return;
		}
		$(this).next().children('select').load("/posts/process.php", { _action:"loadtracks", _albumid:albumid });
	});
	
	$("#getVideoCode").click(function(){
		var _url = $("#inpvidurl").val();
		if(_url == "" || _url == "http://") {
			alert("Please input a valid video URL");
			return;
		}
		$(this).val("Fetching...").attr('disabled', 'disabled');
		$.post(
			"/posts/process.php",
			{ videourl:_url },
			function(res) {
				if(!res) { $("#getVideoCode").val('Failure').removeAttr("disabled"); return; }
				if( !$("input[name='in[heading]']").val() ) $("input[name='in[heading]']").val(res.title).siblings(".tt").hide();
				if( !$("textarea[name='in[text]']").val() ) $("textarea[name='in[text]']").val(res.desc);
				if( !$("textarea[name='in[video_code]']").val() ) $("textarea[name='in[video_code]']").val(res.code);
				$("#in_video_thumbnail").val(res.tn);
				$("#video_thumbnail_src").attr("src", res.tn);
				$("#getVideoCode").val("Fetched!").removeAttr("disabled");
			}, "json"
		);
	});
	
	$("#postopts h4 > a").click(function(e){
		e.preventDefault();
		$(this).toggleClass("arrow-toggle-on").parent().next().toggle();
	});
	
	$("#imgdisp a").live("click", function(){
		$("#inp-img").val('');
		$(this).addClass('on').siblings().removeClass('on');
		$(this).find('img').each(function(){
			if( $(this).attr("rel") == "imgsel" ){ $('#inp-img').val( $(this).attr("src").replace("/posts/img/", "") ); }
		});
	});
	
	$(".buttons input").click(function(){
		confirm_exit = false;
		$.cookie('unsavedSess', null, {path:'/'});
	});
	
	$("input[name='in[unpublished]']").click(function(){
		inunpch = $(this).is(":checked");
		$("input[name='in[unpublished]']").attr("checked", inunpch);
	});
	
});
	
function togglePreview(){
	$(".arrow-toggle").toggleClass("arrow-toggle-on");
	$(".news").toggle();
};

/*var imgQ = 0;
var NNuplnum = 1;
function NNuploadimg() {
	
	if(NNuplnum > 50) { alert("You have reached the limit of 50 uploads for this session."); return false; }
	
	var _src = "";
	if( $("#inpimgupl").val() ) _src = $("#inpimgupl").val();
	else if( $("#inpimgurl").val() ) _src = $("#inpimgurl").val();
	
	if(_src == "") return false;
	if(_src == "http://") return false;
	
	var x = Array();
	x = _src.split("/");
	var br = x.length - 1;
	var fname = x[br];
	var dot = fname.lastIndexOf(".");
	var ext = fname.substr(dot,fname.length).toLowerCase();
	if(ext != ".gif" && ext != ".jpg" && ext != ".png") {
		alert("Please upload only JPG, GIF, or PNG images ["+ext+"]");
		document.uplimgform.reset();
		return false;
	}
	
	$("#uploadedimgs").prepend('<li id="upl-'+NNuplnum+'"><div style="float:right; width:300px;"><iframe name="acceptupl-'+NNuplnum+'" frameborder="0" style="width:300px;"></iframe></div><span id="loading-upl-'+NNuplnum+'" class="loading">Uploading <i id="filename" style="color:black !important;">'+fname+'</i></span></li>');
	
	$("#uplimgform").attr("target", "acceptupl-"+NNuplnum);
	$("#inpparentframe").val('upl-'+NNuplnum);
	
	NNuplnum++;
	
	document.uplimgform.submit();
	$("#uplimgform :input").attr("disabled", "disabled");
	$("#uplimgform .loading").show();
	$("#uplimgform").animate({opacity:.5}, "fast");
	$("#uploadedimgs").animate({opacity:1}, 1800, function(){
		imgQ = $("#uploadedimgs .loading:visible").size();
		if(imgQ <= 1){
			$("#uplimgform :input").removeAttr("disabled");
			$("#uplimgform .loading").hide();
			$("#uplimgform").animate({opacity:1}, "fast");
			document.uplimgform.reset();
		} else {
			//buffer a bit more
			$("#uploadedimgs").animate({opacity:1}, (imgQ * 2300), function(){
				$("#uplimgform :input").removeAttr("disabled");
				$("#uplimgform .loading").hide();
				$("#uplimgform").animate({opacity:1}, "fast");
				document.uplimgform.reset();
			});
		}
	});
	
}*/

/*function updCaption(m) {
	
	$.post(
			"/posts/process.php",
			{ _do: "update_caption",
				_imgid: $(m).attr("name"),
				_caption: $(m).val()
			}
		);
	
}*/

function NNuploadhimg(_src) {
	
	if(_src == "") return false;
	if(_src == "http://") return false;
	
	var x = Array();
	x = _src.split("/");
	var br = x.length - 1;
	var fname = x[br];
	var dot = fname.lastIndexOf(".");
	var ext = fname.substr(dot,fname.length).toLowerCase();
	if(ext != ".gif" && ext != ".jpg" && ext != ".png") {
		alert("Please upload only JPG, GIF, or PNG images ["+ext+"]");
		document.uplhimgform.reset();
		return false;
	}
	
	document.uplhimgform.submit();
	$("#uplhimgform :input").attr("disabled", "disabled");
	$("#uplhimgform .loading").show();
	
}

var minsSinceLastSave = 0;
var draftNotSaved = true;
function saveDraft(ch) {
	
	if(ch) {
		minsSinceLastSave++;
		//update the timer and check if 3 minutes have passed before saving
		if(minsSinceLastSave < 3) {
			if(!draftNotSaved) $("#draftmsg").html("Draft saved "+minsSinceLastSave+" minute"+(minsSinceLastSave != 1 ? 's' : '')+" ago");
			return;
		}
	}
	
	confirm_exit = false;
	$.cookie('unsavedSess', null, {path:'/'});
	
	//save
	draftNotSaved = false;
	minsSinceLastSave = 0;
	$("#draftmsg").html("Draft saved a few seconds ago");
	
	$("#NNform input[name='submit_action']").val('draft');
	$("#NNform").attr("target", "draftspace").submit().removeAttr("target");
	$("#NNform input[name='submit_action']").val('');
	
}
	
function NNpreview() {
	
	$("#NNform input[name='submit_action']").val('preview');
	$("#NNform").attr("target", "_blank").submit().removeAttr("target");
	$("#NNform input[name='submit_action']").val('');
	
}

function retFormRev() {
	
	return retFormRevClick;
	
}
