/* later maybe put into a jquery extension */

var comboBoxes;
var extComponents = {};

function checkMaxChars(element, max_length) {
	var length = $(element).val().length;
	
	if (length >= max_length) {
		var trimmed = $(element).val().substr(0, max_length);
		$(element).val(trimmed);
		return false;
	}
	
	return true;
}


function validEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	return filter.test(email);
}

function createAlertPassword(title, message, scope, callback, data) {
	var xhtml = message+' <div id="msgBoxConfirmDelete">\
			<div class="item">\
				<label>Password:</label>\
				<input type="password" name="password" />\
			</div>\
			<div class="item">\
				<label>Repeat Password:</label>\
				<input type="password" name="repeat_password" />\
			</div>\
		</div>';
	Ext.Msg.show({
		title: title,
		msg: xhtml,
		buttons: Ext.Msg.OKCANCEL,
		fn: onItemDeleteCallback,
		scope: scope,
		data: data,
		callback: callback
	});
}

function onItemDeleteCallback (buttonID, text, opt) {
	if (buttonID == 'ok') {
		var password = Ext.get('msgBoxConfirmDelete').child('input[name=password]').getValue().trim();
		var repeat_password = Ext.get('msgBoxConfirmDelete').child('input[name=repeat_password]').getValue().trim();
		
		if (password != repeat_password) {
			Ext.Msg.alert('Error', 'The password aren\'t the same');
		} else if (password == '') {
			Ext.Msg.alert('Error', 'You must provide your password');
		} else {
			var handler = opt.callback;			
			handler.apply(opt.scope, [password, opt.data]);
		}
	}
}

function popupWindow(url, w, h) {
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	mywindow = window.open(url, "admin_popup_window","location=0,toolbar=0,status=1,scrollbars=1,width=" + w + ",height=" + h + ",top=" + wint + ",left=" + winl);
}


$(document).ready(function(){
	$('.tooltip.sale.small .close-sale').click(function(){
		$.cookie('enjin_admin_sale', $(this).parent().parent().attr('sale_id'));
		$(this).parent().parent().fadeOut(350);
	});
	
	$('.tooltip.help-tooltip .close-tooltip').click(function(){
		var item = this;
		$.ajax({
			type: "POST",
			url: "/ajax.php?s=help",
			data: {cmd: "set-viewed", site_id: $(item).parent().parent().attr('data-site_id'), topic: $(item).parent().parent().attr('data-topic')},
			dataType: "json"
		});
		$(this).parent().parent().fadeOut(350);
	});
	
    $("select[multiple].acl-widget").each(function(){
    	if(window.acl_widget_addurl != undefined) var ajaxadd_url = acl_widget_addurl;
    	else var ajaxadd_url = "/admin/editmodule/add-acl/preset";
    	if(window.acl_widget_removeurl != undefined) var ajaxremove_url = acl_widget_removeurl;
    	else var ajaxremove_url = "/admin/editmodule/remove-acl/preset";
    	
		if($(this).attr('data-mode') == 'ajax')
		{
			var asmOptions = {
								animate: true,
								listType: 'ul',
								onAdd: function(item)
								{
									$.ajax({
										type: "POST",
										url:  ajaxadd_url+ "/" +$(item).parent().attr('data-preset_id'),
										data: {value: $(item).val(), access_type: $(item).parent().attr('data-access_type')},
										dataType: "json"
									});
								},
								onRemove: function(item)
								{
									$.ajax({
										type: "POST",
										url: ajaxremove_url + "/" + $(item).parent().attr('data-preset_id'),
										data: {value: $(item).val(), access_type: $(item).parent().attr('data-access_type')},
										dataType: "json"
									});
								}
							};			
			
			// if the control has a set width assigned to it add if to the options, do not put
			// a width option in otherwise so that the default settings can be used
			var width = $(this).attr('data-width');
			if ( parseInt(width,10) > 0 ) { asmOptions.width = width;} 
			
			$(this).asmSelect(asmOptions);
		}
		else if (!$(this).attr('skip-automatic')) 
		{
			var asmOptions = {
								animate: true,
								listType: 'ul'
							};
			// if the control has a set width assigned to it add if to the options, do not put
			// a width option in otherwise so that the default settings can be used
			var width = $(this).attr('data-width');
			if ( parseInt(width,10) > 0 ) { asmOptions.width = width;} 
			
			$(this).asmSelect(asmOptions);
		}
    });

	$(document).ajaxStart(function(){
		$('#ajax-busy').css('visibility', 'visible');
	}).ajaxStop(function(){
		$('#ajax-busy').css('visibility', 'hidden');
	});

	if(typeof admin_v2 !== 'undefined' && admin_v2) {
		$('select.style-dropdown').each(function(e){
			var width = 332;
			if($(this).hasClass('half')) width = 154;
			
			var self = this;
			var id = $(self).attr('id');
			extComponents[id] = new Ext.form.ComboBox({
				id: 'cmp_' + id,
				triggerAction: 'all',
				transform: self,
				tpl: '<tpl for="."><div class="x-combo-list-item">{text:htmlEncode}</div></tpl>',
				width: 332,
				forceSelection: true,
				editable: false,
				listClass: 'v2-dropdown-list',
				ctCls: 'v2-dropdown',
				shadow: false,
				listeners: {
					//'select': self.switchPage
				}
			});
		});
		$('select.style-dropdown-large').each(function(e){
			var width = 332;
			if($(this).hasClass('half')) width = 154;

			var self = this;
			var id = $(self).attr('id');
			extComponents[id] = new Ext.form.ComboBox({
				id: 'cmp_' + id,
				triggerAction: 'all',
				transform: self,
				tpl: '<tpl for="."><div class="x-combo-list-item">{text:htmlEncode}</div></tpl>',
				width: width,
				forceSelection: true,
				editable: false,
				listClass: 'v2-dropdown-list',
				ctCls: 'v2-dropdown-large',
				shadow: false,
				listeners: {
					//'select': self.switchPage
				}
			});
		});
	}
});
