/**
 * @author Chris G.
 * PETER BERTALAN JS
 */

$(document).ready(function() {
	
	// ACTIVE MENU LINK
	
	$("a.link").click( function() {
		$("a.active").removeClass("active");
		$(this).addClass("active");
	});

        // CONTACT HIDE
	//$("span.contact").addClass("hide");
	$("a.link").click(function(){
		if ($(this).is("#contact")) {
			$("span.contact").removeClass("hide").addClass("show");
		} else {
			$("span.contact").removeClass("show").addClass("hide");
		}
	});
	
	// GALLERY - INIT
	
	//Adding position index as "id" and "usemap" attributes to all gallery img
	
	$('#galleryBox img').each(function(i){
		$(this).attr({ 	
                    id: ++i
                });
        });
	
	$('#navBox').removeClass('hide').addClass('show');
	  
	var FADEOUT = 600;
	var FADEIN = 300;
        var allPhotoNo = $('#galleryBox img').length;
	var firstPhotoID = $('#galleryBox img:first').attr('id'); // Get first gallery img "id"
	
	var lastPhotoID = $('#galleryBox img:last').attr('id'); // Get last gallery img "id"
	
	$('#galleryBox img:first').removeClass('hide').addClass('show'); // Show the first gallery img
	
	var galleryCounter = '<span id="allPhoto"></span>/<span id="currentPhoto"></span>\n';
        if ( allPhotoNo > 1 ) {
		$('#galleryCounter').append(galleryCounter); // Adding photo counter
	}
	
	$('#currentPhoto').html( firstPhotoID ); // Setting number of the first photo
	$('#allPhoto').html( lastPhotoID ); // Setting number of the last photo
	
	// GALLERY - PREV PHOTO
	// OnClick event setting to photo's left half. Showing the previous photo.
	$('#galleryBoxPrev').click(function() {
		$('#galleryBox').fadeOut(FADEOUT , function () {				         			
			if ( $('img.show').attr('id') == firstPhotoID ) {
				$('img.show').removeClass('show').addClass('hide');
				$('#galleryBox img:last').removeClass('hide').addClass('show');
				$('#currentPhoto').html( lastPhotoID );				
			}
			else {
				$('img.show').removeClass('show').addClass('hide').prev('img').removeClass('hide').addClass('show');
				$('#currentPhoto').html( $('img.show').attr('id') );
			}			
          });
		$('#galleryBox').fadeIn(FADEIN);		
	});
	
	// GALLERY - NEXT PHOTO
	// OnClick event setting to photo's right half. Showing the next photo.
	$('#galleryBoxNext').click(function() {
		$('#galleryBox').fadeOut(FADEOUT , function () {						         			
			if ( $('img.show').attr('id') == lastPhotoID ) {
				$('img.show').removeClass('show').addClass('hide');
				$('#galleryBox img:first').removeClass('hide').addClass('show');
				$('#currentPhoto').html( firstPhotoID );				
			}
			else {
				$('img.show').removeClass('show').addClass('hide').next('img').removeClass('hide').addClass('show');
				$('#currentPhoto').html( $('img.show').attr('id') );
			}			
          });
		$('#galleryBox').fadeIn(FADEIN);
	});

	// Custom function for pressing UP arrow key
	$.fn.goToFirstPhoto = function(){
        if (allPhotoNo <= 1) { return }
		if ( $('img.show').attr('id') == firstPhotoID ) {
				return;				
		} else {
			$('#galleryBox').fadeOut(FADEOUT, function(){
				$('img.show').removeClass('show').addClass('hide');
				$('#galleryBox img:first').removeClass('hide').addClass('show');
				$('#currentPhoto').html($('img.show').attr('id'));
			});
			$('#galleryBox').fadeIn(FADEIN);
		}			
	};
	
	// Custom function for pressing DOWN arrow key
	$.fn.goToLastPhoto = function(){
        if (allPhotoNo <= 1) { return }
		if ($('img.show').attr('id') == lastPhotoID) {
			return;
		}
		else {
			$('#galleryBox').fadeOut(FADEOUT, function(){
				$('img.show').removeClass('show').addClass('hide');
				$('#galleryBox img:last').removeClass('hide').addClass('show');
				$('#currentPhoto').html($('img.show').attr('id'));
			});
			$('#galleryBox').fadeIn(FADEIN);
		}
	};
	
	// Handling keyboard arrow key events 
	$(document.documentElement).keyup(function (keyPressed){
  		// handle cursor keys
  		if (keyPressed.keyCode == 37) {
    		$('#galleryBoxPrev').click();
  		} else if (keyPressed.keyCode == 39) {
    		$('#galleryBoxNext').click();
  		} else if (keyPressed.keyCode == 38) {
    		$.fn.goToFirstPhoto();
  		} else if (keyPressed.keyCode == 40) {
    		$.fn.goToLastPhoto();
  		}
	});
});

