function clickNext(){
	//alert('clicknext');
	if (window.index < window.counter-1){
		window.index += 1;
		$('.fullscreen-layer .background ul').animate({
			'opacity':0
		},500);
		$('.fullscreen-layer .content .images-container .images img').animate({
			'opacity':0
		},500);
		jQuery.queue( $(".fullscreen-layer .content .images-container .images img")[0], "fx", function () {
			$(this).html('');
			updateContent();
			updateNavButtons();
		});
	}
}

function clickPrevious(){
	//alert('clickprev');
	if (window.index > 0) {
		window.index += -1;
		$('.fullscreen-layer .background ul').animate({
			'opacity':0
		},500);
		$('.fullscreen-layer .content .images-container .images img').animate({
			'opacity':0
		},500);
		jQuery.queue( $(".fullscreen-layer .content .images-container .images img")[0], "fx", function () {
			$(this).html('');
			updateContent();
			updateNavButtons();
		});
	}
}

function updateContent(){
	//alert('FS.js update slidermax= '+window.slidermax);
	$('.fullscreen-icon').each(function(i){
		var elem = $(this);
		if (i == window.index){
			$('.fullscreen-layer .item-index').html(window.index);
			$('.fullscreen-layer .navigation .title').html( $(this).parent().children('.fullscreen-info').children('.title').html() );
			
			//get the images 
			if ( $(this).parent().find('.images').children('img').length < 2 ){ //there's only one image

				//hide arrows
				$('.fullscreen-layer .content .images-container .nav .FSprevious').hide();
				$('.fullscreen-layer .content .images-container .nav .FSnext').hide();
				$('.fullscreen-layer .content .images-container .nav .FSprevious').unbind('click');
				$('.fullscreen-layer .content .images-container .nav .FSnext').unbind('click');
				
				$('.fullscreen-layer .content .top .indexer').hide();
			
				//remove old stuff
				$('.fullscreen-layer .content .images-container .images').html('');
				$('.fullscreen-layer .background').html('');
				window.clearInterval(window.intFS);
				
				//append single image
				$(this).parent().find('.images').children('img').clone().appendTo('body .fullscreen-layer .content .images-container .images');
				$('.fullscreen-layer .content .images-container .images img').css({'opacity':0,'left':0});
				$('.fullscreen-layer .background').append('<ul><li></li></ul>');
				$(this).parent().find('.images').children('img').clone().appendTo('body .fullscreen-layer .background ul li');
				$('.fullscreen-layer .background ul li img').css('opacity',0);
				$('.fullscreen-layer .content .images-container .images img').load(function(){
					$(this).animate({'opacity':1}, 1000);
					$('.fullscreen-layer .background ul li img').animate({'opacity':1}, 1000)
				});
				//append text
				$('.fullscreen-layer .content .bottom .FSdescription').remove();
				$(this).parent().find('.FSdescription').clone().appendTo( $('.fullscreen-layer .content .bottom') );
			}
			else { //more than one image - slideshow mode
				$('.fullscreen-layer .content .top .indexer').show();
				
				//show arrows on hover 
				$('.fullscreen-layer .content .images-container .nav .FSprevious').show();
				$('.fullscreen-layer .content .images-container .nav .FSnext').show();
				$('.fullscreen-layer .content .images-container .nav .FSprevious').unbind('click');
				$('.fullscreen-layer .content .images-container .nav .FSprevious').click(function(){
					window.clearInterval(window.intFS);
					slidePrevious();
				});
				
				$('.fullscreen-layer .content .images-container .nav .FSnext').unbind('click');
				$('.fullscreen-layer .content .images-container .nav .FSnext').click(function(){
					window.clearInterval(window.intFS);
					slideNext();
				});
				$('.fullscreen-layer .content .images-container .nav').hover(function(){
					$('.fullscreen-layer .content .images-container .nav .FSprevious').stop(true,true).animate({
						'margin-left': 0
					}, {
						duration: 200,
						easing: 'easeInOutExpo'
					});
					$('.fullscreen-layer .content .images-container .nav .FSnext').stop(true,true).animate({
						'margin-right': 0
					}, {
						duration: 200,
						easing: 'easeInOutExpo'
					});	
				}, function(){
					$('.fullscreen-layer .content .images-container .nav .FSprevious').stop(true,true).animate({
						'margin-left': '-29px'
					}, {
						duration: 200,
						easing: 'easeInOutExpo'
					});
					$('.fullscreen-layer .content .images-container .nav .FSnext').stop(true,true).animate({
						'margin-right': '-29px'
					}, {
						duration: 200,
						easing: 'easeInOutExpo'
					});
				});
				window.images = new Array();
				//append images and create events
				window.sliderindex = 0;
				window.slidermax = 0;
				//remove
				$('.fullscreen-layer .content .images-container .images').html('');
				$('.fullscreen-layer .background').html('');
				$('.fullscreen-layer .content .bottom .FSdescription').remove();
				//populate
				$(this).parent().find('.FSdescription').clone().appendTo( $('.fullscreen-layer .content .bottom') );
				$('.fullscreen-layer .content .images-container .images').append('<ul id="slides"></ul>');
				$('.fullscreen-layer .background').append('<ul></ul>');
				$(this).parent().find('.images').children('img').each(function(index){
					window.images[index] = $(this).attr('src');
					$('.fullscreen-layer .content .images-container .images #slides').append('<li><img class="slides" alt="" src="'+ window.images[index] +'"></li>');
					$('.fullscreen-layer .background ul').append('<li><img class="slides" alt="" src="'+ window.images[index] +'"></li>');
					//slice it up
					$('.fullscreen-layer .content .images-container .images #slides li').css({
						'position':'relative',
						'float':'left',
						'width': '125px',
						'height':'191px',
						'overflow-x':'hidden',
						'opacity':'.5'
					});
					
					
					$('.fullscreen-layer .content .images-container .images #slides li').eq(window.sliderindex).css({
						'opacity': 1,
						borderColor: '#3fa8bf' 
					});
					
					$('.fullscreen-layer .background ul li').children('img').css('opacity', 0);
					window.slidermax = index;
					window.sliderindex = 0;
				});
				
				if ( window.slidermax == 1){
					$('.fullscreen-layer .content .images-container .images #slides li').width(188);
					$('.fullscreen-layer .content .images-container .images').width(382);
				} else {
					$('.fullscreen-layer .content .images-container .images').css('width','383px');
				}
					
				$('.fullscreen-layer .content .top .indexer .who').html(window.sliderindex+1);
				$('.fullscreen-layer .content .top .indexer .howMany').html(window.slidermax+1);
				
				$('.fullscreen-layer .content .images-container .images #slides li').eq(window.sliderindex).animate({
					'opacity': 1
				});
				
				$('.fullscreen-layer .background ul li').children('img').eq(window.sliderindex).animate({
					'opacity': 1
				});
				
				window.clearInterval(window.intFS);
				window.intFS = window.setInterval("fullySlider()",5000);
			}
		}
	});
}

function onCloseFSOverlay(){
	
	//alert('onclosefsoverlay');
	$('.fullscreen-layer .content .top .indexer').hide();	
	
	//rearrange things on close
	$('.fullscreen-icon').eq(window.index).parent().children('.add-hover-result').css({
		'opacity': .8
	});
	$('.fullscreen-icon').eq(window.index).parent().children('.description').css({
		'bottom': 0
	});
	$('.fullscreen-icon').eq(window.index).css({
		'opacity': 1
	});
	$('.fullscreen-icon').eq(window.index).children('a').children('img').css('margin-top', 0);
	
	
	//back to the beginning (delay 3000)
	$('.fullscreen-icon').eq(window.index).parent().children('.add-hover-result').delay(3000).animate({
		'opacity': 0
	}, 500);
	$('.fullscreen-icon').eq(window.index).parent().children('.description').delay(3000).animate({
		'bottom': -20
	}, {
		duration: 300,
		easing: 'easeInOutExpo'
	});
	$('.fullscreen-icon').eq(window.index).parent().children('.fullscreen-icon').delay(3000).animate({
		'opacity': 0
	}, 500);

}

function exitFS(){
	
	//alert('exitFS');
	window.clearInterval(window.intFS);
	
	//disable keypress listener
	if ($.browser.msie || $.browser.webkit) {
	    //ie,
		$(document).unbind('keydown');
	} else {
		//everyone else
	    $(document).unbind('keypress');
	}

	$('body').children('.fullscreen-layer').css({
		'position': 'absolute',
		'top': window.pageYOffset
	});

	onCloseFSOverlay();
			
	$('body').children('.fullscreen-layer').animate({
		'width': '24px',
		'height': '18px',
		'opacity': 0,
		'z-index': 0,
		'top': window.callertop,
		'left': window.callerleft
	}, {
		duration: 1500,
		easing: 'easeInOutExpo',
		complete: function(){
			$(this).children('.background').html('');
			$('body').css('overflow-y','auto');
			$('body').css('overflow-x','hidden');
			$('.fullscreen-layer .content .images-container .images').html('');
		}
	});
	
	window.onscroll = null;
	//enable page scrolling back again
}

function updateNavButtons(){
	var offset = $('.fullscreen-icon').eq(window.index).offset();
	window.callertop = offset.top;
	window.callerleft = offset.left;
	if (index == 0){
		//alert('first');
		$('.fullscreen-layer .navigation .arrows .previous').css({
			'color': '#333',
			'cursor': 'arrow'
		});
		$('.fullscreen-layer .navigation .arrows .previous').unbind('click', clickPrevious);
		$('.fullscreen-layer .navigation .arrows .previous').hover(function(){
			$(this).css('color', '#333');
		});
	} else {
		$('.fullscreen-layer .navigation .arrows .previous').css({
			'color': 'white',
			'cursor': 'pointer'
		});
		$('.fullscreen-layer .navigation .arrows .previous').hover(function(){
			$(this).css('color', '#2dd3e3');
		}, function(){
			$(this).css('color', 'white');
		});
		$('.fullscreen-layer .navigation .arrows .previous').unbind('click');
		$('.fullscreen-layer .navigation .arrows .previous').bind('click', clickPrevious);
	}
	//disable next arrow if last
	//alert('index: '+index+';  window.counter: '+window.counter);
	if (index == parseInt(window.counter)-1){
		//alert('last');
		$('.fullscreen-layer .navigation .arrows .next').css({
			'color': '#333',
			'cursor': 'arrow'
		});
		$('.fullscreen-layer .navigation .arrows .next').unbind('click', clickNext);
		$('.fullscreen-layer .navigation .arrows .next').hover(function(){ 
			$(this).css('color', '#333');
		});
	} else {
		$('.fullscreen-layer .navigation .arrows .next').css({
			'color': 'white',
			'cursor': 'pointer'
		});
		$('.fullscreen-layer .navigation .arrows .next').hover(function(){
			$(this).css('color', '#2dd3e3');
		}, function(){
			$(this).css('color', 'white');
		});
		$('.fullscreen-layer .navigation .arrows .next').unbind('click');
		$('.fullscreen-layer .navigation .arrows .next').bind('click', clickNext);
	}
}

function checkKey(e){
	var keyCode = e.keyCode || e.which, arrow = {left: 37, right: 39, esc: 27 };
	switch (keyCode){
		case arrow.esc: e.preventDefault(); exitFS();
			break;
		case arrow.left:  clickPrevious();
			break;
		case arrow.right: clickNext();
			break;
	}
}

function initFS(){
	//alert('FS.js initFS');
	//index do slideshow no fullscreen layer
	window.fsindex = 0;
	
	//unique classid 
	window.callertop = 0;
	window.callerleft = 0;
		
	//number of fullscreenable projects
	window.counter = $('.fullscreen-icon').length;
	
	//alert('window-counter_ '+window.counter);
	
	$('.fullscreen-icon').each(function(index){
		$(this).hover(function(){
			var offset = $(this).offset();
			var xPos = offset.left ;
			var yPos = offset.top;
			//alert(xPos+'x'+yPos);
			$('body').children('.fullscreen-layer').css({
				'top':yPos,
				'left':xPos
			});
		}, function(){
			return false;
		});
	
		//open the fullscreen.layer first time
		$(this).click(function(){		
			//enable keypress listener
			if ($.browser.msie || $.browser.webkit) {
				$(document).unbind('keydown');
			    $(document).keydown(function(e){
					checkKey(e);
				});
			} else {
				$(document).unbind('keypress');
			    $(document).keypress(function(e){
					checkKey(e);
				});
			}
		
			//emulate mouseleave event
			$(this).parent().children('.add-hover-result').animate({
				'opacity': 0
			}, 500);
			$(this).parent().children('.description').animate({
				'bottom': -20
			}, {
				duration: 300,
				easing: 'easeInOutExpo'
			});
			$(this).parent().children('.fullscreen-icon').animate({
				'opacity': 0
			}, 500);
			///
		
			window.index = index;
			
			$('body').css('overflow','hidden');
			
			var maxWidth = $(window).width();
			var maxHeight = $(window).height();	
			
			$('.fullscreen-layer .item-index').html(index);
			
			//checkup
			updateNavButtons();
							
			//generate unique id for closing purposes
			var rand_id = parseInt(Math.random()*10000000);
			var rand_id = "FS-"+rand_id;
			$(this).addClass(rand_id.toString());
			window.caller = rand_id;
			//get the scrolltop value , to fixate the layer
			var offset = $(this).offset();
			var yPos = offset.top - window.pageYOffset;
			//alert(yPos);
			window.callertop = offset.top;
			window.callerleft = offset.left;		
			$('body').children('.fullscreen-layer').css({
				'z-index':99999,
				'position':'fixed',
				'top': yPos,
				'left': window.callerleft
			});
			
			//get the images 
			if ( $(this).parent().find('.images').children('img').length < 2 ){ //there's only one image
			
				$('.fullscreen-layer .content .top .indexer').hide();
				//hide arrows
				$('.fullscreen-layer .content .images-container .nav .FSprevious').hide();
				$('.fullscreen-layer .content .images-container .nav .FSnext').hide();
				$('.fullscreen-layer .content .images-container .nav .FSprevious').unbind('click');
				$('.fullscreen-layer .content .images-container .nav .FSnext').unbind('click');
				$('.fullscreen-layer .content .images-container .nav .FSprevious').unbind('hover');
				$('.fullscreen-layer .content .images-container .nav .FSnext').unbind('hover');
				
				//remove old stuff
				$('.fullscreen-layer .content .images-container .images').html('');
				$('.fullscreen-layer .background').html('');
				
				//append single image
				$(this).parent().find('.images').children('img').clone().appendTo('body .fullscreen-layer .content .images-container .images');
				$('.fullscreen-layer .content .images-container .images img').css({'opacity':0,'left':0});
				$('.fullscreen-layer .background').append('<ul><li></li></ul>');
				$(this).parent().find('.images').children('img').clone().appendTo('body .fullscreen-layer .background ul li');
				$('.fullscreen-layer .background ul li img').css('opacity',0);
				$('.fullscreen-layer .content .images-container .images img').load(function(){
					$(this).animate({'opacity':1}, 1000);
					$('.fullscreen-layer .background ul li img').animate({'opacity':1}, 1000)
				});
				
				//append text
				$('.fullscreen-layer .content .bottom .FSdescription').remove();
				$(this).parent().find('.FSdescription').clone().appendTo( $('.fullscreen-layer .content .bottom') );
			}
			else { //more than one image - slideshow mode
				$('.fullscreen-layer .content .top .indexer').show();
				//show arrows on hover 
				$('.fullscreen-layer .content .images-container .nav .FSprevious').show();
				$('.fullscreen-layer .content .images-container .nav .FSnext').show();
				$('.fullscreen-layer .content .images-container .nav .FSprevious').unbind('click');
				$('.fullscreen-layer .content .images-container .nav .FSprevious').click(function(){
					window.clearInterval(window.intFS);
					slidePrevious();
				});
				
				$('.fullscreen-layer .content .images-container .nav .FSnext').unbind('click');
				$('.fullscreen-layer .content .images-container .nav .FSnext').click(function(){
					window.clearInterval(window.intFS);
					slideNext();
				});
				//hover fullyslider arrows
				$('.fullscreen-layer .content .images-container .nav').hover(function(){
					$('.fullscreen-layer .content .images-container .nav .FSprevious').stop(true,true).animate({
						'margin-left': 0
					}, {
						duration: 200,
						easing: 'easeInOutExpo'
					});
					$('.fullscreen-layer .content .images-container .nav .FSnext').stop(true,true).animate({
						'margin-right': 0
					}, {
						duration: 200,
						easing: 'easeInOutExpo'
					});	
				}, function(){
					$('.fullscreen-layer .content .images-container .nav .FSprevious').stop(true,true).animate({
						'margin-left': '-29px'
					}, {
						duration: 200,
						easing: 'easeInOutExpo'
					});
					$('.fullscreen-layer .content .images-container .nav .FSnext').stop(true,true).animate({
						'margin-right': '-29px'
					}, {
						duration: 200,
						easing: 'easeInOutExpo'
					});
				});
				window.images = new Array();
				//append images and create events
				window.sliderindex = 0;
				window.slidermax = 0;
				//remove
				$('.fullscreen-layer .content .images-container .images').html('');
				$('.fullscreen-layer .background').html('');
				$('.fullscreen-layer .content .bottom .FSdescription').remove();
				//populate
				$(this).parent().find('.FSdescription').clone().appendTo( $('.fullscreen-layer .content .bottom') );
				$('.fullscreen-layer .content .images-container .images').append('<ul id="slides"></ul>');
				$('.fullscreen-layer .background').append('<ul></ul>');
				$(this).parent().find('.images').children('img').each(function(index){
					window.images[index] = $(this).attr('src');
					$('.fullscreen-layer .content .images-container .images #slides').append('<li><img class="slides" alt="" src="'+ window.images[index] +'"></li>');
					$('.fullscreen-layer .background ul').append('<li><img class="slides" alt="" src="'+ window.images[index] +'"></li>');
					//slice it up
					$('.fullscreen-layer .content .images-container .images #slides li').css({
						'position':'relative',
						'float':'left',
						'width': '125px',
						'height':'191px',
						'overflow-x':'hidden',
						'opacity': '.5'
					});
					
					
					$('.fullscreen-layer .content .images-container .images #slides li').eq(window.sliderindex).css({
						'opacity': 1
					});
					
					$('.fullscreen-layer .background ul li').children('img').css('opacity', 0);
					window.slidermax = index;
					window.sliderindex = 0;
				});
				
				if ( window.slidermax == 1){
					$('.fullscreen-layer .content .images-container .images #slides li').width(188);
					$('.fullscreen-layer .content .images-container .images').width(382);
				} else {
					$('.fullscreen-layer .content .images-container .images').css('width','383px');
				}
				
				$('.fullscreen-layer .content .top .indexer .who').html(window.sliderindex+1);
				$('.fullscreen-layer .content .top .indexer .howMany').html(window.slidermax+1);
				
				//alert(window.sliderindex);
				$('.fullscreen-layer .content .images-container .images #slides li').eq(window.sliderindex).css({
					'opacity': 1,
					borderColor: '#3fa8bf'
				});
				
				$('.fullscreen-layer .background ul li').children('img').eq(window.sliderindex).animate({
					'opacity': 1
				});
				
				window.clearInterval(window.intFS);
				window.intFS = window.setInterval("fullySlider()",5000);
			}
			
			$('body .navigation .title').html($(this).parent().find('.title').html());
			$('body').children('.fullscreen-layer').animate({
				'opacity': 1,
				'width': maxWidth,
				'height': maxHeight,
				'top': 0,
				'left': 0,
				'z-index': 99999
			}, {
				duration: 1500,
				easing: 'easeInOutExpo'
			});
			$('.fullscreen-layer').children('.close-btn').hover(function(){
				$(this).animate({
					'margin-right':'-45px'
				}, {
					duration: 200,
					easing: 'easeInOutExpo'
				});
			}, function(){
				$(this).animate({
					'margin-right':'-60px'
				}, {
					duration: 200,
					easing: 'easeInOutExpo'
				});
			});
			
			$(window).resize(function(){
			
				var width = $(window).width();
				var height = $(window).height();
				//adjust background
				$('.fullscreen-layer').children('.background').css({
					'width': width,
					'height': height
				});
				//adjust close button
				$('.fullscreen-layer').children('.close-btn').css({
					'position':'fixed'
				});
			});
		});
	});
}

function fullySlider(){
	var current = window.sliderindex;
	var prox = current;
	if (current == window.slidermax){
		prox = 0;
		if ($('.fullscreen-layer .content .images-container .images #slides li').eq(prox).offset().left != 201 && window.slidermax > 1)
			$('.images-container .images #slides').animate({'left':0},{
				duration: 1500,
				easing: 'easeInOutExpo'
			});
	} else {
		prox += 1;
		if ( prox < window.slidermax ){
			if ($('.fullscreen-layer .content .images-container .images #slides li').eq(prox).offset().left != 201 && window.slidermax > 1)
				$('.images-container .images #slides').animate({'left': '-=127'},{
					duration: 1500,
					easing: 'easeInOutExpo'
				});
		}
	}
	window.sliderindex = prox;
	changeSlide(current, prox);
	$('.fullscreen-layer .content .top .indexer .who').html(window.sliderindex+1);
	$('.fullscreen-layer .content .top .indexer .howMany').html(window.slidermax+1);
};

function changeSlide(current, prox){
	
	$('.fullscreen-layer .content .images-container .images #slides li').eq(current).animate({
		'opacity': .5,
		borderTopColor: '#ffffff',
		borderBottomColor: '#ffffff',
		borderRightColor: '#ffffff',
		borderLeftColor: '#ffffff'
	}, {
		duration: 500,
		easing: 'easeInOutExpo'
	});
	
	$('.fullscreen-layer .background ul li').eq(current).children('img').animate({
		'opacity': 0
	}, {
		duration: 1500,
		easing: 'easeInOutExpo'
	});
	$('.fullscreen-layer .content .images-container .images #slides li').eq(prox).animate({
		'opacity': 1,
		borderTopColor: '#3fa8bf',
		borderBottomColor: '#3fa8bf',
		borderRightColor: '#3fa8bf',
		borderLeftColor: '#3fa8bf'
	},{
		duration: 500,
		easing: 'easeInOutExpo'
	});
	$('.fullscreen-layer .background ul li').eq(prox).children('img').animate({
		'opacity': 1
	}, {
		duration: 1500,
		easing: 'easeInOutExpo'
	});
}

function slideNext(){
	var current = window.sliderindex;
	var prox = current;
	if (current == window.slidermax){
		prox = 0;
		if ($('.fullscreen-layer .content .images-container .images #slides li').eq(prox).offset().left != 201 && window.slidermax > 1)
			$('.images-container .images #slides').animate({'left':0},{
				duration: 1500,
				easing: 'easeInOutExpo'
			});
	} else {
		prox += 1;
		if ( prox < window.slidermax ){
			if ($('.fullscreen-layer .content .images-container .images #slides li').eq(prox).offset().left != 201 && window.slidermax > 1)
				$('.images-container .images #slides').animate({'left': '-=127'},{
					duration: 1500,
					easing: 'easeInOutExpo'
				});
		}
	}
	window.sliderindex = prox;
	changeSlide(current, prox);
	$('.fullscreen-layer .content .top .indexer .who').html(window.sliderindex+1);
	$('.fullscreen-layer .content .top .indexer .howMany').html(window.slidermax+1);
}

function slidePrevious(){
	var current = window.sliderindex;
	var prox = current;
	if (current == 0){
		prox = window.slidermax;
		if ($('.fullscreen-layer .content .images-container .images #slides li').eq(prox).offset().left != 201 && window.slidermax > 1)
			$('.images-container .images #slides').animate({'left': (prox-2)*-128 + 1+(window.slidermax -3)},{
				duration: 1500,
				easing: 'easeInOutExpo'
			});
	} else {
		prox += -1;
		
		if(parseInt($('.images-container .images #slides').css('left')) != 0) {
			if ($('.fullscreen-layer .content .images-container .images #slides li').eq(prox).offset().left != 201 && window.slidermax > 1)
				$('.images-container .images #slides').animate({'left': '+=127'},{
					duration: 1500,
					easing: 'easeInOutExpo'
				});
		}
	}
	window.sliderindex = prox;
	changeSlide(current,prox);
	$('.fullscreen-layer .content .top .indexer .who').html(window.sliderindex+1);
	$('.fullscreen-layer .content .top .indexer .howMany').html(window.slidermax+1);
}
