var cd = {
	init: function() {
	    // Creating custom :external selector
	    $.expr[':'].external = function(obj){
	        return !obj.href.match(/^mailto\:/)
	                && (obj.hostname != location.hostname);
	    };
	    $('a:external').attr('target', '_blank').addClass("external");
		
		switch($('body').attr("class")) {
			case 'HomePage': {}; break;
			case 'PortfolioPage': {
				cd.pagetypes.PortfolioPage.init();
			}; break;
		}
	}, //init
	pagetypes: {
		PortfolioPage: {
			init: function() {
				this.slideshow.init();
			}, //init
			slideshow: {
				_running: false,
				_timer: null,
				_current: 0,
				_autoscroll_speed: 4500,
				_fadespeed: 700,
				container: null,
				items: null,
				init: function() {
					var self = this;
					self.container = $('#photos');
					self.items = self.container.children();
					
					self.setup_nav();

					self.items.first().addClass('current');
					self.items.wrapAll("<div class=\"photos-inner\" />")
						.click(function() {
							var next = self._current+1;
							if(next >= self.items.length) next = 0;
							self.container.find('.photos-nav a:eq('+next+')').click();
							return false;
						});
					
					self.setup_autoscroll(2500);
				}, //init
				setup_nav: function() {
					var self = this;
					var nav = $('<div class="photos-nav" />');
					for(var i=0; i<this.items.length; i++) {
						$('<a href="#">'+(i+1)+'</a>')
							.click(function() {
								self.slide_to_index( $(this).index() );
	
								return false;
							}).appendTo(nav);
					}
					nav.find('a:first-child').addClass("current");
					nav.appendTo(this.container);
				}, //setup_nav
				slide_to_index: function(slide) {
					var self = this;
					if(!self._running) {
						self._running = true;
						self._current = slide;
						
						clearTimeout(self._timer);
						
						var pending = self.items.eq(slide);
						pending.addClass("pending");
						
						self.update_nav();
						
						self.items.filter('.current').animate({opacity:0}, self._fadespeed, function() {
							$(this).removeClass('current').attr("style", "");
							pending.removeClass('pending').addClass('current');
							self.setup_autoscroll();
							self._running = false;
						});
					}
				}, //slide_to_index
				setup_autoscroll: function(speed) {
					if(typeof speed == 'undefined') speed=this._autoscroll_speed;
					this._timer = setTimeout(this.next, speed);
				}, // setup_autoscroll
				next: function() {
					var self = cd.pagetypes.PortfolioPage.slideshow;
					var next = self._current+1;
					if(next >= self.items.length) next = 0;
					self.slide_to_index(next);
				}, //next
				update_nav: function() {
					this.container.find('.photos-nav a:eq('+this._current+')')
						.addClass('current').siblings().removeClass('current');
				}
			} //slideshow
		} //PortfolioPage
	} //pagetypes
};

(function($){
	
	cd.init();

})(jQuery);
