var Codem = function() {};
Codem.Slideshow = function() {};
Codem.Slideshow.prototype = {
	init : function() {
		this.set_width();
	},
	
	set_width : function() {
		var w = 0;
		jQuery('#slideshow #slides  .slide').each(
			function() {
				w += jQuery(this).width();
			}
		);
		jQuery('#slideshow #slides').css('width',w  + 'px');
	},
	
	can_go_next : function(ss) {
		if(this.is_animating(ss)) {
			return false;
		}
		var w = ss.width();
		var pw = ss.parents('#slideshow').width();
		var l = ss.offset().left;
		if(pw > (w+l)) {
			return false;
		} else {
			return true;
		}
	},
	
	can_go_previous : function(ss) {
		if(this.is_animating(ss)) {
			return false;
		}
		var l = ss.offset().left;
		if(l > 0) {
			return false;
		} else {
			return true;
		}
	},
	
	get_slideset : function() {
		return jQuery('#slideshow #slides ul');
	},
	
	is_animating : function(set) {
		return set.hasClass('animating');
	},
	
	animating : function(set, is) {
		if(is) {
			set.addClass('animating');
		} else {
			set.removeClass('animating');
		}
	},
	
	handle : function() {
	
		var _self = this;
		
		jQuery('#slideshow').children('.slides-controller').fadeTo(200,0.4);
		jQuery('#slideshow').children('.slides-controller').mousedown(
			function() {
				jQuery(this).fadeTo(
					200, 1,
					function() {}
				);
			}
		);
		jQuery('#slideshow').children('.slides-controller').mouseleave(
			function() {
				jQuery(this).fadeTo(
					200, 0.4,
					function() {}
				);
			}
		);
		
		jQuery('#slideshow').children('#slides-controller-next').click(
			function() {
				var ss = _self.get_slideset();
				if(_self.can_go_next(ss)) {
					_self.animating(ss, true);
					ss.animate(
						{
							left : '-=300px'
						},
						{
							duration : 300,
							complete : function() {
								_self.animating(jQuery(this), false);
							}
						}
					);
				}
			}
		);
		
		jQuery('#slideshow').children('#slides-controller-prev').click(
			function() {
				var ss = _self.get_slideset();
				if(_self.can_go_previous(ss)) {
					_self.animating(ss, true);
					jQuery('#slideshow #slides ul').animate(
						{
							left : '+=300px'
						},
						{
							duration : 300,
							complete : function() {
								_self.animating(jQuery(this), false);
							}
						}
					);
				}
			}
		);
		
		jQuery('#slideshow .slide .content').mousedown(
			function() {
				jQuery(this).parent().siblings().children('.text').fadeOut(200);
				jQuery(this).siblings('.text').fadeIn(
					200,
					function() {
						jQuery(this).one(
							'click',
							function() {
								jQuery(this).fadeOut(200);
							}
						);
					}
				);
			}
		);
		
		jQuery('#slideshow .slide .content').mouseup(
			function() {
				jQuery(this).siblings('.text').fadeOut(200);
			}
		);
	}
};

jQuery(document).ready(
	function($) {
	
		var slideshow = new Codem.Slideshow;
		slideshow.init();
		slideshow.handle();
		
		jQuery('#GalleryList li a, #slideshow li a').fancybox({
			transitionIn : 'elastic',
			transitionOut : 'elastic',
			autoScale : true,
			autoDimensions : true,
			centerOnScroll : false,
			speedIn : 600,
			speedOut : 200,
			overlayShow : true,
			overlayOpacity : 0.8,
			overlayColor : '#fff',
			showNavArrows : true,
			cyclic : true,
			titleShow : true,
			titlePosition : 'over'
		});
	
		//form messages
		$('form .message').live(
			'click',
			function() {
				$(this).fadeOut('fast');
			}
		);
		$('form :input').live(
			'blur',
			function() {
				setTimeout(
					function(e) {
						$('form .message').fadeOut('fast');
					},
					5000
				);
			}
		);
	}
);
