

var empty_email = "your email here...";
var empty_msg   = "So what's on your mind?";
var _is_on_scoreboard   = false;
var _is_on_bubble       = false;
var _bubble_visible     = false;



$(document).ready( function(){
    
    $('#contact-link').bind("click", function(){
        
        $.scrollTo( $('#contact-zone'), 2000 );
        $('#contact-h2').effect("pulsate", { times: 1 }, 1200 );
        
    })
    
    
    $('input, textarea').bind("click", function(){
        
        $(this).css( { "color" : "black" });
        
        if( $(this).val()==empty_email || $(this).val()==empty_msg)
        {
            $(this).val('');
        }
        
    });
    
    $('input, textarea').bind("focus", function(){
            $(this).trigger('click');
    });
    $('input, textarea').bind("keypress", function(){
            
        if( $(this).val() == empty_msg || $(this).val() == empty_email ){
            
            $(this).val('');
        }
    });
    
    
    $('#send-link').click( function(){
        
        var _email  = $('#email').val();
        var _msg    = $('#msg').val();
        var error   = false;
        
        var email_error = emailCatch('#email');  // empty on pass
        
        $('#email-feedback').html();
        
        if( !_email || _email==empty_email || email_error )
        {
            $('#email').focus();
            
            error = true;
            
            if( _email && email_error )  //email formatting error
                $('#email-feedback').html( email_error );
            else
                // email missing:
                setTimeout( "$('#email').val( empty_email ).effect('highlight', {}, 800);", 200);   //for IE, needs a delay
            
        }
        
        if( !_msg || _msg==empty_msg )
        {
            if(!error) $('#msg').focus();
            setTimeout( "$('#msg').val( empty_msg ).effect('highlight', {}, 800);", 200); //for IE, needs a delay
            error = true;
        }
        
        if( error ) 
            return false;
        else
        {
            $('#contact-internal').load('_send_contact_form.php', { email: _email, msg: _msg }, function(){
                
                $('#contact-h2').html('Thanks!').effect("pulsate", { times: 1 }, 1200 );
                
                //$(this).effect("highlight", {}, 1600); 
                $.scrollTo( $(this), 1000 );
                $(this).hide().slideDown('slow');
                
            });
            return false;
        }
        
        
            
        
    });
    
    
    $('#scoreboard').bind('mouseover', function(){
        
        _is_on_scoreboard = true;
       // console.log( 'on_s:'+_is_on_scoreboard+', on_b:'+_is_on_bubble);
    });
    
    $('#big-bubble').bind('mouseover', function(){
        
        _is_on_bubble = true;
        //console.log( 'on_s:'+_is_on_scoreboard+', on_b:'+_is_on_bubble);
    });
    
    // hiding the bubble when mouse is neither on the scoreboard, or the bubble itself
    $('#scoreboard').bind('mouseleave', function(){
        
        _is_on_scoreboard = false;
        
        setTimeout( "if( !_is_on_bubble ) hide_bubble();", 200);
        
        //console.log( 'on_s:'+_is_on_scoreboard+', on_b:'+_is_on_bubble);
    });
    
    $('#big-bubble').bind('mouseleave', function(){
        
        _is_on_bubble = false;
        
        setTimeout( "if( !_is_on_scoreboard ) hide_bubble();", 200);
        
        //console.log( 'on_s:'+_is_on_scoreboard+', on_b:'+_is_on_bubble);
    });
    
    
    $('#scoreboard a').bind('mouseover', function(){
        
    
        _is_on_scoreboard = true; // links still show count as the entire scoreboard
        
        update_bubble( $(this) ); // trigger the right bubble position & txt
        
        //console.log( 'updating bubble ** on_s:'+_is_on_scoreboard+', on_b:'+_is_on_bubble);
        
    });
    

});


function hide_bubble(){
    
    time = 250;
    distance = 20;
    
    $('#big-bubble').animate({
                            left: '-=' + distance + 'px',
                            opacity: 0
                            }, 
                            time );
                            
    _bubble_visible = false;
}


function show_bubble( top_target ){
    
    time = 150;
    distance = 20;
    left_target = 287; // final px (left) position of bubble
    
    full_opacity = $.support.opacity ? 1 : 0.9; // if not supported (IE), than don't go all the way to 1, otherwise breaks PNG fix..
    
    
    
    
    if( !_bubble_visible )  // not just switching bubble, do get-into-position effect:
    {
    
        $('#big-bubble').css( {"top": top_target} );
        
        left_start = ( left_target - distance ) + "px";
    
        $('#big-bubble').css( {"left": left_start} );
    }
    else
        $('#big-bubble').stop(); // already visible. stop current animations
    
    
    $('#big-bubble').animate({
                            top: top_target     + 'px',
                            left: left_target   + 'px',
                            opacity: full_opacity
                            }, 
                            time);
                            
                            
    _bubble_visible = true;
}


function update_bubble( pointed_link ){
    
    var phrase  = pointed_link.html();
    var txt     = '';
    
    bubble = $('#big-bubble');
    
    bubble_content  = $('#big-bubble-txt');
    
    var _top = (pointed_link.position().top - 98); // gap between position of the link's top to the top of the floating bubble
    
    switch( phrase ){
        
        case 'Curiosity':   
                            txt = "\"Curiosity is the very basis of education<br />"
                                    +"and if you tell me that curiosity killed the cat,<br />"
                                    +"I say only the cat died nobly. \""
                                    +"<p>"
                                    +"- Arnold Edinborough, writer &amp; arts advocate"
                                    +"</p>";
                            break;
                            
        case 'Creativity':   
                            txt = "\"Big ideas come from the unconscious.<br />"
                                    +"This is true in art, in science, and in advertising.<br />"
                                    +"But your unconscious has to be well informed, <br />or your idea will be irrelevant. "
                                    +"Stuff your conscious mind with information, then unhook your rational thought process.\""
                                    +"<p>"
                                    +"- <a target='_blank' href='http://en.wikipedia.org/wiki/David_Ogilvy'>David Ogilvy</a>, the advertising legend"
                                    +"</p>";

                            break;
                            
        case 'Problem solving':   
                            txt = "\"There's never enough to go around: time, money, people. "
                                 +"That's a good thing. "
                                 +"Instead of freaking out about these constraints, embrace them.<br />"
                                 +"Let them guide you. Constraints drive innovation <br />"
                                 +"and force focus. Instead of trying to remove them,<br /> "
                                 +"use them to your advantage.\""
                                 +"<p>"
                                 +"- 37 Signals' book <a target='_blank' href='http://gettingreal.37signals.com/ch03_Embrace_Constraints.php'>\"Getting Real\"</a>"
                                 +"</p>";

                            break;
                            
        case 'Coding':
                            txt = "(on coding long hours to the night)<br />"
                                +"\"...the truth is that these programming blitzkriegs<br />"
                                +"are some of my favorite periods in life.<br />"
                                +"<p>Under the right conditions, writing software " 
                                +"is so intensely pleasurable it should be illegal.\"</p>"
                                +"<p>"
                                +"- <a target='_blank' href='http://duartes.org/gustavo/blog/post/lucky-to-be-a-programmer'>Gustavo Durate</a>, one happy programmer"
                                +"</p>";
                                
                            break;
                                
        case 'Sucking up':
                            txt = "\"I don't want somebody sucking up to me because they think I am going to help their career. "
                                +"<br />I want them sucking up to me because <br />they genuinely love me.\""
                                +"<p>"
                                +"- Michael, \"The Office\" (TV show)"
                                +"</p>";
                                
                            break;


        
        
        
        default         :   //bubble.hide();
                            break;
                            
                            
                            
    }
    
    bubble_content.html( txt );
    
    show_bubble( _top );
    
}



function emailCatch( field )
//---------------------------
{
     var strValue = $( field ).val();
     var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
     
     if( strValue.length <=1 )
                 return "I will need your email address to reply...";

     if(!objRE.test(strValue) )
                 return "email address seems invalid. Please check...";
        
     
     return '';
  
}

