/******************************************************

    Photo Viewer jQuery Plugin
    Version : 1.0
    Author  : Rob Curry
    Created : 15/12/2009
    
    jQuery plugin to allow multiple sliding photo 
    panels on the same page.
    
    Last Updates:
    
    15/12/2009, Rob Curry - Created plugin

******************************************************/

(function($){   
  
    $.fn.extend({    
             
        photoViewer: function(options) {   
            
            var opts = (options) ? options : { controls:'normal', autoplay:'false' } ;
    
            return this.each(function() {   
               
               var id = $(this).attr("id");     // id of the container
               var current = 1;                 // set current slide
               var obj = $(this);               // store the container as an object
               var list = $(".photos", obj)     // store the photo UL as an object
               var items = $("li", obj);        // store the slides
               var max = $("li",obj).length;    // store the total number of slides
               var o = opts;
               var interval = false;
               
               if (o.controls == 'inline') {
                  inlinecontrols();
               } else {
                  controls();
               }
                  
               /* add controls */
               function controls() {
                    var html = '<p class="prev"><a href="#" title="Previous Photo"><img src="/images/arrow-prev.gif" alt="previous" /></a></p><p class="dots">';
                    var i=1;
                    $(items).each(function() {
                        if (i==1) { active = 'class="active"' } else { active = ''; }
                        html += '<a href="#" id="' + id + 'p' + i + 'd" '+active+'>&nbsp;</a>';
                        i++;
                    });
                    html += '</p><p class="next"><a href="#" title="Next Photo"><img src="/images/arrow-next.gif" alt="previous" /></a></p> ';
                    $("#"+id).append('<div class="controls">'+html+'</div>');     
                }
                
                function inlinecontrols() {
                    var html = '';
                    var i=1;
                    $(items).each(function() {
                        if (i==1) { active = 'class="active"' } else { active = ''; }
                        html += '<a href="#" id="' + id + 'p' + i + 'd" '+active+'>'+i+'</a>';
                        i++;
                    });
                    
                    $("#"+id).append('<div class="controls inline">'+html+'</div>');
                }
                
                if (o.autoplay == 'true') {
                    interval = setInterval(slideshow,8000);
                }
                function slideshow() {
                    if (current == max) {
                        current = 1;
                    } else {
                        current++;
                    }
                    var pos = $("#"+id+"p"+(current)).position();
            		$(list).animate({"left": "-"+pos.left+"px"}, "slow");
                    $("#"+id+" .controls p.dots > a").removeClass('active');
            		$("#"+id+"p"+current+"d").addClass('active');
                }

                /* bindings */
                
                // previous button
                $("#"+id+" .controls .prev a").live("click", function() {
                    clearInterval(interval);
                    if (current <= 1) { current = 2; }
            		var pos = $("#"+id+"p"+(current-1)).position();
            		$(list).animate({"left": "-"+pos.left+"px"}, "slow");
                    current = current-1;
            		$("#"+id+" .controls p.dots > a").removeClass('active');
            		$("#"+id+"p"+current+"d").addClass('active');
                    
                    $("."+id+"text").hide();
                    $("#"+id+"text"+current).slideDown();
                    
            		return false;
                });
                
                // next button
                $("#"+id+" .controls .next a").live("click", function() {
            		clearInterval(interval);
                    if (current >= max) { current--; }
            		var pos = $("#"+id+"p"+(current+1)).position();
            		$(list).animate({"left": "-"+pos.left+"px"}, "slow");
            		current++;
            		$("#"+id+" .controls p.dots > a").removeClass('active');
            		$("#"+id+"p"+current+"d").addClass('active');
                    
                    $("."+id+"text").hide();
                    $("#"+id+"text"+current).slideDown();
                    
            		return false;
                });
                
                // dots
                $("#"+id+" .controls .dots a").live("click", function() {
            		clearInterval(interval);
                    var go = $(this).attr("id");
                    go = go.substring(0,go.length-1);
            		var pos = $("#"+go).position();
            		$(list).animate({"left": "-"+pos.left+"px"}, "slow");
            		current = parseInt(go.substring((id.length+1)));
            		$("#"+id+" .controls p.dots > a").removeClass('active');
            		$("#"+id+"p"+current+"d").addClass('active');
                    
                    $("."+id+"text").hide();
                    $("#"+id+"text"+current).slideDown();
                    
            		return false;
            	});
                
                $("#"+id+" .controls.inline a").live("click", function() {
            		var go = $(this).attr("id");
                    go = go.substring(0,go.length-1);
            		var pos = $("#"+go).position();
            		$(list).animate({"left": "-"+pos.left+"px"}, "slow");
            		current = parseInt(go.substring((id.length+1)));
            		$("#"+id+" .controls.inline > a").removeClass('active');
            		$("#"+id+"p"+current+"d").addClass('active');
                    $(".content-tabs li").removeClass('active');
            		$("#"+id+"p"+current+"t").addClass('active');
            		return false;
            	});
                
                $(".content-tabs li").bind("click", function() {
            		var go = $(this).attr("id");
                    go = go.substring(0,go.length-1);
            		var pos = $("#"+go).position();
            		$(list).animate({"left": "-"+pos.left+"px"}, "slow");
            		current = parseInt(go.substring((id.length+1)));
            		$("#"+id+" .controls.inline > a").removeClass('active');
            		$("#"+id+"p"+current+"d").addClass('active');
                    $(".content-tabs li").removeClass('active');
            		$("#"+id+"p"+current+"t").addClass('active');
            		return false;
            	});

            });   

        }   
    });   
          
})(jQuery); 