/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
 // jQuery.fx.off = true;
 
function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;
    
    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";
    
    if(typeof(arr) == 'object') { //Array/Hashes/Objects 
        for(var item in arr) {
            var value = arr[item];
            
            if(typeof(value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}

// definitions


var AJAX_LOADING = '<div class="AJAX_LOADING" style="text-align: center; padding: 10px 5px; "><img src="./_images/loading2.gif" alt="Loading..." /></div>';
var colors_dull = new Array("#383020", "#303820", "#203038", "#382020", "#282030", "#383820");
// var colors_vivid = new Array("#ffcc00", "#ccff00", "#00ccff", "#ff0000", "#00ff00", "#0000ff", "#ff00cc");
var colors_vivid = new Array("#ff0000", "#ffcc00", "#ffff00", "#ccff00", "#00ff00", "#00ffcc", "#00ffff", "#00ccff", "#0000ff", "#cc00ff", "#ff00ff", "#ff00cc");
var latest_url = '';
var latest_overlay = '';   // this is only the query string, ajax.php? has to be added
var indexLoaded = false;
var overlaySolid = false;
var FADEOUT_SEMI = 0.3;    // opacity for semi-fadeout

function ajax_loading_format( sz ) { 
   return '<div class="AJAX_LOADING" style="text-align: center; padding: 10px 5px; "><img src="./_images/loading2.gif" alt="Loading..." /><div style="padding-top: 5px; text-align: center;">'+ sz +'</div></div>';
}

function load_body( url, options ) { 
    setTimeout( function(){ close_overlay(); }, 1 );
    
    $("#blank").stop(true, true).fadeIn(500);
    $("#body-content").load("loader.php?"+url, options, function(){ 
         $("#blank").stop(true, true).fadeOut(500, function(){
            if (!indexLoaded) {
               $("#init-text").text("Fetching page...");
               indexLoaded = true;
            }
         }); 
      });
    
    $(".navbar-button-menu:visible").slideUp(200);
    $("div.bubble").stop(true, true).fadeOut(500).remove();
    
}
function load_overlay( url, options ) {
    latest_overlay = url;
    $("body").css("overflow", "hidden");
    $("#overlay-black").fadeIn(500);
    $("#body-overlay").fadeIn(500, function(){ 
      $("#overlay-content").load("ajax.php?"+url, options, function(){ 
         if (overlaySolid)
            $(this).find(".overlay-fadeout").css("opacity", 0.0);
            $(this).find(".overlay-fadeout-semi").css("opacity", FADEOUT_SEMI);
      }); 
    }); 
    $("#overlay-content").html(ajax_loading_format('Retrieving overlay...'));
}
function close_overlay() { 
    $("body").css("overflow", "auto");
    window.scrollBy(0, 1);
    window.scrollBy(0, -1);
      
    $("#body-overlay").stop(true, true).fadeOut(500);
    $("#overlay-black").stop(true, true).fadeOut(500);
    $("div.bubble").stop(true, true).fadeOut(500).remove();
}
function load_bubble( url, options, x, y, id ) { 
    id = ( id != undefined ) ? ' id="'+id+'"' : '';
    $("body").append('<div'+ id  +' class="bubble"></div>');
    $("div.bubble:last").load("ajax.php?"+url, options, function(e){ 
        $(this).css("top", y + "px").css("left", x + "px").stop(true, true).fadeIn(500); 
    });
}
function load_bubble_html( c, x, y, id ) { 
    id = ( id != undefined ) ? ' id="'+id+'"' : '';
    $("body").append('<div'  +' class="bubble"></div>');
    $("div.bubble:last").html(c)
        .css("top", y + "px")
        .css("left", x + "px")
        .stop(true, true).fadeIn(500);
}
  
$(document).ready(function(){
    //  0-400ms delay
    setTimeout(function(){
      load_body( ($("#body-content").attr("page")===undefined) ? "id=home.index" : "id="+$("#body-content").attr("page"), {});
    }, Math.round(Math.random()*400));
 
    $(window).keyup(function(e) {
      if (e.keyCode==27)
         close_overlay();
   });
   $("body").live("click", function(){
        // $("div#viewsubcategory-content:visible").fadeOut(200);
   });
   
   $("#banner-sitename").click(function(){
      window.location.href = '';
   });
    
    $(".overlay-fade, .overlay-reload, .overlay-close").live("mouseleave", function(){
      $("div.bubble").stop(true, true).fadeOut(200, function(){ $(this).remove(); });
    });    
    $(".overlay-close").live("mouseenter", function(e){
      var pos = $(this).offset();
      load_bubble_html('<span class="title-smallest">Close</span>', pos.left - 7, pos.top - 40);
    });
    $(".overlay-reload").live("mouseenter", function(e){
      var pos = $(this).offset();
      load_bubble_html('<span class="title-smallest">Refresh</span>', pos.left - 7, pos.top - 40);
    });
    $(".overlay-fade").live("mouseenter", function(e){
      var pos = $(this).offset();
      load_bubble_html('<span class="title-smallest">Loupe View</span>', pos.left - 7, pos.top - 40);
    });
    $(".overlay-close").live("click", function(){
        close_overlay();
    });
    $(".overlay-reload").live("click", function(){
      load_overlay(latest_overlay, {});
    });
    $(".overlay-fade").live("click", function(){
      var overlay_black = $("#overlay-black");
      var overlay_black_opacity = overlay_black.css("opacity");
      
      if (!overlaySolid) { 
         overlay_black.stop(true, true).animate({ 
            opacity : 1.0,
            backgroundColor : "#000000"
         }, 500);
         $(".overlay-fadeout").stop(true, true).animate({ opacity : 0.0 }, 500);
         $(".overlay-fadeout-semi").stop(true, true).animate({ opacity : FADEOUT_SEMI }, 500);
      }
      else { 
         overlay_black.stop(true, true).animate({ 
            opacity : 0.8,
            backgroundColor : "#111111"
         }, 500);
         $(".overlay-fadeout").stop(true, true).animate({ opacity : 1.0 }, 500);
         $(".overlay-fadeout-semi").stop(true, true).animate({ opacity : 1.0 }, 500);
      }
      
      overlaySolid = !overlaySolid;
    });
    
    $("[jq^='loader//']").live("click", function(){
        latest_url = 'http://digitalexpander.net/index.php?'+$(this).attr("jq").substring(8);
        // $("#extract-url").val(latest_url);
        
        close_overlay();
        
        load_body($(this).attr("jq").substring(8), {}); 
    });
    $("[jq^='overlay//']").live("click", function(){
        load_overlay($(this).attr("jq").substring(9), {}); 
    });
    $("[jq^='bubble//']").live("click", function(e){
        load_bubble($(this).attr("jq").substring(8), {}, e.pageX, e.pageY); 
    });
    $("div.bubble").live("click", function(){
        $(this).stop(true, true).fadeOut(500, function(){ $(this).remove(); });
    });
    
    $("[bghcolor]").live("mouseenter", function(){
      if ($(this).data("old-bgcolor")===undefined)
         $(this).data("old-bgcolor", $(this).css("backgroundColor"));
      $(this).stop(true, true).animate({
         backgroundColor : $(this).attr("bghcolor")
      }, 500);
    });
    $("[bghcolor]").live("mouseleave", function(){
      $(this).stop(true, true).animate({
         backgroundColor : $(this).data("old-bgcolor")
      }, 500);
    });
    
    $(".link").live("mouseenter", function(){
      if ($(this).data("old-color")===undefined)
            $(this).data("old-color", $(this).css("color"));
        $(this).stop(true, true).animate({
            color:(($(this).attr("hcolor")==undefined)?'#ffffff':$(this).attr("hcolor"))
        }, 200);
    });
    $(".link").live("mouseleave", function(){
        $(this).stop(true, true).animate({
            color: $(this).data("old-color")
        }, 500);
    });
    
    $(".anthology-col-td").live("click", function(){
      var td = this;
      $("div.anthology-content:visible").each(function(){
         if ($(td).attr("name") != $(this).attr("name"))
         $(this).stop(true, true).slideUp(200, function(){
            $("div.anthology-content[name="+ $(td).attr("name")  +"]").stop(true, true).slideDown(500);
         });
      });
    });
});