 /*!
	Tor Sluzewiec
	10/14/2010
*/	
// This file should be compressed before deploying on the production server.
var Sluzewiec = {
	/*
		Initializer
	*/
	init: function()
	{
		this.galleries();
		this.galListHover();
		this.zoomPic();
		this.days();
		this.splash();
		this.tabs();
		this.searchForm();
	},
	
	/*
		scrollable galleries (right/left arrow, 3 items)
	*/
	galleries: function()
	{
		$('.gallery .nav a').click(function(){
			return false;
		});

        function big_click(ev){
            ev.preventDefault();
            $('.fancy_nav a[href='+$(this).attr('href')+']').trigger('click');
        };
		$('.gallery .content .items-wrapper').each(function(){

			var $galleryWrapper = $(this).parents('.gallery');
        
			// support big galleries
			if( $galleryWrapper.hasClass('big-gallery') )
			{
                
                $galleryWrapper.find('.big .pic a').unbind().click(big_click);
                
                $('.fancy_nav a').fancybox();
				$(this).find('.page a').click(function(ev){
					ev.preventDefault();
					// set description
					$galleryWrapper.find('.big .desc').text($(this).attr('title'));
					
					// set author if available
					// otherwise - hide container
					var author = $(this).data('author'); 
					if ( typeof(author) !== 'undefined' )
					{
						$galleryWrapper.find('.big .author').show().find('span').text(author);
					} 
					else
					{
						$galleryWrapper.find('.big .author').hide();
					}
					
					
					if( $galleryWrapper.hasClass('g-video') )
					{
						// @TODO put a video
					}
					else{
						// put an image
						
						$galleryWrapper.find('.big .pic').replaceWith('<div class="pic zoompic"><a href="'+$(this).attr('href')+'" title="'+$(this).attr('title')+'"><img src="'+$(this).attr('thumbnail')+'" /></a></div>');
                        
						$galleryWrapper.find('.big .pic a').unbind().click(big_click);
                        
					} 
					return false;
				});
				
			} // end big


			$(this).scrollable({
				circular: true,
				prev: '.nav .prev a',
				next: '.nav .next a'
			});
			
			var api = $(this).data('scrollable');
			
			$galleryWrapper.
				find('.nav .next a').
					bind('click', function(){
						api.next();
						return false;
					}).
				end().
				find('.nav .prev a').
					bind('click', function(){
						api.prev();
						return false;
					});
		});
	},
	
	/*
		Show picture counter on gallery list
		(on hover)
	*/
	galListHover: function()
	{
		$('.gal-list').each(function(){
			$(this).find('li a, .first a').hover(function(){
				$(this).find('.counter').fadeIn(200);
			}, function(){
				$(this).find('.counter').fadeOut(500);
			});
		});
	},
	
	/*
		Box with one picture and a label "zoom the picture"
	*/
	zoomPic: function()
	{
		$('.zoompic').each(function(){
			$(this).click(function(){
				
				$(this).find('a').fancybox();
				
				return false;
			});
		});
	},
	
	/*
		scrollable activities by days list
		scroll only one day per click
	*/
	days: function()
	{
	    var isScrolling = false;
		var $days = $('div.days:eq(0)'),
			$toAnimate = $days.find('.days-list');

                var start_from = $toAnimate.attr('start');
                var size_container = $days.find('.a-day').length;

                if (isNaN(parseInt(start_from)))
                {
                    start_from = 0;
                }
                else{
                    if (start_from > size_container || start_from < 1)
                    {
                        start_from = 0;
                    }
                    else {

                        if (start_from == size_container)
                        {
                            start_from = start_from - 1;
                        }
                        start_from = -1 * start_from + 1;
                    }
                }

		var daysSetup = {
			current: -1*start_from,
			size: size_container,
			width: $days.find('.a-day:first').outerWidth(true),
			currentLeft: start_from*$days.find('.a-day:first').outerWidth(true)
		};

                $toAnimate.animate({left: (daysSetup.currentLeft)}, 300);
		
		$days.
			find('.nav .prev a').
				click(function(){
					// previous day
					if( isScrolling === false ) {
					    if(daysSetup.current - 2 < -1)
    					{
    						// can't move, the end
    						return false;
    					}

     					daysSetup.currentLeft = parseInt($toAnimate.css('left'));
                        
                        isScrolling = true;
    					$toAnimate.animate({
    						    left: (daysSetup.currentLeft + daysSetup.width)
    					    }, {
    					        duration: 300,
    					        complete: function() {
        					        isScrolling = false;
        					    }
        					});
					
    					daysSetup.current-=1;
    				}
    				return false;
				}).
			end().
			find('.nav .next a').
				click(function(){
					// next day
					if( isScrolling === false ) {
					    
					    if(daysSetup.current + 2 >= daysSetup.size)
    					{
    						// can't move, the end
    						return false;
    					}
					
     					daysSetup.currentLeft = parseInt($toAnimate.css('left'));
     					
     					isScrolling = true;
    					$toAnimate.animate({
    						    left: (daysSetup.currentLeft - daysSetup.width)
    					    }, {
    					        duration: 300,
    					        complete: function() {
        					        isScrolling = false;
        					    }
        					});
					
    					daysSetup.current+=1;
    				}
					return false;
				});
	},
	
	/*
		autorotate splash pictures
	*/
	splash: function()
	{
		var $rotator = $('#rotator');
		
		$rotator.find('.nav a').click(function(){ return false;});
		
		$rotator.scrollable({
			circular: true
		}).navigator({
			navi: '.pages'
		}).autoscroll({
			autoplay: true,
			interval: 9000		/* 2,5s */
		});
		
		var api = $rotator.data('scrollable');
		
		// ensure no animation while mouse is over the splash screen
		$rotator.bind('hover', function(){
			api.stop();
		}, function(){
			api.play();
		});
	},
	
	/*
		tabs for business & events
	*/
	tabs: function()
	{
		var $tabs = $('#tabs');
		
		$tabs.find('.nav a').click(function(){
			if($(this).parents('li').hasClass('current'))
			{
				// current link is clicked
				return false;
			}
			
			
			$(this).parents('ul').find('li').removeClass('current');
			$(this).parents('li').addClass('current');
			
			var target = $(this).attr('href').replace('#', '');
			
			// hida all tabs, then show the one with id specified in link
			$tabs.find('.a-tab').hide();
			$tabs.find('#'+target+'.a-tab').show();
			
			return false;
		});
	},
	
	/*
		copy label to the input and erase it's value on focus
	*/
	searchForm: function()
	{
		var handle = function(element, text){
			if(element.val().length === 0){
				element.val(element.data('defaultText'));
			}
			
			element.bind('focus', function(){
				if($(this).val() === text){
					$(this).val('');
				}
			}).bind('blur', function(){
				if($(this).val() === ''){
					$(this).val(text);
				}
			});
		};
		
		// easily extandable - just change the selector
		// one condition - label must exist immediately before selected input
		$('#search-keyword').each(function(){
			var text = $(this).prev('label').text();
			$(this).data('defaultText', text);
			handle($(this), text);

			return false;
		});
	}
};


/* Ready? Steady? Gooooo! */
$(function() {
	Sluzewiec.init();
}); // EOF

