/*

script: jquery.introduce.js

description: 

copyright: Copyright (c) 2010 [Sadjow Medeiros Lećo](http://sadjow.com/).

authors: Sadjow Medeiros Lećo < sadjow@gmail.com >  Twitter: http://twitter.com/sadjow

inspiration:
- Some functionality inspired by [jquery.highlighter.js](http://www.dailymarkup.com/) Copyright (c) 2009 Christoph Hochgatterer, [MIT License](http://opensource.org/licenses/mit-license.php)


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

(function($){
    $.fn.jintroduce = function(options) {
        
        
        var backgroundScreen = null;
        var descriptionBox = null;
        
        var overlays = new Array();
        
        var defaults = {
            screencolor     : 'black', 
            screenopacity   : '0.8',
            overlaycolor    : 'transparent',
            boxshadow       : '0px 0px 30px white',
            animationspeed  : 500,
            autoload        : false,
            autoloadindex   : 0,
            autoplay        : true,
            autoplaydelay   : 5000,
            description     : false,
            showcount       : 1,
            zindex          : 100000,
            autoscroll      : true
        },
        settings = $.extend({}, defaults, options);
        
        
        
        
        showbackground = function(){
            if(backgroundScreen  == null){
                
                backgroundScreen = jQuery("<div class='introduce-screen'></div>");
                
                jQuery("body").append(backgroundScreen);
    
                                
                backgroundScreen.css({
                    'background-color'     : settings.screencolor,
                    'opacity'            : settings.screenopacity,
                    'z-index' : settings.zindex
                }).fadeIn(settings.animationspeed);
            }
        },
        
        showelements = function(objs, desc){
            showbackground();
            
            // Clear the current overlays
            for(x=0; x < overlays.length; x++){
                var ol = overlays[x];
                jQuery(ol).remove();
            }
            overlays = new Array();
            
            var minTop = Number.MAX_VALUE;
            
            jQuery(objs).each(function(i){
                
                var obj = this;
                
                //dimensions
                var dimensions  = jQuery(obj).offset();
                var top         = dimensions.top;
                var left        = dimensions.left;
                var width       = jQuery(obj).outerWidth();
                var height      = jQuery(obj).outerHeight();
                
                var id          = jQuery(obj).attr('id');
                var classes     = jQuery(obj).attr('class');
                
                if(top < minTop)
                    minTop = top;
                
                var overlay = jQuery("<div class='introduce-overlay' rel='"+id + ' ' +classes+"'></div>");
                var noclick = jQuery("<div class='introduce-noclick'></div>");
                var inset = jQuery("<div class='introduce-inset'></div>");
                
                overlay.append(noclick);
                overlay.append(inset);
                
                overlays.push(overlay);
                
                jQuery("body").append(overlay);
                
                
                            
                
                // Shows the overlay element that have the shadow
                jQuery(overlay).css({
                    'background-color'    : settings.overlaycolor,
                    'width'             : width,
                    'height'             : height,
                    'position'            :'absolute',
                    'left'                : left,
                    'top'                : top,
                    'display'            : 'none',
                    '-moz-box-shadow'    : settings.boxshadow,
                    '-webkit-box-shadow': settings.boxshadow,
                    'z-index' : (settings.zindex + 10),
                    '-moz-border-radius': '5px',
                    '-webkit-border-radius': '5px'
                     
                }).fadeIn(settings.animationspeed);
                
                
                jQuery(noclick).css({
                    'position'          :'absolute',
                    'width'             : width,
                    'height'            : height,
                    'background-color'  :'transparent',
                    'z-index' : (settings.zindex + 11)
                });
                
                // Clear the inset element
                jQuery(inset).html("");
                
                jQuery(inset).append(jQuery(obj).clone());
                jQuery(inset).css({'z-index' : (settings.zindex + 10)});
                jQuery(inset).children(obj).css({'margin' : '0'});  
                
                            
            
            }); // end each
            
            if(settings.autoscroll){
                jQuery('html, body').animate({
                    'scrollTop': minTop - 300
                }, 1000);
            }
            
            //$("#noclick").focus;
            //$("#noclick").trigger("focus");
            if(settings.description == true){
                
                // If already have a description box remove it.
                if(descriptionBox != null)
                    jQuery(descriptionBox).remove();
                    
                descriptionBox = jQuery("<div class='introduce-desc'>"+ desc +"</div>");
                
                jQuery("body").append(descriptionBox);
                jQuery(descriptionBox).fadeIn(settings.animationspeed);
            }

        },
        
        autoplay = function(ocount){
            ocount = $("#control a.introduce").length;
            if(pos != ocount){
                pos++;
                jQuery(objects[pos]).trigger("click");
            }
        },
        
        closer = function(){
            
            if(settings.autoplay == true){
                clearInterval(intervalID);
            }
            
            jQuery(".introduce-screen, .introduce-overlay, .introduce-desc").fadeOut(settings.animationspeed, function() {
                jQuery(this).remove();
                jQuery("a.introduce-active").removeClass("introduce-active");
            });
            backgroundScreen = null;
            overlays = new Array();
        },
        
        $(this).each(function(){              
            
            var jNode = $(this);
            var openerclass     = jQuery(this).attr("class");
            var objs            =  jQuery(jQuery(this).attr("title"));
            var description     = jQuery(this).attr("rel");

            
            
            //TODO:: Autoload
            /*       
            if(settings.autoload == true && $("body").find("#"+settings.autoloadobject).length !=0){
                $("#control a[title="+settings.autoloadobject+"]").trigger('click');
            };
            autoloadindex
            */
            jNode.click(function() {
                
                
                
                setTimeout(function(){
                    showelements(objs, description)   
                }, 200);
                
                $("#control a").removeClass("introduce-active");
                jNode.addClass("introduce-active");
                i = jQuery("a.introduce").index(this)+1;

                }
            );
        
        });  // end each

        if(settings.autoplay){
            var objects = $("#control a");
            var pos = 0;
            intervalID = setInterval(autoplay, settings.autoplaydelay);
            //iid = autoplay();
            $(objects[0]).trigger('click');
        }
        
        // global
        // close
        jQuery("#close").click(function() {
            closer();
            jQuery('#control').trigger('close');
            return false;
        });
        
        
        
        
        //key
        jQuery(document).keyup(function(e){
            
            var key = 0;
            if (e == null) {
                key = event.keyCode;
            } else { // mozilla
                key = e.which;
            }
            switch(key) {
                
                case 37:
                    var active = jQuery("a.introduce-active");
                    var next1 = jQuery(active).prev();
                    if(next1){
                        jQuery(next1).trigger('click');
                    }
                    if(i == 1){i=1};
                    break;
                case 39:
                    var active = jQuery("a.introduce-active");
                    var next1 = jQuery(active).next();
                    if(next1){
                        jQuery(next1).trigger('click');
                    }
                    break;
                case 27:
                    closer();
                    break;
            };
        }); //end key
        
        
        jQuery('.next-step').live('click', function(){
            
            var ar = jQuery("a.introduce").length; 
            var active = jQuery("a.introduce-active");
            var next1 = jQuery(active).next();

            if(next1){
                jQuery(next1).trigger('click');
            }
            
            return false; 
                
        });
        
        
                
        
        
}
})(jQuery);
