$(document).ready
(
	function()
	{	
		
// SCROLLOWANIE
		
		$('nav a, .read_more a, a.scroll_me').click(animate_move_site);
		function animate_move_site(event){
			event.preventDefault();
			$("html, body").animate({ scrollTop: $($(this).attr('href')).offset().top}, {duration: 1000, easing: 'swing'});
		}
		
		$('#main-portfolio a,  #banner-right div.active a').live('click', function(event){
			event.preventDefault();
			
			var button_click = $($(this).attr('href')).index() + 1;
			$('#block-' + button_click).click();
			
			//#portfolio-block-1
			//$("html, body").animate({ scrollTop: $($(this).attr('href')).offset().top - 20}, {duration: 1000, easing: 'swing'});
			$("html, body").animate({ scrollTop: $('#portfolio-block-1').offset().top - 20}, {duration: 1000, easing: 'swing'});
			
			var item_to_go = $($(this).attr('href') + ' img.large');
			item_to_go.css({'border-color': 'red'});
			setTimeout(function(){
				item_to_go.css({'border-color': '#333333'});
			}, 3000);
			
		});
	
		
	// nawigacja fixed strony
		
		if ( $.browser.msie){
			if($.browser.version < 9.0 )
			{
				$('#portfolio-websites li span.folio span.mask').hide();
				$('#portfolio-websites li span.folio').hover(function(){$(this).find('span.mask').show();}, function(){$(this).find('span.mask').hide();});
			};
			$('input.text, #form-message').each(function(){$(this).attr('value', $(this).attr('placeholder'));});
			$('#form-message').css({height: '215px'});
			$('#drop-zone, #files-list-l').hide();
		} else {
		$('#navigation-site-arrows').hover(
				function(){$(this).animate({opacity: 1}, 500)},
				function(){$(this).delay(800).animate({opacity: 0.2}, 500)}
		);
		}
		
		//localStorage.fixed_menu_right=1;
		var fixed_menu_right = 1;		// dla leworeczny praworeczny
		//alert(fixed_menu_right);
		if(typeof(Storage)!=="undefined"){
			//alert(localStorage.fixed_menu);
			if(localStorage.fixed_menu){
				fixed_menu_right = localStorage.fixed_menu;
			}
		};
		
		$(window).bind("resize", set_fixed_navigation);

		function set_fixed_navigation(event){
			var window_width = $(window).width();
			var minus = (window_width - 1114) /2 - 10;
			if(fixed_menu_right == 1){
				$('#navigation-site-arrows').show().css({left: $(window).width() - minus + 'px'});
				$('#navigation-site-arrows').attr('class', 'right');
			} else{
				$('#navigation-site-arrows').show().css({left: minus - 60 + 'px'});
				$('#navigation-site-arrows').attr('class', 'left');
				};
			$('#canvas').css({top: $('footer').offset().top + 200 - $(window).height() + 'px', height: $(window).height() + 'px'});
		};
		set_fixed_navigation();
		
		$('#navigation-toggle').click(function(){
			if(fixed_menu_right == 0) fixed_menu_right = 1; else fixed_menu_right = 0;
			
			set_fixed_navigation();
			if(typeof(Storage)!=="undefined"){
				localStorage.fixed_menu = fixed_menu_right;
			};
		});
		
		
		var pressTimer	// przytrzymanie przycisku
		var fixed_scrolling = 0;
		
		$("#navigation-site-arrows a.buttons").mouseup(function(event){
		  event.preventDefault();
		  clearTimeout(pressTimer);
		  $("html, body").stop();
		 // if(fixed_scrolling == 0)
		//	  animate_move_site;
		  return false;
		}).mousedown(function(event){
			//alert($(this).attr('id'));
			var scroll_top = 0;
			
			if($(this).attr('id') == 'button-down')
				scroll_top = $(document).height();
			
			pressTimer = window.setTimeout(function(){ 
				  $("html, body").animate({scrollTop: scroll_top}, {duration: 10000, easing: 'swing'});
				  fixed_scrolling = 1;
			}, 300);
			
		  return false; 
		}).click(function(event){
			event.preventDefault();
			if(fixed_scrolling == 0)
				$("html, body").animate({ scrollTop: $($(this).attr('href')).offset().top}, {duration: 1000, easing: 'swing'});
			else
				fixed_scrolling = 0;
			
			//alert(fixed_scrolling);
		});;
		
		
		
		var count_fixed_sites = $('div.fixed_navigation').length;
		function change_fixed_navigation(current_scroll_position){
			
			// nastepny
			var next_fixed_site = $('div.fixed_navigation_active').nextAll('div.fixed_navigation').eq(0);
				var is_next = next_fixed_site.length;
			
			if(is_next == 1 && current_scroll_position > $('div.fixed_navigation_active').position().top){
				$('div.fixed_navigation_active').removeClass('fixed_navigation_active').nextAll('div.fixed_navigation').eq(0).addClass('fixed_navigation_active');
			} 
			
			// poprzedni
			var prev_fixed_site = $('div.fixed_navigation_active').prevAll('div.fixed_navigation').eq(0);
				var is_prev = prev_fixed_site.length;
			
			if(is_prev == 1 && current_scroll_position < $('div.fixed_navigation_active').position().top){
				$('div.fixed_navigation_active').removeClass('fixed_navigation_active').prevAll('div.fixed_navigation').eq(0).addClass('fixed_navigation_active');
			}
			
			// zmien przyciski
			
			$('#button-up-name').text($('div.fixed_navigation_active').attr('title'));	// ustaw aktualna strone jako PREV
			$('#button-up').attr('href', '#' + $('div.fixed_navigation_active').attr('id'));
			
			
			var next_fixed_site = $('div.fixed_navigation_active').nextAll('div.fixed_navigation').eq(0);
				var is_next = next_fixed_site.length;
			if(is_next == 1){
				$('#button-down-name').text(next_fixed_site.attr('title'));				// jesli jest nastepna - ustaw nastepna NEXT
				$('#button-down').attr('href', '#' + next_fixed_site.attr('id'));
			} else{																		// jesli nie ma nastepnej - ustaw aktualna PREV
				$('#button-up-name').text($('div.fixed_navigation_active').prevAll('div.fixed_navigation').eq(0).attr('title'));
				$('#button-up').attr('href', '#' + $('div.fixed_navigation_active').prevAll('div.fixed_navigation').eq(0).attr('id'));
				}
			
			if(current_scroll_position == $('div.fixed_navigation_active').position().top){	// jesli pozycja scroll'a rowna jest stronie - ustaw poprzednia PREV
				$('#button-up-name').text($('div.fixed_navigation_active').prevAll('div.fixed_navigation').eq(0).attr('title'));
				if($('div.fixed_navigation_active').prevAll('div.fixed_navigation').eq(0).length == 1)
					$('#button-up').attr('href', '#' + $('div.fixed_navigation_active').prevAll('div.fixed_navigation').eq(0).attr('id'));	// ie
			};
			
		};
		change_fixed_navigation($(window).scrollTop());	// ustaw poczatkowe dane
		
		var all_color_elements = $("div.fixed_navigation").length - 1;
		/*
		var array_colors =  [
		                     	{'r' : 154, 'g' : 23, 'b' : 26, 'top': $('#site-main').position().top, 'grad_r' : 234, 'grad_g' : 39, 'grad_b': 43},	// 0	// czerwien
		                     	
		                     	{'r' : 14, 'g' : 49, 'b' : 61, 'top': $('#site-portfolio').position().top, 'grad_r' : 0, 'grad_g' : 174, 'grad_b': 255},	// niebieski
		                     	
		                     	// {'r' : 14, 'g' : 49, 'b' : 61, 'top': $('#site-portfolio').position().top, 'grad_r' : 0, 'grad_g' : 174, 'grad_b': 255}		// 2 // niebieski
		                     	//{'r' : 229, 'g' : 79, 'b' : 38, 'top': $('#site-portfolio').position().top, 'grad_r' : 250, 'grad_g' : 123, 'grad_b': 13} // zolty
		                     	//{'r' : 17, 'g' : 55, 'b' : 0, 'top': $('#site-portfolio').position().top, 'grad_r' : 128, 'grad_g' : 208, 'grad_b': 11} // zielony
		                     	//{'r' : 53, 'g' : 53, 'b' : 53, 'top': $('#site-portfolio').position().top, 'grad_r' : 211, 'grad_g' : 211, 'grad_b': 211} // szary
		                     	{'r' : 154, 'g' : 23, 'b' : 26, 'top': $('#site-main').position().top, 'grad_r' : 234, 'grad_g' : 39, 'grad_b': 43}	// czerwien
		                     ];
		
		*/
		
		$('#canvas').css({top: $(document).height() - $(window).height() + 'px', height: $(window).height() + 'px'}).prop("unselectable", "on");
		
		$(window).scroll(function(){ 
			
			var current_scroll_position = $(window).scrollTop();
			change_fixed_navigation(current_scroll_position);
			/*
			// ktory kolory
			var actual_color = $("div.fixed_navigation").index($('div.fixed_navigation_active'));	
			
			var next_color = actual_color + 1;
			if(next_color > all_color_elements) next_color = all_color_elements;
			
			var prev_color = actual_color - 1;
			if(prev_color < 0) prev_color = 0;
			
			
			// wysokosc strony strony
	
			//var document_height = $(document).height() - $(window).height();
			var document_height = array_colors[next_color]['top'] - array_colors[actual_color]['top'];	// jest do, ale jeszcze trzeba zmieniac od jakiego koloru a nie od 0 - poczatku zawsze
			
			if(document_height == 0) // gdy dalej nie ma do czego zmieniac koloru
				document_height = $(document).height() - $(window).height();
			
			current_scroll_position = current_scroll_position - array_colors[actual_color]['top'];
			
			
			var procent = (current_scroll_position) / document_height;
			
			
			
			
			
			// kolory od do
			
			//var r_from = 234;
			//var g_from = 39;
			//var b_from = 43;
			var r_from = array_colors[actual_color]['r'];
			var g_from = array_colors[actual_color]['g'];
			var b_from = array_colors[actual_color]['b'];
				var grad_r_from = array_colors[actual_color]['grad_r'];
				var grad_g_from = array_colors[actual_color]['grad_g'];
				var grad_b_from = array_colors[actual_color]['grad_b'];
			
			
			//var r_to = 0;
			//var g_to = 145;
			//var b_to = 191;
			var r_to = array_colors[next_color]['r'];
			var g_to = array_colors[next_color]['g'];
			var b_to = array_colors[next_color]['b'];
				var grad_r_to = array_colors[next_color]['grad_r'];
				var grad_g_to = array_colors[next_color]['grad_g'];
				var grad_b_to = array_colors[next_color]['grad_b'];
			
			var r_actual = r_from + Math.round((r_to - r_from) * procent);
			var g_actual = g_from + Math.round((g_to - g_from) * procent);
			var b_actual = b_from + Math.round((b_to - b_from) * procent);
				var grad_r_actual = grad_r_from + Math.round((grad_r_to - grad_r_from) * procent);
				var grad_g_actual = grad_g_from + Math.round((grad_g_to - grad_g_from) * procent);
				var grad_b_actual = grad_b_from + Math.round((grad_b_to - grad_b_from) * procent);
			
			//ctx.clearRect(0,0,600,600);
			ctx.fillStyle = 'rgba(' + r_actual +',' + g_actual +',' + b_actual +', 1)';
			ctx.fillRect(0,0,600,600);
			ctx.save();
			
			//$('#my-add').attr('class', 'rgba(' + r_actual +',' + g_actual +',' + b_actual +', 1)');
			$('#my-add').attr('class', procent + ' + curr:' + current_scroll_position + ' + all_doc:' +  document_height + ' :: rgb:' + r_from + '-' + r_to + ' ---- '+ actual_color + '/' + all_color_elements + ' p:' + prev_color + ' n:' + next_color);
			
			
			
			// gradient liniowy
			grad = ctx.createLinearGradient(0, 0, width, 0);
		    grad.addColorStop(0, 'rgba(0,0,0, 0.0)');	// przezroczysty
		    grad.addColorStop(1,'rgba(0,0,0, 0.20)');	// czarny pol przezroczysty
		    ctx.fillStyle = grad;
		    ctx.fillRect(0,0,600,600);
		    
		    
		    
			// gradient kreska
			var radgrad = ctx.createRadialGradient(800, 800, 200, 0, 2 * Math.PI, true);	// swiatlo
			radgrad.addColorStop(0, 'rgba(0,0,0, 0.0)');
		    radgrad.addColorStop(0.5, 'rgba(' + grad_r_actual +',' + grad_g_actual +',' + grad_b_actual +', 0.75)');
		    ctx.fillStyle = radgrad;
			ctx.fillRect(0,0,600,600);
			
			// kreska w poprzek
			//radgrad = ctx.createRadialGradient(45,45,200,52,50,200); 
			
			// gradient kolo
			//var g_left = canvas.width / 2; // canavas 300, polowa 600
			//var g_top = canvas.width / 3; // rowniez 300, polowa wysokosci canvas na calej przegladarce
			
			//var radgrad = ctx.createRadialGradient(0, 0, 100, g_left, g_top, false);	// wysrodkowana latarka na logo
			//var radgrad = ctx.createRadialGradient(g_left, 0, 350, g_left, g_top, 250);	// OD DOLU DUZO PODSWIETLENIE
			//var radgrad = ctx.createRadialGradient(150, 150, 150, 300, 300, 150); // PRZEKATNA KRESKA FAJNA
			
			// http://designconcept.webdev20.pl/articles/html5-canvas-podstawy/
			// 3x SRODEK KOLOR PELNY, 3x ZASIEG GRADIENTU - lewy, gora, promien
			var radgrad = ctx.createRadialGradient(280, 260, 120, 300, 270, 380); // podstawowy prawie dobry kolo
			
		    
			radgrad.addColorStop(0, 'rgba(' + grad_r_actual +',' + grad_g_actual +',' + grad_b_actual +', 1.0)');
			radgrad.addColorStop(1, 'rgba(0,0,0, 0.0)');
			
			// TEST
			//radgrad.addColorStop(0, 'rgba(255, 255, 255, 0.5)')	// BIALY SRODEK
			//radgrad.addColorStop(1, 'rgba(0,0,0, 0.0)');	// PRZEZROCZYSTY NA ZEWNATRZ
			
		    ctx.fillStyle = radgrad;
			ctx.fillRect(0,0,600,600);
			
			*/
			
			
		    
		    
		   
		});
		
		
// LOAD
		
		$('#gg a').load('element_gg.php');
		
		$('#google-plus').html('<g:plusone size="medium" count="false"></g:plusone>');
		$('#helion-pl').load('element_helion.php');
		$('#nazwa-pl').load('element_nazwa_pl.php');
		

// BANER 
		/*
		$('#banner-right div img').hover(
			function(){
				$(this).animate({top: '-20px'}, 300);
			},
			function(){
				$(this).animate({top: '0px'}, 200);
			}
		);
		*/
		
		// HOVER
		
		var hover_block = 0;
		var opacity_ie = 0.8;
		if ( $.browser.msie && $.browser.version < 9.0 ){
			opacity_ie = 1.0;
		} 
		var speed_banner = 500;
		$('#banner-right .prev a, #banner-right .next a').live({mouseenter:function(){
				$(this).find('img.main').stop().animate({top: '-10px'}, speed_banner);
				$(this).find('img.banner_shadow').stop().animate({width: '180px', left: '20px', top: '184px', opacity: opacity_ie}, speed_banner);
			},
			 mouseleave:function(){
				if(hover_block == 0)
				{
				$(this).find('img.main').stop().animate({top: '0px'}, speed_banner);
				$(this).find('img.banner_shadow').stop().animate({width: '223px', left: '0px', top: '180px', opacity: 1}, speed_banner);
				} else {
					hover_block = 0;
				}
				
			}
		});
		
		
		$('#banner-right .active a').live({mouseenter:function(){
			$(this).find('img.main').stop().animate({top: '-16px'}, speed_banner);
			$(this).find('img.banner_shadow').stop().animate({width: '223px', left: '40px', top: '200px', opacity: opacity_ie}, speed_banner);
			},
			 mouseleave:function(){
				$(this).find('img.main').stop().animate({top: '0px'}, speed_banner);
				$(this).find('img.banner_shadow').stop().animate({width: '309px', left: '0px', top: '196px', opacity: 1}, speed_banner);
			}
		});
		
		
		var change_speed = 800;
		var is_changing = 0;
		
		function zmiana(){
			hover_block = 1;
			//$('#banner-right div.next').attr('class', 'prev');
			$('#banner-right div.next').css('z-index', 0).animate({left: '0px'}, {duration:change_speed, easing: 'easeOutCubic', queue: false, complete: function(){$(this).attr('class', 'prev')} });
						
			//$('#banner-right div.active').attr('class', 'next');
			$('#banner-right div.active').css('z-index', 10).animate({left: '370px'}, {duration: change_speed, queue: false, complete: function(){$(this).attr('class', 'next')}});
			$('#banner-right div.active img.main').animate({ 
				width:'223px',
				height:'170px',
				'padding-top':'40px'
				}, {duration:change_speed, easing: 'easeOutCubic', queue: false});
			$('#banner-right div.active img.banner_shadow').animate({	
				width:'223px',
				top:'180px'
			}, {duration: change_speed, queue: false});
			
			
			//$(this).parent().attr('class', 'active');
			$('#banner-right div.prev').css('z-index', 20).animate({left: '150px'}, {duration: change_speed,  queue: false, complete: function(){$(this).attr('class', 'active');}});	
			$('#banner-right div.prev img.main').animate({	
				width:'309px',
				height:'236px',
				top: '0px',
				'padding-top':'0px'
				}, {duration: change_speed, queue: false});
			$('#banner-right div.prev img.banner_shadow').animate({	
					width: '309px', 
					left: '0px', 
					top: '196px', 
					opacity: 1
			}, {duration:change_speed, queue: false});
			
			//return is_changing = 0;
		};
		function zmiana_next(){
			hover_block = 1;
			
			//$('#banner-right div.prev').attr('class', 'next');
			$('#banner-right div.prev').css('z-index', 0).animate({left: '370px'}, {duration: change_speed, queue: false, complete: function(){$(this).attr('class', 'next')}});
			
			
			//$('#banner-right div.active').attr('class', 'prev');
			$('#banner-right div.active').css('z-index', 10).animate({left: '0px'}, {duration:change_speed, easing: 'easeOutCubic', queue: false, complete: function(){$(this).attr('class', 'prev')} });
			$('#banner-right div.active img.main').animate({
				width:'223px',
				height:'170px',
				'padding-top':'40px'
				}, {duration: change_speed, queue: false});
			$('#banner-right div.active img.banner_shadow').animate({	
				width:'223px',
				top:'180px'
			}, {duration: change_speed, queue: false});
			
			//$(this).parent().attr('class', 'active');
			$('#banner-right div.next').css('z-index', 20).animate({left: '150px'}, {duration: change_speed, queue: false, complete: function(){$(this).attr('class', 'active');}});	
			$('#banner-right div.next img.main').animate({	
				width:'309px',
				height:'236px',
				top: '0px',
				'padding-top':'0px'
				}, {duration: change_speed, queue: false});
			
			$('#banner-right div.next img.banner_shadow').animate({	
					width: '309px', 
					left: '0px', 
					top: '196px', 
					opacity: 1
			}, {duration: change_speed, queue: false});
			
			//return is_changing = 0;
		};
		
		// ZAMIANA
		$('#banner-right div.prev a').live('click', function(event){
			event.preventDefault();
			
			if(is_changing == 0)
			{
				is_changing = 1;
				$.when(zmiana()).done(function() {
					setTimeout(function()
					{ 
						is_changing = 0;
					//	alert('l');
					}, 1000);
				});
			}
					
		});
		
		// NEXT
		$('#banner-right div.next a').live('click', function(event){
			event.preventDefault();
			
			if(is_changing == 0)
			{
				is_changing = 1;
				$.when(zmiana_next()).done(function() {
					setTimeout(function()
					{ 
						is_changing = 0;
					//	alert('l');
					}, 1000);
				});
			}
			
		});
		
// PORTFOLIO		
		
		// http://jqueryui.com/demos/effect/easing.html	
		$('#portfolio-navigation span').click(function(){

			var $i = $(this).attr('title') - 1;
			$('#portfolio-navigation span').attr('class', 'inactive');
			$(this).attr('class', 'active');
			
			$('#portfolio-websites ul').each(function(index){
				$(this).delay(200*index).animate({left: -1056 * $i}, 300);
			});
		});
		
//
		$("#form-file").change(function(){
			document.getElementById('files-list-l').innerHTML = '<p>Załączono pliki:</p>';

			// $in=$(this);
			// $in.next().html($in.val());
			  
			var lista = document.getElementById('files-list-l');
			
			var input = document.getElementById("form-file");
			for (var i = 0; i < input.files.length; i++) 
			{
				var pTag = document.createElement("p");
				pTag.innerHTML = 1+i + ') ' +input.files[i].name;
				lista.appendChild(pTag);
			};
		});
		
		
		$('a.target').attr('target', '_blank');
/////////
	}
)

