//onload
$(function(){
    Ztl.stack();
    Ztl.images();
    Ztl.forms();
 });

var Ztl = {
    _z: 1,
    /**
    * stack
    **/
    stack: function() {
        $("#stack img").each(function(i) {
            $(this).css({top: Ztl.random(80), left: (i == 0) ? 0 : Ztl.random(55)});
            $(this).bind("click", function() {
                $(this).css("z-index", ++Ztl._z);
                $("#stacker").attr("src", $(this).attr("src").replace(/_small/,"")).removeAttr("width").removeAttr("height");
                $("#images p").text($(this).attr("alt"));
            });
        });
    },
    
    /**
    * image
    * moves images in weblog posts to the right side
    **/
    images: function() {
        $(".imgs .sample").each(function(i) {
            /* var tmp = $(this).parent().each(function() {
                return (this.offsetTop);
            }); */
            $(this).appendTo($("#blogimages"));
            $(this).addClass("sample");
            $(this).css("padding-right", Ztl.random(100));
            $(this).css("padding-left", Ztl.random(20));
        });
        
        Ztl.imgDescription();
    },
    
    /**
    * forms
    *
    **/
    forms: function() {
        // newsletter field modifications
        $("#newsletter-form").bind("focus", function() {
            if (this.value == "_________@_________") {
                $(this).val("");
            }
        });
        $("#newsletter-form").bind("blur", function() {
            if (this.value == "") {
                $(this).val("_________@_________");
            }
        });
        
        //go buttons submit
        $("#misc .go").bind("click", function() {
            $(this).parent().submit();
        });
    
    },
    
    /**
    * imgDescription
    * extracts attribute alt and displays under image
    **/
    imgDescription: function() {
        $("#blogimages img").each(function() {
            $(this).after("<p class=\"sample\">" + $(this).attr("alt") ? $(this).attr("alt") : "" + "</p>")
        });
    },
    
    /**
    * random
    * generates random number
    **/
    random: function(number) {
        return (Math.floor (Math.random() * number + 1));
    }
}

/**
 * log function
 **/
log = function(msg) {
    this.msg = !msg ? "undefined" : msg;
    if (typeof msg == "object") {
        var tmp = "", i, v;
        for (var i in this.msg) {
            if (this.msg.hasOwnProperty(i)) {
                v = this.msg[i];
                switch (typeof v) {
                    case "undefined":
                        break;
                    default:
                        tmp += i+": <strong>"+v+"</strong><br />";
                }
            } else {
                tmp += "<strong>"+i+"</strong><- " + v + "<br />";
            }
        }
        this.msg = tmp;
    } else {
        this.msg = "<strong>" + this.msg + "</strong><hr />";
    }
    if (!document.getElementById("netural-log")) {
        var logDiv = document.createElement("div");
        logDiv.id = "netural-log";
        logDiv.style.position = "absolute";
        logDiv.style["z-index"] = "100";
        logDiv.style.background = "#fff",
        logDiv.style.color = "#000",
        logDiv.style.padding = "15px",
        logDiv.style.fontSize = '1.2em',
        logDiv.style.overflow =  "auto"
        document.body.appendChild(logDiv);
    }
    var d = new Date();
    document.getElementById("netural-log").innerHTML = d + " " +this.msg + document.getElementById("netural-log").innerHTML;
}

/**
 * LAGACY
 **/

var imgWindow = "";

function openPopup(img, width, height) {
   if (img && width && height) {
      width = Math.min(width + 36, 640);
      height = Math.min(height + 30, 480);
      if (imgWindow.location && !imgWindow.closed)
         imgWindow.close();
      imgWindow = window.open(img, "imgWindow" + width + height, "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height);
      // imgWindow.focus();
   }
}
