// edit_loader.js
// -----------------------------------------------------------------------------
// Javascript functions to run the Expression Engine Edit Loader
//
// Project    > EE-EditLoader-May07
// Copyright  > (C) Digital Mail Limited (2005 / 2006)
// Author     > Richard Knight (RAK) for DML
// -----------------------------------------------------------------------------

// NOTE: Define el_editor_script_url in main page

var el_edit_buttons = dmb1_get_cookie('el_editmode');

var el_css_list;

window.onload = function()
{
	el_css_list = el_compile_css_list();
	if (el_edit_buttons == 'on') el_highlight_editable(el_editor_script_url);
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function el_toggle_edit_buttons(editor_script_url)
{
	el_edit_buttons == 'on' ? el_unhighlight_editable() : el_highlight_editable(editor_script_url);
	// Store state, so whether edit buttons are on or off is persistent from page-to-page
	var exp = dmb1_get_exp_date(1,0,0); // Set a cookie expiry 1 day from now
	dmb1_set_cookie('el_editmode', el_edit_buttons, exp, '/');
	el_editbuttons = 'on';
}

// -----------------------------------------------------------------------------

function el_highlight_editable(editor_script_url)
{
	var editable = getElementsByClassName('editable', 'div');
	for (var i=0; i<editable.length; i++)
	{
		var id = editable[i].getAttribute('id');
		var weblog_id = id.substring(1, id.indexOf('_'));
		var entry_id  = id.substring(id.indexOf('E') + 1);
		var eb = document.createElement('a');
		var qu_str = '?C=edit&M=edit_entry&weblog_id=' + weblog_id + '&entry_id=' + entry_id + '&el_css_list=' + el_css_list;
		if (el_styles_list != '') qu_str += '&el_styles_list=' + el_styles_list;
		eb.setAttribute('href', editor_script_url + qu_str);
		eb.setAttribute('target', '_EDITOR');
		eb.setAttribute('class', 'editbutton');
		var tx = document.createTextNode('EDIT');
		var tip = document.createElement('span');
		tip.setAttribute('class', 'tipcontents');
		tip.innerHTML = '<b>Weblog ' + weblog_id + '<\/b><br \/>Entry' + entry_id;
		eb.appendChild(tx);
		eb.appendChild(tip);
		editable[i].insertBefore(eb, editable[i].firstChild);
		editable[i].className = 'editable_on';
	}
	el_edit_buttons = 'on';
}

// -----------------------------------------------------------------------------

function el_unhighlight_editable()
{
	var editable = getElementsByClassName('editable_on', 'div');
	for (var i=0; i<editable.length; i++)
	{
		editable[i].removeChild(editable[i].firstChild);
		editable[i].className = 'editable';
	}
	el_edit_buttons = 'off';
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function el_compile_css_list(exclude_css)
{
	var css_list = '';
	for (i=0; i<document.styleSheets.length; i++)
	{
		if (document.styleSheets[i].href === null) continue;
		var add_css = true;
		for (j in exclude_css)
		{
			if (document.styleSheets[i].href.indexOf(exclude_css[j]) != -1)
			{
				add_css = false;
				break;
			}
		}
		if (document.styleSheets[i].href == document.location.href) add_css = false; // exclude inline CSS
		if (add_css) css_list += document.styleSheets[i].href + ',';
	}
	return css_list.slice(0, -1).replace(/\?/g, '|qu|');
}

// -----------------------------------------------------------------------------

function getElementsByClassName(className, tag, elm)
{
	// get elements with class 'className', that are 'tag' and are children of element 'elm'
	// eg. getElementsByClassName('editable', 'div', document.body.firstChild);
	// 'tag' and 'elm' are optional, but can be used to increase focus
	// Thanks to http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
	//
	var testClass = new RegExp('(^|\\s)' + className + '(\\s|$)');
	var tag = tag || '*';
	var elm = elm || document;
	var elements = (tag == '*' && elm.all) ? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for (var i=0; i<length; i++)
	{
		current = elements[i];
		if (testClass.test(current.className))
		{
			returnElements.push(current);
		}
	}
	return returnElements;
}

// ------------------------------------------------------------
// ------------- C O O K I E   U T I L I T I E S --------------
// ------------------------------------------------------------

// The following functions are taken from the core_1.5.0-7.js,
// and can be deleted when the EE-EditLoader is used on sites utilising TST

function dmb1_get_cookie(name)
{
	// Retreive cookie by name
	var arg = name + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		{
			return dmb1_get_cookie_value(j);
		}
		i = document.cookie.indexOf(' ', i) + 1;
		if (i == 0) break;
	}
	return '';
}

function dmb1_get_cookie_value(offset)
{
	// utility function called by dmb1_get_cookie
	var endstr = document.cookie.indexOf(';', offset);
	if (endstr == -1)
	{
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

// ------------------------------------------------------------

function dmb1_set_cookie(name, value, expires, path, domain, secure)
{
	// store cookie named name with value value
	// name and value are obligatory, the rest of the arguments are optional
	var cs = name + '=' + escape(value) +
		((expires) ? '; expires=' + expires : '') +
		((path) ? '; path=' + path : '; path=/') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');
		document.cookie = cs;
}

// ------------------------------------------------------------

function dmb1_delete_cookie(name, path, domain)
{
	// remove a cookie by setting an ancient expiry date
	if (dmb1_get_cookie(name)) {
		document.cookie = name + '=' +
			((path) ? '; path=' + path : '; path=/') +
			((domain) ? ';domain=' + domain : '') +
			'; expires=Thu, 01-Jan-70 00:00:01 GMT';
	}
}

// ------------------------------------------------------------

function dmb1_get_exp_date(days, hours, minutes)
{
	// utility function to retrieve an expriry date in proper format
	// pass three integer parameters for the number of days, hours and minutes
	// from now that you want the cookie to expire (or negative values for a past date)
	// NB: all three parameters are required, so use zeros where necessary
	var exp_date = new Date();
	if (typeof days == 'number' && typeof hours == 'number' && typeof minutes == 'number')
	{
		exp_date.setDate(exp_date.getDate() + parseInt(days));
		exp_date.setHours(exp_date.getHours() + parseInt(hours));
		exp_date.setMinutes(exp_date.getMinutes() + parseInt(minutes));
		return exp_date.toGMTString();
	}
}

// ------------------------------------------------------------
// ----------- C O M P A T I B I L I T Y   F I X E S ----------
// ------------------------------------------------------------

function get_DOM_ref(obj)
{
	// cross-browser function to get an object's DOM reference given its id
	if(document.getElementById && document.getElementById(obj)) return document.getElementById(obj); // W3C DOM
	if (document.all && document.all(obj)) return document.all(obj); // MSIE 4 DOM
	return false;
}