/**
 * @author Andrei Eftimie
 * Copyright Netlogiq 2008
 * 
 * Lightbox like gallery
 * (with thumbs in the modal window)
 */
(function($){
    $(function(){
	    //Changing the big image on hovering the small images
	    //(require) jquery.hoverIntent
	    var links = $('#modalImages ul a');
	    links.hover(
		    function(){
			    //We take the image and replace its src value with its title value
			    //which should point to the Mid sized thumb
			    var img = $(this).find('table>tbody>tr>td>img').clone();
			    img.attr('src',img.attr('class'));
    	
			    //We take the href value of the link
			    var href = $(this).attr('href');
    			
			    //We replace them in the big image
			    $('#bigImage')
				    .attr('href',href)
				    .find('table>tbody>tr>td')
				    .find('>span')
				    .find('>img').remove().end()
				    .append(img);
    					
		    },
		    function(){}
	    );
    	
	    //Setting up simpleModal
	    //1.Generating the needed HTML code
	    var ulClone = $('#modalImages ul.thumbs').clone();
	    var modalHTML = $('<div id="modalGallery" style="display:none;"></div>');
	    modalHTML.appendTo('body').append('<div id="modalImage"></div>').append(ulClone);
    	
	    //2.Setting Click events to the links
	    links.add('#bigImage').click(function(){
		    //We remove the old image (if there is residue
		    //Removing the present image
		    $('#modalImage').empty();
		    //We create the big image
		    var img = $(this).find('img').clone().attr('src', $(this).attr('href'));
		    //Prepend it to the Modal Content
		    $(img).prependTo('#modalImage');
		    //And calling the modal function
		    $('#modalGallery').modal();
    		
		    //Setting a close event on the bg
		    $('#modalOverlay').click(function(){
			    $.modal.close();		
		    });
		    return false;
	    });
    	
	    //3.Setting Click Events Inside the Modal Gallery
	    $('#modalGallery a').click(function(){
		    var img = $(this).find('img').clone();
		    img.attr('src', $(this).attr('href'));
    		
		    $('#modalImage')
			    //Removing the present image
			    .find('img').remove().end()
			    //Adding the new image
			    .append(img);
    		
		    return false;
	    });
	    
	    //$('a[rel=lightbox]').click(function(){ $(this).attr('href').modal(); return false; });
    });
})(jQuery);
