/**
 * Created: 27 Aug 2007 by Briley Hooper
 * Updated: 04 Mar 2008 by Briley Hooper
 */
 
var Portfolio = {
	xml: null,
	domReady: false,
	
	el_title: null,
	currentSlideFilter: null,
	
	options: {
		state:false,
		autoLoad:true,
		defaultCategory:'recent',
		initialPath: 'recent'
	},
	
	path: {
		category: 'all',
		client: null,
		slide: null
	},
	
	/**
	 * Initializes the Portfolio
	 */
	init: function(options) {
		// Listen for the DOM to become ready
		jQuery(Portfolio.onDomReady);
		
		// Save options
		jQuery.extend(Portfolio.options, options);
		
		// Parse path
		var path = Portfolio.options.initialPath.split('.');
		if (path.length > 0) Portfolio.path.category = path[0];
		if (path.length > 1) Portfolio.path.client = path[1];
		if (path.length > 2) Portfolio.path.slide = path[2];
		
		// If data is already given, don't request it
		if (Portfolio.options.data) {
			Portfolio.loadData(Portfolio.options.data);
		}
		// Otherwise request data from server
		else {
			// Send a request for the XML
			var url = "portfolio.xml.php";
			if (Portfolio.options.state) url += '?state='+Portfolio.options.state;
			jQuery.ajax({
				cache: false,
				dataType: 'xml',
				success: Portfolio.onGetXml,
				url: url
			});
		}
	},
	
	/**
	 * Loads the prepared XML data into the document
	 */
	load: function() {
		// Load clients into clientlist
		Portfolio.Clients.load(Portfolio.data.clients);
		
		// Setup the sidemenu
		Portfolio.Sidemenu.load(Portfolio.data.categories);
		
		// Setup the slides
		Portfolio.Slides.load(Portfolio.data.slides);
		
		// If autoLoad is enabled, show the category
		if (Portfolio.options.autoLoad) {
			Portfolio.showCategory(Portfolio.path.category);
		}
	},
	
	/**
	 * Checks to see if both the XML has been loaded and the DOM is
	 * ready to be modified.  If yes to both, then actually loads
	 * the document.
	 */
	check: function() {
		if (Portfolio.data != null  &&  Portfolio.domReady)
			Portfolio.load();
	},
	/**
	 * Receives and formats the XML response from the server
	 * with the Portfolio data
	 */
	onGetXml: function(data) {
		// Save the XML
		var data = {categories:[], clients:[], slides:[]};
		var xml = jQuery(data);
		
		// Find and save categories
		xml.find('category').each(function() {
			var catObj = {
				id:				this.getAttribute('id'),
				title:			this.getAttribute('name'),
				type:			this.getAttribute('type'),
				slidefilter:	this.getAttribute('slidefilter')
			};
			data.categories.push(catObj);
		});

		// Find and save clients
		xml.find('site').each(function() {
			var clientObj = {
				id:			this.getAttribute('id'),
				domain:		this.getAttribute('domain'),
				name:		this.getAttribute('name'),
				categories:	this.getAttribute('categories'),
				image:		this.getAttribute('image'),
				thumbnail:	this.getAttribute('thumbnail')
			};
			data.clients.push(clientObj);
			
			// Find and save all of this clients' slides
			jQuery(this).find('slide').each(function() {
				var slideObj = {
					id:			this.getAttribute('id'),
					title:		this.getAttribute('title'),
					tags:		this.getAttribute('tags'),
					html:		jQuery.trim(unescape(jQuery(this).text())),
					clientId:	this.getAttribute('clientId')
				};
				data.slides.push(slideObj);
			});
		});
		
		// Check to see if the DOM is ready yet
		Portfolio.loadData(data);
	},
	
	/**
	 * Loads the data for the portfolio and moves on to next step
	 */
	loadData: function(d) {
		Portfolio.data = {categories:{}, clients:{}, slides:{}};
		var clientMap = {};
		
		// Load clients
		jQuery.each(d.clients, function() {
			Portfolio.data.clients[this.id] = this;
		});
		
		// Load categories
		jQuery.each(d.categories, function() {
			for (var i = 0;  i < this.clients.length; i++) {
				this.clients[i] = Portfolio.data.clients[this.clients[i]].code;
			}
			Portfolio.data.categories[this.id] = this;
		});
		// Load slides
		jQuery.each(d.slides, function() {
			this.client = Portfolio.data.clients[this.clientId];
			Portfolio.data.slides[this.clientId + '.' + this.id] = this;
		});
		// Load tags
		Portfolio.Tags.init(d.tags);
		Portfolio.check();
	},
	
	/**
	 * Called when the DOM is ready to be modified
	 */
	onDomReady: function() {
		Portfolio.el_title = jQuery('#portfolioTitle');
		
		// Setup sidemenu
		Portfolio.Sidemenu.init({onChange:Portfolio.showCategory});
		
		// Setup client list 
		Portfolio.Clients.init({
			onScroll:	Portfolio.onClientListScroll,
			onReset:	Portfolio.onClientListReset,
			onChange:	Portfolio.onClientListChange
		});
		
		// Setup client list scrollbar
		Portfolio.Scrollbar.init({onScroll:Portfolio.onScrollbarScroll});
		
		// Setup slides
		Portfolio.Slides.init();
		
		Portfolio.domReady = true;
		Portfolio.check();
	},
	
	/**
	 * Called when an item on the sidebar is clicked
	 * @param category The category object to load
	 */
	showCategory: function(category) {
		if (typeof category == 'string') { category = Portfolio.Sidemenu.get(category) || Portfolio.Sidemenu.get(Portfolio.options.defaultCategory); }
//		var filter = (category.slidefilter&&category.slidefilter!='') ? category.slidefilter.split(' ') : [];
		
		Portfolio.currentSlideFilter = category.slidefilter;
		
		// Write the name of the category to the title <div>
		Portfolio.el_title.html(category.title);
		
		// Update the list of clients
		Portfolio.Clients.filter(category.clients, Portfolio.path.client);
		if (Portfolio.path.client) Portfolio.path.client = null;
	},
	
	/**
	 * Called when the scrollbar is scrolled
	 */
	onScrollbarScroll: function(pct) {
		// Set the client list to the same position as the scrollbar
		Portfolio.Clients.setPos(pct);
	},
	
	/**
	 * Called when the Client List is scrolled
	 */
	onClientListScroll: function(pct) {
		// Move the scrollbar to the correct position
		Portfolio.Scrollbar.scrollTo(pct);
	},
	
	/**
	 * Called when the client list is reset
	 */
	onClientListReset: function(scrolls) {
		Portfolio.Scrollbar.setEnabled(scrolls);
	},
	
	/**
	 * Called when a client is selected in the client list
	 */
	onClientListChange: function(client) {
		// Load slides for this client
		var cid = client || null;
		Portfolio.Slides.filter(cid, Portfolio.currentSlideFilter, Portfolio.path.slide);
		if (Portfolio.path.slide  &&  client) Portfolio.path.slide = null;
	},
	
	/**
	 * Scrolls both the client list and the scrollbar to a certain percentage
	 */
	scrollClientsTo: function(pct) {
		Portfolio.Clients.scrollTo(pct);
		Portfolio.Scrollbar.scrollTo(pct);
	}
};







// In case this browser doesn't support Array.indexOf
[].indexOf || (Array.prototype.indexOf = function(v){
       for(var i = this.length; i-- && this[i] !== v;);
       return i;
});
 
