// script Copyright GESTICOM

$(document).ready(function() {
  gcMenu.init();
  gcImage.init();
  gcDefile.init();
});

var gcDefile = {
  init : function() {
    $("#defile").cycle({ 
        fx: 'scrollUp',
        speed: 2000,
        timeout: 10000
    });
  }
};

var gcMenu = {
	init : function() {
		$("#sidebar").find("h3").wrapInner("<a href='javascript:void(0);'></a>").click(this.onClick)
                 .end().find("div.selected").each( function() {
                    gcMenu.create(this);
                 });
	},
	onClick : function() {
  	// erase previous menu
    $("#popup_menu").parent().removeClass("selected").end().remove();
  
    // and create new one
  	var m = $(this).parent().addClass("selected").get();
    gcMenu.create(m);
	},
	create : function(m) {
	  $(m).find("ul").clone().addClass("popup_menu").attr("id", "popup_menu").appendTo(m);
  }
};

var gcImage = {
  init : function() {
    $("#content").find("a.zoomable").css("cursor", "pointer").click(this.onClick);
    $(document.body).append("<div id='overlay'></div>");
    $("#overlay").css( {
      position : 'absolute',
      display : 'none',
      top : '0',
      left : '0',
      width: '100%',
      height: '100%',
      opacity : '0'
    }).click(function() {
      gcImage.hide();
    });

    $("#global").append("<table id='mon_image'><table>");
    $("#mon_image").hide();
  },
  onClick : function() {
    var image = this;
    gcImage.show(image);
    return false;
  },
  show : function(image) {
    var src_image = $(image).attr("href");
 
    $("#mon_image").css( {
      position : 'absolute',
      display: 'none',
      top :  '0',
      left : '0',
      zIndex : '-1'
    }).html("<tr><td><img src='" + src_image + "' ></td></td>")
      .click(gcImage.hide).show('fast', function() { 
          $("#overlay").show().fadeTo(300, 0.8, function() {
             
              var x = 0;
              var y = 0;
              var w = $("#mon_image").width();
              var h = $("#mon_image").height();
              if (w > 0) {
                x = document.body.scrollLeft + document.documentElement.scrollLeft + (document.body.scrollWidth-w)/2;
                if (x < 0) x = 0;
              }
              if (h > 0) {
                var y = document.body.scrollTop + document.documentElement.scrollTop + (document.body.scrollHeight-h)/2;
                if (y < 0) y = 0;
              }
              $("#mon_image").css( {
                  top :  y+ 'px',
                  left : x+ 'px',
                  zIndex : '1001'
              }).fadeIn("slow");                  
             }); 
      });

  },
  hide : function() {
    $("#mon_image").hide();
    $("#overlay").fadeTo(300,0, function() {
      $(this).hide();
      } );
    
  }
};
