
var Highwood = Class.create();

Highwood.prototype = {

	initialize: function(){
		this.initPortfolioItems();
		this.initSlideshow();
		this.initNewsItems();
	},
	
	initSlideshow: function(){
		var port_images = $$('.portfolio_image');
		if(port_images && port_images.length > 0){
			var more_links = $$('.more_link');
			more_links.each(function(more_link){
				more_link.position = 0;
				more_link.onclick = function(){
					
					var last_underscore = this.id.lastIndexOf('_');
					// Grab num from link id
					var num = this.id.substring(last_underscore+1, this.id.length);
					
					// Find images array
					var images_array = eval("portfolio_images_"+num);
					// Target image
					var image = $('portfolio_image_'+num);
					// If we aren't all the way through the images, 
					this.position = (this.position < images_array.length-1) ? this.position+1 : 0;
					// Swap the src
					if(image && images_array[this.position]){
						image.src = images_array[this.position];
					}
					var large_images_array = eval("large_portfolio_images_"+num);
					
					var anchor = image.up(0);
					if(large_images_array[this.position]){
						anchor.href = large_images_array[this.position];
						anchor.style.cursor = 'pointer';
					}else{
						anchor.href = '#';
						anchor.style.cursor = 'default';
						//this.style.display = 'none';
					}
					// Don't follow the link
					return false;
				}
			});
		}
	},
	
	
	initPortfolioItems: function(){
		var item_links = $$('a.portfolio_link');
		item_links.each(function(link){
			link.onclick = function(){
				highwood.showPortfolioItem(this.id);
				return false;
			};
		});
		
		var close_links = $$('a.close_link');
		close_links.each(function(close_link){
			close_link.onclick = function(){
				highwood.hidePortfolioItem(this);
				return false;
			}
		});
		
		var large_image_links = $$('a.portfolio_full_link');
		large_image_links.each(function(image_link){
			if(image_link.href != '#'){
				image_link.onclick = function(){
					highwood.showFullImage(this.href);
					return false;
				}
			}else{
				image_link.onclick = function(){
					return false;
				}
			}
		});
		
	},
	
	showPortfolioItem: function(id){
		var item = $('item_'+id);
		google_analytics_ext.track_link($(id).innerHTML);
		new Effect.Appear(item);
	},
	hidePortfolioItem: function(link){
		portItem = link.up(0);
		
		qt = $('quicktime');
		if(qt){
			// TRY THE NICE WAY TO STOP QUICKTIME
			try {qt.Stop();}catch(e){}
			// FORCE QUICKTIME TO CLOSE BY REFRESHING
			var loc = window.location.href;
			window.location = loc;
		}else{
			new Effect.Fade(portItem);
		}
		

	},
	
	showFullImage: function(image){
		var full_image = $('full_image')
		if(full_image){
			full_image.src = '';
			full_image.src = image;
			var container = $('full_image_container');
			container.toggle();
			full_image.onclick = function(){container.toggle();full_image.src = '';return false;}
			var full_close = $('full_image_close');
			full_close.onclick = function(){container.toggle();full_image.src = '';return false;}
		}
	},
	
	initNewsItems: function(){
		var news_links = $$('.news_item_link');
		if(news_links && news_links.length > 0){
			news_links.each(function(link){
				link.onclick = function(){
					Highwood.prototype.hideAllNewsItems();
					Highwood.prototype.showNewsItem(link.rel);
					return false;
				}
			});
			
			news_close_links = $$('.news_item_close_link');
			news_close_links.each(function(link){
				link.onclick = function(){
					Highwood.prototype.hideAllNewsItems();
					return false;
				}
			});
		}
	},
	
	showNewsItem: function(id){
		var item = $(id);
		if(item){
			// Google Tracking
			var item_link = $(id+"_link");
			if(item_link){google_analytics_ext.track_link(item_link.innerHTML);}
			new Effect.Appear(item);
		}
	},
	
	hideAllNewsItems: function(){
		var news_items = $$('.news_item_content');
		news_items.each(function(item){
			item.style.display = "none";
		});
	}
	
}

var highwood;
Event.observe(window, "load", function(){highwood = new Highwood()});