var delim = "|";
$(document).ready( function() { 
    var isMoving = false;
    $('#words a').hover( function() {
        isMoving = true;
        if ( $(this).hasClass("left") ) {
            move("right"); 
        } else if ( $(this).hasClass("right") ) {
            move("left");    
        }   
    }, function() {
        isMoving = false; 
    });
    function move(dir) { 
        if (isMoving == true) {
            var rate   = 7;
            if (dir == "left") { rate *= -1; }
            var position = parseInt($("#words strong").css("marginLeft"));
            var newPosition = position + rate;
            /* LEFT BOUNDARY */
            if ( newPosition > 60 ) { newPosition = 60; }
            /* RIGHT BOUNDARY */
            var containerWidth = $("#words").width();
            var wordsWidth = $("#words strong").width();
            if ( newPosition < (containerWidth - wordsWidth - 60) ) { newPosition = containerWidth - wordsWidth - 60; }
            $("#words strong").css({'marginLeft':newPosition}).delayCallback(10, function() {
                move(dir);
            });                  
        } else {
            // do nothing
        }        
    }
    $("input:text").focus( function() {
        if ( $(this).val() == $(this).attr('defaultValue') ) {
            $(this).val("");
        }
    });
    $("input:text").blur( function() {
        if ( $(this).val() == "") {
            $(this).val( $(this).attr('defaultValue') );
        }    
    });
    $("#describe .word").click( function() {
        var value = $("#addWord").val();
        var ip = $("#ip").text();
        value = value + delim + ip;
        var mode  = "word";
        database(mode, value);    
    });
    $("#foot .email").click( function() {
        var value = $("#addEmail").val();
        var ip = $("#ip").text();
        value = value + delim + ip;
        var mode = "addEmail";
        database(mode,value);
    });    
});
function database(mode,value) {
    var url = "http://"+window.location.hostname+"/deezine.php";
    var requestString = "&mode="+mode+"&value="+value;
    var contactEmail = "us@deezine.ca"
    message("thinking...");
    $.ajax({  
        type: "POST",  
        url: url,  
        data: requestString, 
        success: function(text) {
            if (mode == "word" && text == "true" ) {
                var ip = $("#ip").text();
                return database("update",ip);
            } else if ( mode == "update" ) {
                $("#words strong").text(text);
                message("Your word has been added; thanks!");    
            } else {
                message(text);
            }
        },
        error: function() {
            if (mode == "email")       { message("Your email was not added.  Please email <a href='mailto:"+contactEmail+"'>"+contactEmail+"</a>."); }
            else if (mode == "word")   { message("Your word was not submitted.  Please email <a href='mailto:"+contactEmail+"'>"+contactEmail+"</a>. "); }
            else if (mode == "update") { message("Press f5 or alt-r to see your words!  ")}
            else { message("Error"); }        
            return false;
        }  
    }); 
}
function message(text) {
    if ( $("#addWordResponse").html() != " " ) {
       $("#addWordResponse").fadeTo(100,0, function() {
            $("#addWordResponse").html(text).fadeTo(200,1);  
       });
    } else {
        $("#addWordResponse").fadeTo(1,0, function() {
            $("#addWordResponse").html(text).fadeTo(200,1);            
        })            
    }
}
$.fn.delayCallback = function(time, callback){
    jQuery.fx.step.delay = function(){};
    return this.animate({delay:1}, time, callback);
}
