// script Copyright GESTICOM

$(document).ready(function() {
  
  //alert("coucou");
  //gcMenu.init();
  gcImage.init();
  
  //var pageTracker = _gat._getTracker("UA-2980333-2");
  //pageTracker._initData();
  //pageTracker._trackPageview();

});

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);
	$("#global").after("<div id='mon_image' />");
    $("#mon_image").hide();
    $("#global").after("<div id='overlay' />");
    $("#overlay").css( {
      position : 'absolute',
      display : 'none',
      top : '0',
      left : '0',
      opacity : 0
    });

    
  },
  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);
         });   
    //gcImage.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;
    }
	//alert("x="+x+", y="+y);
    $("#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();
      } );
    
  }
};
