/*
 * jQuery Move Object
 * 
 * Examples and documentation at:
 * http://www.astrusweb.com/horizontalmove
 * 
 * 2011 Astrusweb.com / Jonas G. de Medeiros
 * 
 * Copyright 2011 Astrusweb.com
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Version: 2.0 (13-08-2011)
 * 
 */

(function($) {

    $.fn.moveobject = function(options) {
      
      var $this = $(this);
      var initialize = $this.offset().left;  
      var movement_x = 0;      
      var perc = 0;
      var lock = false;
      var square = !options.square ? '#square' : options.square;
      var screen_width = $(window).width();
      
      var cssDeselect = {
          '-moz-user-select':'none',
          '-khtml-user-select':'none',
          '-webkit-user-select':'none',
          'user-select':'none',
      };
      
      var cssSelect = {
          '-moz-user-select':'text',
          '-khtml-user-select':'text',
          '-webkit-user-select':'text',
          'user-select':'text',
      };
      
      $(square).bind("dragstart", function() {
        return false;
      });
      
      $(document).mousemove(function(e){
        if(lock)
        {          
          start(e,true);
          
          document.onselectstart = function () { return false; };
          
          $('.not-select').css(cssDeselect);
        }
      });
            
      var start = function(e,type){
                
        if($(window).width() == screen_width)
        { 
          movement_x = e.pageX - (initialize + ($this.width() / 2));            
                    
        }else{
          resize(e);
        }
                
        goToAndPlay(movement_x,type);
      }
      
      var goToAndPlay = function(movemente,type){
        
        type = !type ? false : true;
        
        if(movemente < 0)
        {
          movemente = 0;
        }
        
        if(movemente >= 0 && movemente <= $(square).width())
        {
          if(movemente >= $(square).width() - $this.width())
          {
            movemente = $(square).width() - $this.width();            
          }
          
          if(type)
          {
            $this.css({'left':movemente + 'px'});
            
            setPerc(type);
          }else{
            $this.stop().animate({left:movemente + 'px'},500,'easeOutExpo',function(){setPerc(type);});            
          }
        }
        
      }
      
      var setPerc = function(type){
        perc = ($this.attr('offsetLeft') * 100) / ($(square).width() - $this.width());
                
        if(perc >= 0 && perc <= 100)
        {
          !options.callBack ? '' : options.callBack(perc,type);
        }        
      }
      
      var resize = function(e){
        screen_width = $(window).width();        
        
        initialize = $this.offset().left - $this.attr('offsetLeft');
        
        movement_x = $this.attr('offsetLeft');
      }
      
      $(square).click(function(e){         
        start(e,false);
      });
      
      $this.mousedown(function(e){    
        lock = true;
      });
      
      var out = function ()
      {        
        lock = false;
        
        document.onselectstart = function () { return true; };
        
        $('.not-select').css(cssSelect);
      }
      
      $(document).mouseup(function(e){
        out();
      });
      
      var setBar = function(percent){
        
        movement_x = ($(square).width() / 100) * percent;
        
        goToAndPlay(movement_x);
      }
      
      return {
        setBar : setBar        
      }      
    }
    
})(jQuery);
