var LightboxClass = function() {
	
	/**
	 * Init 
	 */
	var _init = function() {
		
		$('.lightbox_add,.lightbox_add_single').click(_add);
		$('.show_lightbox').click(_show_lightbox_request);
		
		_set_lightbox_count();
	};
	
	/**
	 * Set lightbox count 
	 */
	var _set_lightbox_count = function() {
	
		var request_status = '';
		var lightbox_count = '';
		
		$.post("/SERVICE/get_lightbox_count",
			function(data) {
 				request_status 	= data.getElementsByTagName("status")[0].childNodes[0].nodeValue;
 				lightbox_count 	= data.getElementsByTagName("count")[0].childNodes[0].nodeValue;
 				
 				if (request_status === "OK") {
		 			$('#lightbox_count').html('(' + lightbox_count+ ')');
 				}
 			});
	};
	
	/**
	 * Add item to lightbox 
	 */
	var _add = function() {
		
		var gallery_id 		= $(this).attr('gallery_id');
		var image_id 		= $(this).attr('image_id');
		var hires_id 		= $(this).attr('hires_id');
		var request_status	= '';
		var lightbox_count 	= '';
		
		$.post("/SERVICE/add_to_lightbox",
			{'gallery_id' : gallery_id, 'image_id' : image_id, 'hires_id' : hires_id},
			function(data) {
			
 				request_status 	= data.getElementsByTagName("status")[0].childNodes[0].nodeValue;
 				lightbox_count 	= data.getElementsByTagName("count")[0].childNodes[0].nodeValue;
 				 				
 				if (request_status === "OK") {
		 			$('#lightbox_count').html('(' + lightbox_count+ ')');
 					$('#lightbox_count').fadeOut('fast').fadeIn('fast').fadeOut('fast').fadeIn('fast');
 				}
			}
		);
	};
	
	
	var _show_lightbox_request = function() {
		
		$.post("/SERVICE/get_lightbox",
			function(data) {
			
 				request_status = data.getElementsByTagName("status")[0].childNodes[0].nodeValue;
 				
 				if (request_status === "OK") {
 					var lightbox_contents = data.getElementsByTagName("result")[0].childNodes[0].nodeValue;
					_show_lightbox(lightbox_contents);
 				}
			}
		);
	};
	
	
	var _show_lightbox = function(lightbox_contents) {
 				
		var lightbox_html = 
			'<div id="lightbox">' + 
			'	<a href="#" class="close lightbox_close"><span>Close</span></a>' + 
			'	<h1>Lightbox</h1>' +
			'	<div id="lightbox_contents">' + 
					lightbox_contents +
			'	</div>'+
			'</div>';
	
		if ( ! $('#lightbox').length) {
			$('#content').prepend(lightbox_html);
		}
		
		$('#overlay').fadeIn();
		$('#lightbox').fadeIn(function() {	
			$('#tooltip_lightbox').fadeOut();
		});
		
		$('.lightbox_close').click(_lightbox_close);
		$('.remove_link').click(_remove);
		
		
	};
	
	
	
	/**
	 * Close lightbox 
	 */
	var _lightbox_close = function() {
		$('#overlay').fadeOut();
		$('#lightbox').fadeOut(function() {$(this).remove()});
	};
	
	
	
	/**
	 * Remove item from lightbox 
	 */
	var _remove = function() {
		
		var image_id 		= $(this).attr('image_id');
		var request_status	= '';
		var lightbox_count 	= '';
		
		$.post("/SERVICE/remove_from_lightbox",
			{'image_id' : image_id},
			function(data) {
			
 				request_status 	= data.getElementsByTagName("status")[0].childNodes[0].nodeValue;
 				lightbox_count 	= data.getElementsByTagName("count")[0].childNodes[0].nodeValue;
 				
 				if (request_status === "OK") {
		 			$('#lightbox_count').html('(' + lightbox_count+ ')');
 					$('#lightbox_count').stop(true, true).fadeOut().fadeIn().fadeOut().fadeIn();
 				}
 				
 				$('#lightbox_item_'+image_id).fadeOut('fast', function() {$(this).remove()});
 				
 				if (lightbox_count == 0) {
 					$('#lightbox_contents').html('This lightbox contains no items.');
 				}
			}
		);
	};
	

	_init();
};





