/*
  Module Multimages Synolia
  20/10/2006
  $Id: ajax.js,v 1.1 2006/10/22 22:53:20 vince Exp $
*/

// permet de déterminer le navigateur en fonction de nos besoins
var dom = net = iex = false;
if (document.getElementById) dom = true; // IE5+ ou Netscape6+/Mozilla
if (document.layers) net = true; // Netscape4 
if (document.all) iex = true; // IE4+

function getWindowSize(t)
{
  if (iex) {
    var windowWidth = document.body.offsetWidth;
    var windowHeight = document.body.offsetHeight;
  }
  else if (dom || net) {
    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;
  }
  else {
  var windowWidth = screen.width;
  var windowHeight = screen.height;
  }
  switch(t)
  {
    case 'h' : return windowHeight; break;
    case 'w' : return windowWidth; break;
  }
}



DIV = new Array();
PROD = new Array();

timer = null;
timerOn = null;

leftIndent = -10;
topIndent = -20;
shadowGap = 20;
shadowTop = -5;
loading = false;
hideTime = 1000
showTime = 100
increaseDif = 80

function Prod(o)
{
  this.obj = o
  this.shadow = null
  this.img = null
  this.objTop = this.obj.offsetTop + this.obj.offsetParent.offsetTop
  this.setShadow = function(S) { this.shadow = S }
  this.setImage = function(I) { this.img = I }
}


function Div(o,p,pth)
{
  this.obj = o
  this.parent = p
  this.path = pth
  this.shadow = null
  this.img = null
  this.imgShadow = null
  this.originalHeight = null  
  
  this.recursif = function(v)
  {
      this.obj.style.visibility = v;
      this.shadow.style.visibility = v;
      if(this.img) 
      { 
    	    this.img.style.left = parseInt(this.obj.style.left)+parseInt(this.obj.offsetWidth) +'px';
    	    this.img.style.top = parseInt(this.obj.style.top) +'px';        
          this.img.style.visibility = v;
      }
      if(this.imgShadow) 
      {
    	  this.imgShadow.style.left = parseInt(this.obj.style.left)+parseInt(this.obj.offsetWidth)+shadowTop +'px';
    	  this.imgShadow.style.top = parseInt(this.obj.style.top)+shadowTop +'px';          
        this.imgShadow.style.visibility = v;
      }
         
      this.parent.className = 'categorieOn';
      if(this.parent.offsetParent.id != "menu_0")
      {
  
          DIV[this.parent.offsetParent.id].recursif(v)
      }

  }
  this.getPath = function(pth)
  {
    pth += this.path +"_";   
    if(this.parent.offsetParent.id != "menu_0")
    {
        DIV[this.parent.offsetParent.id].getPath(pth);
    }
    else 
    {
      newPath = pth; 
    }
    return (newPath); 
  }  
  this.setShadow = function(S) { this.shadow = S }
  this.setImgShadow = function(S) { this.imgShadow = S }
  this.setImage = function(I) { this.img = I }
}

function HideAll()
{

  for (i in DIV)
  {
    DIV[i].obj.style.visibility = 'hidden';
    DIV[i].shadow.style.visibility = 'hidden';
    if(DIV[i].img) DIV[i].img.style.visibility = 'hidden';
    if(DIV[i].imgShadow) DIV[i].imgShadow.style.visibility = 'hidden';
    DIV[i].parent.className = 'categorie';
  }
  theLinks = document.getElementById('menu_0').getElementsByTagName('a');
  for (i in theLinks)
  {
    theLinks[i].className = 'mainCategorie';
  }
  HideImg()
}

function HideImg()
{
  for(i in PROD)
  {
    if(PROD[i].shadow) PROD[i].shadow.style.visibility = 'hidden';  
    if(PROD[i].img) PROD[i].img.style.visibility = 'hidden';    
  }
}

function adjustShadow(obj,shad,args)
{
  		el = document.getElementById(obj+'_'+args);
		  shadow = document.getElementById(shad+'_'+args)
      shadow.style.width = (el.offsetWidth+shadowGap)+'px';
      shadow.style.height = (el.offsetHeight+shadowGap)+'px';
      

	    flash  = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+(el.offsetWidth+shadowGap)+'" height="'+(el.offsetHeight+shadowGap)+'" id="shadow" align="middle">'
      flash += '<param name="allowScriptAccess" value="sameDomain" />'
      flash += '<param name="movie" value="images/design/shadow.swf" />'
      flash += '<param name="quality" value="high" />'
      flash += '<param name="wmode" value="transparent" />'
      flash += '<param name="FlashVars" value="width='+(el.offsetWidth)+'&height='+(el.offsetHeight)+'" />'
      flash += '<embed src="images/design/shadow.swf" quality="high" wmode="transparent" width="'+(el.offsetWidth+shadowGap)+'" FlashVars="width='+(el.offsetWidth)+'&height='+(el.offsetHeight)+'" height="'+(el.offsetHeight+shadowGap)+'" name="shadow" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
      flash += '</object>'
	        
      shadow.innerHTML=flash	    
}

function scrollDiv (args) 
{
  
		el = document.getElementById('menu_'+args);
		topGap = parseInt(document.getElementById('container').offsetTop)
	  elHeight = parseInt(el.style.height)
	  dif = (parseInt(el.style.top)+elHeight+topGap) - getWindowSize('h')
	  if(dif>0) 
	  {
      DIV['menu_'+args].originalHeight = el.style.height;
	    el.style.width = (parseInt(el.offsetWidth)+shadowGap)+'px'
	    el.style.height = (elHeight - (dif+increaseDif)) +'px'
	    el.style.overflow = 'auto';
	    
	    adjustShadow('menu','shadow',args) 
	    
	  }
	  else
	  {
        if((DIV['menu_'+args].originalHeight)) 
        { 
          DIV['menu_'+args].obj.style.height = DIV['menu_'+args].originalHeight; 
          DIV['menu_'+args].originalHeight =null; 
          scrollDiv (args); 
          adjustShadow('menu','shadow',args) 
        }
	  }
	  
}


function ajaxManager()
	{
	var args = ajaxManager.arguments;
	switch (args[0])
		{
		case "load_page":
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			}
			if (x)
				{
			x.onreadystatechange = function()
				{
				if (x.readyState == 4 && x.status == 200)
						{
						el = document.getElementById('menu_'+args[2]);
						el.innerHTML = x.responseText;
						el.style.height = el.offsetHeight+'px';
						
						if(args[3]) el.className = "link";
											
  						if(args[2]!=0)
  						{
  						  el.onmouseout = function () { timer = setTimeout("HideAll()",hideTime); };  
    						el.onmouseover = function () { clearTimeout(timer); DIV['menu_'+args[2]].recursif('visible'); };
  						  adjustShadow('menu','shadow',args[2])
        	    
        	      if(NAV_SHOW_CATEGORIES_IMG)
        	      {
            	    shadow = document.createElement('div'); 
            	    shadow.setAttribute('id','img_shadow_'+args[2]);
            	    document.getElementById('container').appendChild(shadow);        	    
          	    
            	    img = document.createElement('div'); 
            	    img.setAttribute('id','img_'+args[2]);
            	    document.getElementById('container').appendChild(img);
            	    img = document.getElementById('img_'+args[2])
            	    img.className = 'img';
            	    img.style.left = parseInt(el.style.left)+parseInt(el.offsetWidth) +'px';
            	    img.style.top = parseInt(el.style.top) +'px';
                  ajaxManager('load_img', 'ajax_getCategorieImage.php?parent_id='+args[2]+'&w=100&h=100&s=', args[2]);
                }

  						}		
  						
  						if(args[3]) { scrollDiv (args[2])}  	
  						
  						if(args[2]==0) 
  						{
  						  document.getElementById('container').style.top = parseInt(document.getElementById('container').offsetTop) + parseInt(document.getElementById('regions').offsetHeight) +'px';
  						}
					  }
					}
				x.open("GET", args[1], true);
				x.send(null);
				}
			break;
			
		case "load_img":
		
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			}
			if (x)
				{
			x.onreadystatechange = function()
				{
				if (x.readyState == 4 && x.status == 200)
						{
            					  
						el = document.getElementById('img_'+args[2]);
						el.innerHTML = x.responseText;
						el.style.height = el.offsetHeight+'px';
						
						
  						if(el.innerHTML) 
    						{
          	    shadow = document.getElementById('img_shadow_'+args[2])
          	    shadow.className = 'shadow';
          	    shadow.style.left = (el.offsetLeft+shadowTop)+'px';
          	    shadow.style.top = (el.offsetTop+shadowTop)+'px';  
    	    			adjustShadow('img','img_shadow',args[2]);
    	    			DIV['menu_'+args[2]].setImage(img)	
    	    			DIV['menu_'+args[2]].setImgShadow(shadow)
    	    			eval("clearTimeout(timerOn); timerOn = setTimeout(\"DIV['menu_"+args[2]+"'].recursif('visible')\",showTime)");
    	    			}
      	    	else el.style.visibility = 'hidden';
					  }
					}
				x.open("GET", args[1], true);
				x.send(null);
				}
			break;
			
		case "load_imgProd":
		
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			}
			if (x)
				{
			x.onreadystatechange = function()
				{
				if (x.readyState == 4 && x.status == 200)
						{
            						  
						el = document.getElementById('imgProd_'+args[2]);
						
						el.innerHTML = x.responseText;
						el.style.height = el.offsetHeight+'px';
						el.style.left = parseInt(el.style.left) - parseInt(el.offsetWidth) +'px';

						if(el.innerHTML) 
  						{
    						PROD['prod_'+args[2]].setImage(el)
          	    shadow = document.getElementById('imgProd_shadow_'+args[2])
          	    shadow.className = 'prodShadow';
          	    shadow.style.left = (el.offsetLeft+shadowTop)+'px';
          	    shadow.style.top = (el.offsetTop+shadowTop)+'px';   
          	    if (iex)
          	    {
          	      HideImg();
          	      el.style.visibility = 'visible';
          	      shadow.style.visibility = 'visible';           	      
          	    }
    	    			adjustShadow('imgProd','imgProd_shadow',args[2]);
    	    			PROD['prod_'+args[2]].setShadow(shadow)
  	    		  }
  	    		else el.style.visibility = 'hidden';
					  }
					}
				x.open("GET", args[1], true);
				x.send(null);
				}
			break;		

		case "load_imagePage":

		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

			}
			if (x)
				{

			x.onreadystatechange = function()
				{
				if (x.readyState == 4 && x.status == 200)
						{
						el = document.getElementById('iconContainer');
						el.innerHTML = x.responseText;							
					  }
					}
				x.open("GET", args[1], true);
				x.send(null);
				}
			break;			
				
	  case "load_image":
		
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			}
			if (x)
				{
			x.onreadystatechange = function()
				{
				if (x.readyState == 4 && x.status == 200)
						{
						el = document.getElementById('imageContainer');
						el.innerHTML = x.responseText;									
					  }
					}
				x.open("GET", args[1], true);
				x.send(null);
				}
			break;
			
				
		case "start_up":
			ajaxManager('load_page', 'ajax_getCategories.php?class=mainCategorie', '0');
			break;
		case "hide_menu":
		  if(args[1].offsetParent.id=='menu_0') timer = setTimeout("HideAll()",hideTime);
		  if(args[3])
		  {
  		    if(PROD['prod_'+args[3]])
  		    {		    
      		  if(PROD['prod_'+args[3]].img) 
      		  {
      		    PROD['prod_'+args[3]].img.style.visibility='hidden';
      		    PROD['prod_'+args[3]].shadow.style.visibility='hidden';
      		    
      		  }
      		}
		  }
		break;
		case "show_menu":
		if(document.getElementById('menu_'+args[2])) 
		{
		  clearTimeout(timer);	
		  if(!args[6]) scrollDiv(args[2]) 
		  if(NAV_SHOW_PRODUCTS || ( !NAV_SHOW_PRODUCTS && args[5]==0))
		  {
		   HideAll();
		   eval("clearTimeout(timerOn); timerOn = setTimeout(\"DIV['menu_"+args[2]+"'].recursif('visible')\",showTime)");
		  
  		  if(args[6])
  		  {
  		    if(PROD['prod_'+args[6]])
  		    {

      		  if(PROD['prod_'+args[6]].img) 
      		  {
      		    PROD['prod_'+args[6]].img.style.top = (parseInt(PROD['prod_'+args[6]].objTop) - parseInt(PROD['prod_'+args[6]].obj.offsetParent.scrollTop)) +'px';
      		    PROD['prod_'+args[6]].shadow.style.top = (parseInt(PROD['prod_'+args[6]].objTop) - parseInt(PROD['prod_'+args[6]].obj.offsetParent.scrollTop)) +shadowTop +'px';
      		    PROD['prod_'+args[6]].img.style.visibility='visible';
      		    PROD['prod_'+args[6]].shadow.style.visibility='visible';
      		  }
    		  }
    		}
		  }

	  }
	  else
	  {
		  clearTimeout(timer);	

		  if(NAV_SHOW_PRODUCTS || ( !NAV_SHOW_PRODUCTS && args[5]==0))
		  {	 
   	 
	    shadow = document.createElement('div'); 
	    shadow.setAttribute('id','shadow_'+args[2]);
	    document.getElementById('container').appendChild(shadow);
	    shadow = document.getElementById('shadow_'+args[2])
	    shadow.className = 'shadow';
	    shadow.style.left = (args[4]+args[1].offsetParent.offsetWidth+leftIndent+shadowTop)+'px';
	    shadow.style.top = (args[3]+args[1].offsetTop+topIndent+shadowTop)+'px';		
      		     
	    div = document.createElement('div'); 
	    div.setAttribute('id','menu_'+args[2]);
	    document.getElementById('container').appendChild(div);
	    div = document.getElementById('menu_'+args[2])
	    div.className = 'menu';
	    div.style.left = (args[4]+args[1].offsetParent.offsetWidth+leftIndent)+'px';
	    div.style.top = (args[3]+args[1].offsetTop+topIndent)+'px';
	    
	    DIV['menu_'+args[2]] = new Div(div,args[1],args[2])
	    DIV['menu_'+args[2]].setShadow(shadow)
	    
      ajaxManager('load_page', 'ajax_'+((args[5]==0)?'getCategories':'getProducts')+'.php?parent_id='+args[2]+'&cPath='+(DIV['menu_'+args[2]].getPath(''))+'&top='+div.offsetTop+'&left='+div.offsetLeft, args[2],args[5]);
       
      HideAll();
            
	    eval("clearTimeout(timerOn); timerOn = setTimeout(\"DIV['menu_"+args[2]+"'].recursif('visible')\",showTime)");
	    }
	  }
	  if(args[6]) 
	  {
  	      if(NAV_SHOW_PRODUCTS_IMG)
  	      {
  	        if(!(document.getElementById('imgProd_'+args[6])))
  	        {
    	        PROD['prod_'+args[6]] = new Prod(args[1])
        	    shadow = document.createElement('div'); 
        	    shadow.setAttribute('id','imgProd_shadow_'+args[6]);
        	    document.getElementById('container').appendChild(shadow);        	    
      	    
        	    img = document.createElement('div'); 
        	    img.setAttribute('id','imgProd_'+args[6]);
        	    document.getElementById('container').appendChild(img);
        	    img = document.getElementById('imgProd_'+args[6])
        	    img.className = 'imgProd';
  
        	    img.style.left = parseInt(args[1].offsetLeft) + parseInt(args[1].offsetParent.offsetLeft) + parseInt(args[1].offsetWidth) + 50 +'px';
        	    img.style.top = parseInt(args[1].offsetTop)+parseInt(args[1].offsetParent.offsetTop) - 10 +'px';
  
              ajaxManager('load_imgProd', 'ajax_getProductImage.php?parent_id='+args[6]+'&w=40&h=40&s=', args[6]);
            }
          }
	  }
		break;		
		}
	}

