/*
 * EstateVue House Directory
 * Ads Module JavaScript Library
 * Author: Sigit Noviandi
 * Copyright EstateVue 2011
 * 
 */
var ads;

var housedir_ads = new Class({
	available_pos : new Hash(),
	wrapper : null,					// overall ads wrapper
	height : null,					// height of the overal ads wrapper
	width : null,					// width of the overal ads wrapper
	containers : new Hash(),		// the ads container within a page
	ads : null,				// the ads data
	page : 'front',
	url : '',
	ads_container : new Hash(),
	
	initialize : function(page){
		this.url = site_url + 'search/';
		this.page = page;
		if(page == 'front'){
			this.wrapper = $('left-ads');
			this.height = $('search-wrapper').offsetHeight - $('search-form-wrapper').offsetHeight;
			this.wrapper.setStyle('height',this.height+'px');
		} else if(page == 'back_right'){
			this.wrapper = $('right-ads');
			this.height = $('detail-wrapper').offsetHeight - $('right-info').offsetHeight;
			this.wrapper.setStyle('height',this.height+'px');
		} else if(page == 'bottom_left'){
			this.wrapper = $('bottom-ads');
			this.width = $('contact-agent-wrapper').getElement('.left-col').offsetWidth;
			this.wrapper.setStyle('width',this.width+'px');
		}
		this.get_ads();
	},
	
	get_ads : function(){
		var param = {
			'page'		: this.page,
			'height'	: this.height,
			'width'		: this.width
		};
		var ads_search = new Request.JSON({
			url: this.url + 'ajax_get_ads', 
			onComplete: function(respons) {
				if(respons.total_ads > 0){
					this.wrapper.innerHTML = '';
					this.ads = respons.ads;
					this.build_ads(respons);
				} else {
					html = '';
					this.wrapper.innerHTML = html;
				}
			}.bind(this)
		}).post(param);
	},
	
	build_ads : function(data){
		$each(data.ads, function(a,i){
			var html = '';
			$each(a, function(ads){
				html += ads+'';
			},this);
			var cont = new Element('div',{
				'class' : 'ads_container_item',
				'html'	: html,
				'id'	: 'ads_container_item'+i
			});
			cont.inject(this.wrapper,'bottom');
			//this.bind_effect(cont, i);
			var slideshow = new jbSlide(cont);
		}.bind(this));
	},
	
	bind_effect : function(e, index){
		this.ads_container.set('ads_container_'+index,{'index':0,'d':index});
	}
});
