var defaultcon = '<div class="centre"><ul id="toastermenu"><li><a href="#" onclick="addSection();return false;">Add section</a></li><li><a href="?edit">Edit section</a></li><li><a href="#" onclick="deleteSection();return false;">Delete this section</a></ul></div>';
var toastertoggle = false;
$(document).ready(function() {
	if($("#toaster_app").html()=='')
		$("#toaster_app").html(defaultcon);	
	$('#toastertoggle').html('<img src="/themes/toaster/images/min.png" alt="Minimise" />');
	$('#toastertoggle').click(function() {
		if(toastertoggle==false) {
			$('#toastertoggle').html('<img src="/themes/toaster/images/max.png" alt="Maximise" />');
			$('#toaster h3').fadeOut("fast");
			$("#toaster_app").fadeOut("fast",function() {
				$("#toaster").animate({ 
        			width: "20px",
					height: "20px"
      			}, "fast", "linear");
			});
			toastertoggle = true;
		}
		else {
			$('#toastertoggle').html('<img src="/themes/toaster/images/min.png" alt="Minimise" />');
			$('#toaster h3').fadeIn("fast");
			$("#toaster").animate({ 
        		width: "150px",
				height: "150px"
      		}, "fast", "linear", function() {$("#toaster_app").fadeIn("fast");} );
			toastertoggle = false;
		}
		return false;
	});
});

function addSection() {			
	$("#toaster_app").fadeOut("fast", function() {
		$("#toaster_app").html('');		
		$('#toastertoggle').css('display','none');
		$("#toaster").animate({ 
        	width: "370px",
			height: "275px"
      	}, "fast", "linear", function() {$("#toaster_app").fadeIn("fast");} );
		$.ajax({
			url: "/toaster/ajax.addsection.php",
			cache: false,
			success: function(html){
				$("#toaster_app").html(html.substr(1));
			}
		});
	});	
}

function deleteSection() {			
	$("#toaster_app").fadeOut("fast", function() {
		$('#toastertoggle').css('display','none');
		$("#toaster_app").html('');									
		$("#toaster").animate({ 
        	width: "500px",
			height: "390px"
      	}, "fast", "linear", function() {$("#toaster_app").fadeIn("fast");} );
		$.ajax({
			type: "POST",
			url: "/toaster/ajax.deletesection.php",
			data: 'sid='+$('#toaster_sec').html(),
			cache: false,
			success: function(html){
				$("#toaster_app").html(html);
			}
		});
	});	
}

function deleteSectionSubmit() {
	$.ajax({
		type: "POST",
		url: "/toaster/ajax.deletesection.php",
		data: $('#deletesectionfrm').serialize()+'&deletesection=Delete section&sid='+$('#toaster_sec').html(),
		success: function(msg){
			//$("#toaster_app").html(msg);
			window.location = '/';
		}
	});
}

function moveSectionSubmit() {
	$.ajax({
		type: "POST",
		url: "/toaster/ajax.deletesection.php",
		data: $('#deletesectionfrm').serialize()+'&movesection=Delete section&sid='+$('#toaster_sec').html(),
		success: function(msg){
			//$("#toaster_app").html(msg);
			window.location = '/';
		}
	});
}

function addSectionSubmit() {
	$.ajax({
		type: "POST",
		url: "/toaster/ajax.addsection.php",
		data: $('#addSectionFrm').serialize()+'&addsectionbtn=Add section&pid='+$('#toaster_sec').html(),
		success: function(msg){
			if(msg.substr(0,1)=='2')
				alert('An error occured! Please try again.');
			else if(msg.substr(0,1)=='0') {
				$("#toaster_app").html(msg.substr(1));
			}
			else
				window.location = msg.substr(1);
		}
	});
}

function closeToaster() {
	$("#toaster_app").fadeOut("fast", function() {
		$("#toaster_app").html('');
		$("#toaster").animate({ 
        	width: "150px",
			height: "150px"
      	}, "fast", "linear", function() {$("#toaster_app").fadeIn("fast");} );
		$("#toaster_app").html(defaultcon);
		$('#toastertoggle').css('display','block');
	});	
}

function editSection(sido){
	$('#toastertoggle').css('display','none');
	$.ajax({
		   type:"POST",
		   url:"/toaster/ajax.editSection.php",
		   data: "sido=" + sido,
		   success: function(msg){
			   $("#selectsDiv").html(msg);
			   	$(".ss").change(function(){	
					//var selected = $("#"+this.attr("id")+" option:selected"); 
					var selected = $(this).val();
					editSection(selected);
				})
		   }
	});
	checkEditing(sido);
}
var conrev;
function openContentRevision(cid) {
	conrev=window.open('/toaster/contentRevision.php?cid='+cid,'name','height=700,width=900,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,status=yes');
	if (window.focus) {conrev.focus()}
}
var concontent;
function editContentPopup(sid) {
	concontent=window.open('/toaster/sections/popup.editContent.php?s='+sid,'name','height=700,width=900,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,status=yes');
	if (!concontent.opener)
		concontent.opener = self;
	if (window.focus) {concontent.focus()}
}
var cE;
function checkEditing(sectionid) {
	$.getJSON("/toaster/ajax.currentlyEditing.php", { s: sectionid }, function(json){
		str = '';
		if(json.length==1) {
			str += '<a href="mailto:'+json[0].email+'">'+json[0].first_name+' '+json[0].last_name+'</a> is also editing this page';
		}
		else if(json.length>1) {
			for(var i=0;i<json.length;i++) {
				if(i==(json.length-2))
					str += '<a href="mailto:'+json[i].email+'">'+json[i].first_name+' '+json[i].last_name+'</a> and ';
				else if(i==(json.length-1))
					str += '<a href="mailto:'+json[i].email+'">'+json[i].first_name+' '+json[i].last_name+'</a>';
				else
					str += '<a href="mailto:'+json[i].email+'">'+json[i].first_name+' '+json[i].last_name+'</a>, ';
			}
			str += ' are also editing this page';
		}
		if(str!='')
			$('#status').html('<strong>'+str+'</strong>');
	});
	cE = setTimeout("checkEditing("+sectionid+")",30000);
}

function revertContent(cido) {
	$.post("/toaster/contentRevision.php", { cid: cido },
		function(data){
			location.reload(true);
		}
	);
	conrev.close();
}