var Sppc = {};

Sppc.Core = {
	extendSuper: function(target, Super, $Class, options){
		var __f;  
		var __super = new Super(options);  
		jQuery.extend(true, target, __super);  
		jQuery.extend(true, target, $Class);  
		jQuery.extend(true, target, options);  
		target.fnStore = []; 
		var fnI = 0;
		for(var f in __super){  
			if(jQuery.isFunction(__super[f])){
					__f = __super[f];
					target.fnStore[fnI] = __super[f];
					__super[f] = function(fnI){ 
						return function(){ 
							if(!target.__top){ 
								this.__top = {__target:target}; 
							}else{ 
								this.__top = target.__top 
							} 
							this.__top.__target.__super = this.__super; 
							var __r = target.fnStore[fnI].apply(this.__top.__target, arguments); 
							this.__top.__target.__super = __super; 
							return __r; 
						}; 
					}(fnI); 
				fnI++;
			} 
		}	
		target.__super = __super; 
	},
	redirectIfNeeded: function(response) {
		if (response.indexOf('<-------redirect------->') != -1) {
			location.reload(true);
			return true;
		}
		return false;
	},
	textMetrixWidth: function(el) {
		var Styles = Array('font-size','font-style', 'font-weight', 'font-family','line-height', 'letter-spacing', 'text-transform');
		if ($('#textmetrixdiv').length == 0) {
			var newDiv = document.createElement('div');
			$(newDiv).attr('id', 'textmetrixdiv').css({
				position: 'absolute',
				width: 'auto',
				visibility: 'hidden'
			});
			$(document.body).prepend(newDiv);	
		} else {
			newDiv = $('#textmetrixdiv'); 
		}
		
		for (var i=0; i < Styles.length; i++) $(newDiv).css(Styles[i], $(el).css(Styles[i]));
		$(newDiv).html($(el).html());
		return $(newDiv).width();
	},
	reloadPage: function() {
		if (location.href.indexOf('?') != -1) {
			location.href = site_url + 'landing/'
		} else {
			location.reload(true);
		}
	}
};

Sppc.Landing = {};

Sppc.Landing.Manager$Class = {
	_tabs: null,
	_boxes: null,
	_currentTab: null,
	_columnsCount: null,
	_searchKeyword: '',
	_currentTabForSettings: null,
	
	constructor: function() {
		jQuery.extend(true, this, Sppc.Landing.Manager$Class);
	},
	initialize: function(config) {
		this._columnsCount = config.columnsCount;
		
		this._searchKeyword = config.keyword;
		jQuery('#searchQueryInput').attr('value', this._searchKeyword);
		
		this._tabs = [];
		var i;
		var tabsCount = 0;

		for(i in config.tabs) {
			var tab = new Sppc.Landing.Tab(config.tabs[i]);
			this._tabs[config.tabs[i].position] = tab;
			if (tab.getId() == config.currentTab) {
				this._currentTab = tab;
			}
			tabsCount++;
		}
		/*
		if (tabsCount >= 5) {
			jQuery('#protoTab').hide();
		}
		*/
		this._boxes = [];
		for(i in config.boxes) {
			eval('var box = new '+config.boxes[i].boxClass+'(config.boxes['+i+']);');
			this._boxes[i] = box;
		}
		this._initTabSettingsForm();
		this._initSearchForm();
		this._renderBoxes();
		this._initilizeDragAndDrop();
		
		jQuery('#protoTab').click(function(){Sppc.Landing.Manager.toggleTabSettings('new'); return false;});
	},
	getBox: function(id) {
		var i;
		for(i in this._boxes) {
			if (this._boxes[i].getId() == id) {
				return this._boxes[i];
			}
		}
		return null;
	},
	removeBox: function(boxId) {
		var boxes = [];
		var i;
		for(i in this._boxes) {
			if (this._boxes[i].getId() != boxId) {
				boxes.push(this._boxes[i]);
			}
		}
		this._boxes = boxes;
	},
	getTab: function(id) {
		var i;
		for(i in this._tabs) {
			if (this._tabs[i].getId() == id) {
				return this._tabs[i];
			}
		}
		return null;
	},
	removeTab: function(tabId) {
		if (tabId != this.getCurrentTab().getId()) {
			jQuery('#tab_'+tabId).remove();
			var tabs = [];
			var i;
			//var tabsCount = 0;
			
			for(i in this._tabs) {
				if(this._tabs[i].getId() != tabId) {
					tabs.push(this._tabs[i]);
				}
				//tabsCount++;
			}
			this._tabs = tabs;
			
			/*tabsCount--;
			if (tabsCount < 5) {
				jQuery('#protoTab').show();
			} else {
				jQuery('#protoTab').hide();
			}
			*/
		} else {
			Sppc.Core.reloadPage();
		}
	},
	getCurrentTab: function() {
		return this._currentTab;
	},
	showTabSettings: function(tabId) {
		var tab = this.getTab(tabId);
		this._currentTabForSettings = tabId;
		
		jQuery('.zone2').removeClass('active');
		jQuery('#protoTab').removeClass('active');
		jQuery('.tab-btnSettings').removeClass('shown').attr('title', Sppc.LL.show_settings);
		jQuery('.tab-btnSettings', jQuery('#tab_'+tabId)).addClass('shown').attr('title', Sppc.LL.hide_settings);
		
		jQuery('#tabId').attr('value', 'new');
		jQuery('#tabTitle').attr('value', '');
		jQuery('.thumb').removeClass('selectedThumb');
		jQuery('.thumb input:radio').attr('checked', '');
		
		jQuery('#tabSettingsMsg').hide();
		
		if ((tabId == 'new') || (!tab)) {
			jQuery('#protoTab').addClass('active');
			jQuery('.thumb:first').addClass('selectedThumb');
			jQuery('.thumb:first input:radio').attr('checked', 'checked');
		} else {
			jQuery('#tab_'+tab.getId()).addClass('active');
			
			jQuery('#tabId').attr('value', tab.getId());
			jQuery('#tabTitle').attr('value', tab.getTitle());
			jQuery('#layout_'+tab.getLayout()).attr('checked', 'checked');
			jQuery('#layout_'+tab.getLayout()).parent().addClass('selectedThumb');
		}
		jQuery('#tabSettings').show();
	},
	hideTabSettings: function() {
		this._currentTabForSettings = null;
		jQuery('#tabSettings').hide();
		jQuery('.zone2').removeClass('active');
		jQuery('.tab-btnSettings').removeClass('shown').attr('title', Sppc.LL.show_settings);
		jQuery('#protoTab').removeClass('active');
		jQuery('#tab_'+Sppc.Landing.Manager.getCurrentTab().getId()).addClass('active');
	},
	toggleTabSettings: function(tabId) {
		if (this._currentTabForSettings == tabId) {
			this.hideTabSettings();
		} else {
			this.showTabSettings(tabId);
		}
	},
	getSearchKeywords: function() {
		return this._searchKeyword;
	},
	_initSearchForm: function() {
		jQuery('#searchForm').bind('submit', this, function(e){
			e.data._onSearchFormSubmmit();
			e.stopImmediatePropagation();
			return false;
		});
		this._searchKeyword = jQuery('#searchQueryInput').val();
	},
	_onSearchFormSubmmit: function() {
		var keywords = jQuery('#searchQueryInput').val();
		
		if (keywords != this._searchKeyword) {
			this._searchKeyword = keywords;
			
			var options = {
				path: '/',
				expires: 10
			};
			jQuery.cookie('landingKeyword', this._searchKeyword, options);
			
			if (this._searchKeyword == '') {
				jQuery('#searchPageLink').attr('href', site_url + 'home/enable_standart/');
			} else {
				jQuery('#searchPageLink').attr('href', site_url + 'home/enable_standart/?query='+encodeURIComponent(this._searchKeyword));
			}
			
			var i ;
			for (i in this._boxes) {
				if (this._boxes[i].isSearchBox()) {
					this._boxes[i].search();
				}
			}
		}
		return false;
	},
	_initTabSettingsForm: function(){
		 $('.thumb').click(function(e){
             $('.thumb').removeClass('selectedThumb');
             $(this).addClass('selectedThumb');
             $('input:radio').attr('checked', '');
             $('input:radio', $(this)).attr('checked', 'checked');
    	});
		 
		 jQuery('#tabSettingsSubmit').click(function(){Sppc.Landing.Manager._onTabSettingsFormSubmit(); return false;});
		 jQuery('#closeTabSettings').click(function(){Sppc.Landing.Manager.hideTabSettings(); return false;});
	},
	_onTabSettingsFormSubmit: function() {
		jQuery('#tabSettingsMsg').hide();
		
		var settings = {};
		settings.id = jQuery('#tabId').val();
		settings.title = jQuery('#tabTitle').val();
		settings.layout = jQuery('.thumb :radio:checked').attr('value');
		
		var url = site_url+'ajax/tabs/settings/';
		if (settings.id == 'new') {
			url = site_url+'ajax/tabs/add/';
		}
		jQuery.post(url, settings, function(response){
			if (response.status == 'ok') {
				var tab = Sppc.Landing.Manager.getTab(response.tab.id_tab);
				if (tab) {
					tab.setTitle(response.tab.title);
					tab.setLayout(response.tab.layout);
					jQuery('#tabSettingsMsg').html(Sppc.LL.tab_updated);
					if (tab.getId() == Sppc.Landing.Manager.getCurrentTab().getId()) {
						Sppc.Landing.Manager.hideTabSettings();
					} else {
						tab.setAsCurrent();
					}
				} else {
					tab = new Sppc.Landing.Tab(response.tab);
					tab.setAsCurrent();
					jQuery('#tabSettingsMsg').html(Sppc.LL.tab_created);
				}
			} else {
				jQuery('#tabSettingsMsg').html(response.error);
				jQuery('#tabSettingsMsg').show();
			}
			jQuery('#tabSettingsMsg').show();
		}, 'json');
	},
	_renderBoxes: function() {
		var i;
		for(i in this._boxes) {
			this._boxes[i].render();
		}
	},
	_initilizeDragAndDrop: function() {
		jQuery('#tabs').sortable({
			items: '.zone2',
			axis: 'x',
			//opacity: 0.5,
			forcePlaceholderSize: true,
			containment: $('#tabsWrapper'),
			//forceHelperSize: true,
			cursor: 'move',
			start: function(event, ui){
				var x = $(ui.helper.context)
				x.css('top','');
			},
			
			stop: function(event, ui) {
				jQuery('a', ui.item).unbind('click');
				jQuery('a', ui.item).one('click', function(e){
					e.stopImmediatePropagation();
					var tabId = jQuery(this).parent().parent().attr('id');
					//console.log(jQuery(this).parent());
					tab = Sppc.Landing.Manager.getTab(tabId.substr(4));
					
					//console.log(tab);
					jQuery(this).bind('click', tab, function(ev){ev.data.setAsCurrent(); return false;});
					//jQuery(this).click();
				});
				Sppc.Landing.Manager.saveTabOrder();
			}
		});
		
		jQuery('.zone, .zone2:not(.active)').sortable({
			revert: true,
			connectWith: '.zone, .zone2',

			distance: 10,
			cancel: 'a',
			opacity: 0.5,
			
			appendTo: 'body',
			items: '.boxWrapper',
			handle: '.box-title',
			
			forcePlaceholderSize: true,
			forceHelperSize: true,
			cursorAt: {top: -5, left:-5},
			
			cursor: 'crosshair',
			tolerance: 'pointer',
			placeholder: 'placeholder',
			stop: function(event, ui) {
				var parentNode = jQuery(ui.item.context.parentNode); 
				if (parentNode.hasClass('zone2')) {
					var boxId = jQuery(ui.item[0]).attr('id');
					var tabId = parentNode.attr('id');
					
					var box = Sppc.Landing.Manager.getBox(boxId.substr(4));
					var tab = Sppc.Landing.Manager.getTab(tabId.substr(4));
					
					if (box && tab) {
						box.moveToTab(tab);
					}
				} else {
					Sppc.Landing.Manager.saveBoxOrder();
					var boxId = jQuery(ui.item[0]).attr('id');
					var box = Sppc.Landing.Manager.getBox(boxId.substr(4));
					box._afterMove();
				}
				$(document.body).removeClass('showSortable');
			},
			start: function(event, ui){
				$(document.body).addClass('showSortable');
			}
		});
		jQuery('.box-title').disableSelection();
	},
	saveBoxOrder: function() {
		var boxOrder = [];
		
		for(i = 0; i < this._columnsCount; i++) {
			var boxes = jQuery('#zone'+i+' .boxWrapper');
			jQuery.each(boxes, function(j, obj){
				var id = obj.id;
				var boxId = id.substr(4);
				
				boxOrder.push({
					'id': boxId,
					'column': i,
					'position': j
				});
			});
		}
		jQuery.post(site_url + 'ajax/boxes/save_order/', {'order': jQuery.toJSON(boxOrder)});
	},
	saveTabOrder: function() {
		var tabOrder = [];
		var tabs = jQuery('#tabs .zone2');
		jQuery.each(tabs, function(j, obj){
			var id = obj.id;
			var tabId = id.substr(4);
			
			tabOrder.push({
				'id': tabId,
				'position': j
			});
		});
		jQuery.post(site_url + 'ajax/tabs/save_order/', {'order': jQuery.toJSON(tabOrder)});
	}
};
Sppc.Landing.Manager = new Sppc.Landing.Manager$Class.constructor();

Sppc.Landing.Tab$Class = {
	_id: null,
	_title: null,
	_position: null,
	_layout: null,
	
	constructor: function(data) {
		jQuery.extend(true, this, Sppc.Landing.Tab$Class);
		this._id = data.id_tab;
		this._title = data.title;
		this._position = data.position;
		this._layout = data.layout;
		
		jQuery('#tab_'+this._id+' a').attr('title', this._title);
		this._cutTitle();
		jQuery('#tab_'+this._id+' a').bind('click', this, function(e){e.data.setAsCurrent(); return false;});
		jQuery('#tab_'+this._id+' .tab-btnClose').bind('click', this, function(e){
			e.data.close();
			return false;
		});
		jQuery('#tab_'+this._id+' .tab-btnSettings').bind('click', this, function(e){
			Sppc.Landing.Manager.toggleTabSettings(e.data.getId());
		});
	},
	getId: function() {
		return this._id;
	},
	getTitle: function() {
		return this._title;
	},
	setTitle: function(title) {
		if (title != this._title) {
			this._title = title;
			jQuery('#tab_'+this._id+' a').html(this._title);
			jQuery('#tab_'+this._id+' a').attr('title', this._title);
			this._cutTitle();
		}
	},
	getPosition: function() {
		return this._position;
	},
	setPosition: function(position) {
		this._position = position;
	},
	getLayout: function() {
		return this._layout;
	},
	setLayout: function(layout) {
		if (this._layout != layout) {
			this._layout = layout;
			if (Sppc.Landing.Manager.getCurrentTab().getId() == this._id) {
				Sppc.Core.reloadPage();
			}
		}
	},
	setAsCurrent: function() {
		if (Sppc.Landing.Manager.getCurrentTab().getId() != this._id) {
			var options = {
				path: '/',
				expires: 10
			};
			jQuery.cookie('currentTab', this._id, options);
			if (Sppc.Landing.Manager.getSearchKeywords()) {
				jQuery.cookie('landingKeyword', Sppc.Landing.Manager.getSearchKeywords(), options);
			}
			Sppc.Core.reloadPage(); 
		}
	},
	close: function() {
		if (confirm(Sppc.LL.close_tab_question)) {
			jQuery.post(site_url+'ajax/tabs/close/', {'id': this._id}, function(response){
				if (response.status == 'ok') {
					Sppc.Landing.Manager.removeTab(response.id);
				}
			}, 'json');
		}
	},
	_cutTitle: function() {
		var tabTitleEl = jQuery('#tab_'+this._id+' a');
		if (tabTitleEl.length != 0) {
			if (Sppc.Core.textMetrixWidth(tabTitleEl) >= (tabTitleEl.width() - 12)) {
				while(Sppc.Core.textMetrixWidth(tabTitleEl) >= (tabTitleEl.width() - 12)) {
					tabTitleEl.text(tabTitleEl.text().substring(0, tabTitleEl.text().length - 1));
				}
				tabTitleEl.text(tabTitleEl.text() + '...');
			}
		}
	}
};
Sppc.Landing.Tab = Sppc.Landing.Tab$Class.constructor;

Sppc.Landing.AbstractBox$Class = {
	_id: null,
	_title: null,
	_column: null,
	_position: null,
	_state: null,
	_controller: null,
	_settingsState: 'minimized',
	_cntrlElements: [],
	_isSettingsLoaded: false,
	_isSearchBox: false,
	_menuTimer: null,
	
	constructor: function(data) {
		jQuery.extend(true, this, Sppc.Landing.AbstractBox$Class);
		this._id = data.id;
		this._title = data.title;
		this._column = data.column;
		this._position = data.position;
		this._state = data.state;
		this._controller = data.controller;
	},
	render: function() {
		var boxPlace = jQuery('#box_'+this._id);
		this._cntrlElements['place'] = boxPlace;
		
		var boxTitle = document.createElement('DIV');
		jQuery(boxTitle).addClass('box-title').html(this._title).attr('title', this._title);
		this._cntrlElements['title'] = jQuery(boxTitle);

		var boxTitleWrap = document.createElement('DIV');
		jQuery(boxTitleWrap).addClass('box-titlewrap').append(boxTitle);
		
		var boxContent = document.createElement('DIV');
		$(boxContent).addClass('box-contentWrapper');
		this._cntrlElements['content'] = jQuery(boxContent);
		
		/* box menu */
		var menuDiv = document.createElement('div');
		$(menuDiv).addClass('box-menu');
		jQuery(menuDiv).bind('mouseenter', this, function(e){
			if (e.data._menuTimer) {
				clearTimeout(e.data._menuTimer);
				e.data._menuTimer = null;
			}
		});
		this._cntrlElements['menu'] = $(menuDiv); 
		
		var menuSeparatorDiv = document.createElement('div');
		$(menuSeparatorDiv).addClass('box-menu-option-separator');
		
		var menuRefreshDiv = document.createElement('div');
		$(menuRefreshDiv).addClass('box-menu-option').hover(
			function(){
				$(this).addClass('box-menu-option-hover');
			}, 
			function(){
				$(this).removeClass('box-menu-option-hover');
			}
	    ).text(Sppc.LL.refresh_box);
	    $(menuRefreshDiv).bind('click', this ,function(e){
	    	e.data.toggleMenu();
	    	e.data.reloadContent();
	    })

		var menuOptionsDiv = document.createElement('div');
        $(menuOptionsDiv).addClass('box-menu-option').hover(
        	function(){
        		$(this).addClass('box-menu-option-hover');
        	}, 
        	function(){
        		$(this).removeClass('box-menu-option-hover');
        	}
        ).text(Sppc.LL.show_settings);
        $(menuOptionsDiv).bind('click', this, function(e){
	        e.data.settingsToggle();
        });
        this._cntrlElements['menuSettingsItem'] = $(menuOptionsDiv);

        $(menuDiv).append([menuOptionsDiv,menuSeparatorDiv,menuRefreshDiv])
		
		var box = document.createElement('DIV');
		jQuery(box).addClass('box').prepend(boxTitleWrap).append(boxContent);
		boxPlace.append(box);
			
		var boxControls = document.createElement('DIV');
		jQuery(boxControls).addClass('box-controls');
		jQuery(boxControls).bind('mouseleave', this, function(e){
			var thisObj = e.data;
			e.data._menuTimer = setTimeout(function(){
				thisObj.hideMenu();
			}, 500);
		});
		
		var btnClose = document.createElement('div');
		jQuery(btnClose).addClass('box-btnClose').attr('title', Sppc.LL.close);
		jQuery(btnClose).bind('click', {'box': this}, function(e){e.data.box.close(); return false;});
		this._cntrlElements['closeBtn'] = jQuery(btnClose);
		
		var btnState = document.createElement('div');
		if (this._state == 'maximized') {
			jQuery(btnState).addClass('box-btnMin').attr('title', Sppc.LL.minify_gadget);
		} else {
			jQuery(btnState).addClass('box-btnMax').attr('title', Sppc.LL.maxify_gadget);
		}
		jQuery(btnState).bind('click', {'box': this}, function(e){e.data.box.toggle(); return false;});
		this._cntrlElements['stateBtn'] = jQuery(btnState);
				
		var btnOptions = document.createElement('div');
		jQuery(btnOptions).attr('title', Sppc.LL.show_options).addClass('box-btnOptions');
		jQuery(btnOptions).bind('click', this, function(e){e.data.toggleMenu(); return false;});
		this._cntrlElements['optionsBtn'] = jQuery(btnOptions);
		
		jQuery(boxControls).append(btnClose).append(btnState).append(btnOptions).append(menuDiv);

		boxPlace.prepend(boxControls);
		
		this._cutTitle();
		this._loadContent(boxContent);
	},
	close: function() {
		if (confirm(Sppc.LL.close_box_question)) {
			Sppc.Landing.Manager.removeBox(this._id);
			jQuery('#box_'+this._id).remove();
			jQuery.post(site_url+'ajax/boxes/close/', {'id': this._id});
		}
	},
	toggle: function() {
		if (this._state == 'maximized') {
			this._state = 'minimized';
			this._cntrlElements['stateBtn']
			    .removeClass('box-btnMin')
			    .addClass('box-btnMax')
			    .attr('title', Sppc.LL.maxify_gadget);
		} else {
			this._state = 'maximized';
			this._cntrlElements['stateBtn']
			    .removeClass('box-btnMax')
			    .addClass('box-btnMin')
			    .attr('title', Sppc.LL.minify_gadget);
		}
		this._cntrlElements['content'].toggle();
		jQuery.post(site_url+'ajax/boxes/state/', {'id': this._id, 'state': this._state});
	},
	moveToTab: function(tab) {
		Sppc.Landing.Manager.removeBox(this._id);
		jQuery('#box_'+this._id).remove();
		jQuery.post(site_url+'ajax/boxes/move_to_tab/', {'boxId': this._id, 'tabId': tab.getId()});
	},
	settingsToggle: function() {
		if (this._settingsState == 'minimized') {
			this.toggleMenu();
			this._loadSettings();
			this._settingsState = 'maximized';
			if (this._state == 'minimized') {
				this.toggle();
			}
			this._cntrlElements['menuSettingsItem'].text(Sppc.LL.hide_settings)
			jQuery('#box_'+this._id+' .box-options').show();
		} else {
			if (this._cntrlElements['menu'].css('display') != 'none') {
				this.toggleMenu();
			}
			this._settingsState = 'minimized';
			this._cntrlElements['menuSettingsItem'].text(Sppc.LL.show_settings);
			jQuery('#box_'+this._id+' .box-options').hide();
		}
	},
	toggleMenu: function() {
		if (this._cntrlElements['menu'].css('display') == 'none') {
			jQuery('.box-menu').hide();
			this._cntrlElements['optionsBtn']
			    .removeClass('box-btnOptions')
			    .addClass('box-btnOptionsOpen')
			    .attr('title', Sppc.LL.hide_options);
		} else {
			this._cntrlElements['optionsBtn']
			    .removeClass('box-btnOptionsOpen')
			    .addClass('box-btnOptions')
			    .attr('title', Sppc.LL.show_options);
		}
		this._cntrlElements['menu'].toggle();
	},
	hideMenu: function() {
		this._cntrlElements['optionsBtn']
		    .removeClass('box-btnOptionsOpen')
		    .addClass('box-btnOptions')
		    .attr('title', Sppc.LL.show_options);
		this._cntrlElements['menu'].hide();
	},
	getId: function() {
		return this._id;
	},
	setTitle: function(title) {
		if (this._title != title) {
			this._title = title;
			this._cntrlElements['title'].html(this._title);
			this._cntrlElements['title'].attr('title', this._title);
			this._cutTitle();
		}
	},
	saveSettings: function() {
		
		var settingsForm = jQuery('#settingsForm_'+this._id);
		var settings = jQuery(':input', settingsForm).serializeArray();
		var data = {};
		var i;
		for(i in settings) {
			data[settings[i].name] = settings[i].value;
		}
		var thisObj = this;
		jQuery.post(site_url+this._controller+'save/'+this._id, data, function(response){
			if (response.redirect) {
				Sppc.Core.redirectIfNeeded(response.redirect);
				return;
			}
			thisObj._settingsSaved(response);
		}, 'json');
		return false;
	},
	reloadContent: function() {
		this._isSettingsLoaded = false;
		this._cntrlElements['menuSettingsItem'].text(Sppc.LL.show_settings)
		this._cntrlElements['content'].html('');
		this._settingsState = 'minimized';
		this._loadContent(this._cntrlElements['content']);
	},
	_loadContent: function(contentElement){
		var thisObj = this;
		jQuery(contentElement).load(site_url+this._controller+'content/'+this._id, function(response, status, request){
			if (Sppc.Core.redirectIfNeeded(response)) {
				thisObj._cntrlElements['content'].css('display', 'none');
			}
			thisObj._contentLoaded(response);
		});
	},
	_contentLoaded: function(response) {
		if (this._state == 'minimized') {
			this._cntrlElements['content'].hide();
		}
	},
	_loadSettings: function() {
		if (this._isSettingsLoaded == false) {
			var thisObj = this;			
			jQuery('#options_'+this._id).load(site_url+this._controller+'settings/'+this._id, function(response, status, request) {
				if (Sppc.Core.redirectIfNeeded(response)) {
					jQuery('#options_'+thisObj._id).css('display', 'none');
				}
				thisObj._settingsLoaded();
			});
		}
	},
	_settingsLoaded: function() {
		this._isSettingsLoaded = true;
		jQuery('#settingsForm_'+this._id).bind('submit', this, function(e){
			e.data.saveSettings();
			e.stopImmediatePropagation();
			return false;
		});
		jQuery('#boxSettingsCancelButton_'+this._id).bind('click', this, function(e){e.data.settingsToggle(); return false;});
	},
	_settingsSaved: function(response) {
		if (response.status == 'ok') {
			this.setTitle(jQuery("#settingsForm_"+this._id+" input[name='box_title']").attr('value'));
			this.reloadContent();
		} else {
			alert(response.error);
		}
	},
	_afterMove: function() {
		
	},
	_cutTitle: function() {
		var boxTitleEl = jQuery(this._cntrlElements['title']);
		
		if (Sppc.Core.textMetrixWidth(boxTitleEl) >= (boxTitleEl.width() - 60)) {
			while(Sppc.Core.textMetrixWidth(boxTitleEl) >= (boxTitleEl.width() - 60)) {
				boxTitleEl.text(boxTitleEl.text().substring(0, boxTitleEl.text().length - 1));
			}
			boxTitleEl.text(boxTitleEl.text() + '...');
		}
	},
	isSearchBox: function() {
		return this._isSearchBox;
	}
};
Sppc.Landing.AbstractBox = Sppc.Landing.AbstractBox$Class.constructor;
