var PortfolioSlides = {
	el_base: null,
	el_body: null,
	height:0,
	
	el_balls: null,
	el_ballcounter: null,
	el_balltemplate: null,
	
	el_slides:null,
	el_slideLeft:null,
	el_slideRight:null,
	el_titleSlide:null,
	
	currentSet: null,
	selected: null,
	selectedIndex:0,
	numberOfSlides:0,
	
	slideWidth:676,
	contentWidth:0,
	contentLeft:0,
	
	options: {
		scrollDuration:350
	},
	
	init: function(options) {
		// Save element pointers
		PS.el_base = jQuery('#slides');
		PS.height = PS.el_base[0].offsetHeight;
		PS.el_body = PS.el_base.find('.body');

		PS.el_balls = PS.el_base.find('.balls');
		PS.el_balltemplate = PS.el_balls.find('img').removeClass('selected').remove();
		PS.el_ballcounter = PS.el_base.find('.ballcounter');
		
		PS.el_slides = PS.el_base.find('.slides');
		PS.el_slideLeft = PS.el_base.find('.arrow.left')
			.click(function() { PS.scrollSlides(-1); return false; })
			;
		PS.el_slideRight = PS.el_base.find('.arrow.right')
			.click(function() { PS.scrollSlides(1); return false; })
			;
		PS.el_titleSlide = PS.el_base.find('#titleSlide');


		// Save options
		jQuery.extend(PS.options, options);
	},
	
	filter: function(client, slidefilter, slideToSelect) {
		PS.hide(function() {
			PS.el_slides.find('.slide:visible:not(.title)').css('display', 'none');
			
			if (client) {
				var filter = '.slide[client='+client.id+']';
				if (slidefilter) filter += '[tags*='+slidefilter+']';
				
				PS.currentSet = PS.el_slides.find(filter).css('display', '');
				
				// If this client uses the title slide, generate it
				// CHANGE: 20 Oct 2008 by Briley
				// Normally, having a slidefilter disables the title slide.  An exception is
				// added for the "Web Applications" project, because it's special.
				if (client.titleImage  &&  (!slidefilter || slidefilter == 'webapp')) {
					// Set values in title slide
					PS.el_titleSlide
						.find('#tsImage')
							.css('background-image', 'url(/portfolio/images/titleSlides/' + client.titleImage + ')')
							.end()
						.find('#tsName').html(client.title).end()
						.find('#tsDomain')
							.attr('href', 'http://' + client.domain)
							.html(client.domain)
							.css('display', client.domain == '' ? 'none' : '')
							.end()
						.find('#tsSlides').empty().end()
						.find('#tsLaunch')
							.css('display', client.domain == '' ? 'none' : '')
								.find('a').attr('href', 'http://' + client.domain).end()
							.end()
						.css('display', '')
						;
					
					// Set fields
					var list = PS.el_titleSlide.find('#tsSlides');
					PS.currentSet.each(function(i) {
						jQuery('<li><a href="javascript:Portfolio.Slides.scrollToSlide('+(i+1)+')">'+this.spdata.title+'</a></li>')
							.appendTo(list)
							;
					});
					
					// Add to items
					PS.currentSet = PS.el_titleSlide.add(PS.currentSet);
				}
				else {
					PS.el_titleSlide.css('display', 'none');
				}
				
				// Set the position of each slide
				PS.currentSet.each(function(i) { this.setAttribute('position', i); });
				
				PS.numberOfSlides = PS.currentSet.size();
				PS.contentWidth = PS.slideWidth * PS.numberOfSlides;
				
				
				PS.setBalls(PS.numberOfSlides);
				
				var idx = 0;
				if (slideToSelect) {
					var temp = PS.currentSet.filter('.slide[name='+slideToSelect+']');
					if (temp.size() > 0) idx = temp.attr('position') * 1;
				}
				
				PS.setPos(idx);
				PS.scrollToSlide(idx);
				
				// Clear Shadowbox cache and initialize it for these slides
				var shadowBoxLinks = PS.currentSet.find('a[rel^=shadowbox]');
				if (shadowBoxLinks.size() > 0) {
					Shadowbox.clearCache();
					shadowBoxLinks.each(function() { this.shadowboxCacheKey = undefined; });
					Shadowbox.setup(shadowBoxLinks, { gallery: client.title });
				}
				
				PS.show();
			}
			else {
				PS.selectedIndex = -1;
			}
		});
	},
	
	load: function(slides) {
		var numSlides = 0;
		jQuery.each(slides, function(key, val) {
			// If client not defined, skip this slide
			if (!val.client) return;
			numSlides++;
			// get tags
			var tags = [];
			jQuery.each(val.tags, function() { tags.push(Portfolio.Tags.getById(this)); });
			tags = tags.join(' ');
			
			var obj = jQuery('<div class="slide"></div>')
				.attr('name', val.code || '')
				.attr('tags', tags)
				.attr('client', val.client.id)
				.append(val.html)
				.css('display', 'none')
				.appendTo(PS.el_slides)
			;
			obj[0].spdata = val;
		});
		PS.el_slides.css('width', numSlides * PS.slideWidth + 1000);
	},
	
	setBalls: function(num) {
		var count = PS.el_balls.find('.ball').size();
		if (count < num) {
			while (count < num) {
				PS.el_balltemplate.clone()
					.attr('slidenum', (count))
					.attr('title', 'Go to slide ' + (count+1))
					.click(PS.onClickBall)
					.appendTo(PS.el_balls)
					;
				count++;
			}
		}
		else {
			PS.el_balls.find('.ball:gt('+(num-1)+')').unbind('click').remove();
		}
	},
	
	hide: function(cb) {
		cb();
//		PS.el_base.animate({height:0, top:parseInt(PS.height/2)}, 250);
//		PS.el_base.animate({top:-225}, {duration:250, complete:cb});
	},
	show: function() {
//		PS.el_base.animate({top:0, height:PS.height}, 250);
//		PS.el_base.animate({top:0}, 250);
	},
	
	onShadowboxChange: function(obj) {
		// If the shadowbox moves to an item on a different slide,
		// move the slides to the same position so they aren't out of place
		// when the user closes the shadowbox
		var el = jQuery(obj.el);
		var slideNum = el.parents('.slide').attr('position');
		PS.scrollToSlide(slideNum * 1);
	},
	
	scrollToSlide: function(num) {
		PS.stopScroll();
		
		PS.el_balls.find('.ball').removeClass('selected');
		PS.selectedIndex = num;
		PS.el_balls.find('.ball:eq('+PS.selectedIndex+')').addClass('selected');
		PS.el_ballcounter.html('Slide ' + (num+1));
		
		if (num < 0) num = 0; else if (num >= PS.numberOfSlides) num = PS.numberOfSlides - 1;
		PS.contentLeft = num * PS.slideWidth * -1;
		PS.el_slides.animate({left:PS.contentLeft}, PS.options.scrollDuration);
		
		PS.checkArrows();
	},
	
	scrollSlides: function(amt) {
		var ns = PS.selectedIndex + amt;
		PS.scrollToSlide(ns);
	},
	
	setPos: function(pos) {
		PS.contentLeft = pos;
		PS.el_slides.css('left', pos);
	},
	
	stopScroll: function() {
		PS.el_slides.stop();
	},
	
	onClickBall: function() {
		PS.scrollToSlide(this.getAttribute('slidenum') * 1);
	},
	
	checkArrows: function() {
		if (PS.selectedIndex == 0) PS.hideArrow(PS.el_slideLeft);
		else PS.showArrow(PS.el_slideLeft);
		
		if (PS.selectedIndex == PS.numberOfSlides-1) PS.hideArrow(PS.el_slideRight);
		else PS.showArrow(PS.el_slideRight);
	},
	showArrow: function(arrow) { arrow.stop().css('display', '').animate({opacity:1}, 100); },
	hideArrow: function(arrow) { arrow.stop().animate({opacity:0}, {duration:100, complete:function() { this.style.display='none'; }}); }
	
};

var PS = PortfolioSlides;
Portfolio.Slides = PS;