function Atlas(type, container_div){
	this.rotationTime = 45000;
	this.type = type;
	this.object = new Array;
	this.id = 'atlas' + Math.floor(Math.random()*952368);
	this.ready = false;
	this.container = '';
	this.container_div = container_div;
	
	//constructor
	this.atlas_constructor = function(){
		if(this.type == 'top'){
			this.width = 728;
			this.height = 90;
		} else {
			this.width = 250;
			this.height = 250
		}
		
		this.get_data();
		this.display();
	};
	//end constructor
	
	this.get_adsense = function(hide){
		html = '<iframe src="/atlas/get_as/" width="728" height="90" frameborder="0" scrolling="no" style="padding: 0px;"></iframe>'
		
		if(hide == false)
			return $('<div></div>').append(html);
		else
			return $('<div></div>').addClass('hide').append(html);
	}
	
	this.get_data = function(){
		var a = this;
		$.getJSON('/atlas/get/' + this.type, function(data){ a.set_data(data); });
	};
	
	this.set_data = function(data){
		for(var i in data){
			this.object[i] = data[i]
		}
		this.ready = true;
	};
	
	this.alert_data = function(){ alert(this.object); };
	
	this.display = function(){
		if(this.ready == true){
			this.container = $('<div id="' + this.id + '"></div>');
			//add the hover cursor change
			this.container.css({'width':this.width, 'height':this.height, 'overflow':'hidden'}).hover(function(){ $(this).css('cursor', 'pointer'); },function(){ $(this).css('cursor', 'default'); })
			this.set_each_place();
			var a = this;
			if(this.container.children('div').length > 1)
				this.interval = setInterval(function(){ a.rotate(); }, a.rotationTime);
			this.container_div.append(this.container);
		} else {
			if(typeof(this.ready_timer) == 'undefined'){
				var a = this;
				this.ready_timer = setTimeout(function(){ a.display(); }, 3000);
			}
		}
	};
	
	this.set_each_place = function(){
		var a = this;
		$.each(this.object, function(ind){
			if(ind == 0){
				if(this.adsense == 1){
					theHTML = a.get_adsense(false);
				} else {
					if(this.image_id <= 0){
						theHTML = '<div class="text-center" onClick="atlas_click_thru(\'' + this.campaign_id + '\', \'' + this.banner_id + '\', \'' + this.business_profile_id + '\');">' + this.js + '</div>';
					} else {
						theHTML = '<div class="text-center" onClick="atlas_click_thru(\'' + this.campaign_id + '\', \'' + this.banner_id + '\', \'' + this.business_profile_id + '\');"><img style="margin: 0 auto;" src="/images/display/image/' + this.image_id + '/"></div>';
					}
				}
			} else {
				if(this.adsense == 1){
					theHTML = a.get_adsense(true);
				} else {
					if(this.image_id <= 0){
						theHTML = '<div class="hide text-center" onClick="atlas_click_thru(\'' + this.campaign_id + '\', \'' + this.banner_id + '\', \'' + this.business_profile_id + '\');">' + this.js + '</div>';
					} else {
						theHTML = '<div class="hide text-center" onClick="atlas_click_thru(\'' + this.campaign_id + '\', \'' + this.banner_id + '\', \'' + this.business_profile_id + '\');"><img style="margin: 0 auto;" src="/images/display/image/' + this.image_id + '/"></div>';
					}
				}
			}
			a.container.append(theHTML);
		});
	};
	
	this.rotate = function(){
		var stopper = false;
		this.container.children('div').each(function(){
			if(stopper == false){
				if(!$(this).hasClass('hide')){
					//this one is visible, let's set it invisible and the next one visible.
					stopper = true;
					div = $(this);
					
					if(div.next().length){
						next = div.next().removeClass('hide');
					} else {
						next = div.siblings(':first').removeClass('hide');
					}
					div.addClass('hide');
				}
			}
		});
	};

	this.atlas_constructor();
}
atlas_click_thru = function(campaign_id, banner_id, business_profile_id){
	url = '/atlas/go/' + campaign_id + '/' + banner_id + '/' + business_profile_id + '/';
	window.open(url);
};