var root = "";
//goodies
//http://www.gscottolson.com/weblog/2007/12/09/sum-array-prototype-for-javascript/
Array.prototype.sum = function() {
  return (! this.length) ? 0 : this.slice(1).sum() +
      ((typeof this[0] == 'number') ? this[0] : 0);
};
//http://www.martienus.com/code/javascript-remove-duplicates-from-array.html
Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

//arrays
var rows = [];
var projects = [];
var newsItems = [];
//site
var Site = {
	scrollPos: 0,
	state: 'home',
	init: function() {
		if($('.homePage').length > 0){
			Site.loadHomeXML();
			Site.Navigation.init();
		}
		if($('.newsPage').length > 0){
			Site.loadNewsXML();
			$('.nav li.projects a').attr('href','index.html');
			$('.nav li.exhibitions a').attr('href','index.html');
			$('.nav li.publications a').attr('href','index.html');
		}
		if($('.profilePage').length > 0){
			Site.loadProfileXML();
			$('.nav li.projects a').attr('href','index.html');
			$('.nav li.exhibitions a').attr('href','index.html');
			$('.nav li.publications a').attr('href','index.html');
		}
		
	},
	loadHomeXML: function(){
		$.ajax({
			type: "GET",
			url: "main.xml",
			dataType: "xml",
			complete: function(xml){
				//console.log('complete')
			},
			success: function(xml){
				//console.log('sucess');
				$(xml).find('Row').each(function(i){
					var id = 'row'+i;
					rows.push(new Row(this,id));
				});
				Site.MainScroller.init();
			},
			error: function(xml){
				alert('problem with xml');
			}
		});
	},
	loadNewsXML: function(){
		$.ajax({
			type: "GET",
			url: "main.xml",
			dataType: "xml",
			complete: function(xml){
				//console.log('complete')
			},
			success: function(xml){
				//console.log('sucess');
				$('.loader').append('<ul class="newsList"></ul>');
				$(xml).find('Row').each(function(i){
					if($(this).attr('name') === 'News'){
						$(this).find('project').each(function(j){
							var id = 'newsItem'+j;
							newsItems.push(new NewsItem(this,id));
						});
					}
				});
				Site.MainScroller.init();
			},
			error: function(xml){
				alert('problem with xml');
			}
		});
	},
	loadProfileXML: function(){
		$.ajax({
			type: "GET",
			url: "profile.xml",
			dataType: "xml",
			complete: function(xml){
				//console.log('complete')
			},
			success: function(xml){
				//console.log('sucess');
				var biomarkup;
				var firmmarkup;
				var contactmarkup;
				$(xml).find('bio').each(function(i){
					biomarkup = '<div class="bioTab profiletab clearfix">'+
					'<div class="txtColumn">'+$(this).text()+'</div>'+
					'<img src="images/'+$(this).attr('img')+'" class="Pic">'+
					'</div>';
				});
				$(xml).find('firm').each(function(i){
					firmmarkup = '<div class="firmTab profiletab clearfix">'+
					'<div class="txtColumn">'+$(this).text()+'</div>'+
					'<img src="images/'+$(this).attr('img')+'" class="pic">'+
					'</div>';
				});
				$(xml).find('contact').each(function(i){
					contactmarkup = '<div class="contactTab profiletab clearfix">'+
					'<div class="txtColumn">'+$(this).text()+'</div>'+
					'</div>';
				});
				var menu = '<ul class="profileMenu">'+
				'<li><a href="javascript:void(0);" class="firmBtn">FIRM</a></li>'+
				'<li><a href="javascript:void(0);" class="bioBtn">PRINCIPAL BIO</a></li>'+
				'<li><a href="javascript:void(0);" class="contactBtn">CONTACT</a></li>'+
				'</ul>';
				$('.loader').append(menu);
				$('.loader').append(biomarkup);
				$('.loader').append(firmmarkup);
				$('.loader').append(contactmarkup);
				$('.bioBtn').addClass('active');
				$('.bioTab').fadeIn();
				
				$('.firmBtn').click(function(){
					$('.profiletab').fadeOut();
					$('.firmTab').fadeIn();
					$('ul.profileMenu li a').removeClass('active');
					$(this).addClass('active');
				});
				$('.bioBtn').click(function(){
					$('.profiletab').fadeOut();
					$('.bioTab').fadeIn();
					$('ul.profileMenu li a').removeClass('active');
					$(this).addClass('active');
				});
				$('.contactBtn').click(function(){
					$('.profiletab').fadeOut();
					$('.contactTab').fadeIn();
					$('ul.profileMenu li a').removeClass('active');
					$(this).addClass('active');
				});
			},
			error: function(xml){
				alert('problem with xml');
			}
		});
	},
	MainScroller:{
		init: function(){
			//new loader height
			var loaderheight = $('.loader').height();
			$('.loader').height(loaderheight+40);
			//scroll
			var scrollContainer = $('.content');
			var scrollArea = $('.loader');
			var track = $('.mainScrollBar');
			var thumb = $('.mainScrollThumb');
			
			/*attach thumb pos to scrollarea pos*/
			function positionThumb(){
				var saHeight = $(scrollArea).height();
				var saTop = $(scrollArea).position().top;
				var saPercent = Math.abs(saTop / saHeight);
				var newTop = track.height() * saPercent;
				thumb.css({ 'top': newTop });
			}
			positionThumb();
			
			thumb.mouseover(function(){
				$(this).css('background-color','#a80000');
			}).mouseout(function(){
				$(this).css('background-color','#fff');
			});
			thumb.draggable({ 
				axis: 'y',
				containment: 'parent', 
				start: function() {
				},
				drag : function(){
					$(this).css('background-color','#a80000');
					var ThumboffSet = thumb.position();
					var trackHeight = track.parent().height() - thumb.height();
					var percentageTop = ThumboffSet.top / trackHeight;
					var newTop = ( scrollArea.height() - scrollArea.parent().height() ) * percentageTop;
					scrollArea.css({ 'top': -newTop });
					Site.scrollPos = -newTop;
				},
				stop: function(){
					$(this).css('background-color','#fff');
				}
			});
		}
	},
	Navigation:{
		init: function(){
			//$('.nav li.home a').mouseover(function(){}).mouseout(function(){}).click(function(){});
			//$('.nav li.profile a').mouseover(function(){}).mouseout(function(){}).click(function(){});
			
			//projects
			var projectsClicked = false;
			$('.nav li.projects a').mouseover(function(){
				Site.Navigation.over(projectsClicked,'project');
			}).mouseout(function(){
				Site.Navigation.off(projectsClicked,'project');
			}).click(function(){
				projectsClicked = true;
				Site.Navigation.click(projectsClicked,'project');
				if(Site.state === 'home'){
					Site.Navigation.projectList();
				}
			});
			
			//exhibitions
			var exhibitionsClicked = false;
			$('.nav li.exhibitions a').mouseover(function(){
				Site.Navigation.over(exhibitionsClicked,'exhibition');
			}).mouseout(function(){
				Site.Navigation.off(exhibitionsClicked,'exhibition');
			}).click(function(){
				exhibitionsClicked = true;
				Site.Navigation.click(exhibitionsClicked,'exhibition');
			});
			
			//publications
			var publicationsClicked = false;
			$('.nav li.publications a').mouseover(function(){
				Site.Navigation.over(publicationsClicked,'publication');
			}).mouseout(function(){
				Site.Navigation.off(publicationsClicked,'publication');
			}).click(function(){
				publicationsClicked = true;
				Site.Navigation.click(publicationsClicked,'publication');
				if(Site.state === 'home'){
					Site.Navigation.publicationList();
				}
			});
			
			//$('.nav li.news a').mouseover(function(){}).mouseout(function(){}).click(function(){});
		},
		offsets: [],
		over: function(clicked,type){
			//1. fade out others to .5
			//2. start others if stopped
			//3. self show if hidden by other click (1)
			//4. autoscroll
			if(clicked === false){
				$('li.box').each(function(){
					if(!$(this).hasClass(type)){
						if(!$(this).hasClass('news')){
							$(this).animate({opacity: 0.1},500);
						}
						$('li.box').children('p.name').css('z-index','0');
						for(var i = 0; i<projects.length; i++){
							if(projects[i].type != 'news'){
								if(projects[i].type != type){
									if(projects[i].rotator.paused === true){
										projects[i].rotator.play();
									}
								}
							}
						}
					}else{
						/*determine if showing*/
						var offset = $(this).offset();
						var offsetTop = offset.top;
						Site.Navigation.offsets.push(offsetTop);
					}
				});
				function checkIfShowing(){
					var showing = false;
					for(var i = 0; i<Site.Navigation.offsets.length; i++){
						if(Site.Navigation.offsets[i]<$('.content').height()){
							showing = true;
							Site.Navigation.offsets.length = 0;
						}
						
					}
					return showing;
				}
				if(Site.state === 'home'){
					if(checkIfShowing() === false){
						var last = Site.Navigation.offsets[Site.Navigation.offsets.length-1];
						var newTop = (last + $('li.box').height())- $('.content').height();
						$('.loader').animate({
							top : -newTop
						},500);
					}
				}
			}
		},
		off: function(clicked, type){
			//1. if not hidden in click (1) all fadeback to normal
			
			if(clicked === false){
				$('li.box').each(function(){
					$(this).animate({opacity: 1},100);
				});
				$('.loader').animate({
					top : Site.scrollPos
				},500);
			}
		},
		click: function(clicked, type){
			//1. fade out others completely
			//2. start others if stopped
			//3. stop self and bring title to top
			for(var i = 0; i<projects.length; i++){
				//console.log(projects[i].type);
				if(projects[i].type === type){
					projects[i].rotator.pause();
				}
			}
			$('li.box').each(function(){
				if($(this).hasClass(type)){
					$(this).children('p.name').css('z-index','1000');
				}
				if(!$(this).hasClass(type)){
					if(!$(this).hasClass('news')){
						$(this).animate({opacity: 0},500);
					}
				}
			});
		},
		projectList: function(){
			if($('.publicationList').length > 0){
				$('.publicationList').remove();
			}
			var architecture = [];
			var urban = [];
			var productDesign = [];
			for(var i = 0; i<projects.length; i++){
				if(projects[i].type === 'project'){
					if(projects[i].category === 'ARCHITECTURE'){
						//'+projects[i].clickHandler();+'
						var markup = '<li><a href="javascript:void(0);" id="'+projects[i].parent.id+projects[i].id+'">'+projects[i].name+'</a><li>'
						architecture.push(markup);
					}
					if(projects[i].category === 'URBAN'){
						var markup = '<li><a href="javascript:void(0);" id="'+projects[i].parent.id+projects[i].id+'">'+projects[i].name+'</a><li>'
						urban.push(markup);
					}
					if(projects[i].category === 'PRODUCT DESIGN'){
						var markup = '<li><a href="javascript:void(0);" id="'+projects[i].parent.id+projects[i].id+'">'+projects[i].name+'</a><li>'
						productDesign.push(markup);
					}
					
				}
			}
			var markup = '<div class="projectList">'+
			'<p>ARCHITECTURE</p><ul>'+architecture.join('')+'</ul>'+
			'<p>URBAN</p><ul>'+urban.join('')+'</ul>'+
			'<p>PRODUCT DESIGN</p><ul>'+productDesign.join('')+'</ul>'+
			'</div>';
			$('.loader').append(markup);
			
			$('.projectList ul li a').mouseover(function(){
				var id = $(this).attr('id')
				$('li.box').each(function(){
					if($(this).attr('id') === id){
						$(this).animate({opacity: 1},1);
					}else{
						if($(this).hasClass('project')){
							if(!$(this).hasClass('news')){
								$(this).animate({opacity: 0.1},100);
							}
						}
					}
				});
			}).click(function(){
				for(var i = 0; i<projects.length; i++){
					var id = projects[i].parent.id + projects[i].id;
					if(id === $(this).attr('id')){
						projects[i].loadPage();
					}
				}
			});
		},
		publicationList: function(){
			if($('.projectList').length > 0){
				$('.projectList').remove();
			}
			var press = [];
			var books = [];
			var magazines = [];
			for(var i = 0; i<projects.length; i++){
				if(projects[i].type === 'publication'){
					if(projects[i].category === 'PRESS'){
						//'+projects[i].clickHandler();+'
						var markup = '<li><a href="javascript:void(0);" id="'+projects[i].parent.id+projects[i].id+'">'+projects[i].name+'</a><li>'
						press.push(markup);
					}
					if(projects[i].category === 'BOOK'){
						var markup = '<li><a href="javascript:void(0);" id="'+projects[i].parent.id+projects[i].id+'">'+projects[i].name+'</a><li>'
						books.push(markup);
					}
					if(projects[i].category === 'MAGAZINE'){
						var markup = '<li><a href="javascript:void(0);" id="'+projects[i].parent.id+projects[i].id+'">'+projects[i].name+'</a><li>'
						magazines.push(markup);
					}
					
				}
			}
			var markup = '<div class="publicationList">'+
			'<p>PRESS</p><ul>'+press.join('')+'</ul>'+
			'<p>BOOKS</p><ul>'+books.join('')+'</ul>'+
			'<p>MAGAZINES</p><ul>'+magazines.join('')+'</ul>'+
			'</div>';
			$('.loader').append(markup);
			
			$('.publicationList ul li a').mouseover(function(){
				var id = $(this).attr('id')
				$('li.box').each(function(){
					if($(this).attr('id') === id){
						$(this).animate({opacity: 1},1);
					}else{
						if($(this).hasClass('publication')){
							if(!$(this).hasClass('news')){
								$(this).animate({opacity: 0.1},100);
							}
						}
					}
				});
			}).click(function(){
				for(var i = 0; i<projects.length; i++){
					var id = projects[i].parent.id + projects[i].id;
					if(id === $(this).attr('id')){
						projects[i].loadPage();
					}
				}
			});
		}
	}
}
$(Site.init);

//ROW
var Row = Class.extend({
	init: function(thisObj,id){
		this.self = thisObj;
		this.name = $(thisObj).attr('name');
		this.id = id;
		this.markup = '<div class="row '+this.name+' clearfix" id="'+this.id+'"><ul class="clearfix"></ul></div>';
		
		//functions
		$('.loader').append(this.markup);
		this.createBoxes();
		this.scrollBar();
	},
	createBoxes: function(){
		var self = this;
		$(this.self).find('project').each(function(j){
			var id = 'project'+j;
			projects.push(new Project(this,self,id));
		});
	},
	scrollBar: function(){
		var self = this;
		var numofboxes = $('#'+this.id+' ul li').length;
		if(numofboxes > 5){
			var width = (numofboxes*119);
			$('#'+this.id+' ul').css({width:width});
			var scrollmarkup = '<div class="horizontalScrollBar"><div class="horizontalScrollThumb"></div></div>'
			$('#'+this.id).append(scrollmarkup);
			scroll();
		}
		function scroll(){
			var scrollContainer = $('#'+self.id);
			var scrollArea = $('#'+self.id+' ul');
			var track = $('#'+self.id+' .horizontalScrollBar');
			var thumb = $('#'+self.id+' .horizontalScrollThumb');
			thumb.mouseover(function(){
				$(this).css('background-color','#a80000');
			}).mouseout(function(){
				$(this).css('background-color','#fff');
			});
			thumb.draggable({ 
				axis: 'x',
				containment: 'parent', 
				start: function() {
				},
				drag : function(){
					$(this).css('background-color','#a80000');
					var ThumboffSet = thumb.position();
					var trackWidth = track.parent().width() - thumb.width();
					var percentageLeft = ThumboffSet.left / trackWidth;
					var newLeft = ( scrollArea.width() - scrollArea.parent().width() ) * percentageLeft;
					scrollArea.css({ 'left': -newLeft });
				},
				stop: function(){
					$(this).css('background-color','#fff');
				}
			});
		}
	}
});

//NEWS ITEM
var NewsItem = Class.extend({
	init: function(thisObj,id){
		this.self = thisObj;
		this.name = $(thisObj).attr('name');
		this.id = id;
		var text;
		$(thisObj).find('img').each(function(k){
			text = $(this).text();
		});
		this.text = text;
		this.markup = '<li class="newsItem clearfix" id="'+this.id+'"><p class="name"><a name="'+this.name+'">'+this.name+'</a></p>'+this.text+'</li>';
		$('ul.newsList').append(this.markup);
		var self = this;
		var hashname = '#'+this.name;
		if(window.location.hash === hashname){
			$('li.newsItem a').each(function(){
				if($(this).attr('name') === self.name){
					$('.loader').css('top',(-$(this).position().top+30))
				}
			});
		}
	}
});
