(function($){
	
	$.fn.galleryPlugin = function(options) {
		
		var pages = $('#objects a', this);
		var active = null;
		var activeId = 0;
		var activeImage = null;
		var images = null;
		var container = $(this);
		var pager = $('#scroll', this);
		var activePage = null;
		var activePageIndex = 0;
		
		pages.each(function(){
			var id = $(this).attr('id').replace('gp', '');
			$(this).data('images', $('#gPage' + id + ' .pageImage'));
		})
		
		$('.left', pager).click(function(e){
			if (activePageIndex == 0) {
				var index = images.length-1;	
			} else {
				var index = activePageIndex-1;
			}
			$('.page a', pager).eq(index).click();
			e.preventDefault();
		});
		$('.right', pager).click(function(e){
			if (activePageIndex == images.length-1) {
				var index = 0;	
			} else {
				var index = activePageIndex+1;
			}
			$('.page a', pager).eq(index).click();
			e.preventDefault();
		});
						
		var showImage = function(index){
			var image = $(images.get(index)).css({'z-index' : '30', 'display' : 'block'});
			container.append(image);
			if (activeImage) {
				$(activeImage).fadeOut(function(){
					image.css({'z-index' : '50'});
					$(this).hide();
					activeImage = image;
				});
			} else {
				image.css({'z-index' : '50'});
				activeImage = image;
			}
			
			
		};
		
		var generatePager = function(){
			
			if (images.length > 1) {
				$('.page', pager).remove();
				for (var i = 0; i < images.length; i++) {
					var page = $('<li class="page"><a '+ (i == 0 ? 'class="active"' : '') +' href="#"></a></li>');
					$('.sRight', pager).before(page);
				
					$('a', page).click((function(x){
					
						return function(e){
							e.preventDefault();
							if ($(this).hasClass('active')) {
								return;
							}
							$(activePage).removeClass('active');
							$(this).addClass('active');
							activePage = $(this);
							showImage(x);
							activePageIndex = x;
							
						};
					
					})(i));
				
					if (i==0){
						activePage = $('a', page);
						activePageIndex = 0;
					}
				}
				
				pager.slideDown();
			} else {
				pager.slideUp();	
			}
		};
		
		var showPage = function(element, id){
			if($(activeImage).is(':animated')){
				return false;	
			}
			$(active).removeClass('active');
			$('#gPage' + activeId).hide();
			
			images = $(element).data('images');
			$('#gPage' + id).fadeIn();
			$(element).addClass('active');
			active = $(element);
			activeId = id;
			showImage(0);
			generatePager();
		};	
		
		pages.click(function(e){
			e.preventDefault();
			if ($(this).hasClass('active')) {
				return;
			}
			showPage(this, $(this).attr('id').replace('gp', ''));
			
		});
		
		$(pages.get(0)).click();
		$(this).show();
		/* page->click -> */
	
	};
	
})(jQuery);

(function ($) {
	$(document).ready(function(){

		$('.galleryPage').hide();
		$('#image').galleryPlugin();
		
		$.fn.setCursorPosition = function(pos) {
			if ($(this).get(0).setSelectionRange) {
				$(this).get(0).setSelectionRange(pos, pos);
			} else if ($(this).get(0).createTextRange) {
				var range = $(this).get(0).createTextRange();
				range.collapse(true);
				range.moveEnd('character', pos);
				range.moveStart('character', pos);
				range.select();
	        }
	    }

		$('label').each(function (i) {
			if ($.trim($(this).next('input').val()) == "") {
				$(this).next('input').attr('value',$(this).html());
			}
			$(this).hide();
		});

		$('input').focus(function() {
			if($(this).prev('label').html() == this.value){
				$(this).val('').addClass('focus').setCursorPosition(0);
			}
		});

		$('input').keypress(function() {
			if($(this).prev('label').html() == this.value){
				this.value = '';
				$(this).removeClass('focus');
			}
		});

		$('input').blur(function() {
			$(this).removeClass('focus');
			if(this.value == ''){
				this.value = $(this).prev('label').html();
			}
		});

		$("a[rel='gallery']").colorbox({'photo' : true});
		
		$('#search .toggle').click(function(e){
			if ($('#detailed').is(':visible')) {
				$(this).text(lang.show);
				$('#detailed').stop(true).slideUp();				
			} else {
				$(this).text(lang.hide);
				$('#detailed').stop(true).slideDown();		
			}
			
			e.preventDefault();
		});
		
		$.ajax({
			url: '/ping.php',
			cache: false,
			type: 'GET',
			dataType: 'html',
			timeout: 30000,
			success: function(data, textStatus, XMLHttpRequest){
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
			} 
		});

	});
}) (jQuery);
