/*
* Image Gallery with iPhone border (jQuery Plugin)
* 
* @author adamjarret
* @date 2011/08/28
* 
* Depends: jquery.js
*/

(function( $ ){

    // Abbreviations
    var slice = Array.prototype.slice;

    // iphonegallery constructor
    var iphonegallery = function(el, options)
    {    
        this.itemCount = 0;
            
        // Store reference to element in instance    
        this.el = $(el);
        
        // Store instance in data for element
        this.el.data('iphonegallery', this);
        
        // Initialize instance
        this._init(options);
    }

    $.extend(iphonegallery.prototype,
    {
        // Private methods
        _init : function ( options )
        {
            var self = this;
            this.el.wrap('<div class="iphone-screenshots-wrapper"/>');

            // Default settings (load after .wrap())
            this.settings = {
                "container":this.el.parent()
            };
    
            // If options exist, merge them with default settings
            if ( options )
                $.extend( this.settings, options );
        
            var lis = this.el.find("li");
            this.itemCount = lis.length;
            lis.click(function(e){ self.next(); e.preventDefault(); });
            lis.addClass('ipg-hidden');;
            this.setSelectedIndex(0);
        },
        
        _bind : function ( event, element, handler )
        {
            var instance = this;
            
            // Handle passing only (event, handler) if this.el is intended element
            if(!handler)
            {           
                handler = element;
                element = this.el;
            }
            
            //Bind event to element, passing the element as eventData
            element.bind( event + ".iphonegallery", { element: element }, function()
            {
                return ( typeof handler === "string" ? instance[ handler ] : handler )
                .apply( instance, arguments );
            });
        },
                        
        triggerEvent : function(name, el)
        {
            var opts = ( el ? [el] : [] );
            this.el.trigger('iphonegallery-'+name, opts);
        },
        
        //
        
        showAll : function()
        {
            //this.triggerEvent('willShowAll');
            this.settings.container.addClass("exploded");
            this.el.find("li").removeClass('ipg-hidden');;
            //this.triggerEvent('didShowAll');
        },
        
        isExploded : function()
        {
            return this.settings.container.hasClass("exploded");
        },                
        
        getSelectedIndex : function()
        {
            return this.el.find("li:visible").first().index();
        },        

        setSelectedIndex : function(idx)
        {
            this.settings.container.removeClass("exploded");
            this.el.find("li").addClass('ipg-hidden');;
            this.el.find("li").eq(idx).removeClass('ipg-hidden');;
        },        

        prev : function()
        {
            if(!this.isExploded()) {
                var idx = this.getSelectedIndex()-1;
                if(idx < 0) {
                    idx = this.itemCount-1;
                }
                this.setSelectedIndex(idx);
            }
        },

        next : function()
        {
            if(!this.isExploded()) {
                var idx = this.getSelectedIndex()+1;
                if(idx >= this.itemCount) {
                    idx = 0;
                }
                this.setSelectedIndex(idx);
            }
        }
        
        
    });

    $.fn.iphonegallery = function( options )
    {
        var isMethodCall = (typeof options === "string");
        var args = slice.call( arguments, 1 );
        var returnValue = this;
        var name = "iphonegallery";

        if ( isMethodCall )
        {
            this.each(function()
            {
                var instance = $.data( this, name );

                if ( !instance )
                    return $.error( "cannot call methods on " + name + " prior to initialization; " +
                                    "attempted to call method '" + options + "'" );

                if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" )
                    return $.error( "no such method '" + options + "' for " + name + " widget instance" );

                var methodValue = instance[ options ].apply( instance, args );
                if ( methodValue !== instance &&  methodValue !== undefined )
                {
                    returnValue = methodValue;
                    return false;
                }
            });
        }
        else
        {
            this.each(function() {
                new iphonegallery(this, options);
            });
        }
        
        return returnValue;
    };
    
})( jQuery );

