jQuery.fn.center = function () {
	var popup = this;
	if ($.browser.msie){
		popup.css('left','0px');
		popup.css('top','0px');
	}
	
		popup.find('.lightbox-container').css("margin-left", "0");
		popup.find('.lightbox-container').css("margin-top", "0");
		popup.css({ position: "fixed", visibility: "hidden", display: "block" });
		popup.find('.lightbox-container').css("left", ( $('body').width() - popup.find('.lightbox-container').outerWidth() ) / 2 + "px");
		popup.find('.lightbox-container').css("top", ( $(window).height() - popup.find('.lightbox-container').outerHeight() ) / 2 + "px");
		
		popup.css({ position: "", visibility: "", display: "" });
	
    return this;
}

$.widget('ui.miniLightbox', {

	_init: function() {
		var obj = this;
		var parent = this.element;
		var infoId = this.element.attr('rel');
		var collection = this.element.attr('title');
		var infoElement = $('.image-info'+infoId);
		
		$('body').prepend('<div class="temp-gallery"></div>');

		parent.addClass('clicked');
		$('.mini-lightbox-button[title="'+collection+'"]').clone().appendTo('body .temp-gallery').hide();
		parent.removeClass('clicked');

		obj.show(infoElement, collection);

		$('#lightbox .lightbox-container .close, #lightbox .overlay').unbind().bind('click',function(){
			obj.hide();
		});

	},

	show: function(el){
		var obj = this;
		if($.browser.msie){
			$('#lightbox .lightbox-container .close').hide();
			$('#lightbox').children('.close').remove();
		}
		$('#lightbox .image-container').children('img').replaceWith('<img src="" alt="" />');
		$('#lightbox .image-title').html('');
		$('#lightbox .image-description').html('');
		var imgPath = el.find('.image-path').html();
		imgPath = imgPath.replace(' ','%20');
		var imgTitle = el.find('.image-title').html();
		var imgDesc = el.find('.image-description').html();

		if(parseInt($('#lightbox .lightbox-container').width()) < 100){
			var fix_width = 100;
		}else{
			var fix_width = 100;
		}
		if(parseInt($('#lightbox .lightbox-container').height()) < 100){
			var fix_height = 100;
		}else{
			var fix_height = 100;
		}
		$('#lightbox .lightbox-container').css({ width: fix_width })
		$('#lightbox .lightbox-container').css({ height: fix_height })
		$('#lightbox').center();
		var top = parseInt( $('#lightbox').css('top') );
		var diff = top - parseInt( $('#lightbox .image-title').css('height') );
		$('#lightbox').css('top', diff);
		$('#lightbox').show();
		$(window).trigger('scroll');
		var margin = ( parseInt($('#lightbox .preloader').height()) - 30 ) / 2;
		$('#lightbox .preloader img').css({ marginTop: margin });
		$('#lightbox .preloader').show();

		$('#lightbox .lightbox-container').css('opacity', 0);
		$('#lightbox .overlay').css('opacity', 0);
		$('#lightbox .lightbox-container').animate({ opacity: '1' }, 500, function(){
			$('#lightbox .image-container').children('img').replaceWith('<img src="'+imgPath+'" alt="" />');
			$('#lightbox .image-container').children('img').css('visibility', 'hidden');

			$('#lightbox .image-container').children('img').unbind().bind('load',function(){
				var max_width = parseInt( $('body').width() );
				var max_height = parseInt( $(window).height() ) - parseInt( $('#lightbox .image-title').css('height') ) - 100;
				$(this).css({ maxWidth: max_width+'px', maxHeight: max_height+'px' });
				if($.browser.msie){
					$('#lightbox .lightbox-container .close').hide();
					$('#lightbox').children('.close').remove();
				}
				$(this).click(function(){
					if($('.temp-gallery .mini-lightbox-button.clicked').next('.mini-lightbox-button').length > 0){
						var rel = $('.temp-gallery .mini-lightbox-button.clicked').removeClass('clicked').next('.mini-lightbox-button').addClass('clicked').attr('rel');
					}else{
						$('.temp-gallery .mini-lightbox-button.clicked').removeClass('clicked');
						var rel = $('.temp-gallery .mini-lightbox-button:first').addClass('clicked').attr('rel');
					}
					var img = $('.image-info'+rel).find('.image-path').html();
					var imgTitle = $('.image-info'+rel).find('.image-title').html();
					var imgDesc = $('.image-info'+rel).find('.image-description').html();
					var prev_width = $(this).width();
					var prev_height = $(this).height();
					$('#lightbox').addClass('centered');
					$(this).fadeOut(function(){
						$('#lightbox .image-container').css({ width: prev_width });
						$('#lightbox .image-container').css({ height: prev_height });
						$('#lightbox .preloader').css({ width: '100%', height: '100%' }).show();
						var margin = ( parseInt($('#lightbox .preloader').height()) - 100 ) / 2;
						$('#lightbox .preloader img').css({ marginTop: margin });
						$(this).attr('src', img).css({ width: '', height: '' }).css('visibility', 'hidden').show();
						$('#lightbox .image-title').html(imgTitle).hide();
						$('#lightbox .image-description').html(imgDesc).hide();
					});
					$(this).unbind('click');
				});

				if(!$('#lightbox').hasClass('centered')){
					var orig_width = parseInt($(this).width()); //319 600
					var width = (orig_width - $('#lightbox .lightbox-container').width() ) /2; // -140 140
					var orig_height = parseInt($(this).height()); //480 300
					var height = (orig_height - $('#lightbox .lightbox-container').height() ) /2; //90 -90
				}else{
					var orig_width = parseInt($(this).width()); //319 600
					var width = (orig_width - $('#lightbox .image-container').width() ) /2; // -140 140
					var orig_height = parseInt($(this).height()); //480 300
					var height = (orig_height - $('#lightbox .image-container').height() ) /2; //90 -90
				}
 
				if(!$('#lightbox').hasClass('centered')){
					$('#lightbox .image-container').children('img').css({ width: $('#lightbox .image-container').width() });
				}else{
					$('#lightbox .image-container').children('img').css({ width: $('#lightbox .lightbox-container').width() });
				}
				$('#lightbox .image-container').children('img').css('opacity','0');

				if($.browser.msie && $.browser.version < 7){
					$('#lightbox .lightbox-container').css({ width: $('#lightbox .image-container').width()+0 });
					$('#lightbox .lightbox-container').css({ height: $('#lightbox .image-container').height()+0 });
				}else{
					$('#lightbox .lightbox-container').css({ width: 'auto' });
					$('#lightbox .lightbox-container').css({ height: 'auto' });
				}

				$('#lightbox .image-container').css({ width: 'auto' });
				$('#lightbox .image-container').css({ height: 'auto' });
				
				$('#lightbox .image-container').children('img').css('visibility', 'visible');
				$('#lightbox .preloader').hide();

				if(!$('#lightbox').hasClass('centered')){
					$('#lightbox').center();
					$('#lightbox').show();
				}

				$('#lightbox .lightbox-container').animate({
					top: '-='+height,
					left: '-='+width
				}, 500, function(){
					if(!$('#lightbox').hasClass('centered')){
						$('#lightbox .image-title').html(imgTitle).fadeIn();
						$('#lightbox .image-description').html(imgDesc).fadeIn();
					}else{
						$('#lightbox .image-title').fadeIn();
						$('#lightbox .image-description').fadeIn();
					}
				});

				$('#lightbox .image-container').children('img').animate({
					width: orig_width,
					opacity: '1'
				}, 500, function(){
					if($.browser.msie){
						var position = $('#lightbox .lightbox-container').position();
						var width = $('#lightbox .lightbox-container').width()+5;
						$('#lightbox .lightbox-container .close').hide().clone().appendTo('#lightbox').css({ top: position.top-14, left: position.left + width }).show();
						$('#lightbox').children('.close').unbind().bind('click',function(){
							obj.hide();
						});
					}

					if($.browser.msie && $.browser.version < 7){
						$('#lightbox .lightbox-container').css({ width: $(this).width()+0 });
						$('#lightbox .lightbox-container').css({ height: $(this).height()+0 });
					}else{
						$('#lightbox .lightbox-container').css({ width: 'auto' });
						$('#lightbox .lightbox-container').css({ height: 'auto' });
					}
					
					$('#lightbox .image-title').css({ maxWidth: $('#lightbox .image-container').children('img').css('width') });

				});

			});
		});
		$('#lightbox .overlay').animate({ opacity: '0.65' }, 500);

	},

	hide: function(){
		var obj = this;
		$('.temp-gallery').remove();
		$('#lightbox').removeClass('centered');
		$('#lightbox').children('.close').hide();
		$('#lightbox .lightbox-container').animate({ opacity: '0' }, 500);
		$('#lightbox .overlay').animate({ opacity: '0' }, 500, function(){
			$('#lightbox').hide();
			$('#lightbox .image-container img').css({ width: 'auto', height: 'auto' });
			$('#lightbox .lightbox-container').css({ width: 'auto', height: 'auto' });
			obj.destroy();
		});
		/*$('#lightbox .lightbox-container').animate({ width: '0px', height: '0px', opacity: '0' }, 500, function(){
			$('#lightbox').hide();
			$('#lightbox .image-container img').css({ width: 'auto', height: 'auto' });
			$('#lightbox .lightbox-container').css({ width: 'auto', height: 'auto' });
			obj.destroy();
		});*/
	},

	destroy: function() {
       $.Widget.prototype.destroy.apply(this, arguments); // default destroy
        // now do other stuff particular to this widget
   }
	
});

$.widget("ui.helperValue", {
	_create: function() {
		var obj = this;
		var parent = this.element;
		var form = parent.closest('form');

		if(parent.val() == ''){
			parent.val(parent.attr('title'));
		}

		parent.focus(function(){
			if($(this).val() == $(this).attr('title')){
				$(this).val('');
			}
		});

		parent.blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).attr('title'));
			}
		});
	}
});

$(document).ready(function(){

	$('.mini-lightbox-button').click(function(){
		$(this).miniLightbox();
		return false;
	});
	
	$('.helpervalue').helperValue();
	
});

