function registerbanner(bannerid, string) {
	setTimeout("switch_banner('" + bannerid + "','" + string + "');", wpbanners_refreshinterval);
}

function wpbajaxloading(action) {
	if (action == "show") {
		Effect.Appear('wpbannersloading', {duration:0.15, from:0, to:0.5});
		Element.show('wpbannersloadingimage');
	} else {
		Effect.Fade('wpbannersloading', {duration:0.15, from:0.5, to:0});
		Element.hide('wpbannersloadingimage');
	}
}

function wpb_bannersactionchange(action) {
	Element.hide('changegroupdiv');
	
	if (action == "changegroup") {
		Element.show('changegroupdiv');
	}
}

function banners(page, group) {
	new Ajax.Updater('banners', wpBannersBase + '?cmd=banners&wpbpage=' + page + '&group=' + group + '', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function switch_banner(id, string) {
	new Ajax.Updater('' + id + '', wpBannersBase + '?cmd=switch_banner&id=0&string=' + string + '', {
		asynchronous: true,
		evalScripts: true,
		parameters: "string=" + string + "",
		onLoading: function(request) {
			$(id).innerHTML = '<span><img src="' + wpBannersUrl + '/images/loading.gif" alt="loading..." /> Loading...</span>';	
		},
		onComplete: function(request) {
			setTimeout("switch_banner('" + id + "','" + string + "');", wpbanners_refreshinterval);
		}
	});
}

function show_banner_form() {
	new Ajax.Updater('banners', wpBannersBase + '?cmd=save_banner', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function view_banner(id) {
	new Ajax.Updater('banners', wpBannersBase + '?cmd=view_banner&id=' + id + '', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function save_banner(id, form) {
	
	if (form == false) {
		var formValues = false;
	} else {
		var formValues = form.serialize(true);
	}
	
	new Ajax.Updater('banners', wpBannersBase + '?cmd=save_banner&id=' + id + '', {
		asynchronous:true,
		evalSripts:true,
		parameters:formValues,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function delete_banner(id, update) {
	new Ajax.Request(wpBannersBase + '?cmd=delete_banner&id=' + id + '&update=' + update + '', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) { 
			wpbajaxloading('show'); 
		},
		onComplete:function(request) {
			wpbajaxloading();
			$('banner_row_' + id + '').style.backgroundColor = '#FF0000';
			Effect.Fade('banner_row_' + id + '');
		}
	});
}

function delete_banners() {
	new Ajax.Updater('banners', wpBannersBase + '?cmd=delete_banners', {
		asynchronous:true,
		parameters:$('bannersform').serialize(true),
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function group_change(group_id) {
	Element.hide('creategroupdiv');
	
	if (group_id == "0") {
		Element.show('creategroupdiv');
	}
}

function groups(page) {
	new Ajax.Updater('groups', wpBannersBase + '?cmd=groups&wpbpage=' + page + '', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function show_group_form() {
	new Ajax.Updater('groups', wpBannersBase + '?cmd=save_group', {
		asynchronous:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function view_group(id, update) {
	new Ajax.Updater(update, wpBannersBase + '?cmd=view_group&id=' + id + '&update=' + update + '', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function save_group(id, form, update) {

	if (form == false) {
		var formValues = false;
	} else {
		var formValues = Form.serialize(form);
	}
	
	if (update == "" || update == false) {
		var update = 'groups';
	}

	new Ajax.Updater(update, wpBannersBase + '?cmd=save_group&id=' + id + '', {
		asynchronous:true,
		evalScripts:true,
		parameters:formValues,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function delete_group(id) {
	new Ajax.Request(wpBannersBase + '?cmd=delete_group&id=' + id + '', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
			$('group_row_' + id + '').style.backgroundColor = '#FF0000';
			Effect.Fade('group_row_' + id + '');
		}
	});
}

function delete_groups() {
	new Ajax.Updater('groups', wpBannersBase + '?cmd=delete_groups', {
		asynchronous:true,
		parameters:$('groupsform').serialize(true),
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function sizes(page) {
	new Ajax.Updater('sizes', wpBannersBase + '?cmd=sizes&wpbpage=' + page + '', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function show_size_form() {
	new Ajax.Updater('sizes', wpBannersBase + '?cmd=save_size', {
		asynchronous:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function save_size(id, form) {
	if (form == "") {
		var formValues = false;
	} else {
		var formValues = Form.serialize($(form));
	}

	new Ajax.Updater('sizes', wpBannersBase + '?cmd=save_size&id=' + id + '', {
		asynchronous:true,
		evalScripts:true,
		parameters:formValues,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	})
}

function delete_size(id) {
	new Ajax.Request(wpBannersBase + '?cmd=delete_size&id=' + id + '', {
		asynchronous:true,
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
			$('sizerow' + id + '').style.backgroundColor = '#ff0000';
			Effect.Fade('sizerow' + id, {
				from:1,
				to:0
			});
		}
	});
}

function delete_sizes() {
	new Ajax.Updater('sizes', wpBannersBase + '?cmd=delete_sizes', {
		asynchronous:true,
		evalScripts:true,
		parameters:$('sizesform').serialize(true),
		onLoading:function(request) {
			wpbajaxloading('show');
		},
		onComplete:function(request) {
			wpbajaxloading();
		}
	});
}

function wpb_confirmMessage(mess) {

	var messages = new Array();
	messages['group_delete'] = 'Are you sure you wish to remove this group and all its banners?';
	messages['groups_delete'] = 'Are you sure you wish to remove the selected groups and all their banners?';
	messages['banner_delete'] = 'Are you sure you wish to remove this banner?';
	messages['banners_delete'] = 'Are you sure you wish to remove all the selected banners?';
	messages['banners_mass'] = 'Are you sure you wish to execute this action on the selected banners?';
	messages['size_delete'] = 'Are you sure you wish to remove this size?';
	messages['sizes_delete'] = 'Are you sure you wish to remove the selected sizes?';
	
	var agree = confirm(messages[mess]);
	
	if (agree == true) {
		return true;
	} else {
		return false;
	}
}

function wpb_checkAllBoxes(obj, formname, fieldname) {		
	if(document.forms[formname].elements[fieldname+"[]"].length) {
		for (i = 0; i < document.forms[formname].elements[fieldname+"[]"].length; i++) {
			document.forms[formname].elements[fieldname+"[]"][i].checked = true;
		}
	} else {
		document.forms[formname].elements[fieldname+"[]"].checked = true;
	}
	
	$('mainCheckLink').innerHTML = '<a href="javascript:wpb_unCheckAllBoxes(this,\'' + formname + '\',\'' + fieldname + '\');" title="Uncheck all checkboxes">Uncheck</a>';
}

function wpb_unCheckAllBoxes(obj, formname, fieldname) {
	if(document.forms[formname].elements[fieldname+"[]"].length) {
		for (i = 0; i < document.forms[formname].elements[fieldname+"[]"].length; i++)
		{
			document.forms[formname].elements[fieldname+"[]"][i].checked = false;
		}
	} else {
		document.forms[formname].elements[fieldname+"[]"].checked = false;
	}
	
	$('mainCheckLink').innerHTML = '<a href="javascript:wpb_checkAllBoxes(this,\'' + formname + '\',\'' + fieldname + '\');" title="Check all checkboxes">Check</a>';
}

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

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
