// JavaScript Document

jQuery(window).load(function() {
	/* Works as a nice SMOOTHER for the whole Page Navigation */
	/* Later add the loader .gif (though other than ottodegerman.com) */
	jQuery('#main').fadeIn();
});

jQuery(document).ready(function($){
	// Populate Embedd-Code >> Iframe \\
	$('.scrollableArea').children().click(function(){		
		var isLoaded = $('iframe.' + $(this).attr('class')).parent().hasClass('loaded');
	
		if( isLoaded == false ){
			// get the TEXT/embedded SRC from the hidden preLoaded section
			var test = $('#divClone p.' + $(this).attr('class')).text();		
			
			// hide/show – enskild-movie-wrapper
			// populate the Iframe corresponding to the clicked Thumbnail
			$('.film-wrapper').addClass('hidden');
			$('iframe.' + $(this).attr('class')).attr('src', test).parent().removeClass('hidden').addClass('loaded');
		}else{
			$('.film-wrapper').addClass('hidden');
			$('iframe.' + $(this).attr('class')).parent().removeClass('hidden');
		}
	});
	
	$('.slider-inner-wrapper img:first-child').addClass('currentSliderPos');
	// Pilar \\
	var	objectCount;
	var sliderSpeed = 600;
	var sliderRollbackSpeed = sliderSpeed;
	var totalWidth;
	var currentPosition;
	var objectWidth = 840;
	var checker;
	$('.navigation-left, .navigation-right').live('click', function() {
		objectCount = $(this).siblings('.work_img-wrapper').find('.slider-inner-wrapper > *').size();
		// Determine SLIDER ROLLBACK SPEED and check if it is else than the minimum speed == sliderSpeed;
		if((objectCount * 100) > sliderSpeed){ sliderRollbackSpeed = objectCount*100; }		
		// objectWidth * Amout Of Objects MINUS Starting IMG
		totalWidth = objectWidth * (objectCount-1);
		// Measure the Slider Position.
		currentPosition = $(this).siblings('.work_img-wrapper').find('.slider-inner-wrapper').css('left').replace(/[^\d\.]/g, '');
		currentPosition = (currentPosition/objectWidth) + 1;
		
	})
	
	var grouperValueText = null;
	var currentImg = null;
	// Pil LEFT
	$('.slider-wrapper .navigation-left').live('click', function() {
		numOfTextBlocks = $(this).parent('.slider-wrapper').find('p.project_body').size();
		
		if(!$(this).parent().find('.slider-inner-wrapper').is(':animated')){	// Prevent the animation to stack
			if(currentPosition > 1){												// Next
			  	$(this).parent().find('.slider-inner-wrapper').animate({
		  	        'left': '+='+objectWidth+'px'
				}, sliderSpeed, 'easeInOutExpo', function() {
					$(this).children('img.currentSliderPos').removeClass('currentSliderPos').prev().addClass('currentSliderPos');
				});
				// Faktorisera denna in i en funktion!
				if(numOfTextBlocks > 1){
					grouperValueText = $(this).parent('.slider-wrapper').find('.project_body:visible').attr('class').split('num-').slice(-1);
					currentImg = $(this).parent('.slider-wrapper').find('.slider-inner-wrapper .currentSliderPos').prev().attr('class').split('num-').slice(-1);
				}				
			}else{																	// Rewind
				$(this).parent().find('.slider-inner-wrapper').animate({
		  	        'left': -totalWidth
				}, sliderRollbackSpeed, 'easeInOutExpo', function() {
					$(this).children('img.currentSliderPos').removeClass('currentSliderPos').siblings(':last').addClass('currentSliderPos');
				});
				if(numOfTextBlocks > 1){
					grouperValueText = $(this).parent('.slider-wrapper').find('.project_body:visible').attr('class').split('num-').slice(-1);
					currentImg = $(this).parent('.slider-wrapper').find('.slider-inner-wrapper .currentSliderPos').siblings(':last').attr('class').split('num-').slice(-1);
				}
			}
		}
		// Byter textblock utifall värdet för TEXT resp BILD inte är samma
		if(grouperValueText != null){
			if((grouperValueText.toString() != currentImg.toString()) && (numOfTextBlocks > 1)){		
				$(this).parent('.slider-wrapper').find('.project_body:visible').fadeOut(400);
				$(this).parent('.slider-wrapper').find('.project_body.num-'+currentImg).fadeIn(500);
			
				$(this).parent('.slider-wrapper').find('.site_url:visible').fadeOut(400);
				$(this).parent('.slider-wrapper').find('.site_url.num-'+currentImg).fadeIn(500);
			}
		}
	});


	// Pil RIGHT
	$('.slider-wrapper .navigation-right').live('click', function() {
		numOfTextBlocks = $(this).parent('.slider-wrapper').find('p.project_body').size();
		
		if(!$(this).parent().find('.slider-inner-wrapper').is(':animated')){	// Prevent the animation to stack
			if(currentPosition != objectCount){  									// Next
				$(this).parent().find('.slider-inner-wrapper').animate({
		  	        'left': '-='+objectWidth+'px'
				}, sliderSpeed, 'easeInOutExpo', function() {
					$(this).children('img.currentSliderPos').removeClass('currentSliderPos').next().addClass('currentSliderPos');
				});
				
				if(numOfTextBlocks > 1){
					grouperValueText = $(this).parent('.slider-wrapper').find('.project_body:visible').attr('class').split('num-').slice(-1);
					currentImg = $(this).parent('.slider-wrapper').find('.slider-inner-wrapper .currentSliderPos').next().attr('class').split('num-').slice(-1);
				}
				
			}else{  																// Rewind
				$(this).parent().find('.slider-inner-wrapper').animate({
			        'left': '0px'
				}, 	sliderRollbackSpeed, 'easeInOutExpo', function() {
					$(this).children('img.currentSliderPos').removeClass('currentSliderPos').siblings(':first').addClass('currentSliderPos');
				});
				if(numOfTextBlocks > 1){
					grouperValueText = $(this).parent('.slider-wrapper').find('.project_body:visible').attr('class').split('num-').slice(-1);
					currentImg = $(this).parent('.slider-wrapper').find('.slider-inner-wrapper .currentSliderPos').siblings(':first').attr('class').split('num-').slice(-1);
				}
			}
		}
		if(grouperValueText != null){
			if(grouperValueText.toString() != currentImg.toString() && numOfTextBlocks > 1){		
				$(this).parent('.slider-wrapper').find('.project_body:visible').fadeOut(400);
				$(this).parent('.slider-wrapper').find('.project_body.num-'+currentImg).fadeIn(500);
			
				$(this).parent('.slider-wrapper').find('.site_url:visible').fadeOut(400);
				$(this).parent('.slider-wrapper').find('.site_url.num-'+currentImg).fadeIn(500);
			}
		}
	});
	
	// Mer Info \\
	var knapptext;
	var slideTopValue = '320px';
		
	$('#footer-twigg-wrapper').click(function(){
	if($(this).css('top') != slideTopValue) {		
		// Slide Down
		$(this).animate({
			'top': slideTopValue,			
		}, 1000, 'easeOutBack', function(){
			$('#footer-twigg-text').fadeOut(function(){
				$(this).html('<span>&#187;</span>mindre om mig<span>&#171;</span>').fadeIn().children().addClass('small_font-ajust');
			});
		});

		$('#footer-about-text').fadeIn(500);
		knapptext = $(this).html();
	}else {		
		// Slide Up
		$(this).animate({
			'top': '0px'
		}, 500, 'easeInBack', function(){
			$('#footer-twigg-text').fadeOut(function(){
				$('#footer-twigg-wrapper').html(knapptext).fadeIn();
			});
		});
		$('#footer-about-text').fadeOut(600);
	}
	});

	// Spotify Current Track
	var initLeft 	= parseInt($('#header-spotify-loggo').css('left'));
	var initTop		= parseInt($('#header-spotify-loggo').css('top'));
	
	var newLeft 	= initLeft + 35;
	var newTop 		= initTop - 20;
	
	$('#header-cloud-spotify').bind('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			// event mouseover
			$('#header-spotify-loggo').stop().animate({
				left: 	newLeft,
				top: 	newTop,
				opacity: 1
			}, 600, function() {
				$('.header-spotify-text-wrapper').fadeIn(1);
			});
			$('.header-spotify-text-wrapper').delay('300').fadeIn(1);	
			// Cloud animation
			$('#header-cloud-spotify').animate({
				left: -10
			}, 1000);
			
			
		} else {
			// event mouseout
			$('#header-spotify-loggo').delay('7000').animate({
				left: 	initLeft,
				top: 	initTop,
				opacity: 0.4
			}, 1200, 'easeInCirc', function(){
				$('.header-spotify-text-wrapper').delay('600').fadeOut(100);	
			});
			//SpotifyCloud animation reverse
			$('#header-cloud-spotify').animate({
				left: 0
			}, 1000);
			
		}
	});

	// Funktion: Driv
	function drivFunction(direction, scale, posX, posY, duration, togglerActivated) {
		direction = direction || "-";
		scale = scale || "1";
		posY = posY || "0px";
		posY = posY || "0px";
		duration = duration || 1000;
		togglerActivated = togglerActivated || false;
		
		$('#header-driv-hjul').animate({
			rotate: direction + '=600deg', 
			scale: 	'+=' + scale,
			right: 	'-=' + posX,
			top: 	'-=' + posY
			}, duration, 'easeInOutExpo', function(){
				$('#header-driv-hjul').animate({rotate: '-=25deg', scale: '1', right: '+=' + posX, top: '+=' + posY}, (duration/2));
			});
		if(togglerActivated === true){
			$('#header-main_whitestripe').toggleClass('toggler-newHeight', duration, 'easeInOutExpo');
		}
		return $;
	}

	// Driv – Click
	var direction 	= '+';
	var scale		= '.10';
	var posX		= '0px';
	var posY		= '0px';
	var duration	= 1200;
	$('#header-driv-wrapper').live('click', function(){			// Click Effect – Logoshow
		if($('#header-main_whitestripe').hasClass('toggler-newHeight')){ direction = '-'; } else { direction = '+'; }
		if(!$('#header-driv-hjul').is(':animated')){
			drivFunction(direction, scale, posX, posY, duration, true);
		}
	});
	
	// Driv – Hover
	$('#header-driv-cloud').live('mouseover mouseout', function(event) {
		if(event.type == 'mouseover'){		
			$('#header-driv-hjul').stop().animate({ rotate: '+=30deg' }, 600);
		} else {
			if(!$('#header-driv-hjul').is(':animated')){
				$('#header-driv-hjul').stop().animate({ rotate: '-=25deg' }, 400);
			}
		}
	});
	
	
	
	
	
	// Driv – Loggor
	$('#driv-inner tr td img:not(.kugghjul_litet)').live('click', function(){
		if(!$(this).parents('tr').hasClass('drivHeighlight')){
			$(this).parents('td').siblings().find('img').stop().animate({ rotate: '-=220deg' }, 1400);
			$('.kugghjul_stort').stop().animate({ rotate: '+=30deg' }, 1500, 'easeOutExpo');
		}
	});
	
	
	
	
	
	
	
	
	var heightParagraph = $('#driv_text p:first').innerHeight();
	$('#driv_text').css('height', heightParagraph + 'px');
	// Interact with Driv:s
	$('#driv-inner tr > td:first-child img').live('click', function(){
		if(!$(this).parents('tr').hasClass('drivHeighlight')){					// 	This is highlighted			
			// Logga
			var oldObject = $('tr.drivHeighlight td:first').children('img');	//	Store reusable object
			var oldHigh = oldObject.attr('src');								//	Store old Src fr Old object
			oldObject.attr('src', oldHigh.replace('.png','_grey.png'));			//	Replace with new Src on Old object

			$(this).parents('tr').siblings().removeClass('drivHeighlight');		//	Remove highlight fr Old object
			$(this).parents('tr').addClass('drivHeighlight');					//	Add highlight to New object
			
			var oldSrc = $(this).attr('src');									//	Store old Src fr New object
			$(this).attr('src', oldSrc.replace("_grey",""));					//	Replace with New Src on New object
			
			// Liten Kugge
			var litenKugge 			= 'http://localhost/~bjorn/mass-produktion/wp-content/themes/clear/images/driv/kugghjul_litet_light.png';
			var litenKugge_active	= 'http://localhost/~bjorn/mass-produktion/wp-content/themes/clear/images/driv/kugghjul_litet_dark.png';
			
			
			$('img[src = ' + litenKugge_active + ']').attr('src', litenKugge);

			
			$('.drivHeighlight td:last img').attr('src', litenKugge_active);
			
			
			var highClass = jQuery('tr.drivHeighlight').attr('class').match('driv-\\d');
			var strHighClass = highClass.join("");

			$('#driv_text p').each(function(){
			    if($(this).css('display') == 'block'){
					$(this).fadeOut();
				} else {
					if($(this).hasClass(strHighClass)){ 
						heightParagraph = $(this).innerHeight();
						$(this).fadeIn().parents('#driv_text').css('height', heightParagraph + 'px');
					}
				}
			});
		}
	});
	
});
