var Cookies = {};
Cookies.set = function(name, value){
     var argv = arguments;
     var argc = arguments.length;
     var expires = (argc > 2) ? argv[2] : null;
     var path = (argc > 3) ? argv[3] : '/';
     var domain = (argc > 4) ? argv[4] : null;
     var secure = (argc > 5) ? argv[5] : false;
     document.cookie = name + "=" + escape (value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
};

Cookies.get = function(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	var j = 0;
	while(i < clen){
		j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return Cookies.getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0)
			break;
	}            
	return null;
};

Cookies.clear = function(name) {
  if(Cookies.get(name)){
    document.cookie = name + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
};

Cookies.getCookieVal = function(offset){
   var endstr = document.cookie.indexOf(";", offset);
   if(endstr == -1){
       endstr = document.cookie.length;
   }
   return unescape(document.cookie.substring(offset, endstr));
};

Ext.onReady(function() {
	Ext.getBody().addClass('x-gray');
	Ext.QuickTips.init();
     /* Ext.apply(Ext.QuickTips.getQuickTip(), {
          maxWidth: 500
      });*/
	//Ext.getBody().on("mousemove",function() {montre();})

	//Ext.get("men1").on('mouseover',function() {montre('smenu1');},{stopEvent :true});
	//montre();
	/*Ext.get("men").on('mouseover',montre,{stopEvent :true});
	Ext.get("men1").on('mouseover',function() {montre('smenu1');},{stopEvent :true});
	Ext.get("men1").on('mouseout',function() {montre();},{stopEvent :true,delay:500});
	Ext.get("men2").on('mouseover',function() {montre('smenu2');},{stopEvent :true});
	Ext.get("men2").on('mouseout',function() {montre();},{stopEvent :true,delay:500});
	
	Ext.get("men4").on('mouseover',montre,{stopEvent :true});
	Ext.get("men5").on('mouseover',montre,{stopEvent :true});
	
	
	montre();
	
	*/
	playPub()
	
});
	

WIN_IMG = null;



/*------------------------------MENU------------------------------------------------*/
/*


function changeMenu(monDiv) {
	//alert(Element.childElements(monDiv)[0].style.top);
	Element.childElements(monDiv)[0].style.top = '-25px';

}
function initMenu(monDiv) {
	//alert(Element.childElements(monDiv)[0].style.top);
	Element.childElements(monDiv)[0].style.top = '0px';
}
*/

/*---------------------------- PUBLICITE ------------------------------------------*/
	imgEffects = {currentID:null,img:[{id:'asia',serial:1,intance:null},
			{id:'imprim',serial:1,intance:null},
			{id:'familia',serial:1,intance:null},
			{id:'largueze',serial:1,intance:null},
			{id:'perez',serial:1,intance:null},
			{id:'pizza',serial:1,intance:null},
			//{id:'prev',serial:1,intance:null},
			{id:'rg',serial:1,intance:null},
			{id:'securite',serial:1,intance:null},
			{id:'anglais',serial:1,intance:null},
			{id:'lys',serial:1,intance:null}]};
	MAX = 0;
	MIN = 10000;
	function playPub() {
		var URL = "./img/pub/mini/";
		var last = "mini.jpg";
		for (var i =0; i < imgEffects.img.length; i++ ) {
			imgEffects.img[i].serial = Math.random();
			imgEffects.img[i].instance = new Image();
			imgEffects.img[i].instance.src = URL+imgEffects.img[i].id+last;
			if (imgEffects.img[i].serial < MIN)
				MIN = imgEffects.img[i].serial;
			if (imgEffects.img[i].serial > MAX)
				MAX = imgEffects.img[i].serial;
		}

		SortArrayObject(imgEffects.img);
		imgEffects.currentID  = 0;
		//setTimeout(play,3000);
		
		// générations des images du bas
		var body = Ext.get("sponsorsBottom");
            var t = new Ext.Template(
                  '<img alt="publicité" id="feet_{id}" src="./img/pub/feet/{id}.jpg"></img>'
              );
            
            var tp = new Ext.QuickTip({
               elements:'header,body',
               trackMouse : true,
               maxWidth: 500,
               maxHeight : 500
            });
		Ext.each(imgEffects.img,function(item) {
               t.append("sponsorsBottom",item);
               tp.register({
                  target : "feet_"+item.id,
                  text:"Cliquez pour voir notre sponsor."
               })
               Ext.get("feet_"+item.id).on("click",showImg);
            },this);
            
            
            
            
            var task = {
                  run: function(){
                     Ext.get("mypub").dom.src = imgEffects.img[imgEffects.currentID].instance.src;
                     imgEffects.currentID = imgEffects.currentID+1;
                     if (imgEffects.currentID == imgEffects.img.length)
                           imgEffects.currentID = 0;
                  },
                  interval: 3000,
                  scope : this
              }
              Ext.TaskMgr.start(task);
            
	}
      
      function showImg(e) {
         var cmp = e.getTarget();
         var url = cmp.getAttribute("src");
         var newUrl = url.substring(url.lastIndexOf("/")+1,url.length);
         window.open("publicite.html?"+newUrl,"Notre sponsor","status=yes,toolbar=no,menubar=no,location=no,resizable=false,width=450,height=500");
         
      }
      
	function SortArrayObject(monArray) {
		var tab = new Array();
		var lg = monArray.length;
	    for (var i = 1; i < lg; i++) {
	        /* stockage de la valeur en i */
	        var x = monArray[i];

	        /* recherche du plus petit indice p inférieur à i tel que t[p] >= t[i] */
	        for(var p = 0; monArray[p].serial < x.serial; p++);

	        /* p pointe une valeur de t supérieure à celle en i */

	        /* décalage avant des valeurs de t entre p et i */
	        for (var j = i-1; j >= p; j--) {
	            monArray[j+1] = monArray[j];
	        }

	        /* insertion de la valeur stockée à la place vacante */
	        monArray[p] = x;
	    }
	}



/*-------------------------------------------- MENU ----------------------------------------------------------*/

function montre(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=3; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
	if (d) {d.style.display='block';}
}


