var the_id = false;
var the_tree_div = '#server-tree';
var current_page = location.hash;
var lang_code = '';
var page_title = '';
var loading = '<img src="/images/loading_ce.gif" alt="Loading..." title="Loading..." />';
var img_loading = '<div class="page-loading"><div>'+loading+'</div></div>';
$(document).ready(function()
{
	lang_code = $('#frm_language_code').val();
	page_title = document.title;
	if (typeof $.history != 'undefined') { $.history.init(pageload); }
	$("li.hijax a, a.hijax").livequery('click', function() { $.history.load($(this).attr('href').replace(/^.*#/, '')); return false; });
	$('#error').livequery('click', function() { $(this).slideUp('slow'); return false; });
	$('select#frm_lang_select').livequery('change', function() { document.getElementById('languagesform').submit(); });
	$('select#frm_site_selector').livequery('change', function() { document.getElementById('siteselectorform').submit(); });
	$('select#frm_site_selector2').livequery('change', function() { document.getElementById('siteselectorform2').submit(); });
	$('.help a').livequery('click', function() { return !window.open($(this).attr('href'), 'Help', 'width=400px,height=300px,resize,resizable=yes'); });
	$('a.confirm').livequery('click', function() { return confirm('Are you sure you wish to do this?'); });
	$('a.confirm-delete-editor').livequery('click', function()
	{
		var result = ($('#frm_txt_confirm_delete_editor'))? confirm($('#frm_txt_confirm_delete_editor').val()) : confirm('Are you sure you wish to delete this editor?');
		if (result) { $.history.load($(this).attr('href').replace(/^.*#/, '')); }
		return false;
	});
	$('a.confirm-delete-site').livequery('click', function()
	{
		var result = ($('#frm_txt_confirm_delete_site'))? confirm($('#frm_txt_confirm_delete_site').val()) : confirm('Are you sure you wish to delete this site?');
		if (result) { $.history.load($(this).attr('href').replace(/^.*#/, '')); $.post(lang_code+'/admin/head/', function(data) { $('#welcome').html(data); }); }
		return false;
	});
	$('.generate-password input').livequery('click', function()
	{
		if ($('#frm_password')) {
			var chars = 'abcdefghjkmnpqrstuvwxyz23456789';
		  var the_password = '';
		  for (var i = 0; i < 8; i++) {
		    var j = Math.floor(Math.random() * chars.length);
		    the_password += chars.charAt(j);
		  }
			$('#frm_password').val(the_password);
		}
		return false;
	});
	$('.remove-logo input').livequery('click', function()
	{
		var result = ($('#frm_txt_confirm_remove_logo'))? confirm($('#frm_txt_confirm_remove_logo').val()) : confirm('Are you sure you wish to remove your logo?');
		if (result) { $.history.load(lang_code+'/designer/branding_remove_logo/'); }
		return false;
	});
	$('.remove-branding a').livequery('click', function()
	{
		var result = ($('#frm_txt_revert_changes'))? confirm($('#frm_txt_revert_changes').val()) : confirm("This will revert back to the original 'Content Editor' logo and colour scheme.\n\nAre you sure this is what you would like to do?");
		if (result) { $.history.load(lang_code+'/designer/branding_reset/'); }
	});
	$('.remove-login-branding a').livequery('click', function()
	{
		var result = ($('#frm_txt_revert_changes'))? confirm($('#frm_txt_revert_changes').val()) : confirm("This will revert back to the original 'Content Editor' login and forgotten password templates.\n\nAre you sure this is what you would like to do?");
		if (result) { $.history.load(lang_code+'/designer/brand_login_reset/'); }
	});
	$('a.toggle').livequery('click', function()
	{
		var the_id = 'toggle';
		if ($(this).attr('rel') != '' && $(this).attr('rel') != 'undefined') { the_id = $(this).attr('rel'); }
		$('#'+the_id).slideToggle('slow');
		return false;
	});
	$('a.new-window').livequery('click keypress', function() { return !window.open($(this).attr('href')); });
	$('a.new-pwindow').livequery('click keypress', function() { return !window.open($(this).attr('href'),'vv','width=600,height=550,scrollbars=yes,resizable=0,top=100,left=100'); });
	$('.newsletter').livequery('click', function()
	{
		the_img = $('#newsletter_checkbox').attr('src');
		the_id = 'frm_newsletter';
		if (the_img.indexOf('_on.gif') === -1) {
			if ($('#'+the_id)) { $('#newsletter_checkbox').attr('src', str_replace('_off.gif', '_on.gif', the_img)); $('#'+the_id).val(1); }
		} else {
			if ($('#'+the_id)) { $('#newsletter_checkbox').attr('src', str_replace('_on.gif', '_off.gif', the_img)); $('#'+the_id).val(0); }
		}
	});
	$('.enable-new-pages').livequery('click', function()
	{
		the_img = $('#enable_new_pages_checkbox').attr('src');
		the_id = 'frm_enable_new_pages';
		if (the_img.indexOf('_on.gif') === -1) {
			if ($('#'+the_id)) { $('#enable_new_pages_checkbox').attr('src', str_replace('_off.gif', '_on.gif', the_img)); $('#'+the_id).val('1'); }
		} else {
			if ($('#'+the_id)) { $('#enable_new_pages_checkbox').attr('src', str_replace('_on.gif', '_off.gif', the_img)); $('#'+the_id).val('0'); }
		}
	});
	$('.enable-tidy').livequery('click', function()
	{
		the_img = $('#enable_tidy_checkbox').attr('src');
		the_id = 'frm_enable_tidy';
		if (the_img.indexOf('_on.gif') === -1) {
			if ($('#'+the_id)) { $('#enable_tidy_checkbox').attr('src', str_replace('_off.gif', '_on.gif', the_img)); $('#'+the_id).val(1); }
		} else {
			if ($('#'+the_id)) { $('#enable_tidy_checkbox').attr('src', str_replace('_on.gif', '_off.gif', the_img)); $('#'+the_id).val(0); }
		}
	});
	$('.info img').livequery('mouseover', function(e)
	{
		if (!e) e = window.event;
		if (e) {
			if (e.pageX || e.pageY) {
				mousex = e.pageX;
				mousey = e.pageY;
				algor = '[e.pageX]';
				if (e.clientX || e.clientY) algor += ' [e.clientX] '
			} else if (e.clientX || e.clientY) {
				mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
				mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
				algor = '[e.clientX]';
				if (e.pageX || e.pageY) algor += ' [e.pageX] '
			}
		}
		$('#information-box').html($(this).attr('alt'));
		if ($('#information-box').html() != '') {
			$('#information-box').attr('style', 'top: '+String(parseInt(mousey + 15))+'px; left: '+String(parseInt(mousex - 175))+'px;');
			$('#information-box').fadeIn('slow');
		}
	});
	$('.info img').livequery('mouseout', function() { $('#information-box').fadeOut('slow', function () { /*$('#information-box').html('');*/ }); });
	$('.home-info img').livequery('mouseover', function(e)
	{
		if (!e) e = window.event;
		if (e) {
			if (e.pageX || e.pageY) {
				mousex = e.pageX;
				mousey = e.pageY;
				algor = '[e.pageX]';
				if (e.clientX || e.clientY) algor += ' [e.clientX] '
			} else if (e.clientX || e.clientY) {
				mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
				mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
				algor = '[e.clientX]';
				if (e.pageX || e.pageY) algor += ' [e.pageX] '
			}
		}
		//$('#information-box').html($(this).attr('alt'));
		if ($('#information-box').html() != '') {
			$('#information-box').attr('style', 'top: '+String(parseInt(mousey + 15))+'px; left: '+String(parseInt(mousex - 175))+'px;');
			$('#information-box').fadeIn('slow');
		}
	});
	$('.home-info img').livequery('mouseout', function() { $('#information-box').fadeOut('slow', function () { /*$('#information-box').html('');*/ }); });
	$('.revert-default-email').livequery('click', function() { $('#frm_email_html').val($('#frm_email_html_default').val()); $('#frm_email_subject').val($('#frm_email_subject_default').val()); return false; });
	$('.save-email').livequery('click', function()
	{
		if ($('#frm_save_email').val() != '1' && $('#frm_email_subject').val() != 1) {
			$('#save-email-checkbox').attr('src', '/images/btn_checkbox_on.gif'); $('#frm_save_email').val(1);
		} else {
			$('#save-email-checkbox').attr('src', '/images/btn_checkbox_off.gif'); $('#frm_save_email').val(0);
		}
		return false;
	});
	$('.rte-checkbox img').livequery('click', function()
	{
		the_id = 'frm_rte_icons';
		if ($(this).attr('src').indexOf('_on.gif') === -1) { // add icon to hidden field
			if ($('#'+the_id)) { $(this).attr('src', str_replace('_off.gif', '_on.gif', $(this).attr('src'))); $('#'+the_id).val($('#'+the_id).val()+$(this).attr('alt')+','); }
		} else { // remove icon from hidden field
			if ($('#'+the_id)) { $(this).attr('src', str_replace('_on.gif', '_off.gif', $(this).attr('src'))); $('#'+the_id).val(str_replace($(this).attr('alt')+',', '', $('#'+the_id).val())); }
		}
	});
	$('.choose-root input').livequery('click', function() { return choose_folder(false); });
	$('.choose-upload-path a').livequery('click', function() { return choose_folder(true); });
	$('.node').livequery('click', function()
	{
		switch ($(this).parent().get(0).className)
		{
			case 'root-closed': $(this).parent().toggleClass('root-open'); $(this).parent().toggleClass('root-closed'); break;
			case 'folder-open': case 'folder-closed': $(this).parent().toggleClass('folder-open'); $(this).parent().toggleClass('folder-closed'); break;

			case 'folder-open choose-upload': case 'folder-closed choose-upload':
			case 'choose-upload folder-open': case 'choose-upload folder-closed':
				if ($('#frm_uploads_path')) { $(this).parent().toggleClass('folder-open'); $(this).parent().toggleClass('folder-closed'); $('#frm_uploads_path').val($(this).attr('rel')); } break;

			case 'folder-open choose-folder': case 'folder-closed choose-folder':
			case 'choose-folder folder-open': case 'choose-folder folder-closed':
				if ($('#frm_root_dir'))
				{
					$(this).parent().toggleClass('folder-open');
					$(this).parent().toggleClass('folder-closed');
					$('#frm_root_dir').val($(this).attr('rel'));
					if (typeof $('.selected-folder').get(0) != 'undefined') {
						$('.tick').remove();
						$('.selected-folder').removeClass('selected-folder');
					}
					$(this).addClass('selected-folder').prepend('<img src="/images/btn_selected_folder.gif" alt="" class="tick" />');
				}
				break;

			case 'page':
				$('#frm_root_dir').val($(this).attr('rel'));
				if (typeof $('.selected-folder').get(0) != 'undefined') {
					$('.tick').remove();
					$('.selected-folder').removeClass('selected-folder');
				}
				$(this).addClass('selected-folder').prepend('<img src="/images/btn_selected_folder.gif" alt="" class="tick" />');
				return false;
				break;

			default: break;
		}

		the_id = '#'+$(this).attr('rev');

		if (the_id != '' && the_id != '#' && $(the_id)) {
			if (!validate_site_details()) { return false; }

			if ($(the_id).html() == '') {
				$(the_id).toggleClass('loading');

				var the_url = $(this).attr('href') + '?path=' + $(this).attr('rel');

				if (strpos(document.location, '/sites/add/') !== false || strpos(document.location, '/sites/update/') !== false) {
					if ($(this).parent().hasClass('choose-upload')) {
						the_url += '&class_name=choose-upload';
					} else if ($(this).parent().hasClass('choose-folder')) {
						the_url += '&class_name=choose-folder';
					}
				}

				if (strpos(document.location, '/pages/') !== false) { the_url += '&checkboxes=true'; }

				$.get(the_url+'&'+get_ftp_string(),{},function (data) {
					$(the_id).removeClass('loading');
					$(the_id).addClass('open');
					$(the_id).html(data);
					if (strpos(data, 'unable to connect') !== false) { $(this).addClass('outline-error'); }
				});
			} else { $(the_id).toggleClass('open'); }
		}
		return false;
	});
	$('.editor-node').livequery('click', function()
	{
		switch ($(this).parent().get(0).className)
		{
			case 'root-closed': $(this).parent().toggleClass('root-open'); $(this).parent().toggleClass('root-closed'); break;
			case 'folder-open': case 'folder-closed': $(this).parent().toggleClass('folder-open'); $(this).parent().toggleClass('folder-closed'); break;

			case 'folder-open choose-folder': case 'folder-closed choose-folder':
			case 'choose-folder folder-open': case 'choose-folder folder-closed':
				$(this).parent().toggleClass('folder-open');
				$(this).parent().toggleClass('folder-closed');
				break;

			case 'page':
				var no_of_selected_pages = parseInt($('#page-count').html());
				var restricted_pages = $('#frm_pages').val().split(',');
				if ($(this).hasClass('selected-folder')) {
					$('.tick', $(this)).remove();
					$(this).removeClass('selected-folder');
					$('#page-count').html((no_of_selected_pages - 1));
					if ($.inArray($(this).attr('rel'), restricted_pages) == -1) { $('#frm_pages').val($('#frm_pages').val()+','+$(this).attr('rel')); }
				} else {
					$(this).addClass('selected-folder').prepend('<img src="/images/btn_selected_folder.gif" alt="" class="tick" />');
					$('#page-count').html((no_of_selected_pages + 1));
					if ($.inArray($(this).attr('rel'), restricted_pages) != -1) {
						restricted_pages.splice($.inArray($(this).attr('rel'), restricted_pages), 1)
						$('#frm_pages').val(restricted_pages.join(','));
					}
				}
				if ($('#page-count').html() < 0) { $('#page-count').html('0'); }
				return false;
				break;

			default: break;
		}
		return false;
	});
	$('#choose-background').livequery('click', function() {
		$(this).ColorPicker({ color: $('#frm_css_background').val(), onChange: function (hsb, hex, rgb) { $('#frm_css_background').val(hex); } });
		$(this).ColorPickerShow();
	});
	$('#choose-text').livequery('click', function() {
		$(this).ColorPicker({ color: $('#frm_css_text').val(), onChange: function (hsb, hex, rgb) { $('#frm_css_text').val(hex); } });
		$(this).ColorPickerShow();
	});
	$('#choose-link').livequery('click', function() {
		$(this).ColorPicker({ color: $('#frm_css_link').val(), onChange: function (hsb, hex, rgb) { $('#frm_css_link').val(hex); } });
		$(this).ColorPickerShow();
	});
	$('#loginform #frm_ce_lang').change(function() { $('#loginform').attr('action', '/'+$(this).val()+'/ce_index/log_in/') });
	$('#forgottenpasswordform #frm_ce_lang').change(function() { $('#forgottenpasswordform').attr('action', '/'+$(this).val()+'/ce_index/forgotten_password/') });
	$('form.ajax').livequery('submit', function() {
		if (typeof $('.validation-failed', $(this)).get(0) != 'undefined') { return false; }
		$('html, body').animate({scrollTop:0}, 'fast');
		$('#content #messages').slideUp('slow', function () { $('#content #messages').remove();} );
		$('.page-loading').remove();
		$('#content').prepend(img_loading);
		$.post($(this).attr('action')+'?'+$(this).serialize(), {}, function(data) {
			if (data.substring(0, 10) == 'redirect: ') {
				$.history.load(str_replace('/admin/#', '', data.substring(10)));
			} else { $('.page-loading').remove(); $('#content').prepend('<div id="messages">'+data+'</div>'); }
		});
		return false;
	});
	$('#cms-ce-close-show-box').livequery('click', function() {
		$('#cms-ce-show-box').fadeOut('fast', function () { close_box(); });
	});
});

var error_displayed = false;

function display_error (result, the_form)
{
	if (result === true) { return true; }
	var form_id = the_form.id;
	if (error_displayed === false) {
		error_displayed = true;
		var current_html = $('#'+form_id).html();
		$('#'+form_id).html('<p id="error" class="validation" style="display: none;"><a href="#">Information is required in the boxes highlighted in red</a></p>')+current_html;
	}
	$('#error').slideDown('slow');
	return false;
}

function validate_site_details ()
{
	if ($('#frm_url').val() == '') { alert('Please enter your website URL.'); return false; }
	if ($('#frm_ftp_server').val() == '') { alert('Please enter your FTP server address.'); return false; }
	if ($('#frm_username').val() == '') { alert('Please enter your username.'); return false; }
	if ($('#frm_password').val() == '') { alert('Please enter your password.'); return false; }
	if ($('#frm_root_dir').val() == '') { alert('Please select the root directory.'); return false; }
	return true;
}

function get_ftp_string ()
{
	var ftp_server = $('#frm_ftp_server').val();
	var the_type = $('#frm_type').val();
	var pasv_mode = $('#frm_pasv').val();

	var ftp_parts = ftp_server.split(':');
	var the_port = array_slice(ftp_parts, -1);
	the_port = the_port[0];

	if (!isNaN(the_port * 1)) { ftp_server = str_replace(':'+the_port, '', ftp_server); } else { the_port = (the_type == 'SFTP')? 22 : 21; }

	var username = $('#frm_username').val();
	var the_password = $('#frm_password').val();
	var the_url = $('#frm_url').val();

	root_dir = ($('#frm_root_dir').val() != '')? $('#frm_root_dir').val() : '/';
	return 'ftp_server='+urlencode(ftp_server)+'&port='+the_port+'&type='+the_type+'&pasv='+pasv_mode+'&username='+urlencode(username)+'&password='+urlencode(the_password)+'&url='+urlencode(the_url)+'&root_dir='+urlencode(root_dir);
}

function choose_folder (is_upload_path)
{
	if ($(the_tree_div)) {
		if (!validate_site_details()) { return false; }

		$(the_tree_div).parent().removeClass('outline-error');
		$(the_tree_div).addClass('loading');
		$(the_tree_div).html('');

		var the_url = lang_code+'/process/folders/?'+get_ftp_string();

		if (is_upload_path) { the_url += '&class_name=choose-upload'; } else { the_url += '&class_name=choose-folder'; }
		if ($(the_tree_div).parent().get(0).id == 'the-outline') { $(the_tree_div).parent().addClass('outline'); }

		$.get(the_url,{},function (data)
		{
			$(the_tree_div).removeClass('loading');
			$(the_tree_div).addClass('open');
			$(the_tree_div).html(data);
			if (strpos(data, 'unable to connect') !== false) { $(the_tree_div).parent().addClass('outline-error'); }
		});
	}
	return false;
}

function str_replace (search, replace, subject)
{
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Gabriel Paderni
	// +   improved by: Philip Peterson
	// +   improved by: Simon Willison (http://simonwillison.net)
	// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   bugfixed by: Anton Ongson
	// +      input by: Onno Marsman
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +    tweaked by: Onno Marsman
	// *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
	// *     returns 1: 'Kevin.van.Zonneveld'
	// *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
	// *     returns 2: 'hemmo, mars'
	var f = search, r = replace, s = subject;
	var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
	while (j = 0, i--) { if (s[i]) { while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){} } }
	return sa ? s : s[0];
}

function urlencode (str)
{
	// http://kevin.vanzonneveld.net
	// +   original by: Philip Peterson
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +      input by: AJ
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Brett Zamir
	// %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
	// *     example 1: urlencode('Kevin van Zonneveld!');
	// *     returns 1: 'Kevin+van+Zonneveld%21'
	// *     example 2: urlencode('http://kevin.vanzonneveld.net/');
	// *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
	// *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
	// *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
	var histogram = {}, tmp_arr = [];
	var ret = str.toString();
	var replacer = function (search, replace, str) {
		var tmp_arr = [];
		tmp_arr = str.split(search);
		return tmp_arr.join(replace);
	};
	// The histogram is identical to the one in urldecode.
	histogram["'"]   = '%27';
	histogram['(']   = '%28';
	histogram[')']   = '%29';
	histogram['*']   = '%2A';
	histogram['~']   = '%7E';
	histogram['!']   = '%21';
	histogram['%20'] = '+';
	// Begin with encodeURIComponent, which most resembles PHP's encoding functions
	ret = encodeURIComponent(ret);
	for (search in histogram) {
		replace = histogram[search];
		ret = replacer(search, replace, ret) // Custom replace. No regexing
	}
	// Uppercase for full PHP compatibility
	return ret.replace(/(\%([a-z0-9]{2}))/g, function (full, m1, m2) { return "%"+m2.toUpperCase(); });
	return ret;
}

function strpos (haystack, needle, offset)
{
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Onno Marsman
	// *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
	// *     returns 1: 14
	var i = (haystack+'').indexOf(needle, offset);
	return i===-1 ? false : i;
}

function array_slice (arr, offst, lgth, preserve_keys)
{
	// http://kevin.vanzonneveld.net
	// +   original by: Brett Zamir
	// -    depends on: is_int
	// +      input by: Brett Zamir
	// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// %          note: Relies on is_int because !isNaN accepts floats
	// *     example 1: array_slice(["a", "b", "c", "d", "e"], 2, -1);
	// *     returns 1: {0: 'c', 1: 'd'}
	// *     example 2: array_slice(["a", "b", "c", "d", "e"], 2, -1, true);
	// *     returns 2: {2: 'c', 3: 'd'}
	/*if ('callee' in arr && 'length' in arr) { arr = Array.prototype.slice.call(arr); }*/
	var key = '';
	if (!(arr instanceof Array) || (preserve_keys && offst != 0)) {
		// Assoc. array as input or if required as output
		var lgt =0, newAssoc = {};
		for (key in arr) {
			//if (key !== 'length') {
			lgt += 1;
			newAssoc[key] = arr[key];
			//}
		}
		arr = newAssoc;
		offst = (offst < 0) ? lgt + offst : offst;
		lgth  = lgth == undefined ? lgt : (lgth < 0) ? lgt + lgth - offst : lgth;
		var assoc = {};
		var start = false, it=-1, arrlgth=0, no_pk_idx=0;
		for (key in arr) {
			++it;
			if (arrlgth >= lgth) { break; }
			if (it == offst) { start = true; }
			if (!start) { continue; }
			++arrlgth;
			if (is_int(key) && !preserve_keys) {
				assoc[no_pk_idx++] = arr[key];
			} else {
				assoc[key] = arr[key];
			}
		}
		//assoc.length = arrlgth; // Make as array-like object (though length will not be dynamic)
		return assoc;
	}
	if (lgth === undefined) {
		return arr.slice(offst);
	} else if (lgth >= 0) {
		return arr.slice(offst, offst + lgth);
	} else { return arr.slice(offst, lgth); }
}

function pageload (hash) {
	if (current_page == '') { current_page = hash; }
	$('html, body').animate({scrollTop:0}, 'fast');
	if ($('#wrapper #container #lft li a[href = "'+hash+'"]').get(0) != 'undefined') {
		$('#wrapper #container #lft li a.hi').removeClass('hi');
		$('#wrapper #container #lft li a[href = "'+hash+'"]').addClass('hi');
	}
	if(hash) {
		$('.page-loading').remove();
		$('#content').prepend(img_loading);
		$.post(hash, function(data) {
			if (current_page == lang_code+'/designer/update/' || current_page == lang_code+'/sites/add/' || strpos(current_page, '/sites/update/') !== false) {
				$.post(lang_code+'/admin/head/', function(data) { $('#welcome').html(data); });
			}
			if (data.substring(0, 10) == 'redirect: ') {
				$.history.load(str_replace('/admin/#', '', data.substring(10)));
			} else {
				$('#content').html(data);
				document.title = $('#content h1').text()+' :: '+page_title;
				current_page = hash;
			}
		});
	} else { $('#content').empty(); }
}
if (jQuery.browser == 'Safari') { document.write('<style type="text/css">form .row label { padding-top: 6px; } form .row span.info { padding-top: 8px; }</style>'); }