// script Copyright GESTICOM

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

var gcDefile = {

  init : function() {
    $("#defile").cycle({ 
        fx: 'scrollLeft', 
        speed:    8000, 
        timeout:  2000
    });
  }
};


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("img.zoomable").css("cursor", "pointer").click(this.onClick);
    $(document.body).append("<div id='overlay' />");
    $("#overlay").css( {
      position : 'absolute',
      display : 'none',
      top : '0',
      left : '0',
      opacity : 0
    });

    $("#global").append("<div id='mon_image' />");
    $("#mon_image").hide();
  },
  onClick : function() {
    var h = document.body.clientHeight;
    var w = document.body.clientWidth;
    var image = this;
    $("#overlay").css('height', h + 'px')
         .css('width', w + 'px')
         .show()
         .fadeTo(300, 0.8,
         function() {
          gcImage.show(image);
         });   
    //Image.show(this);
  },
  show : function(image) {
    var nom_image = $(image).attr("image_name");
    var h = $(image).attr("image_height");
    var w = $(image).attr("image_width");
    var x = document.body.scrollLeft + document.documentElement.scrollLeft + (document.body.scrollWidth-w)/2;
    if (x < 0) {
      x = 0;
    }
    var y = document.body.scrollTop + document.documentElement.scrollTop + (document.body.scrollHeight-h)/2;
    if (y < 0) {
      y = 0;
    }

    $("#mon_image").css( {
      position : 'absolute',
      top :  y+ 'px',
      left : x+ 'px',
      zIndex : '1001'
    }).html("<img src='" + nom_image + "' >")
      .click(gcImage.hide).fadeIn("slow");
  },
  hide : function() {
    $(this).hide();
    $("#overlay").fadeTo(300,0, function() {
      $(this).hide();
      } );
    
  }
};
