jQuery.noConflict();

/**
 * Kaikenlaissii työkaluloita
 */
var Tools = {
			
	parseNumber : function( _string )
	{
		return typeof(_string) == 'number' ? _string : parseInt(_string.match(/\d+/));
	},

	setHash : function( _id )
	{
		window.location.hash = _id;
	}
};

(function($) { 
	$(function() {

		$().framerate();

		var sliders_string = '';

		$('.slider').each( function(i, item)
		{
			$(this).attr('id', 'slider-'+(i+1));
			if ( i == $('.slider').length-1 )
			{
				sliders_string += '#slider-'+(i+1);
			}
			else
			{
				sliders_string += '#slider-'+(i+1)+',';
			}
		});

		/**
		 *	Footer
		 */
		var $footer 			= $('#footer'),

		/**
		 *	All pages
		 */
			$all_pages			= $('#all-pages'),

		/**
		 *	#header stuff
		 */
			$header				= $('#header'),
			header_height		= $header.height() + Tools.parseNumber( $header.css('marginTop') ),

		/**
		 *	Doc height & width
		 */
			doc_height			= $(document).height(),
			doc_width			= $(document).width(),

		/**
		 *	Window height & width
		 */
			win_height			= $(window).height(),
			win_width			= $(window).width(),

		/**
		 *	All .page-wrappers
		 */
			$page_wrappers 		= $('.page-wrapper'),

		/**
		 *	Page transition speed
		 */
			transition_speed 	= 1000;

		// Kun Cufon valmis
		Cufon.CSS.ready(function()
		{
			$('html').removeClass('overlay-hidden');
			$('#overlay-loading').fadeOut('slow', function()
			{
				$(this).remove();
			});
		});

		/**
		 *	Image slider master singleton controller!!
		 *
		 *	@function slide_to_image
		 *	@function handle_prev_next
		 *
		 */
		var control = {
			
			/**
			 * function slide_to_page
			 * @param opts (obj)
			 */
			slide_to_page: function( opts )
			{
				// Jos ollaan jo klikkaillulla sivulla -> return false
				if ( $('#nav li.current a').attr('href') == opts.id ) return false;

				var _prev_page = $('#nav li.current a').attr('href'),
					_prev_index = $('#nav li.current').index();

				$('#nav li.current').removeClass('current').parent().find('li a[href="'+opts.id+'"]').parent().addClass('current');

				var _next_index = $('#nav li.current').index();

				var $next_page 			= $(opts.id),
					_left_pos 			= $next_page.position().left,
					next_page_height 	= $next_page.height(),
				
					add_param			= opts.add_param ? opts.add_param : null ;
									
				//$footer.slideUp();
				
				if ( add_param )
				{
					$('html, body').stop().animate( { scrollTop: $(opts.id+' '+opts.add_param).position().top }, 500 );
				}

				var $_page_content = $next_page.find('.page-content');
				
				if ( _next_index < _prev_index )
				{
					$_page_content.css( { marginLeft: win_width / 2 - ( $_page_content.width() / 2 ) - 200, opacity: 0 } );
				}
				else
				{
					$_page_content.css( { marginLeft: win_width / 2 - ( $_page_content.width() / 2 ) + 200, opacity: 0 } );
				}
					
				$all_pages.stop().animate(
					{
						left: '-'+_left_pos,
						height: next_page_height
					}, transition_speed, function()
					{
						$(_prev_page).find('.page-content').css({ opacity: 0 });

						// Refreshataan navi
						// Spesifinen refreshaus ei jostakin syystä toimi -> refreshataan kaikki
						Cufon.replace('#nav li a', { fontFamily: 'Titillium-Reg' });

						//this.style.removeAttribute('filter');
					}
				);
				
				// Slaidataan .page-content paikalleen
				var $_page_content = $next_page.find('.page-content');		
					$_page_content.css('filter', '');		
					$_page_content.delay(500).animate( { marginLeft: win_width / 2 - ( $_page_content.width() / 2 ), opacity: 1, filter: '' }, 1000, function(){ /*this.style.removeAttribute('filter');*/ } );

				var footer_start_pos 	= next_page_height+header_height+$footer.innerHeight();
				var footer_end_pos 		= next_page_height+header_height;

				if ( (next_page_height+header_height) <= (win_height - $footer.innerHeight() ) )
				{
					footer_start_pos 	= win_height + $footer.innerHeight();
					footer_end_pos 		= win_height - $footer.innerHeight();
				}

				$footer.css(
				{
					top: footer_start_pos
				}).show().stop().animate( { top: footer_end_pos }, transition_speed );
			}
		}

		// Initoidaan systeemit
		init();

		/**
		 * init method
		 */
		function init()
		{
			var _wrapper_count = $page_wrappers.length;

			var _height = ($page_wrappers.eq(0).height()+50)+'px';

			if ( $page_wrappers.eq(0).height() < $(window).height() )
			{
				_height = $(window).height() - 156;
				//_height = $(window).height();

			}

			$all_pages.css( { 'height': _height } );

			$page_wrappers.each( function(i, item)
			{
				$(item).css( { 'width': win_width+'px' } );

				/*if ( i !== 0 )
				{
					var $_page_content = $(this).find('.page-content');				
					$_page_content.css( { marginLeft: win_width / 2 - ( $_page_content.width() / 2 ) + 200, opacity: 0 } );
				}*/

				if ( (i+1) === _wrapper_count )
				{
					init_ready();
				}
			});

			// Sliderit toimimaan
			// Jos aiemmin, niin divin display: none estää lataamisen
			$(sliders_string).anythingSlider(
			{
				autoPlay            : true,
				delay               : 5000,
				buildArrows         : false,
				buildStartStop      : false,
				hashTags            : false
			});
		}

		/**
		 * initReady method
		 * Laukaistaan kun init valmis
		 */
		function init_ready()
		{
			var footer_pos 		= $all_pages.find(':first-child').height()+header_height;

			if ( ($all_pages.find(':first-child').height()+header_height) <= (win_height - $footer.innerHeight() ) )
			{
				footer_pos 		= win_height - $footer.innerHeight();
			}
			else
			{
				$all_pages.css( { height: $all_pages.find(':first-child').height() } );
			}


			$footer.css(
				{
					top: footer_pos
				});

			var hash = window.location.hash ? window.location.hash.replace('!', '') : null;				

			if ( hash )
			{
				control.slide_to_page( { id: hash } );
			}
		};

		$('body').delegate( 'a.inner-link', 'click', function(e)
		{
			var href 	= $(this).attr('href').split(';'),
				pat 	= /^http/;
			
			if ( !pat.test(href[0]) )
			{
				var _o = { id: href[0] };
				_o.add_param = href[1] ? href[1] : null ;

				control.slide_to_page( _o );

				e.preventDefault();
			}
		});

		$('.navigation').delegate( 'li a', 'click', function(e)
		{
			control.slide_to_page( { id: $(this).attr('href'), sender: $(this) } );

			e.preventDefault();
		});

		$('#all-pages').delegate( 'a:not(.anythingSlider a)', 'click', function(e)
		{
			var _pat = /#/;

			if ( _pat.test($(this).attr('href')) )
			{
				control.slide_to_page( { id: $(this).attr('href'), sender: $(this) } );
				e.preventDefault();
			}

		});

		$('p.title a').click( function(e)
		{
			var _target = $(this);

			$(this).toggleClass('open').parent().next('div.hide').slideToggle( function()
			{
				if ( _target.hasClass('open') )
				{
					$all_pages.css( { 'height': '+='+$(this).height() } );
				}
				else
				{
					$all_pages.css( { 'height': '-='+$(this).height() } );
					
				}
					var footer_start_pos 	= $all_pages.eq(0).height()+header_height+$footer.innerHeight();
					var footer_end_pos 		= $all_pages.eq(0).height()+header_height;

					if ( ($all_pages.eq(0).height()+header_height) <= (win_height - $footer.innerHeight() ) )
					{
						footer_start_pos 	= win_height + $footer.innerHeight();
						footer_end_pos 		= win_height - $footer.innerHeight();
					}

					$footer.css(
					{
						top: footer_start_pos
					}).show().stop().animate( { top: footer_end_pos }, transition_speed );
			});


			e.preventDefault();
		});
	});
})(jQuery);
