function backgroundHelper(baseUrl, sectionUrlMapping) {
	
	this.baseUrl = baseUrl;
	this.sectionUrlMapping = sectionUrlMapping;
	
	this.section = '';
	this.lastAppliedItem = '';
	
	this.cache = [];
	this.pageCache = [];
	
	this.pageAsInput = false;
}

backgroundHelper.prototype.init = function(section) {
	
	this.initSection(section);
	
	this.initLayout();
	this.initEvents();
}

backgroundHelper.prototype.initLayout = function() {
	
	this.container = $('#bg-container');
	this.listContainer = $('#bg-list-container');
	
	this.pagesContainer = $('#bg-page-container');
	this.pageInput = $('#bg-page-input');
}

backgroundHelper.prototype.initSection = function(section, pageAsInput) {
	
	this.section = section;
	
	this.currentPage = 1;
	this.pageCount = 0;
	
	this.pageAsInput = pageAsInput;
	this.busy = false;
}

backgroundHelper.prototype.initEvents = function() {

	var self = this;
	
	self.listContainer.find('li.default').click(function() {
		self.removeBackground();
	})
	
	self.getItems().live('click', function() {
		
		var id = $(this).find('span.hidden:first').html();
		self.applyBackground(id);
	}) 
	
	self.getItems().find('div.delete').live('click', function(event) {
		
		var id = $(this).parent().find('span.hidden').html();
		
		var callback = function() {
			
			self.deleteAction(id,  function() {
				alertPopup.hide();
			});
		}	

		var alertPopup = new alertPopupHelper('alert', 'txt_bgimage_delete_warning', 'button_confirm', 'button_cancel', callback);	
		alertPopup.show();
		
		return false;
	})
	
	self.getItems().live('mouseover', function() {
		$(this).find('div.delete').show();
	})
	
	self.getItems().live('mouseout', function() {
		$(this).find('div.delete').hide();
	})
		
	self.pagesContainer.find('ul.lovers-nav li.prev-arrow').click(function() {
		self.pagePreviousAction();
	})
	
	self.pagesContainer.find('ul.lovers-nav li.next-arrow').click(function() {
		self.pageNextAction();
	})
	
	self.pageInput.live('keypress', function(event) {
		
		if (event.keyCode == 13) {
			
			event.preventDefault();
			
			var page = parseInt($(this).val());
			page = isNaN(page) || page < 1 ? 1 : page;
			
			self.pageAction(page);
		}
	}) 
}

backgroundHelper.prototype.getUrl = function(section) {
	
	section = section ? section : this.section;
	
	return this.baseUrl + sectionUrlMapping[this.section];
}

backgroundHelper.prototype.getPageCount = function() {
	
	if (!this.pageCache[this.section]) {
		return 0;
	}
	
	return this.pageCache[this.section];
}

backgroundHelper.prototype.persistPageCount = function(Object) {
	
	if (!this.pageCache[this.section]) {
		this.pageCache[this.section] = [];
	}
	
	this.pageCache[this.section] = Object;
}

backgroundHelper.prototype.getList = function() {
	
	if (!this.cache[this.section] || !this.cache[this.section][this.currentPage]) {
		return false;
	}

	return this.cache[this.section][this.currentPage];
}

backgroundHelper.prototype.persistList = function(Object) {
	
	if (!this.cache[this.section]) {
		this.cache[this.section] = [];
	}
	
	if (!this.cache[this.section][this.currentPage]) {
		this.cache[this.section][this.currentPage] = [];
	}
	
	this.cache[this.section][this.currentPage] = Object;
}

backgroundHelper.prototype.getItems = function() {
	
	return this.listContainer.find('li[class!="default"]');
}

backgroundHelper.prototype.clearBackground = function() {
	
	$('body').css({
		'backgroundImage': '',
		'backgroundAttachment': '',
		'backgroundColor': '',
		'backgroundRepeat': ''
	});
}

backgroundHelper.prototype.removeBackground = function() { 
	
	var self = this;
	
	if (!self.busy) {
		
		self.flagBusy();
		
		$.ajax({
			
			type: 'POST',
			url: self.getUrl(),
			data: '_action=ajaxRemove',
			
			success: function(Object) {
			
				self.resetBGPositionOptions();
				self.clearBackground();
				
				self.flagReady();
			}	
		})
	}	
}

backgroundHelper.prototype.previewBackground = function(id) {
	
	var list = this.getList();
	var image = list.items[id].image;
	
	$('body').css({
		
		'backgroundImage': 'url("' + image + '")',
		/* 'backgroundColor': 'transparent', */
	});
	
	if (this.section == 'bgimages') {
		
		$('body').css({
			'backgroundAttachment': 'scroll',
			'backgroundRepeat': 'no-repeat',
			'backgroundPosition': 'center top',
			
		});
		
		offsetBG(bgBodyOffsetY);
	}
	else {
		
		$('body').css({
			'backgroundAttachment': 'fixed',
			'backgroundRepeat': 'repeat',
		});
	}
}

backgroundHelper.prototype.flagBusy = function () {
	
	self.busy = true;
	$('#' + whiteLayerId).removeClass('hidden');
}

backgroundHelper.prototype.flagReady = function () {

	$('#' + whiteLayerId).addClass('hidden');
	self.busy = false;
}

backgroundHelper.prototype.applyBackground = function(id) { 

	var self = this;	

	if (!self.busy) {
		
		self.flagBusy();
		
		$.ajax({
			
			type: 'POST',
			url: self.getUrl(),
			data: '_action=ajaxChoose&backgroundId=' + id,
			
			success: function(Object) {
			
				self.resetBGPositionOptions();
				
				self.previewBackground(id);
				self.lastAppliedItem = id;
				
				self.afterApplyBackground();
			
				self.flagReady();
			}	
		})
	}	
}

backgroundHelper.prototype.afterApplyBackground = function() { 
} 

backgroundHelper.prototype.pageCountAction = function(callback) {

	var self = this;	
	
	var pageCount = self.getPageCount();
	
	if (pageCount) {
		self.afterPageCountAction();
	}
	else {
		$.ajax({
			
			type: 'POST',
			url: self.getUrl(),
			data: '_action=ajaxPageCount',
			dataType: 'json',
			
			success: function(Object) {
				
				if (!Object) {
					return;
				}
			
				self.persistPageCount(Object.pageCount);
				self.afterPageCountAction();
				
				if (callback) {
					callback();
				}
			}	
		})
	}	
}

backgroundHelper.prototype.afterPageCountAction = function() {
	this.listAction();
} 

backgroundHelper.prototype.pageAction = function(page) {
	this.listAction(page);
}

backgroundHelper.prototype.pagePreviousAction = function() { 
	
	if (this.currentPage > 1) {
		this.currentPage--;
	}
	
	this.pageAction(this.currentPage);
}

backgroundHelper.prototype.pageNextAction = function() { 
	
	if (this.currentPage < this.getPageCount()) {
		this.currentPage++;
	}
	
	this.pageAction(this.currentPage);
}

backgroundHelper.prototype.listAction = function(page) {

	var self = this;
	
	self.currentPage = page ? page : 1;
	
	var Object = self.getList();
	
	if (Object) {
		self.afterListAction(Object);
	}
	else {
	
		$.ajax({
			
			type: 'POST',
			url: self.getUrl(),
			data: '_action=ajaxList&page=' + self.currentPage,
			dataType: 'json',
			
			success: function(Object) {
			
				if (!Object) {
					return ;
				}
				
				self.persistList(Object);
				self.afterListAction(Object);
			}	
		})
	}	
}

backgroundHelper.prototype.deleteAction = function(id, callback) { 

	var self = this;
	
	$.ajax({
		
		type: 'POST',
		url: self.getUrl(),
		data: '_action=ajaxDelete&backgroundId=' + id,
		dataType: 'json',
		
		success: function(isApplied) {
		
			self.deletePageCache();
			self.deleteCache();
			
			if (isApplied) {
				self.removeBackground();
			}	
	
			self.pageCountAction(callback);
		}	
	})
}

backgroundHelper.prototype.deletePageCache = function() {
	this.cache[this.section] = '';
}

backgroundHelper.prototype.deleteCache = function() {
	this.pageCache[this.section] = '';
}

backgroundHelper.prototype.afterListAction = function(Object) {
	
	this.appendItems(Object);
	this.updatePageStatus();
}

backgroundHelper.prototype.updatePageStatus = function() {
	
	var pageCount = this.getPageCount();
	
	if (pageCount > 1) {
		
		this.pagesContainer.show();
		
		if (this.currentPage == 1) {
			
			this.pagesContainer.find('ul.lovers-nav li.prev-arrow').hide();
			this.pagesContainer.find('ul.lovers-nav li.next-arrow').show();
		}
		else if (this.currentPage == pageCount) { 
			
			this.pagesContainer.find('ul.lovers-nav li.next-arrow').hide();
			this.pagesContainer.find('ul.lovers-nav li.prev-arrow').show();
		}
		else {
			
			this.pagesContainer.find('ul.lovers-nav li.prev-arrow').show();
			this.pagesContainer.find('ul.lovers-nav li.next-arrow').show();
		}
		
		if (this.pageAsInput) {
			var pageCountHTML = 
				'<span class="page-txt">' + phrases['txt_pages'] + ':</span> ' + 
				'<input type="text"  id="bg-page-input" class="pages-input" value="' + this.currentPage + '" /> ' + 
				'<span class="page-txt">/ ' + pageCount + '</span>';
		}
		else {
			var pageCountHTML = this.currentPage + ' / ' + pageCount;
		}
		
		this.pagesContainer.find('ul.lovers-nav li.count-adm').html(pageCountHTML);
	}
	else {
		this.pagesContainer.hide();
	}
}

backgroundHelper.prototype.appendItems = function(Object) {
	
	var itemCount = assosiativeCount(Object.items);
	
	this.getItems().remove();
	
	if (!Object.items || !itemCount) {
		return ;
	}
	
	var itemKeys = array_keys(Object.items);
	itemKeys.sort(function(a, b) {return b - a});
	
	for (key in itemKeys) { 
		
		if (!itemKeys.hasOwnProperty(key)) {
			continue ;
		}
	
		var item = Object.items[itemKeys[key]];
		this.appendItem(item);
	}	
}

backgroundHelper.prototype.appendItem = function(item) {
	
	var deleteButton = item.owner ? '<div class="delete" style="display: none"><span>delete</span></div>' : '';
	
	var format =
		'<li>' + 
			
			'<span class="hidden">' + item.id + '</span>' +
		
			deleteButton + 
			
			'<div class="thumb" style="background-image:url(#IMAGE#);">' + 
				'<span>background</span>' + 
			'</div>' + 
		'</li>';
	
	var html = format.replace('#IMAGE#', item.imageThumb);	
	this.listContainer.append(html); 
}

backgroundHelper.prototype.previewBgPosition = function(key) {
	
	var Object = bgPositonOptions[key];
	
	$('body').css({
		'backgroundPosition': Object.backgroundPosition.replace(/ 0/, ' ' + bgBodyOffsetY + 'px'),
		'backgroundRepeat': Object.backgroundRepeat
	})
}

backgroundHelper.prototype.applyBgPosition = function(key) {

	var self = this;	
	
	if (!self.busy) {
		
		self.flagBusy();
		
		$.ajax({
			
			type: 'POST',
			url: self.getUrl('bgimages'),
			data: '_action=ajaxBGPosition&position=' + key,
			
			success: function() {
			
				self.previewBgPosition(key);
				self.flagReady();
			}	
		})
	}	
}

backgroundHelper.prototype.previewBgAttachment = function(key) {
	
	var Object = bgAttachmentOptions[key];
	
	$('body').css({
		'backgroundAttachment': Object.backgroundAttachment
	})
}

backgroundHelper.prototype.applyBgAttachment = function(key) {
	
	var self = this;	
	
	if (!self.busy) {
		
		self.flagBusy();
		
		$.ajax({
			
			type: 'POST',
			url: self.getUrl('bgimages'),
			data: '_action=ajaxBGAttachcment&attachment=' + key,
			
			success: function() {
			
				self.previewBgAttachment(key);
				self.flagReady();
			}	
		})
	}	
}

backgroundHelper.prototype.resetBGPositionOptions = function() { 
	
	backgroundPanel.imageOptionsContainer.find('select').each(function() {
		 
		 var firstValue = $(this).find('option:first').val();
		 
		 $(this).val(firstValue);
		
		 $(this).find('option:not(:first)').removeAttr('selected');
		 $(this).find('option:first').attr('selected', 'selected');
		 
	 });
}
