jQuery.noConflict();

(function($) { 
	$(function() {

		/**
		 *
		 * Defaultti transitio
		 *
		 */
		jQuery.easing.def = "easeOutQuart";

		/**
		 *
		 * Sivun mode on
		 *
		 */
		var	slide_nav			= $('body').hasClass('nav-slide') ? true : false,

		/**
		 *
		 * Onko thumbit näkyvissä
		 *
		 */
			thumbs_up 			= $('#bottom-image-slider').length == 0 ? false : true,
		
		/**
		 *
		 * Nykyinen ikkunan korkeus
		 * -1 on borderille!!!!
		 *
		 */
			wrapper_height 		= slide_nav ? $(window).height() - 20 : $(window).height() - $('#nav-bg').innerHeight() -1,
		
		/**
		 *
		 * Nykyinen ikkunan leveys ja korkeus
		 *
		 */
			win_width 			= $(window).width(),
			win_height 			= $(window).height(),

		/**
		 *
		 * Kuvien containeri
		 * Päivitetään samalla korkeus
		 *
		 */
			$img_cont 			= $('#big-images').css( { 'height': wrapper_height } ),
		
		/**
		 *
		 * Kaikki kuvat (div)
		 *
		 */
			$img_cont_childs 	= $img_cont.children( 'div' );
		
		/**
		 *
		 * Kuvaobjekti
		 *
		 */
			//imgs 				= {  },
		
		/**
		 *
		 * Ison sliderin nykyinen kuvaindexi
		 *
		 */
			current_image 		= 1,
		
		/**
		 *
		 * Viimeisen kuvan indexi
		 *
		 */
			last_image_num 		= 0,
		
		/**
		 *
		 * Ison sliderin transitionopeus
		 *
		 */
			transition_speed 	= 3500,

		/**
		 *
		 * Sliderin autoslidenopeus
		 *
		 */
			autoslide_speed 	= 8000,

		/**
		 *
		 * Autoslider
		 *
		 */
			autoslider 		 	= null,

		/**
		 *
		 * Autoslider stop
		 *
		 */
			autoslider_stopped 	= false,
		
		/**
		 *
		 * Ison sliderin kuvien leveyssumma
		 *
		 */
			all_img_width_sum 	= 0,
		
		/**
		 *
		 * Thumbkokoelma
		 *
		 */
			$small_images 		= $('#bottom-image-slider'),

		/**
		 *
		 * Thumbeissa buttonit?
		 *
		 */
			thumbs_with_buttons	= $small_images.hasClass('with-buttons'),

		/**
		 *
		 * Isojen kuvien korkeus on...
		 * Tää menee semmoseks kuraks että kukaan ei saa selvää.
		 *
		 */
			big_image_height 	= slide_nav ?
									// ^Jos nav on slide tilassa
									( thumbs_up
										? wrapper_height - $small_images.innerHeight()
										: wrapper_height )
									:
									// Jos nav ei ole slide tilassa
									( thumbs_up
										? wrapper_height - $('#bg-nav').innerHeight() - $small_images.innerHeight()
										: wrapper_height - $('#bg-nav').innerHeight()
								   	);
		
		/**
		 *
		 *	Image slider master singleton controller!!
		 *
		 *	@function slide_to_image
		 *	@function handle_prev_next
		 *
		 */
		var control = {
			
			/**
			 *
			 * function slide_to_image
			 * @param _id (int)
			 * Jos targetin id tiedossa, niin tällä voi skrollailla
			 *
			 */
			slide_to_image: function( _id )
			{

				// Autoslider clearataan tässä
				if ( thumbs_with_buttons )
				{
					window.clearInterval( autoslider );
					autoslider = setInterval( function(){ init_autoslider(); }, autoslide_speed );
				}

				_id = _id === 0 ? last_image_num : _id;
				_id = _id > last_image_num ? 1 : _id;

				var $_img 			= $('#big-image-'+_id);
					_left_pos 		= $_img.position().left;
				

				if ( _id === 1 )
				{
					_left_pos = 0;
				}
				else
				{
					_left_pos = ( win_width / 2 - _left_pos ) - $_img.width() / 2;
				}

				if ( _left_pos > 0 )
				{
					_left_pos = 0;
				}

				if ( thumbs_with_buttons )
				{
					$small_images.find('a').removeClass('current');
					$('#thumb-'+_id).addClass('current');
				}
						
				$img_cont.stop().animate(
					{
						left: _left_pos
					}, transition_speed);
				
				current_image = _id;
			}
			// End control
		}

		function init()
		{
			if ( thumbs_with_buttons )
			{
				$small_images.css( { marginLeft: '-'+ ($small_images.width()/2) + 'px' } );
				big_image_height = big_image_height + $small_images.innerHeight();

				
			}

			if ( !thumbs_with_buttons && $img_cont_childs.length < 2 )
			{
				$('.image-nav').hide();
			}
		}

		init();

		// Fulllscreen
		$('body, #wrapper').css( 'height', wrapper_height+'px' );

		// Kaikki kuvat containereihinsa
		$img_cont_childs.each( function( i, item  )
		{
			var _title = item.title;

			// Poistetaan title ettei mousehoverina näy
			item.title = '';

			//imgs[i] = { id: this.id };
			var img = $("<img />")
					.attr('src', _title)
	      			.load(function(a)
	      			{

      					scale_image( this, big_image_height );

         				// Replacetaan div img:llä
         				// Takaa sen, että kuvat tulee oikeassa
         				// järjestyksessä vaikka ladataankin asynkisti
         				$('#'+item.id).append( $(this).clone() ).animate( { 'width': $(this).width() } );
         				
	      				if ( i === 0 )
	      				{
		      				//$img_cont.css( 'left', (win_width / 2) - ( $(this).width() / 2 ) ).fadeIn('slow');
		      				$img_cont.fadeIn('slow');
		      				
		      				if ( thumbs_with_buttons )
		      					autoslider = setInterval( function(){ init_autoslider(); }, autoslide_speed );
		      			}

         				$('#big-images').trigger('imageLoaded', [item.id]);

         				last_image_num++;

         				all_img_width_sum += this.width;
	      			});
		});

		function init_autoslider()
		{
			if ( !autoslider_stopped )
			{
				control.slide_to_image( current_image+1 );
			}
		}

		/**
		 *
		 *	imageLoaded triggeri
		 *
		 */
		$('#big-images').bind('imageLoaded', function(e, id)
		{
			var $this = $('#'+id);
			var $img = $this.find('img');

			var _width = $img.width();
			var _height = $img.height();
			var _old_img = $this.find('.image-info-cont').show().eq(0).attr('ratio');

			var _old_img_width = 0;
			var _old_img_height = 0;

			if ( _old_img !== undefined )
			{
				_old_img = _old_img.split(';');
				_old_img_width = parseInt(_old_img[0]);
				_old_img_height = parseInt(_old_img[1]);

				var $_all_marks = $this.children('.image-info-cont');

				$_all_marks.each( function(i, item)
				{
					var _this_pos = $(item).position();
					var _this_posx = _this_pos.left;
					var _this_posy = _this_pos.top;

					$(item).css( { 'left': Math.round((_this_posx / _old_img_width) * _width), 'top': Math.round((_this_posy / _old_img_height) * _height) } );
				});	
			}
		});
		
		/**
		 *	Skaalaa kuvan oikean kokoiseksi
		 *
		 *	@param img object
		 *	@return img object
		 */
		function scale_image( _img, _wish_height )
		{
			var _ratio = _img.width / _img.height;
			
			if ( _wish_height !== undefined || _wish_height !== '' )
			{
				_img.height = _img.height;
				_img.width = _img.width;
				$(_img).css(
				{
					width: Math.round(_wish_height * _ratio),
					height: _wish_height
				});
			}
			else
			{
				_img.height = wrapper_height;
				_img.width = Math.round(wrapper_height * _ratio);
			}

			return _img;
		}

		$('#next').bind( 'click', function(e)
		{
			control.slide_to_image( current_image+1 );
			e.preventDefault();
		});

		$('#prev').bind( 'click', function(e)
		{
			control.slide_to_image( current_image-1 );
			e.preventDefault();
		});
		
		$('#bottom-image-slider').delegate( 'a', 'click', function(e)
		{
			control.slide_to_image( parseNumber(this.id) );
			e.preventDefault();
		});

		/*$('#big-images').delegate( 'div.img-cont', 'click', function(e)
		{
			var _id = parseInt( this.id.replace('big-image-', '') );

			control.slide_to_image( _id );

			e.preventDefault();
		});*/

		function parseNumber( _string )
		{
			return typeof(_string) == 'number' ? _string : parseInt(_string.match(/\d+/));
		}

		// Näytetään image navi jos hiiri tarpeeksi lähellä reunoja
		$(document).mousemove( function(e)
		{
			var left 		= e.pageX;
			var tolerance 	= 100;
			
			if ( left > win_width - tolerance )
			{
				$('#next').fadeIn();
			}
			else
			{
				if ( $('#next:visible') )
				{
					$('#next').fadeOut();
				}
			}

			if ( left < tolerance )
			{
				$('#prev').fadeIn();
			}
			else
			{
				if ( $('#prev:visible') )
				{
					$('#prev').fadeOut();
				}
			}
		});

		$(window).resize( function()
		{
			setTimeout( function()
			{
				if ( thumbs_up )
				{

					window.clearInterval( autoslider );
					autoslider = setInterval( function(){ init_autoslider(); }, autoslide_speed );

					win_width 			= $(window).width();
					wrapper_height 		= slide_nav ? $(window).height() - 20 : $(window).height() - $('#nav-bg').innerHeight() -1;
					big_image_height 	= slide_nav ?
											// ^Jos nav on slide tilassa
											( thumbs_up
												? wrapper_height - $small_images.innerHeight()
												: wrapper_height )
											:
											// Jos nav ei ole slide tilassa
											( thumbs_up
												? wrapper_height - $('#bg-nav').innerHeight() - $small_images.innerHeight()
												: wrapper_height - $('#bg-nav').innerHeight()
										   	);
					
					$img_cont.css( { 'height': wrapper_height } );
					$('body, #wrapper').css( 'height', wrapper_height+'px' );

					$img_cont.find('img').each( function(i, item)
					{
						var _img = item;
						var _ratio = _img.width / _img.height;
						var _wish_height = wrapper_height;
						
						if ( _wish_height !== undefined || _wish_height !== '' )
						{
							$(_img).css(
							{
								width: Math.round(_wish_height * _ratio),
								height: _wish_height
							}).parent().css('width', _img.width);
						}
					});

					var $_img 			= $('#big-image-'+current_image);
						_left_pos 		= $_img.position().left;
					
					if ( current_image === 1 )
					{
						_left_pos = 0;
					}
					else
					{
						_left_pos = ( win_width / 2 - _left_pos ) - $_img.width() / 2;
					}
					$img_cont.css(
					{
						left: _left_pos
					});
				}
			}, 200);
		});

	});
})(jQuery);
