var gallery = {
	_img_list:			{},
	_img_info:			[],
	_img_pointer:		0,
	
	initialize:			function(){
							gallery._img_list = $('div.img_row img');
							
							$(gallery._img_list).each(function(i){
								$(this).css('cursor','pointer');
								gallery._img_info[i] = {};
								
								var str_pos = $(this).attr('src').lastIndexOf('th_');
								var img_path = $(this).attr('src').slice(0,str_pos);
								var img_name = $(this).attr('src').slice(str_pos + 3);
								var curr_img = new Image();
								curr_img.src = img_path + img_name;
								gallery._img_info[i]['imgLink'] = curr_img;
								
								var img_text = $(this).attr('title').split('--');
								gallery._img_info[i]['imgTitle'] = img_text[0].length ? img_text[0] : null;
								gallery._img_info[i]['imgDesc'] = img_text[1] ? img_text[1] : null;
								
								$(this).click(function(){
									gallery._img_pointer = i;
									$('#image_container').modal({onShow: function(){
										gallery.create_frame();
									}});
								});
							});
							
							$('a#close').click(function(e){ // assign 'close' functionality to 'close' link
								e.preventDefault();
								$.modal.close();
							});
						},
	
	create_frame:	function(){
						// Remove 'click' action from 'prev' and 'next' links so we don't have duplicates
						$('a#prev_link, a#next_link').unbind();
						
						if(gallery._img_pointer <= 0) {
							$('a#prev_link').addClass('disabled');
						} else {
							$('a#prev_link').removeClass();
						}
						if(gallery._img_pointer + 1 >= gallery._img_list.length) {
							$('a#next_link').addClass('disabled');
						} else {
							$('a#next_link').removeClass();
						}
						
						gallery.image_loader();
						
						$('a#prev_link').click(function(e){
							e.preventDefault();
							if(gallery._img_pointer > 0) {
								gallery._img_pointer--;
								gallery.create_frame();
							} else {
								$.modal.close();
							}
						});
						$('a#next_link').click(function(e){
							e.preventDefault();
							if(gallery._img_pointer + 1 < gallery._img_list.length) {
								gallery._img_pointer++;
								gallery.create_frame();
							} else {
								$.modal.close();
							}
						});
					},
	
	image_loader:	function(){
						// Make #simplemodal-container absolutely positioned to the top left
						$('#simplemodal-container').css({
							position: 'absolute',
							top: 0,
							left: 0
						});
						
						var img_link = gallery._img_info[gallery._img_pointer]['imgLink'];
						var img_title = gallery._img_info[gallery._img_pointer]['imgTitle'];
						var img_desc = gallery._img_info[gallery._img_pointer]['imgDesc'];
						
						$('#img_holder img').replaceWith(img_link);
						if(img_title){ $('#title_holder').html(img_title); } else { $('#title_holder').empty(); }
						if(img_desc){ $('#desc_holder').html(img_desc); } else { $('#desc_holder').empty(); }
						
						// Apply the new top positioning to the overlay box
						var top = ($(window).height() / 2) - ($('#image_container').height() / 2) + $(window).scrollTop();
						var left = ($(window).width() / 2) - ($(img_link).width() / 2) + $(window).scrollLeft();
						top = (top > 0 ? Math.round(top) : '5px');
						left = (left > 0 ? Math.round(left) : '5px');
						$('#image_container').css({
							position: 'absolute',
							top: top,
							left: left,
							marginTop: 0
						});

						// Apply new height & width to content div
						var height = $(img_link).height() > $(window).height() ? ($(window).height()*0.80) : $(img_link).height();
						var width = $(img_link).width() > $(window).width() ? ($(window).width()*0.90) : $(img_link).width() + 20;
						$('#img_holder').css({
							height: height,
							width: width
						});
					}
						
};

$(document).ready(function(){
   gallery.initialize();
 });
