/* Author: 
    brunnolou
*/

function scrollTo(top, speed) 
{
    if(!speed) speed = 1000
    var doc = $('html, body');
    var pScroll = top - $(window).scrollTop();
    doc.animate({scrollTop: '+=' + pScroll + 'px'}, speed);
    
    // $('body').animate({ scrollTop: top }, 2000);
}

(function($){
    /* Author:
    *    brunnolou
    */
    $.fn.fixate = function(options){
        var $$ = $(this);
        var defaults = {
            "smooth": true,
            "speed": .6,
            "top": 0,
            "coeff": 1
        };
        var opts = $.extend(defaults, options);
        
        $(window).bind('scroll', function() {
            didScroll = true;
        });
        
        var goT=0,
            windowTop = 0,
            didScroll,
            i=0;
        
        function enterFrame(eachObj) {
            if ( didScroll ) {
                offset = eachObj.offset();
                
                if($(window).scrollTop() <= Math.abs(eachObj.height()) || true) {

                    windowTop = (($(window).scrollTop()*opts.coeff) * -1) + opts.top;

                    eachObj.css({
                        "top": " "+ (goT) + "px"
                    });
                }else{
                    // console.log('t:'+offset.top);
                    // console.log('h:'+eachObj.height());
                    // console.log('\\_______' + eachObj.attr('id'));
                }
                
                if(opts.smooth)
                {
                    if(Math.round(goT) != Math.round(windowTop))
                    {
                        goT += (windowTop-goT) * opts.speed;
                    }else
                    {
                        didScroll = false;
                    }
                }else
                {
                    goT = windowTop;
                }    
            }
            setTimeout(function() { 
                enterFrame(eachObj); 
            }, 100);
        }

        return this.each(function(){
    
            $(this).css('position','relative');
            
            enterFrame($(this));

        });
    };
})(jQuery);


(function($){


    ////////////////////////////////////////
    $('#filegest').fixate({
        coeff: 0
    });
        $('.filegest-imac').fixate({
            coeff: .01
        });
        $('.filegest-iphone').fixate({
            coeff: .1
        });
    // // // // // // // // // // // // // // 

    // $('#sis').fixate({
    //     coeff: .2,
    //     top: 100
    // });
        $('#funcionalidadesSis').fixate({
            coeff: .05,
            top: -50
        });
        // $('#screen_sis').fixate({
        //     coeff: .1,
        //     top: 100
        // });
        // $('#screen_sis').fixate({
        //     coeff: .1,
        //     top: 100
        // });
        // $('#sis-info').css({
        //     top: 0
        // });
    // // // // // // // // // // // // // // 
    
    // $('#favbooking').fixate({
    //     coeff: .2
    // });
        // $('.favbooking-info').fixate({
        //     coeff: .1,
        //     top: 225
        // });
        // $('#bag_favbooking').fixate({
        //     coeff: .1,
        //     top: 225
        // });
    // // // // // // // // // // // // // // 

    $('#funcionalidadesCib').fixate({
        coeff: .08,
        top: 300
    });

    ////////////////////////////////////////
    
    $('#produtos .filegest').fadeIn('slow');
    

    $('#contact_link').click(function() {
        scrollTo(4238,2000);
        return false;
    });

    
        
    
    var scrollPositions = [725, 1600, 2280, 3173];
    var light = [ [230,-448,0.7], [150,-158,1], [193,57,.5], [123,315,1]];
    
    $('.goTop').click(function() {
        scrollTo(500);
        return false;
    });
    
    $('#produtos nav a').each(function(index) {
        $(this).click(function(e) {
            scrollTo(
                scrollPositions[index] + 50
            );
            // e.preventDefaults();
            return false;
        });
        $(this).hover(function() {
            $('#light').css({
                'width'       : light[index][0],
                'margin-left' : light[index][1],
                'opacity'     : light[index][2]
            })
            .hide()
            .fadeIn('fast')
        })
    });
    

})(this.jQuery);




window.log = function(){
  log.history = log.history || [];   
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);



function reload(){
    $.ajax({
        url: BASE_URL + "/captcha/reload.php",
        cache: false,
        success: function(html){
            $("#captcha").html(html);
        }
    });
}

function doSetCaretPosition(oField, iCaretPos) {
  // IE Support
  if (document.selection) { 
    // Set focus on the element
    oField.focus ();
    // Create empty selection range
    var oSel = document.selection.createRange ();
    // Move selection start and end to 0 position
    oSel.moveStart ('character', -oField.value.length);
    // Move selection start and end to desired position
    oSel.moveStart ('character', iCaretPos);
    oSel.moveEnd ('character', 0);
    oSel.select ();
  }

  // Firefox support
  else if (oField.selectionStart || oField.selectionStart == '0') {
    oField.selectionStart = iCaretPos;
    oField.selectionEnd = iCaretPos;
    oField.focus ();
  }
}
function doGetCaretPosition(oField) {
  // Initialize
  var iCaretPos = 0;
  // IE Support
  if (document.selection) { 
    // Set focus on the element
    oField.focus ();
    // To get cursor position, get empty selection range
    var oSel = document.selection.createRange ();
    // Move selection start to 0 position
    oSel.moveStart ('character', -oField.value.length);
    // The caret position is selection length
    iCaretPos = oSel.text.length;
  }

  // Firefox support
  else if (oField.selectionStart || oField.selectionStart == '0')
    iCaretPos = oField.selectionStart;
  // Return results
  return (iCaretPos);
}

jQuery.fn.toUpper = function() {
    // alert($(this).html());
    var el = this;
    el.keyup(function(e) {

        console.log(e.keyCode);

        if(e.keyCode != 37 && e.keyCode != 39 && e.keyCode != 16 && e.keyCode != 17 && e.keyCode != 18 && e.keyCode != 91){
            console.log('MODIFICOU');
            var caret = doGetCaretPosition(el[0]);
            el.val(el.val().toUpperCase());
            doSetCaretPosition(el[0], caret);
        }
    });
    return el;
}




















