
var ajaxblock = Class.create();

ajaxblock.prototype = {
	_link: null,
	_corp: null,
	_content: null,
	_opened: false,
	_loadingDiv: null,
	_arrowDiv: null,
	_is_ajax_enabled: true,
	
	initialize: function(block, options) {
	
		var ajaxBlock = $(block);
		
		this.options = Object.extend({			
			classNames : {
				_linkSelector: 'a.updatelink',
				_contentSelector: 'div.content',
				_corpSelector: 'div.wrap_block_corp',
				_arrowSelector: 'div.arrow', 
				_loadingDivSelector: 'div.ajaxblock_loading',
				_arrowOpenCssClass: 'vertical_arrow',
				_arrowCloseCssClass: 'horizontal_arrow'
			},			
			_is_ajax_enabled : true			
		}, options || {});		
		
		if (!ajaxBlock)
			throw (((typeof (block) == 'string') ? block + " doesn't exist!"
					: "block parameter is null"));
		
		this._corp = ajaxBlock.down(this.options.classNames._corpSelector);
		
		if (!this._corp)
			throw (this.options.classNames._corpSelector + " doesn't exist in ajaxblock!");
		
		this._link = ajaxBlock.down(this.options.classNames._linkSelector);
		
		if (!this._link)
			throw (this.options.classNames._linkSelector + " doesn't exist in ajaxblock!");		

		this._content = ajaxBlock.down(this.options.classNames._contentSelector);
		
		if (!this._content)
			throw (this.options.classNames._contentSelector + " doesn't exist in ajaxblock!");
		
		this._arrowDiv =ajaxBlock.down(this.options.classNames._arrowSelector);
		
		this._loadingDiv = ajaxBlock.down(this.options.classNames._loadingDivSelector);
		
		this.setOpened(!this._content.empty());	
		
		this._addEventListeners();
	},
	setOpened: function(boo){		
		this._opened = boo;
		this._opened ? this._corp.show() : this._corp.hide();
		this._updateArrowState();
	},
	isOpened: function(){
		return this._opened;
	},
	
	_addEventListeners: function() {		
		this._link.observe('click', this.toogle.bind(this));
	},	
	_updateArrowState: function(){
		
		if(this._arrowDiv!= null){
			this._arrowDiv.removeClassName(this.options.classNames._arrowCloseCssClass);
			this._arrowDiv.removeClassName(this.options.classNames._arrowOpenCssClass);
			
			this._opened ? this._arrowDiv.addClassName(this.options.classNames._arrowOpenCssClass):
				this._arrowDiv.addClassName(this.options.classNames._arrowCloseCssClass);
		}		
	},
	toogle: function(event){		
		event.stop();
		var context = this;		
		if(this.options._is_ajax_enabled)
		{
			new Ajax.Updater({ success: this._content },this._link.href,{
				method: 'get',
				evalScripts: true,
				parameters: { iecache: new Date().getTime() }, /* IE caching hack */
				onLoading: function(){
										if(context.isOpened()){
											context.setOpened(false);
										}
										context._showLoadingDiv();										
									 },				
				onComplete: function(){ 
										context._hideLoadingDiv(); 
										context.setOpened(!context._content.empty());										
										links =  context._content.select(Monhyip.Common.hyipImageLinkSelector);
										//init hyip images links
										Monhyip.Common.initHyipImageLinks(links)
									  }			
			});	
		}
		else
		{	
			this.setOpened(!this._opened);
		}	
	},  	
	_showLoadingDiv: function(){
		if(this._loadingDiv!=null){			
			this._loadingDiv.show();
		}
	},
	_hideLoadingDiv: function(){
		if(this._loadingDiv!=null){
			this._loadingDiv.hide();
		}
	}
}
