var Ads = Class.create();
	
	Ads.prototype = {

		ad_count: 20,
		ad_width: 256,
		min_scroll: 300,
		max_scroll: 903,
		curr_left: 1,
		
		expanded: -1,
		noclick: false,
		
		initialize: function()
		{
			document.write('<!--[if lt IE 7.]><script defer type="text/javascript" src="./js/src/pngfix.js"></script><![endif]-->');
			$('prev').setOpacity(0.2);
		},
		
		getCount: function()
	    {   // Returns number of pages in this section
			return Ads.ad_count;
	    },

		setCount: function(ads_count)
		{
			Ads.ad_count = ads_count;
		},
	    
	    getContentWidth: function()
	    {   // Returns absolute width of page content
	    	
	    	return ((Ads.ad_width)*Ads.ad_count);
	    },

		toggle: function(id)
		{
			if(Ads.noclick == true) return;
			if(Ads.expanded == -1)
			{
				Ads.open(id);
				Ads.expanded = id;
			}
			else
			{
				Ads.close(Ads.expanded);
				Ads.expanded = -1;
			}
			
			//console.log('ad: '+Ads.expanded);
		},
		
		
		open: function(id)
		{
			if(Ads.expanded != -1)
				Ads.close(Ads.expanded);
				
			if(Ads.noclick == true)
				return;
				
			left = Ads.getViewport();
			right = id%3;
			
			if(right == 2)
			{	
				new Effect.Move('ads',{ x: -345, y: 0, mode: 'relative', duration: .7, 
				afterFinish: function() { 
					Ads.expanded = id;
				}});
			}
			
			detail 	= 'ad_' + id + '_detail';
			image 	= 'ad_' + id + '_image';
			
			new Effect.Morph('ad_'+id, {
			  style: {
			    width: ((Ads.ad_width * 2.5)+45)+'px'
			  },
			  duration: 0.6,
			  beforeStart: function() { 
				  Ads.noclick = true; 
				  
				  var images = $('ads').getElementsByClassName('ad_image');
				  
				  for(i=0; i < images.length; i++)
				  {
				  	if(images[i].id != $(image).id) {
				  	new Effect.Opacity(images[i].id, { from: 1.0, to: 0.2, duration: 0.5 });
				  	}
				  }
				  
				  $('next').setOpacity(0.2);
				  $('prev').setOpacity(0.2);
			  },
			  afterFinish: function () { 
			  	new Effect.Appear(detail); 
			  	Ads.noclick = false; 
			  	Ads.expanded = id;
			  }
			});
			
		},
		
		close: function(id)
		{
			details = 'ad_' + id + '_detail';
			
			if(Ads.noclick == true)
				return;
			
			new Effect.Fade(details, {
				duration: 0.2,
				beforeStart: function() { 
				Ads.noclick = true;
				left = Ads.getViewport();
				right = id%3;
				
				if(right == 2)
				{	//it's been moved because it was in pos 3
					//console.log('i need to move back! '+left);
					new Effect.Move('ads',{ x: 345, y: 0, mode: 'relative', duration: .7 });
				}
				},
				afterFinish: function() { 
					new Effect.Morph('ad_'+id, {
					  style: {
					    width: Ads.ad_width+'px'
					  },
					  duration: 0.6,
					  beforeStart: function() { 
						  Ads.noclick = true;
					  },
					  afterFinish: function () { 
					  	  var images = $('ads').getElementsByClassName('ad_image');
			  
						  for(i=0; i < images.length; i++)
						  {
						  	if(images[i].id != $(image).id) {
						  	new Effect.Opacity(images[i].id, { from: 0.2, to: 1.0, duration: 0.5 });
						  	}
						  }
						  
						  Ads.noclick = false;
						  //console.log('ad: '+ Ads.expanded);
						  //$('next').setOpacity(1.0);
				  		  //$('prev').setOpacity(1.0);
				  		  Ads.evalScrolling();
					  }
					});
			
					Ads.noclick = false; 
					Ads.expanded = -1;
				}
			});
		},
		
		setViewport: function(left)
		{
			Ads.curr_left = left;
		},
		
		getViewport: function()
		{
			return Ads.curr_left;
		},
		
		evalScrolling: function()
		{
			left = Ads.getViewport();
			
			if(left+3 > Ads.ad_count)
			{
				$('next').setOpacity(0.2);
			} else {
				$('next').setOpacity(1.0);
			}			
			if(left <= 1)
			{
				$('prev').setOpacity(0.2);
			} else {
				$('prev').setOpacity(1.0);
			}
		},
		
		next: function()
		{
			left = Ads.getViewport();
			
			var scroll = -903;

			if(left+2 >= Ads.ad_count)
				return;
			
			if(Ads.noclick == true)
				return;
				
			if(Ads.expanded != -1)
				return;
			
			new Effect.Move('ads',{ x: scroll, y: 0, mode: 'relative', duration: .7, 
				beforeStart: function() {
					Ads.noclick = true;
					if(Ads.expanded != -1)
						Ads.close(Ads.expanded);
				},
				afterFinish: function() { 
					Ads.setViewport(left+3); 
					Ads.evalScrolling();
					Ads.noclick = false;
				}});
				
		},
		
		prev: function()
		{
			left = Ads.getViewport();
			
			if(Ads.noclick == true)
				return;
				
			if(Ads.expanded != -1)
				return;
				
			var scroll = 903;
			
			if(left-1 == 0)
				return;
				
			new Effect.Move('ads',{ x: scroll, y: 0, mode: 'relative', duration: .7, 
				beforeStart: function() {
					Ads.noclick = true;
					if(Ads.expanded != -1)
						Ads.close(Ads.expanded);
				},
				afterFinish: function() { 
					Ads.setViewport(left-3); 
					Ads.evalScrolling();
					Ads.noclick = false;
				}});
				
		}
		
	};	
	

