/**
 * @author mkwok
 * @version 0.2 Nov 13,2008
 * function to create a slideshow of a list of images
 * also creates pagination numbers based on
 * the number of images in the list,numbers
 * will allow users to navigate to the specific image.
 */
(function(){

    $.fn.slideshow = function(settings){
    
        settings = jQuery.extend({
			paging:true,
		    name:"Images",
		    callback:null
		}, settings);
        
        var imagelist = $(this).find("li");
        var imagelength = imagelist.length;
        var cur_img = 0;
		
        if (imagelength <= 1) 
            return;
        
        var indexlist = ((imagelength == 1)||(settings["name"]==null)) ? $("<ul></ul>") : $("<ul><li>"+settings["name"]+"</li></ul>");
        $(this).after(indexlist);
        $(indexlist).addClass("indexList");
		
        if (imagelength > 1) {
            for (var i = 1; i <= imagelength; i++) {
                $(imagelist[i - 1]).attr("id", "img_" + i); // assign unique id to img list
                $(indexlist).append("<li><a href='" + i + "' rel='" + i + "'>" + i + "</a></li>");
            }
		
			if (settings["paging"]) {
				// append left and right arrows
				$(indexlist).append("<li><a href='#' class='slideLeft'>&lt;</a></li>");
				$(indexlist).append("<li><a href='#' class='slideRight'>&gt;</a></li>");
				
				//console.log($(indexlist));
				
			}

        }		
        if ($(this).find("a").hasClass("viewFilm")) {
        
            var filmlink = $(this).find("a.viewFilm").clone(true);
            $(this).find("a.viewFilm").remove();
            $(indexlist).append(filmlink);
            $(indexlist).find("a.viewFilm").wrap("<li class='viewFilm'></li>")
           
        }
        
        $(indexlist).addClass("indexList");
        $(this).find("ul").after(indexlist);
        
        $(imagelist).hide();

        
        $(indexlist).find("a").not('.viewFilm, .slideRight, .slideLeft').each(function(){
            $(this).click(function(e){
                e.preventDefault();
				cur_img = $(indexlist).find("a").index(this);
                var img_id = "img_" + $(this).attr("rel");
                
                // remove current highlight
                $(this).parents("ul").find("a").removeClass("current");
                // add highlight to current item
                $(this).addClass("current");
                
                // check if item clicked on is already displayed
                if (cur_img != img_id) {
                    $(imagelist).fadeOut("1000").find("img").hide().animate({
                        opacity: 1.0
                    }, 200).parents("ul").find("#" + img_id).fadeIn("1000").find("img").show();
                    

                }
                
                if (cur_img == 0) {
    		        $(indexlist).find("a.slideLeft").addClass("disabled");	
    		    }
    		    else if (cur_img == imagelength-1) {
    		        $(indexlist).find("a.slideRight").addClass("disabled");	
    		    }
    		    else {
    		        $(indexlist).find("a.slideLeft").removeClass("disabled");	
    		        $(indexlist).find("a.slideRight").removeClass("disabled");	
    		    }
                
                
                // add callback for indexlist
                if (settings["callback"]){
                    settings["callback"]();
                }


            });
        });
		
		$(indexlist).find("a.slideLeft").click(function(e){
            e.preventDefault();
			if (cur_img > 0){
				cur_img = cur_img-1;
				$(indexlist).find("li").eq(cur_img+1).find("a").trigger("click");
			}
			
		});

		$(indexlist).find("a.slideRight").click(function(e){
            e.preventDefault();
			if (cur_img < imagelength-1){
				cur_img = cur_img+1;
				$(indexlist).find("li").eq(cur_img+1).find("a").trigger("click");
			}
			
		});

		if ((imagelength==1)||(settings["name"]==null)){
			// show first image
			$(indexlist).find("li").eq(cur_img).find("a").trigger("click");
		}else{
			$(indexlist).find("li").eq(cur_img+1).find("a").trigger("click");
		}
     
    };
    
    
})(jQuery);

