
window.addEvent('domready', function(){
  leftImages.init();
  bannerBottom.init();
});

var leftImages = {
	// create the homepage left side image switcher
	init: function(){
		// variables
		this.imagePath = "/siteflow/images/";
		this.middle = $('middle');
		this.left = $('leftSide');
		this.center = $('main');
		this.right = $('rightSide');
		this.padding=36;
		
		// clears any contents out of the left side (should only be &nbsp;)
		this.left.empty();
		
		// gets the images and captions out of the leftImages div
		this.images = $('leftImages').getElements('img');
		this.objects = $('leftImages').getElements('object');
		if(this.images.length){
			this.captions = $('leftImages').getElements('p');
			this.links = $('leftImages').getElements('a');
		
			// used for knowing where we are in the image list
			this.imageIndex = 1;
			
			this.images[0].sizeX = this.images[0].getSize().x;
			this.images[0].sizeY = this.images[0].getSize().y;
			
			if(this.images[0].sizeX!=199){
				this.images[0].nHeight=this.images[0].sizeY*(this.images[0].sizeX/199);
			}else{
				this.images[0].nHeight=this.images[0].sizeY;
			}
			
			// creates the image and inserts it in the leftSide div
			//this.image = new Element('img', {'src': this.images[0].src, 'width': '199'}).injectInside(this.left);
			this.image = new Element('img', {'src': this.images[0].src}).injectInside(this.left);
		
			// if there is more than 1 left side image, create & run the selector
			this.imageSelector();
		
			// creates the caption and inserts it in the leftSide div
			this.caption = new Element('p').set('html', this.captions[0].innerHTML).injectInside(this.left);
		
			// If there is a Read More... Link
			this.readmore = $('readmore');
			if(this.readmore){
				if(this.readmore.href){
					this.image.onclick = this.followLink.bind(leftImages);
				}
			}
		}else if(this.objects.length){
			this.flash = this.objects[0];
			//this.flash.injectInside(this.left);
			/*this.flash.brs = this.flash.getElements('br');
			this.flash.brs.each(function(br){
				br.dispose();
			});*/
			this.left.grab(this.flash);
			/*
			if(this.flash.width>199){
				this.flash.nscale=(this.flash.width/199);
				this.flash.width=199;
				this.flash.height=this.flash.height/this.flash.nscale;
				this.flash.set('scale','default');
				this.flash.embed=this.flash.getElements('embed');
				this.flash.embed[0].width=199;
				this.flash.embed[0].height=this.flash.height;
				this.flash.embed[0].set('scale','default');
			}*/
			this.padding=96;
		}
		
		// this section makes sure that the borders go to bottom of middle section 
		this.center.height=this.getHeight(this.center);
		this.right.height=this.getHeight(this.right);
		this.left.height=this.getHeight(this.left);
		if(this.rBh = $('rightBuyOnline')){
			this.rBh.height = this.getHeight(this.rBh);
			this.right.height = this.right.height + this.rBh.Height;
		}else{
			this.rBh = {'height':0};
		}	
		//$('searchInput').value =this.left.height+' '+this.center.height+' '+this.right.height;
		
		if((this.center.height>this.right.height)&&(this.center.height>this.left.height)){
			this.setHeight(this.left,this.center.height);
			this.setHeight(this.right,this.center.height-this.rBh.height);
		}else if(this.right.height>this.left.height){
			this.setHeight(this.left,this.right.height);
		}else{
			this.setHeight(this.right,this.left.height-this.rBh.height);
		}
		
	},
	
	getHeight: function(element){
		element.height = parseInt(element.getSize().y);
		element.marginTop = parseInt(element.getStyle('margin-top'));
		element.marginBottom = parseInt(element.getStyle('margin-bottom'));
		return ((element.height+element.marginTop)+element.marginBottom);
	},
	
	setHeight: function(element,height){
		element.setStyle('height',height-element.marginTop-element.marginBottom+'px');
	},
	
	imageSelector: function(){
		// if there is more than one image, continue
		if(this.images.length>1){
			// if we haven't already created the image selector, do so.
			if(!this.imageSelect){
				this.ImageSelectorCreate();
			// looks like we are just trying to update arrows & image number	
			}else{
				this.imageSelectorUpdate();
			}
		}
	},
	
	ImageSelectorCreate: function(){
		// create main div
		this.imageSelect = new Element('div', {'id': 'photoSelector'}).injectInside(this.left);
		// this div is so contents can be centered
		this.imageSelectFrame = new Element('div', {'id':'photoSelectorFrame'}).injectInside(this.imageSelect);
		// divs for left image, center text & right image
		this.imageSelectLeft  = new Element('div', {'id':'photoSelectorArrow'}).injectInside(this.imageSelectFrame);
		this.imageSelectCenter = new Element('div', {'id':'photoSelectorCenter'}).injectInside(this.imageSelectFrame);
		this.imageSelectRight = new Element('div', {'id':'photoSelectorArrow'}).injectInside(this.imageSelectFrame);
		// set the proper contents for left, center & right
		this.imageSelectorUpdate();
	},
	
	imageSelectorUpdate: function(){
		// if it's not the first image, show darkened back arrow with link, else light arrow, no link
		if(this.imageIndex>1){
			this.imageSelectLeft.set('class','leftOn').onclick = this.clickPrev.bind(this);
		}else{
			this.imageSelectLeft.set('class','leftOff').onclick = null;
		}
		// set the text of the center div (image x of y)
		this.imageSelectCenter.set('html',' ' + this.imageIndex + ' of '+ this.images.length+' ');
		// if it's not the last image, show darkened next arrow with link, else light arrow, no link
		if(this.imageIndex<this.images.length){
			this.imageSelectRight.set('class','rightOn').onclick = this.clickNext.bind(this);
		}else{
			this.imageSelectRight.set('class','rightOff').onclick = null;
		}
		// get the width of the 3 elements, then set the width of frame, so it is centered.
		this.imageSelectFrame.width = this.imageSelectLeft.getSize().x + this.imageSelectCenter.getSize().x + this.imageSelectRight.getSize().x;
		this.imageSelectFrame.setStyle('width', this.imageSelectFrame.width+'px');
			
	},
	
	clickNext: function(){
		// if you click on the next button, change image and caption
		this.image.set('src', this.images[this.imageIndex].src, 'width', 199);
		this.caption.set('html', this.captions[this.imageIndex].innerHTML);
		this.imageIndex++;
		// update the image selector
		this.imageSelector();
	},
	
	clickPrev: function(){
		// if you click on the previous button, change image and caption
		this.imageIndex--;
		this.image.set('src', this.images[this.imageIndex-1].src, 'width', 199);
		this.caption.set('html', this.captions[this.imageIndex-1].innerHTML);
		// update the image selector
		this.imageSelector();
	},
	
	followLink: function(){
		// follow read more... link
		window.location = this.readmore.href;	
	}
};

var bannerBottom = {
	init: function(){
		this.divs = $('bannerBottom').getElements('div');
		this.divs.each(function(div){
			if(!div.hasClass('dealerLocatorBottom')){
				//div.flash=div.getElements('object')
				div.images=div.getElements('img');
				/*
				if(div.flash.length){
					div.set('align','center');
					div.brs = div.getElements('br');
					div.brs.each(function(br){
						br.dispose();
					});
					div.flash[0].maxW=170;
					div.flash[0].maxH=220;
					if((div.flash[0].width>div.flash[0].maxW)||(div.flash[0].height>div.flash[0].maxH)){
						div.flash[0].nscale=((div.flash[0].width/div.flash[0].maxW)>(div.flash[0].height/div.flash[0].maxH))?div.flash[0].width/div.flash[0].maxW:div.flash[0].height/div.flash[0].maxH;
						div.flash[0].set('width',div.flash[0].width/div.flash[0].nscale);
						div.flash[0].set('height',div.flash[0].height/div.flash[0].nscale);
						div.flash[0].set('scale','default');
						div.flash[0].embed=div.flash[0].getElements('embed');
						div.flash[0].embed[0].set('width',div.flash[0].width);
						div.flash[0].embed[0].set('height',div.flash[0].height);
						div.flash[0].embed[0].set('scale','default');
					}
				}*/
				if(div.images.length){
					div.images.each(function(image){
						image.maxW=170;
						image.maxH=113;
						if((image.width>image.maxW)||(image.height>image.maxH)){
							image.scale = ((image.width/image.maxW)>(image.height/image.maxH)) ? image.width/image.maxW : image.height/image.maxH ;
							image.set('width',image.width/image.scale);
							image.set('height',image.height/image.scale);
						}
					});
				}
			}
		});
	}
};

