var banner_selected; // which banner is selected currently
var is_banner_editable = false;
var info;
var closeFromButton = false;

// Animate banner buttons
$(document).ready(function() {
	banner_selected = $("div.slide.selected").attr("id");
	$("#p").attr("value", banner_selected.replace("s", ""));
	$("#banner_bottom pre.overlay").append('<img src="'+root_path+'templates/images/bbbgb.png" class="overlay" />');
	$("#banner_bottom pre.overlay").mouseover(
			function() {
				$(this).stop();
				$(this).css({opacity:1});
				$(this).animate({top:0}, 50)
			}
	).mouseout(function() {
					$(this).animate({opacity:0, top:"-10px"}, 500);
					//$(this).animate({opacity:0}, 300);
				}
	).click(function() {
					var id=$(this).parent().attr("id");
					var id = id.replace("b", "");
					
					$("#addBtn").hide();
					$("#deleteBtn").hide();
					$("#saveBtn").hide();
					
					var info_div = $("#"+banner_selected).find("div.info");
					info_div.html(info);
					is_banner_editable = false;
					$("#editBtn").find("span").html("Edit");
					
					$("div.slide.selected").removeClass("selected");
					$("#s"+id).addClass("selected");
					banner_selected = "s"+id;
					$("#p").attr("value", id);
				}
	);
});

function makeBannerEditable(name, nameLeft, nameRight) {
	//alert("Editing " + banner_selected);
	$("#saveBtn").toggle();
	
	var info_div = $("#"+banner_selected).find("div."+nameRight);
	
	if (!is_banner_editable) {
		info = info_div.html();
		
		$("#addBtn").toggle();
		$("#deleteBtn").toggle();
		
		info_div.html('<textarea name="banner_content" id="banner_content">' + info + '</textarea>');
		
		tinyMCE.init({
			theme : "advanced",
			theme_advanced_resizing : true,
			mode: "exact",
			elements : "banner_content",
			auto_focus : "banner_content",
			plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			height:"350px"
		});
		is_banner_editable = true;
		$("#editBtn").find("span").html("Close");
		tinyMCE.execCommand('mceFocus',false,'banner_content');
		
		
	}
	else {
		$("#addBtn").toggle();
		$("#deleteBtn").toggle();
		
		info_div.html(info);
		is_banner_editable = false;
		$("#editBtn").find("span").html("Edit");
	}
	
	return false;
}



function addNewBanner() {
	$("#addNewDialog").modal({
		overlayClose:true,
		onClose: function (dialog) {
			if (!closeFromButton) {
				var newName = $("#addNewName").val();
				if (newName) {
					if (confirm("Are you sure you want to cancel?"))
						$.modal.close();
				} else
					$.modal.close()
			} else
				$.modal.close();
		}

	});
}

function deleteBanner() {
	if (!confirm("Are you sure you want to delete this banner?"))
		return false;
	
	$.ajax({
		type: "POST",
		url: root_path + 'ajax/',
		data: '&action=deleteBanner&p='+banner_selected+'&curPage='+current_selected,
		success: function(data) {
			if (data == 1)
				window.location.reload();
		}
	});
}

