/**
*    jQuery.noticeAdd() and jQuery.noticeRemove()
*    These functions create and remove growl-like notices
*        
*   Copyright (c) 2009 Tim Benniks
*
*    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.
*    
*    @author     Tim Benniks <tim@timbenniks.com>
*     @copyright  2009 timbenniks.com
*    @version    $Id: jquery.notice.js 1 2009-01-24 12:24:18Z timbenniks $
**/

var gotcha = [];


(function(jQuery)
{
    jQuery.fn.extend({            
        snapcarousel: function(options)
        {    
            innerId = jQuery(this).attr("id");
            
            
            
            
            var defaults = {
                        direction: 'leftright',
                        pixelCount: 0,
                        scrollSteps: 20,
                        scrollRange: 60,
                        scrollInterval : null,
                        scrollbuttonDown : null,
                        scrollbuttonUp : null,
                        scrollbuttonLeft : null,
                        scrollbuttonRight : null,
                        isMouseDown : false,
                        height : null,
                        width : null,
                        elements : 0,
                        elements : 0,
                        nextIndex : 3,
                        curIndex : 0,
                        innerWidth: 200,
                        duration: 200
            }
            
            this.myId = Math.round(Math.random()*10000);
            
              var options = $.extend(defaults, options);           
            this.lastInterval = false; 
            
            this.options = options;
                                               
            this.innerElement = document.getElementById(innerId);   

            table = document.createElement('table');
            table.id = "carousel"+this.myId;
            table.className = "newsbox";
            
            // top row
            tr = document.createElement('tr'); 
            td = document.createElement('td');
            
            

            
            if( this.options.direction == 'topdown') {
                div = document.createElement('div');
                div.style.width     = '100%';
                div.style.cursor    = 'pointer';
                div.className       = 'scrollbutton_up inactive';
                /*div.id              = 'scrollbutton_up';*/
                div.onmousedown     = function() { me.isMouseDown=true;me.scrollUp(me); };
                div.onmouseup       = function() { me.isMouseDown=false;/*clearInterval(me.scrollInterval);*/ };
                div.innerHTML       = '<center><a href="javascript:void(0)"></a></center>';
                this.scrollbuttonUp     = div;
                 
                td.appendChild(div);
                tr.appendChild(td);
                table.appendChild(tr);
                
                tr = document.createElement('tr');  
            } 
            
            if( this.options.direction == 'leftright' ) {
            
                div = document.createElement('div');
                div.style.cursor    = 'pointer';
                div.className       = 'scrollbutton_left inactive';
                /*div.id              = 'scrollbutton_left';*/
                div.onmousedown     = function() { /*me.isMouseDown=true;*/me.scrollToLeft(me); };
                div.onmouseup       = function() { me.isMouseDown=false;/*clearInterval(me.scrollInterval);*/ };
                div.innerHTML       = '<center><a href="javascript:void(0)"></a></center>';
                this.scrollbuttonLeft = div; 
                 
                td.appendChild(div);
                tr.appendChild(td);
                table.appendChild(tr);    

            }
            
            // middle row    
            td = document.createElement('td'); 
            
            fake = this.innerElement.cloneNode(true);
            pn = this.innerElement.parentNode;
            pn.removeChild(this.innerElement); 
            divOuter = document.createElement('div'); 
            divOuter.id = 'outer_element'+this.myId;
            divInner = document.createElement('div'); 
            divInner.id = 'inner_element'+this.myId;
            
            divInner.appendChild( fake );
            divOuter.appendChild( divInner );
            td.appendChild( divOuter );
            tr.appendChild(td);
            table.appendChild(tr);
            
            
            var me = this;
            gotcha.push(me);
            
            
            
            // bottom row
            if( this.options.direction == 'topdown' ) {
                tr = document.createElement('tr');
                
                td = document.createElement('td');
                div = document.createElement('div');
                div.style.width     = '100%';
                div.style.cursor    = 'pointer';
                div.className       = 'scrollbutton_down';
                /*div.id              = 'scrollbutton_down';  */
                
                
                
                div.onmousedown     = function() { me.isMouseDown=true;me.scrollDown(me); };
                div.onmouseup       = function() { me.isMouseDown=false;/*clearInterval(me.scrollInterval);*/ };
                div.innerHTML       = '<center><a href="javascript:void(0)"></a></center>';
                
                this.scrollbuttonDown = div; 
                 
                td.appendChild(div);
                 
                tr.appendChild(td);    
            } 
            
            if( this.options.direction == 'leftright' ) { 
                
                td = document.createElement('td');
                div = document.createElement('div');
                div.style.width     = '100%';
                div.style.cursor    = 'pointer';
                div.className       = 'scrollbutton_right';
                /*div.id              = 'scrollbutton_right';*/
                div.onmousedown     = function() { me.isMouseDown=true;me.scrollToRight(me); };
                div.onmouseup       = function() { me.isMouseDown=false;/*clearInterval(me.scrollInterval);*/ };
                div.innerHTML       = '<center><a href="javascript:void(0)"></a></center>';
                
                this.scrollbuttonRight  = div;
                 
                td.appendChild(div);
                 
                tr.appendChild(td);    
                
            }   
            
            table.appendChild(tr);
            
            pn.appendChild( table );
            
            this.outerElement = document.getElementById('outer_element'+this.myId);
            
            this.outerElement.style.overflow    = 'hidden';
            
            if( this.options.height ) {
                this.outerElement.style.height      = this.options.height+'px';
            }
            
            if( this.options.width ) {
                this.outerElement.style.width      = this.options.width+'px';
            }
            
            this.innerElement = document.getElementById('inner_element'+this.myId); 

            this.innerElement.style.marginTop = '0px';
            this.innerElement.style.marginLeft = '0px';
            if( this.options.innerWidth ) {
                this.innerElement.style.width      = this.options.innerWidth+'px';
            }
            me.snappers = [];
            me.snappersWidth = [];
            completeWidth = 0;
            jQuery('#'+innerId+' .snap').each( function() {
                if( me.options.direction == 'topdown' ) {
                    me.snappers.push($(this).position().top); 
                    me.snappersWidth.push($(this).height());
                } else {
                    me.snappers.push($(this).position().left); 
                    me.snappersWidth.push($(this).width());
                    completeWidth+=$(this).width();
                }
            } );

            me.currentSnapper = 0;
            me.currentPosition = 0;
            
            if( this.options.direction == 'leftright' && completeWidth < this.options.width ) {
                $(me.scrollbuttonLeft).css('display','none');
                $(me.scrollbuttonRight).css('display','none');
            }
            
        },
        
        
        scrollToLeft : function(me) {
            if( me.currentSnapper == 0) {
                return;
            }
            $(me.scrollbuttonRight).removeClass('inactive');
            me.scrollbuttonRight.className = 'scrollbutton_right';  
            me.currentSnapper--;
            $(me.innerElement).animate({marginLeft:'-'+me.snappers[me.currentSnapper]+'px'},me.options.duration);
            if( me.currentSnapper == 0) {
                $(me.scrollbuttonLeft).addClass('inactive');
            }
        },
        
        
        scrollToRight : function(me) {;
            
            /*console.log(( me.snappers[me.snappers.length-1] 
                        +   me.snappersWidth[me.snappers.length-1] 
                        +   parseInt($(me.innerElement).css('marginLeft') ) 
                        -   $(me.outerElement).width()), me.snappersWidth[me.snappers.length-1]);
            
            me.snappersWidth[me.snappers.length-1]*/
            
            if(    ( me.snappers[me.snappers.length-1] 
                        +   me.snappersWidth[me.snappers.length-1] 
                        +   parseInt($(me.innerElement).css('marginLeft') ) 
                        -   $(me.outerElement).width()) <= 0 ) {
                           return;
            }
            
            
            
            if( gotcha[0].snappers.length == me.currentSnapper /*|| $(me.innerElement).width()+parseInt($(me.innerElement).css('marginLeft'))-$(me.outerElement).width()-me.snappers[me.currentSnapper+1] < me.snappers[1]*/) {
                $(me.scrollbuttonRight).addClass('inactive');
                return;
            }
            $(me.scrollbuttonLeft).removeClass('inactive');
            me.scrollbuttonLeft.className = 'scrollbutton_left';
            me.currentSnapper++;
            $(me.innerElement).animate({marginLeft:'-'+me.snappers[me.currentSnapper]+'px'},me.options.duration);

            if(    ( me.snappers[me.snappers.length-1] 
                        +   me.snappersWidth[me.snappers.length-1] 
                        +   parseInt($(me.innerElement).css('marginLeft') ) 
                        -   $(me.outerElement).width()) <= me.snappersWidth[me.snappers.length-1] ) {
                            $(me.scrollbuttonRight).addClass('inactive');
                        }
                        
        },
        
        
        
        scrollDown : function(me) {;
            if( gotcha[0].snappers.length == me.currentSnapper ) {
                $(me.scrollbuttonDown).addClass('inactive');
                return;
            }
            $(me.scrollbuttonUp).removeClass('inactive');
            me.scrollbuttonUp.className = 'scrollbutton_up';
            me.currentSnapper++;
            $(me.innerElement).animate({marginTop:'-'+me.snappers[me.currentSnapper]+'px'},me.options.duration);
        },
        
        
                 
        scrollUp : function(me) {
            if( me.currentSnapper == 0) {
                return;
            }
            $(me.scrollbuttonDown).removeClass('inactive');
            me.scrollbuttonDown.className = me.scrollbuttonDown.className = 'scrollbutton_down';  
            me.currentSnapper--;
            $(me.innerElement).animate({marginTop:'-'+me.snappers[me.currentSnapper]+'px'},me.options.duration);
            if( me.currentSnapper == 0) {
                $(me.scrollbuttonUp).addClass('inactive');
            }
        }
        

        
            
    });
})(jQuery);






        