$(function() {
	
	var totalPanels			= $(".scrollContainer").children().size();
		
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	
	var movingDistance	    = 185;
	
	var curWidth			= 300;
	var curImgWidth			= 300;

	var $panels				= $('#slider .scrollContainer > div');
	var $container			= $('#slider .scrollContainer');

	$panels.css({'float' : 'left','position' : 'relative'});
    
	$("#slider").data("currentlyMoving", false);

	$container
		.css('width', ($panels[0].offsetWidth * $panels.length)+300)
		.css('left', "0px");

	var scroll = $('#slider .scroll').css('overflow', 'hidden');
	
	function showNext(element) {
		$('#noclick').show();
		$('#buttons-noclick').show();
		$('#ProjectDescriptionText').html($('#panel_description_'+element).html());
		
		$('#panel_'+(element-2))
			.stop()
			.find('div.hint_title').hide(100)
			.end()
			.animate({ width: regWidth, height: '133px', 'margin-top': '41px', 'opacity': 0.3, 'line-height': '90px' }, {duration: 300})
			.find("img").stop()
			.animate({ width: regImgWidth, height: '133px'}, {duration: 75});
			
		
		$('#panel_'+(element-1))
			.stop()
			.unbind()
			.css('cursor', 'default')
			.find('div.hint_title').css('text-align', 'left').show(100)
			.end()
			.removeClass('now')
			.animate({ width: '231px', height: '166px', 'margin-top': '24px', 'opacity': 0.7, 'line-height': '90px' }, {duration: 100})
			.find("img").stop()
			.animate({ width: '231px', height: '166px'}, {duration: 75});
		
		$('#panel_'+element)
			.stop()
			.animate({width: curWidth, height: '216px', 'margin-top': 0, 'margin-bottom': 0, 'line-height': '120px'}, {duration: 100})
			.find("img")
			.animate({ width: curImgWidth, height: '216px'}, {duration: 100})
			.parent()
			.parent()
			.addClass('now')
			.animate({'opacity': 1 }, {duration: 250})
			.unbind()
			.css('cursor', 'pointer')

			.click(function(){window.open($(this).find('#panel_description_'+element+' a').attr('href'));return false;}); 
		$('#panel_'+(element+1))
			.stop()
			.unbind()
			.css('cursor', 'default')
			.find('div.hint_title').css('text-align', 'right').show('fast')
			.end()
			.removeClass('now')
			.animate({ width: '231px', height: '166px', 'margin-top': '24px', 'margin-': '24px', 'opacity': 0.7, 'line-height': '90px' }, {duration: 100})
			.find("img").stop()
			.animate({ width: '231px', height: '166px'}, {duration: 75});
		
		$('#panel_'+(element+2))
			.stop()
			.find('div.hint_title').hide('slow')
			.end()
			.animate({ width: regWidth, height: '133px', 'margin-top': '41px', 'opacity': 0.3, 'line-height': '90px' }, {duration: 300})
			.find("img")
			.stop()
			.animate({ width: regImgWidth, height: '133px'}, {duration: 75});
			
		setTimeout(function(){$('#noclick').hide();}, 300);
		setTimeout(function(){$('#buttons-noclick').hide();}, 300);
	}
	
	//direction true = right, false = left
	function change(direction) {
	
	    //if not at the first or last panel
		if((direction && !(curPanel < totalPanels-2)) || (!direction && (curPanel <= 1))) { return false; }	
        
        //if not currently moving
        if (($("#slider").data("currentlyMoving") == false)) {
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue) - movingDistance : parseFloat(leftValue) + movingDistance;
			
			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				},
				{
					duration: 300
				},
				function() {
					$("#slider").data("currentlyMoving", false);
				});
			
			showNext(next);
			
			curPanel = next;
			
		}
	}
	
	//go to
	function goTo(id){
		if(id < curPanel){
				setTimeout(function(){change(false), goTo(id)}, 310);
		}
		else if(id > curPanel){
				setTimeout(function(){change(true), goTo(id)}, 310);
		}
	}
	
	$('.image').click(function(){
		$('#noclick').show();
		id = parseInt($(this).parent().parent().attr('id').replace('panel_',''));
		
		goTo(id);
	});
	
	// Set up "Current" panel and next and prev
	var start = Math.floor((totalPanels)/2);
	showNext(start);	
	var curPanel = start;
	
	var widthW = parseInt(document.body.offsetWidth); // dla IE
	if(widthW == '' || widthW == null)
		widthW = parseInt(window.innerWidth);
		
	var width = $container.css('width');
	width = parseInt(width.replace('px', ''));
	
	
	if(totalPanels%2)
		var pos = Math.floor((width-widthW)/2)+128;
	else
		var pos = Math.floor((width-widthW)/2)+223;
	
	//185*(start-3)+120
	$container
		.css('left', -(pos)+'px');
	
	//when the left/right arrows are clicked
	$(".right").click(function(){ change(true); });	
	$(".left").click(function(){ change(false); });

	$(window).keydown(function(event){
	  switch (event.keyCode) {
			case 13: //enter
				$(".right").click();
				break;
			case 32: //space
				$(".right").click();
				break;
			case 37: //left arrow
				$(".left").click();
				break;
			case 39: //right arrow
				$(".right").click();
				break;
	  }
	});
});

