// -------------------------------------------------------------------------------
// add in a plugin for checking and unchecking check boxes
// -------------------------------------------------------------------------------

$.fn.check = function(mode) {
	var mode = mode || 'on'; // if mode is undefined, use 'on' as default
	return this.each(function() {
		switch(mode) {
		case 'on':
			this.checked = true;
			break;
		case 'off':
			this.checked = false;
			break;
		case 'toggle':
			this.checked = !this.checked;
			break;
		}
	});
};


function ajaxify() {
	ajaxify_checkboxes();
	ajaxify_links();
}

function replace_with_throbber( item ) {
	return item.hide().after('<img src="/skins/standard/images/throbber.gif" width="20" height="20"/>').next();
}

function ajaxify_links() {
	$("a[ajaxurl]").unbind('click').each(function(){
		var this_link = $(this);
		var onclick = ''+this_link.attr('onclick');
		this_link.attr('ajaxonclick',onclick);
		this_link.attr('onclick','');
	}).click(function(){
		var this_link = $(this);

		var confirmed = true;
		if (this_link.attr('ajaxonclick')) {
			var confirm_function;
			eval('confirm_function='+this_link.attr('ajaxonclick'));
			confirmed = confirm_function();
		}
		if (!confirmed) return false;

		var throbber = replace_with_throbber(this_link);
		throbber.css('float',this_link.css('float'));
		var url_str = this_link.attr('ajaxurl');
		if (url_str=='.') url_str = this_link.attr('href');
		$.ajax({
			url:		url_str,
			type:		"GET",
			dataType:	"html",
			success:	function() {
				if (this_link.attr('ajaxonsuccess')) {
					eval(this_link.attr('ajaxonsuccess'));
				}
			},
			complete:	function(data,type){
				throbber.hide().remove();
				this_link.show();
			}
		});
		return false;
	});
}

function ajaxify_checkboxes() {
	$("input[ajaxurl]").unbind('click').click(function(){
		var this_checkbox = $(this);
		var throbber = replace_with_throbber(this_checkbox);
		$.ajax({
			url:		this_checkbox.attr('ajaxurl')+((this.checked)?this.value:''),
			type:		"GET",
			dataType:	"html",
			success:	function(data) {
				this_checkbox.check('toggle');
				if (this_checkbox.attr('ajaxonsuccess')) eval(this_checkbox.attr('ajaxonsuccess'));
			},
			complete:	function(data,type){
				throbber.hide().remove();
				this_checkbox.show();
				this_checkbox.check('toggle');
			}
		});
	});
}



// -------------------------------------------------------------------------------
// OpenWindow
// -------------------------------------------------------------------------------

if (typeof onerror != 'undefined') reportErrors = onerror;

window_link = '';
window_name = '';
window_width = '';
window_height = '';

function retry_window_open(errorMessage, url, line) {
	new_window = window.open('',window_name);
	new_window.close();
	openWindow(window_link,window_name,window_width,window_width);
	onerror = reportErrors;
	return true;
}

function openWindow( link, name, width, height ) {
	if (name==undefined) name='';
	name = name.replace(/[^a-z0-9]/gi,'_');
	if (width==undefined) width=800;
	if (height==undefined) height=500;
	// if we have been assed in the link object rather than the url string
	// then extract then read the url from the link
	if ( link.href!=undefined ) link = link.href;

	// if we have previously opened this window and then they have
	// navigated to another site trying to re-open a window with the
	// same name causes errors. So... we remember what we were doing
	// and when opening the window set the error handler to a function
	// which closes the old window before retrying the window open operation.
	window_link = link;
	window_name = name;
	window_width = width;
	window_height= height;

	onerror = retry_window_open;
	new_window = window.open('',name,'width='+width+',height='+height+',resizable=yes,directories=no,location=yes,scrollbars=1,status=yes,toolbar=yes');
	new_window.name = name; //for safari

	new_window.document.write('<HTML>Loading. Please wait...</HTML>');

	if (link) new_window.location.href=link;
	//if (link) window.open(link,name);

	new_window.focus();
	return new_window;
}


// -------------------------------------------------------------------------------
// Cross Browser support
// -------------------------------------------------------------------------------

function debug_alert( thing, print_values ) {
        output = '';
        for( property in thing ) {
                output += property;
                if (print_values && eval('thing.'+property) ) output += '="'+eval('thing.'+property+'.toString()')+'"';
                output += ', ';
        }
        window.alert( output );
}



//===========//
//  Sniffer  //
//===========//
var browser_type = new Object;

browser_type.is_ns4 = (document.layers) ? true:false;
browser_type.is_ie4 = (document.all) ? true:false;
browser_type.is_w3c = (document.getElementById) ? true:false;

//========================================//
//  Initialise cross-browser normalizers  //
//========================================//
norm = new Object;

if (browser_type.is_ie4) {
	norm.start_wrap 	= "document.all.";
	norm.style_object	= ".style";
	norm.end_wrap 		= "";
	norm.show 			= "visible";
	norm.hide 			= "hidden";
	norm.layer_borderx 	= 8;
	norm.layer_bordery 	= 13;
} else if (browser_type.is_w3c) {
	norm.start_wrap		= "document.getElementById('";
	norm.style_object	= ".style";
	norm.end_wrap		= "')";
	norm.show 			= "visible";
	norm.hide 			= "hidden";
	norm.layer_borderx 	= 6;
	norm.layer_bordery 	= 5;
} else if (browser_type.is_ns4) {
	norm.start_wrap 	= "document.layers.";
	norm.style_object	= "";
	norm.end_wrap 		= "";
	norm.show 			= "show";
	norm.hide 			= "hide";
	norm.layer_borderx 	= 10;
	norm.layer_bordery 	= 10;
}

function get_layer( name ) {
	return eval(norm.start_wrap + name + norm.end_wrap);
}

function get_style( object ) {
	return eval('object'+norm.style_object);
}

function get_image( name ) {
	return document.images[name]
}

function get_dims( layer ) {
	dims = new Object;

	if (browser_type.is_ns4) {
		dims.clipw = layer.clip.width;
		dims.cliph = layer.clip.height;
		dims.posx = layer.clip.left;
		dims.posy = layer.clip.top;
		dims.winw  = window.innerWidth + window.pageXOffset;
		dims.winh  = window.innerHeight + window.pageYOffset;
	} else if (browser_type.is_ie4) {
		dims.clipw = layer.offsetWidth;
		dims.cliph = layer.offsetHeight;
		dims.posx = layer.offsetLeft;
		dims.posy = layer.offsetTop;
		dims.winw  = document.body.clientWidth + document.body.scrollLeft;
		dims.winh  = document.body.clientHeight + document.body.scrollTop;
	} else if (browser_type.is_w3c) {
		dims.clipw = layer.offsetWidth;
		dims.cliph = layer.offsetHeight;
		dims.posx = layer.offsetLeft;
		dims.posy = layer.offsetTop;
		dims.winw  = window.innerWidth + window.pageXOffset;
		dims.winh  = window.innerHeight + window.pageYOffset;
	}

	return dims;
}

function clicktoshow( layer ) {
	l = get_layer(layer);
	style = get_style(l);
	style.display = 'block';
}

function clicktohide( layer ) {
	l = get_layer(layer);
	style = get_style(l);
	style.display = 'none';
}	

