function AqbBnrMain(oOptions) {
    this._sActionsUrl = oOptions.sActionUrl;
    this._sObjName = oOptions.sObjName == undefined ? 'oAqbBnrMain' : oOptions.sObjName;
    this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'slide' : oOptions.sAnimationEffect;
    this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
    this._oHtmlIds = oOptions.oHtmlIds == undefined ? {} : oOptions.oHtmlIds;
    this._sWrnDelete = oOptions.sWrnDelete != undefined ? oOptions.sWrnDelete : '';
}
AqbBnrMain.prototype.executeRequest = function(sAction, aParams, onResult, sType) {
	if(onResult == undefined || onResult == null) 
		onResult = function(){};
	if(sType == undefined || sType == "")
		sType = 'xml';

	var oDate = new Date();
	aParams._t = oDate.getTime();
	$.post(this._sActionsUrl + 'act_'+ sAction, aParams, onResult, sType);
};
AqbBnrMain.prototype.click = function(oLink, iId) {
	this.executeRequest(
		'click',
		{id:iId},
		function(oData) {},
		'json'
	);

	return true;
};
AqbBnrMain.prototype.approve = function(oLink, iId) {
	var $this = this;

	this.executeRequest(
		'approve',
		{id:iId},
		function(oData) {
			alert(oData.message);
			if(oData.code != 0) return;
		
			$(oLink).parents("#" + $this._oHtmlIds['banner'] + oData.id).find("div.bnr_ban_stats > span.bnr_ban_status").html(oData.status);
		},
		'json'
	);

	return true;
};
AqbBnrMain.prototype.reject = function(oLink, iId) {
	var $this = this;

	this.executeRequest(
		'reject',
		{id:iId},
		function(oData) {
			alert(oData.message);
			if(oData.code != 0)	return;
			
			$(oLink).parents("#" + $this._oHtmlIds['banner'] + oData.id).find("div.bnr_ban_stats > span.bnr_ban_status").html(oData.status);
		},
		'json'
	);

	return true;
};
AqbBnrMain.prototype.remove = function(oLink, iId) {
	var $this = this;

	if(!confirm(this._sWrnDelete)) 
		return;

	this.executeRequest(
		'remove',
		{id:iId},
		function(oData) {
			if(oData.code != 0) {
				alert(oData.message);
				return;
			}

			$("#" + $this._oHtmlIds['banner'] + oData.id).bx_anim('hide', $this._sAnimationEffect, $this._iAnimationSpeed, function(){
				$(this).remove();
			});
		},
		'json'
	);

	return true;
};
AqbBnrMain.prototype.changeType = function(oSelect) {
	var $this = this;
	var oChanger = {
		txt: 'img', 
		img: 'txt'
	};
	
	var oForm = $(oSelect).parents('form:first');
	var sValue = oForm.find(":input[name='bnr_type']").val();

	$('#' + this._oHtmlIds['banner_type_' + oChanger[sValue] + '_prv'] + ',tr:has(#' + this._oHtmlIds['banner_type_' + oChanger[sValue] + '_cpt'] + ',#' + this._oHtmlIds['banner_type_' + oChanger[sValue] + '_cnt'] + ')').bx_anim('hide', this._sAnimationEffect, this._iAnimationSpeed, function() {
		$('#' + $this._oHtmlIds['banner_type_' + sValue + '_prv'] + ',tr:has(#' + $this._oHtmlIds['banner_type_' + sValue + '_cpt'] + ',#' + $this._oHtmlIds['banner_type_' + sValue + '_cnt'] + ')').bx_anim('show', $this._sAnimationEffect, $this._iAnimationSpeed);
	});

	oForm.find(".button_wrapper:has(:input[name='bnr_preview'])").bx_anim((sValue == 'txt' ? 'show' : 'hide'), this._sAnimationEffect, this._iAnimationSpeed);
};
AqbBnrMain.prototype.calculate = function(oButton) {
	var $this = this;
	var aParams = {};
	var oForm = $(oButton).parents('form:first');

	var iDays = parseInt(oForm.find("[name='bnr_days']").val());
	aParams['days'] = (iDays ? iDays : 0);

	var aPages = new Array();
	$.each(oForm.find(":checkbox[name='bnr_pages[]']:checked"), function() {
		aPages[aPages.length] = $(this).val();
	});
	aParams['pages[]'] = aPages;

	if(aParams['days'] > 0 && aParams['pages[]'].length > 0)
		this.executeRequest(
			'calculate',
			aParams,
			function(oData) {
				if(oData) {
					$('#' + $this._oHtmlIds['banner_total'] + ' > span').html(oData.total);
					alert(aDolLang['_aqb_bnr_msg_total_is'] + ' ' + oData.total + ' ' + oData.currency_code);
				}
			},
			'json'
		);
};
AqbBnrMain.prototype.preview = function(oButton) {
	var oForm = $(oButton).parents('form:first');

	if(oForm.find(":input[name='bnr_type']").val() != 'txt') 
		return; 

	$('#' + this._oHtmlIds['banner_type_txt_prv'] + ' .bnr_ban_title').html(oForm.find(":input[name='bnr_caption']").val());
	$('#' + this._oHtmlIds['banner_type_txt_prv'] + ' .bnr_ban_text').html(oForm.find(":input[name='bnr_content']").val());
};
AqbBnrMain.prototype.onSubmit = function(oForm) {
	var aPages = new Array();
	$.each($(oForm).find(":checkbox[name='bnr_pages[]']:checked"), function() {
		aPages[aPages.length] = $(this).val();
	});
	if(aPages.length <= 0) {
		alert(aDolLang['_aqb_bnr_wrn_enter_page']);
		return false;
	}

	var iDays = parseInt($(oForm).find("[name='bnr_days']").val());
	if(!iDays) {
		alert(aDolLang['_aqb_bnr_wrn_enter_days']);
		return false;
	}
	
	var sStart = $(oForm).find("[name='bnr_start']").val();
	if(!sStart) {
		alert(aDolLang['_aqb_bnr_wrn_enter_start']);
		return false;
	}

	return true;
}
AqbBnrMain.prototype.addDiscount = function(oLink) {
	var oDiscount = $(oLink).parents('.bnr_discounts:first').find('.bnr_discount:last').clone();
	oDiscount.find(':input').val('');	
	oDiscount.find('.bnr_delete').html('');
	$(oLink).parent().siblings('.bnr_pd_right').append(oDiscount);
};

AqbBnrMain.prototype.removeDiscount = function(iId) {
	var $this = this;

	this.executeRequest(
		'removeDiscount',
		{id:iId},
		function(oData) {
			if(oData.code != 0) {
				alert(oData.message);
				return;
			}
				
			$('#bnr_discount_' + iId).bx_anim('hide', $this._sAnimationEffect, $this._iAnimationSpeed, function() {
				$(this).remove();
			});
		},
		'json'
	);
};

AqbBnrMain.prototype.changeAdminBannersType = function(oLink) {
	var $this = this;
    var sId = $(oLink).attr('id');
    var sType = sId.substr(sId.lastIndexOf('-') + 1, sId.length);

    //--- Change Control ---//
    $(oLink).parent().siblings('.active:visible').hide().siblings('.notActive:hidden').show().siblings('#' + sId + '-pas:visible').hide().siblings('#' + sId + '-act:hidden').show();

	//--- Change Content ---//
	var oContents = $(oLink).parents('.adm-db-head').siblings('.adm-db-content').children(':visible').bx_anim('hide', $this._sAnimationEffect, $this._iAnimationSpeed, function() {
		$('#' + $this._oHtmlIds['banners_admin'] + sType).bx_anim('show', $this._sAnimationEffect, $this._iAnimationSpeed);
	});
};
