// +----------------------------------------------------------------------+
// | editEase 1.1 - edit content withg no fuss, no database, no worries   |
// | $date:  24 January 2009                                              |
// +----------------------------------------------------------------------+
// | By Stephen Neate (http://jquerystuff.net)                            |
// | Copyright (c) 2007/2009 Stephen Neate                                |
// | Licensed under the MIT License:                                      |
// | -- http://www.opensource.org/licenses/mit-license.php                |
// +----------------------------------------------------------------------+
// | editEase REQUIREMENTS (TESTED WITH)                                  |
// +----------------------------------------------------------------------+
// | PHP  (5.1.2 - 5.2+)                                                  /
// | Apache (1.3.7 - 1.3.9, 2.2+)                                         /
// | jQuery (1.2.6+)    *required                                         /
// | thickbox (3.1)    *required                                          /
// +----------------------------------------------------------------------+

/* default path file for editEase*/
var eePath = '/_ee';

// you shoudln't need to edit below this line //
// +----------------------------------------------------------------------+

jQuery.fn.editease = function() {
/*#### This little function activates the system ####*/
jQuery("head").append('<link rel="stylesheet" type="text/css" media="all" href="'+eePath+'/editease.css">');
if(jQuery.browser.msie && jQuery.browser.version < 7){ jQuery("head").append('<link rel="stylesheet" type="text/css" media="all" href="'+eePath+'/editeaseIE6.css">'); }
var lgnLink = '<a href="javascript:void(0)" onclick="loginModal()" class="eelgn" title="editEase - click to login and edit"><span>Admin</span></a><div id="eelogin" style="display:none;"><div class="customDialog"><h1><a href="javascript:void(0)" onclick="tb_remove();" title="close window"><img src="'+eePath+'/img/cross.png" alt="close window"/></a>Login Now</h1><div class="text">Login to <strong>editEase</strong> to manage your content.<form action="javascript:void(0)" method="post" onsubmit="return doLogin(\'login\');" id="loginform"><fieldset><span class="warning">Your "username" or "password" don\'t appear to be valid.  Please try again.</span><label for="username">Username</label><input type="text" name="username" id="username" maxlength="100" value=""/><label for="password">Password</label><input type="password" name="password" id="password" maxlength="100" value=""/><button type="submit" id="eesbt">&nbsp;&nbsp;LOGIN&nbsp;&nbsp;</button></fieldset></form></div></div></div>';
jQuery(this).append(lgnLink);

 jQuery.post(""+eePath+"/session.php",
	function(data){
		if(jQuery.trim(data)==='eeloggedin'){
		eeLive();
		}
	 }
	);
};


/*######## START LOGIN & DISPLAY PANEL FUNCTIONS #########*/
/*login popup*/
function loginModal(){
  tb_show('LOGIN NOW', '#TB_inline?height=230&width=335&inlineId=eelogin&modal=true', null);
  jQuery("#TB_window").css({ border: "0", background:"transparent" });
}

function eeLogoutCheck(){
var lgoutData = '<div id="eelogout" style="display:none;"><div class="customDialog"><h1>Logout of editEase</h1><span class="text">Logout of editEase and continue browsing your website.</span><button type="submit" onclick="tb_remove();">&nbsp;&nbsp;No&nbsp;&nbsp;</button> <button type="submit" onclick="eelogout();tb_remove();">&nbsp;&nbsp;Yes&nbsp;&nbsp;</button></div></div>';
 jQuery("body").append(lgoutData);
  tb_show('LOGIN NOW', '#TB_inline?height=130&width=335&inlineId=eelogout&modal=true', null);
  jQuery("#TB_window").css({ border: "0", background:"transparent" });
}


function eelogout(){
 jQuery("#eepanel").remove();
 jQuery.get(""+eePath+"/sess-destroy.php?killsession=true");
 unbindEE();
}

function doLogin(login) {
 jQuery("#eesbt").attr({ disabled: "disabled" });
  var user = jQuery("#username").val();
  var pass = jQuery("#password").val();
  jQuery.post(eePath+'/loginnow.php?password='+pass+'&username='+user, function(data){
         if(data === 'fail'){
            jQuery(".warning").css({ display: "block" });
						jQuery("#eesbt").removeAttr("disabled");
         }else{
           jQuery(".warning").html('Welcome back, loading editEase now, please wait a moment');
           jQuery(".warning").addClass("success");
           jQuery(".warning").css({ display: "block" });
					 eeLive(); tb_remove();
					 jQuery("#eesbt").removeAttr("disabled");
           jQuery(".warning").html('Your "username" or "password" don\'t appear to be valid.  Please try again.');
           jQuery(".warning").removeClass("success");
           jQuery(".warning").css({ display: "none" });
         }
    });
		 return false;
}

function eeLive(){
 jQuery.post(""+eePath+"/eepanel.php?val="+Math.random(),function(data){jQuery("body").prepend(data);eeActive();});

		function eeActive(){
		jQuery("#eepanel").animate({
			top:'0'}, "slow",function(){
					jQuery("#eepanelWrap").fadeIn("fast");
						bindEE();
				});
		}
}


/*logged in event listener controls*/
function bindEE(){
		jQuery("#eepages").bind("change", function(){
		 if(jQuery(this).val()!==null){
			 location = jQuery(this).val();
			}
		});
		jQuery("#eehelpt").bind("click", function(){
		 jQuery("#eeinfo").slideUp("normal");
		 jQuery("#eefiles").slideUp("normal");
		 jQuery("#eehelp").slideToggle("normal");
		});
		jQuery("#eeinfot").bind("click", function(){
		 jQuery("#eehelp").slideUp("normal");
		 jQuery("#eefiles").slideUp("normal");
		 jQuery("#eeinfo").slideToggle("normal");
		});
		jQuery("#eefilest").bind("click", function(){
		 jQuery("#eehelp").slideUp("normal");
		 jQuery("#eeinfo").slideUp("normal");
		 jQuery("#eefiles").slideToggle("normal");
		});

		jQuery("#eeinclo").bind("click", function(){
		 eeLogoutCheck();
		});


		/*Setup edit buttons and behaviours for content areas*/
		jQuery(".e-ease").each(function() {
			 var v = '#'+jQuery(this).attr("id");
			 jQuery(v).addClass("editactive");
			 jQuery(v).before('<div class="eeClick" id="ee-'+jQuery(this).attr("id")+'"><img src="'+eePath+'/img/page_edit.png" alt="click to edit" title="click to edit"/> <span>click to edit</span></div>');
			 jQuery(v).before('<div class="eeClickEdit" style="display:none;" id="ed-'+jQuery(this).attr("id")+'"><div class="eesave" id="es-'+jQuery(this).attr("id")+'"><img src="'+eePath+'/img/page_save.png" alt="save" title="save"/><span> save</span></div><div class="eecancel" id="ex-'+jQuery(this).attr("id")+'"><img src="'+eePath+'/img/page_delete.png" alt="cancel" title="cancel"/><span> cancel</span></div></div>');
		});

		jQuery(".eeClick").bind("click", function() {
			 var u = jQuery(this).attr("id").split("ee-");
			 editNow(u[1]);
		} );

		/* hover effects on the editable areas */
		jQuery(".e-ease").bind("mouseover", function() {
			 var v = '#'+jQuery(this).attr("id");
			 jQuery(v).addClass("edithover");
		} );

		jQuery(".e-ease").bind("mouseout", function() {
			 var v = '#'+jQuery(this).attr("id");
			 jQuery(v).removeClass("edithover");
		} );

		/*add selected to the drop down list of pages*/
		url = location.href.split("/");
		var last = url.pop();
		jQuery("#eepages option").each(function() {
		 if(jQuery(this).val()===last){
		  jQuery(this).attr({ selected: "selected" })
		 }
		});


		jQuery(".eesave").bind("click", function() {
			 eeSave(jQuery(this).attr("id"));
		} );

		jQuery(".eecancel").bind("click", function() {
			 eeCancel(jQuery(this).attr("id"));
		} );


}

function unbindEE(){
		jQuery(".e-ease").each(function() {
			 var v = '#'+jQuery(this).attr("id");
			 jQuery(v).removeClass("editactive");
		});

		jQuery(".eeClick").remove();
		jQuery(".eeClickEdit").remove();
		jQuery(".e-ease").unbind("mouseover");
		jQuery(".e-ease").unbind("mouseout");
		jQuery(".e-ease .eeClick").unbind("click");
		jQuery("#eepages").unbind("change");
		jQuery("#eehelpt").unbind("click");
		jQuery("#eeinfot").unbind("click");
		jQuery("#eeinclo").unbind("click");
		jQuery("#eesave").unbind("click");
		jQuery("#eecancel").unbind("click");
}
/*######## END LOGIN & DISPLAY PANEL FUNCTIONS #########*/

/*########    START EDIT CONTENT FUNCTIONS     #########*/
function editNow(val){
		var vID = '#'+val;
		var vh = jQuery(vID).outerHeight();
		var vw = jQuery(vID).outerWidth();
		var vCSS = jQuery(vID).attr("class");
		var veditor = /ee:\S*/i.exec(vCSS);
		var nW = (jQuery(window).width()-150);
		var nH = (jQuery(window).height()-130);


		if(veditor=='ee:wwig'){
		jQuery(vID).before('<div id="ma-'+val+'" class="emask2" style="height:'+vh+'px;width:'+vw+'px;"></div>');
				jQuery(vID).addClass("edithoverActive");
				jQuery('#ee-'+val).fadeOut("slow");
				tb_show('EDIT CONTENT', ''+eePath+'/tiny_mce.php?file='+val+'&amp;TB_iframe=true&amp;height='+nH+'&amp;width='+nW+'&amp;modal=true', null);
				jQuery("#TB_window").css({ border: "6px solid #92C2D3" });

		}else{
		jQuery(vID).before('<div id="ma-'+val+'" class="emask" style="height:'+vh+'px;width:'+vw+'px;"></div>');

				var jQuerycontents = jQuery.post(""+eePath+"/getfile.php",{file:val,height:vh,width:vw,eType:veditor,eData:"edit"}, function(data){
						jQuery(vID).html(data);
						jQuery(vID).addClass("edithoverActive");
						jQuery('#ed-'+val).fadeIn("slow");
						jQuery('#ee-'+val).fadeOut("slow");
						jQuery('#ma-'+val).fadeOut("fast",function(){
								jQuery('#ma-'+val).remove();
						});
				});

		}

}


function eeSave(tVal){
		var fval = tVal.split("es-");
		var nData = jQuery("#eeta-"+fval[1]).val();
		var vID = '#'+fval[1];
		var vh = jQuery(vID).outerHeight();
		var vw = jQuery(vID).outerWidth();
		jQuery(vID).before('<div id="ma-'+fval[1]+'" class="emask" style="height:'+vh+'px;width:'+vw+'px;"></div>');
				var jQuerycontents = jQuery.post(""+eePath+"/getfile.php",{file:fval[1],eData:"save",nData:nData}, function(data){
						jQuery('#'+fval[1]).html(data);
						jQuery('#'+fval[1]).removeClass("edithoverActive");
						jQuery('#ed-'+fval[1]).fadeOut("slow");
						jQuery('#ee-'+fval[1]).fadeIn("medium");
						jQuery('#ma-'+fval[1]).fadeOut("fast",function(){
								jQuery('#ma-'+fval[1]).remove();
						});
				});
}


function eeCancel(tVal){
		var fval = tVal.split("ex-");
		var vID = '#'+fval[1];
		var vh = jQuery(vID).outerHeight();
		var vw = jQuery(vID).outerWidth();
		jQuery(vID).before('<div id="ma-'+fval[1]+'" class="emask" style="height:'+vh+'px;width:'+vw+'px;"></div>');
				var jQuerycontents = jQuery.post(""+eePath+"/getfile.php",{file:fval[1],eData:"cancel"}, function(data){
						jQuery('#'+fval[1]).html(data);
						jQuery('#'+fval[1]).removeClass("edithoverActive");
						jQuery('#ed-'+fval[1]).fadeOut("slow");
						jQuery('#ee-'+fval[1]).fadeIn("medium");
						jQuery('#ma-'+fval[1]).fadeOut("fast",function(){
								jQuery('#ma-'+fval[1]).remove();
						});
				});
}


function closeEditing(tVal,nData){
	tb_remove();
		if(nData!=='xxlee'){
				var jQuerycontents = jQuery.post(""+eePath+"/getfile.php",{file:tVal,eData:"save",nData:nData}, function(data){
						jQuery('#'+tVal).html(data);
						jQuery('#'+tVal).removeClass("edithoverActive");
						jQuery('#ed-'+tVal).fadeOut("slow");
						jQuery('#ee-'+tVal).fadeIn("medium");
						jQuery('#ma-'+tVal).fadeOut("fast",function(){
								jQuery('#ma-'+tVal).remove();
						});
				});
		}else{
				jQuery('#'+tVal).removeClass("edithoverActive");
				jQuery('#ed-'+tVal).fadeOut("slow");
				jQuery('#ee-'+tVal).fadeIn("medium");
				jQuery('#ma-'+tVal).fadeOut("medium",function(){
						jQuery('#ma-'+tVal).remove();
				});
		}

}
/*########     END EDIT CONTENT FUNCTIONS      #########*/

function popData(showThis,title){
		tb_show(''+title+'', '#TB_inline?height=400&width=500&inlineId='+showThis+'', null);
		jQuery("#TB_window").css({ border: "6px solid #92C2D3" });
}