﻿/*
*
* workScroll.js
* DEVELOPED BY
* Lukasz Lukasiewicz
* lukasiewicz.info
* lukasz@lukasiewicz.info
* 05 Aug 2011
*
*
*
*
*/

(function(){

  // declare plugin name
  $.fn.worksScroll = function( options ) {
  
  		// declare default options
        var defaults = {
			/** options **/
			'scWidth'	       : $(window).width(),
			'speed'	       : 1000
			
		}


		options = $.extend(defaults, options);
		
		var container = $("div#content");
		var nav = $("#nav");
		var menu = $(".menu");
		var currentSection = 0;
		var sections = $("section");
		var maxSections = sections.length;
		nav.height(0);
		/** initialization **/
		initFn = function()
		{		
			
		
			
			items = $("ul li");
			items.width(options.scWidth);
			($(window).height()<800)?$("li div>img").height(.8*$(window).height()):$("li div>img").height(600);
		}
		
		
		function schowSection(sectionNr) {
		
			
				container.stop().animate({ scrollTop:(sectionNr*$(window).height())},options.speed);
				nav.find("a").removeClass("active");
				nav.find("a").eq(sectionNr).addClass("active");
				if(sectionNr!=0) {
					nav.stop().animate({height:100},options.speed);				
				} else {
					nav.stop().animate({height:0},options.speed);
				}
		
		}
		
		menu.find("a").click(function(event){
			event.preventDefault();
			crClass=$(event.target).attr("class");
			
			
			switch(crClass) {
				case "home":
					currentSection=0;break;
				case "id":
					currentSection=1;break;
				case "web":
					currentSection=2;break;
				case "contact":
					currentSection=3;break;
			
			}
			schowSection(currentSection);
			
		
		});
		
		menu.find("a").hover(function(){

			crClass=$(this).attr("class");
			
			switch(crClass) {
				case "id":
					$(this).parent().css('backgroundPosition', '0 -220px');break;
				case "web":
					$(this).parent().css('backgroundPosition', '0 -440px');break;
				case "contact":
					$(this).parent().css('backgroundPosition', '0 -660px');break;
			
			}
			
			
		
		}, function () {
			$(this).parent().css('backgroundPosition', '0 0');
		});
		
		//keyboard navigation
		$(window).keydown(function(event) { 
			if(event.keyCode==40) {
				if(currentSection<maxSections-1){currentSection++;
				schowSection(currentSection);}
			
			} else if(event.keyCode==38) {
				if(currentSection>0){currentSection--;
				schowSection(currentSection);}
			
			} else if(event.keyCode==39) {
			
				var el = sections.eq(currentSection).find("ul");
				
				if ( el.data('current')<el.data('maxSlides')-1) {
					el.data('current',el.data('current')+1);
					
					var scTo = el.data('current')*(-options.scWidth);			
					el.stop().animate({left:scTo},options.speed);
					el.parent().find("div span").stop().animate({
						left: el.parent().find("div span").width()*el.data('current')
					},options.speed)
				}
				
				if(el.data('current') > 0){
					el.parent().find(".prev").fadeIn(options.speed)
				}
				if(el.data('current') == el.data('maxSlides')-1){
					el.parent().find(".next").fadeOut(options.speed)
				}
				
			
				
				
				
				
				
			} else if(event.keyCode==37) {
				var el = sections.eq(currentSection).find("ul");
				
				if ( el.data('current')>0) {
					el.data('current',el.data('current')-1);
					
					var scTo = el.data('current')*(-options.scWidth);			
					el.stop().animate({left:scTo},options.speed);
					el.parent().find("div span").stop().animate({
						left: el.parent().find("div span").width()*el.data('current')
					},options.speed)
				}
				
				
				if(el.data('current') == 0){
					el.parent().find(".prev").fadeOut(options.speed)
				}
				if(el.data('current') < el.data('maxSlides')-1){
					el.parent().find(".next").fadeIn(options.speed)
				}
				
				
				
			}
		});
		
		
		$(window).resize(function () {
				
			
			container.scrollTop(currentSection*$(window).height());
			
		});
		
		
	
		// cycling all elements
		return this.each(function(){
		
			// object
			var scObject = $(this);
			//current slide
			scObject.data('current', 0);
			//number of slides
			scObject.data('maxSlides', scObject.find('li').length);
			//
			var items = scObject.find("li");
			// set size
			initFn();
			
			scObject.width(options.scWidth*scObject.data('maxSlides'));
			
			
			
			var prevBtn =  $( jQuery( '<a href="#" class="prev">Prev Item</a>'));
			var nextBtn =  $( jQuery( '<a href="#" class="next">Next Item</a>'));
			var bar =  $( jQuery( '<div class="bar"><span></span></div>'));
			var icons = $( jQuery( '<p class="icons"><span class="jq"></span><span class="css"></span><span class="html"></span><span class="ps"></span><span class="ai"></span><span class="fl"></span></p>'));
			
			// Add nav butons
			
			scObject.parent().append(prevBtn);
			scObject.parent().append(nextBtn);
			scObject.parent().append(bar);
			
			
			bar.children("span").width(bar.width()/scObject.data('maxSlides'));
			prevBtn.hide();
			
 			//scObject.find("li>div").append('<div class="overlay">BUM</div>');
			items.wrapInner('<div><div class="center"></div></div>');
			items.append('<span class="logo"></span>')
			
			if(scObject.is(".work")) {
			
			
				items.append(icons)
					
				
			
			}
			
			
			function prev(event) {
				event.preventDefault();
				
				(scObject.data('current')==0)?scObject.data('current')=0:scObject.data('current', scObject.data('current')-1);
			
				
				if(scObject.data('current') == 0){prevBtn.fadeOut(options.speed)}
				if(scObject.data('current') < scObject.data('maxSlides')-1){nextBtn.fadeIn(options.speed)}
				
				scObject.stop().animate({left:(-options.scWidth*scObject.data('current'))},options.speed);
				
				
				
				bar.children("span").stop().animate({
					left:bar.children("span").width()*scObject.data('current')
				},options.speed);
				
			}
			
			function next(event) {
				event.preventDefault();
				(scObject.data('current')== scObject.data('maxSlides')-1)?scObject.data('current',scObject.data('maxSlides')-1):scObject.data('current',scObject.data('current')+1);
				
				
				if(scObject.data('current') > 0){prevBtn.fadeIn(options.speed)}
				if(scObject.data('current') == scObject.data('maxSlides')-1){nextBtn.fadeOut(options.speed)}
				
			
				scObject.stop().animate({left:(-options.scWidth*scObject.data('current'))},options.speed);
				
				
				
				
				bar.children("span").stop().animate({
					left:bar.children("span").width()*scObject.data('current')
				},options.speed);
			
			}
			
			prevBtn.click(function(event){prev(event);});
			
			nextBtn.click(function(event){
				
				next(event);
				
			});
			
			
			
			// resize items to fit window after change of window size
			$(window).resize(function () {
				
				
				
				bar.children("span").width(bar.width()/scObject.data('maxSlides')).css('left',bar.children("span").width()*scObject.data('current'));
				
				//resize and set position to current slide
				initFn();
				options.scWidth =  $(window).width();
				var scTo = -options.scWidth*scObject.data('current');
				scObject.width(options.scWidth*scObject.data('maxSlides')).css('left',scTo);
			});
		
		});

  };
})(jQuery);
