/**********************************************************************************************

  CSS on Sails Framework
  Title: Soulflower Floral Design
  Author: Paweł Ludwiczak, XHTMLized (http://www.xhtmlized.com/)
  Date: March 2010

***********************************************************************************************/

var gallery = function(){
	
	var base;
	var thumbWrapper;
	var widthLI, widthTotal, widthWrapper, offset;
	var intervalID;
	
	var self = {
		
		init: function(selector){
			
			base = $(selector);
			thumbWrapper = base.find('ul');
			
			//count width
			widthLI = base.find('li:first').outerWidth(true);
			widthTotal = base.find('li').length * widthLI;
			widthWrapper = base.find('.gallery-thumb-wrapper').outerWidth(true);
			thumbWrapper.width(widthTotal);
			offset = Math.floor(widthWrapper/2) - widthLI;
			var i=0;
			base.find('li a').each(function(){
				$(this).data('left',widthLI*i);
				i++;
			});
			
			// handle thumb
			self.handleThumb();
			
			// handle thumb nav
			self.handleNav();
		},
		
		handleNav: function(){
			
			base.find('.gallery-prev, .gallery-next').fadeTo(300,0.6);
			
			base.find('.gallery-prev')
				.hover(
					function(){
						$(this).fadeTo(300,1);
						self.startScrollThumbs('right',3000);
					},
					function(){
						$(this).fadeTo(300,0.6);
						thumbWrapper.stop();
					}
				)
				.mousedown(function(){
						self.startScrollThumbs('right',500);
				})
				.click(function(){
					return false;
				});
				
			base.find('.gallery-next')
				.hover(
					function(){
						$(this).fadeTo(300,1);
						self.startScrollThumbs('left',3000);
					},
					function(){
						$(this).fadeTo(300,0.6);
						thumbWrapper.stop();
					}
				)
				.mousedown(function(){
						self.startScrollThumbs('left',500);
				})
				.click(function(){
					return false;
				});
				
		},
		
		startScrollThumbs: function(dir,duration){
			var toLeft = 0;
			if(dir=='right'){
				toLeft = 0;
			}else{
				toLeft = widthTotal - (widthWrapper - 35);
			}
			
			thumbWrapper.stop(true).animate({
				'left': '-'+toLeft+'px'
			},duration);
			
		},
		
		handleThumb: function(){
			base.find('li a').click(function(){
				
				// set current
				base.find('li').removeClass('current');
				$(this).parent().addClass('current');
				
				// animate thumbs position
				var thumbLeft = $(this).data('left');
				var toLeft = 0;
				if(thumbLeft > offset){
					toLeft = thumbLeft - offset;
				}
				if(widthTotal - toLeft < (widthWrapper - 35)){
					toLeft = widthTotal - (widthWrapper - 35);
				}
				thumbWrapper.stop(true).animate({
					'left': '-'+toLeft+'px'
				});
				
				base.find('.gallery-content img').addClass('old');
				
				var newTitle = $(this).find('img').attr('title');
				var newDesc = $(this).find('img').attr('longdesc');
				var newHeight =  $(this).find('img').attr('heightori');
				var newWidth =  $(this).find('img').attr('widthori');
				
				var img  = new Image();
				$(img)
					.load(function(){
						
						$(this).hide();
						base.find('.gallery-content-wrapper > p').slideUp();
						$('.gallery-content img').remove();
						$('.gallery-content').prepend(this);
						base.find('.gallery-content img.old').remove();
						
						$('.gallery-content-wrapper').animate({ 'width':newWidth },300,
							function(){
								$('.gallery-content').animate({ 'height':newHeight },200,
								function(){
									$(img).fadeIn(function(){
										base.find('.gallery-content-wrapper > p .image-title').html(newTitle);
										base.find('.gallery-content-wrapper > p .image-desc').html(newDesc);
										base.find('.gallery-content-wrapper > p').slideDown();
									});
								});
							});
					})
					.attr('src', $(this).attr('href'));
				
				return false;
			});
		}
		
	};
	return self;

}();

$(document).ready(function() {

	$(".single-category:last, .blog-left .post:last").css({background: "transparent"});
	$(".events-thumbs li:nth-child(3n)").css({marginRight: "0"});
  
	$('#nav li ul').each(function() {
		$(this).wrap('<div class="subnav kudos-item">');
		$(this).parents('li').addClass('has_subnav').find('> a').click(function(e) {
			if( $(this).attr('href').toLowerCase().indexOf('/about/') < 0) {
				e.preventDefault();
				return false;
			}
		});
	});

	$("#nav > ul > li").hover(function(){
		$(this).find(".subnav").show();
		$(this).addClass("hover");
	}, function() {
		$(this).find(".subnav").hide();
		$(this).removeClass("hover");
	});

	$('.photos-slider').innerfade({
		speed: 'slow',
		timeout: 3000,
		type: 'sequence',
		containerheight: '322px'
	});
	
	gallery.init('.gallery-wrapper');

});

if (typeof DD_belatedPNG != 'undefined') { 
  DD_belatedPNG.fix('.single-category, #header, #nav .subnav, #nav .subnav ul, #main, #footer, .intro, .quotes-list li');
}
