var LANG_CORE_MSG_SUPP = 'Vous êtes sur le point de supprimer une entrée définitivement. Voulez-vous continuer ?';
/*
function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}
*/


function confirmSubmit( message )
{
    var agree=confirm(message);
    if (agree)
      return true ;
    else
      return false ;
}

function openWin( windowURL, windowName, windowFeatures )
{ 
    return window.open( windowURL, windowName, windowFeatures ) ; 
}

function openModifier(lien,wi,he)
{
    newWindow=openWin(lien,'Modification','width='+wi+'px,height='+he+'px,top=50,left=100,topmargin=0,toolbar=0,location=0,directories=0,status=no,menuBar=0,scrollBars=0,resizable=1');
    newWindow.focus()
}

// save_call_back de tinyMCE
function save_fcn(element_id, html, body) {

  return html;
  var tf = document.getElementById('tiny_form');
  var newnode = document.createElement('input');
  newnode.setAttribute('type','hidden');
  newnode.setAttribute('name','enregistrer');
  newnode.setAttribute('value','1');
  tf.appendChild(newnode);

  return html;
}

function panel_resize()
{
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
    }
    
    if( total = document.getElementById('panel_total'))
    {
        total.style.width = String(myWidth-20) + 'px';
        total.style.height = String(myHeight-35) + 'px';
       
        topdroit = document.getElementById('panel_top_droit');
        topdroit.style.width = String(myWidth-146) +'px';
        
        pcenter = document.getElementById('panel_center');
        pcenter.style.height = String(myHeight-70) +'px';
        
        pcenterint = document.getElementById('panel_center_int');
        pcenterint.style.width = String(myWidth-45) +'px';
    }
    
}



//Sons of Suckerfish
//http://www.htmldog.com/articles/suckerfish/
//pour pseudo-class focus, pour IE
sfFocus = function() {
    var sfEls = document.getElementsByTagName("INPUT");
    for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onfocus=function() {
            this.className+=" sffocus";
        }
        sfEls[i].onblur=function() {
            this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", sfFocus);

//AJAX
var xmlHttp
var ajax_inner
var ajax_list = new Array()
var ajax_list_started, ajax_counter=0

function ajax_action(url,ajax_in)
{
    document.body.style.cursor="Wait";
    ajax_inner = ajax_in;
    
    new Ajax.Updater(ajax_in,url,
    {
        method: 'get',
        evalScripts: true,
        onComplete: function(requester) {
            document.body.style.cursor="";
            
            if( ajax_in == 'menu_ajax')
                ajax_action('gestion_menu.php?ajax_menu_refresh=1','menu');
            if( ajax_list && ajax_list_started )
                continue_ajax();
            
        }
    });
}
function continue_ajax()
{
    ajax_counter++;
            
    var i = ajax_counter;
    var total = ajax_list.length;

    if( i<total )
        ajax_action(ajax_list[i],ajax_inner);
    else
        ajax_list_started = false;
}
/*
function stateChanged() 
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    { 
        document.body.style.cursor="";
        document.getElementById(ajax_inner).innerHTML=xmlHttp.responseText 
        
        //si le text retrouner contient du JS, on l'exécute !
        var txt = new String()
        txt = xmlHttp.responseText
        var pos = txt.indexOf('#JS#');
        if( pos>0  )
        {
            pos2 = txt.indexOf('#/JS#',pos)
            eval(txt.slice(pos+4,pos2));
            
        }
            
        png_test()
        //alert(ajax_inner)
        if( ajax_inner == 'menu_ajax')
        {
            ajax_action('gestion_menu.php?ajax_menu_refresh=1','menu')
        }
        
        if( ajax_list && ajax_list_started )
        {
            
            ajax_counter++
            
            var i = ajax_counter;
            var total = ajax_list.length;
            //alert(total );
            if( i<total )
            {
                ajax_action(ajax_list[i],ajax_inner);
            }else
                ajax_list_started = false;
        }
    } 
} 
* */
function png_test()
{
    var arVersion = navigator.appVersion.split("MSIE")
    var version = parseFloat(arVersion[1])
    
    if ((version >= 5.5) && (document.body.filters)) 
    {
        
       for(var i=0; i<document.images.length; i++)
       /*for(var i=(document.images.length-1); i>=0; i--)*/
       {
             
          var img = document.images[i]
    
          if (img.src.indexOf('cp_')==-1 )
          {
              
              var imgName = img.src.toUpperCase()
              if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
              {
                 var imgID = (img.id) ? "id='" + img.id + "' " : ""
                 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                 var imgStyle = "display:inline-block;" + img.style.cssText 
                 if (img.align == "left") imgStyle = "float:left;" + imgStyle
                 if (img.align == "right") imgStyle = "float:right;" + imgStyle
                 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                 var strNewHTML = "<span " + imgID + imgClass + imgTitle
                 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
                 img.outerHTML = strNewHTML
                 i = i-1
              }
                
           }
       }
    }
}
/*
function interchange( id )
{   
    var e1 = document.getElementById(id + '_1');
    var e2 = document.getElementById(id + '_2');
    //var y = get_y();
    
    if( e1.style.display == 'none' )
    {
        e1.style.display = ''
        e2.style.display = 'none'
        
        
    }else if( e2.style.display == 'none' )
    {
        e2.style.display = ''
        e1.style.display = 'none'
        
    }
    
    //window.scroll(0,y);
    
}*/

function interchange( id )
{
    var e1 = $(id + '_1');
    var e2 = $(id + '_2');
    
    e1.toggle();
    e2.toggle();
    
}

//liste remarques
var liste_generale = [];

function add_liste_generale(txt)
{
    liste_generale[liste_generale.length] = txt;
    refresh_liste_generale();
}

function refresh_liste_generale()
{
    //alert('allo 54654');
    var div = $('liste_generale');
    var txt = '';
    if( div )
    {
        for( x=0; x<liste_generale.length; x++)
        {
            txt += '<li>'+ liste_generale[x] + '</li>\n';
        }
        div.update(txt);
    }
}

function get_y()
{
    if( window )
    {
        var scrOfX = 0, scrOfY = 0;
        if( typeof( window.pageYOffset ) == 'number' ) {
          //Netscape compliant
          scrOfY = window.pageYOffset;
          scrOfX = window.pageXOffset;
        } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
          //DOM compliant
          scrOfY = document.body.scrollTop;
          scrOfX = document.body.scrollLeft;
        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
          //IE6 standards compliant mode
          scrOfY = document.documentElement.scrollTop;
          scrOfX = document.documentElement.scrollLeft;
        }
        
    }
    return(scrOfY );
}

function set_fatale_btn_msg()
{
    var liste_fatale = document.getElementsByClassName("fatale_msg");
    liste_fatale.each(function(e){
        Event.observe(e,'click',function(event){
            if( !confirmSubmit(LANG_CORE_MSG_SUPP) )
                Event.stop(event);
        })
    })
}
Event.observe(window,'load',set_fatale_btn_msg);

var sortUl = {
	gestion_tab_no : 0,
	url : '',
	qs : '',
	initSort : function(ul){
		var nouvUL,li;
		
		if( undefined != ( li = $(ul).down('li') ))
		{
			do{
				//new Draggable(li.getAttribute('id'),{ revert: true});
				if( undefined != (liul = li.down('ul') ))
					sortUl.initSort(liul);
				li = li.next('li');
			}
			while( undefined != li );
		}
		
		Sortable.create(ul,{
			onUpdate: sortUl.callBack.bind(this), 
			handle: 'gestion_tab_handle'
			});
	},
	callBack : function(ul)	{
		post = Sortable.serialize(ul)
			+'&ul='+ul.id
			+'&gestion_tab_no=' + this.gestion_tab_no
			+'&'+this.qs;
		new Ajax.Request(this.url,{
			parameters: post
		});
		console.info(post);
	}
}
var container = {
	initSortables: function(){
		firstLi = $('container_gestion_div');
		
		//this.initTable(firstLi);
		//this.initBlock($('gestion_tab_total'));
		//this.initDrop();	
		/*Sortable.create($('container_gestion_div').down('ul'),{
			onUpdate: container.callBack, 

			handle: 'gestion_tab_handle',
			
			});*/
	},
	
	
	
	initTable :  function(div){
		Sortable.create($('table_body'),{
			onUpdate: container.callBack, 
			constraint: false,
			tag: 'tr'
			});
	},
	
	callBack : function(ul){
			//console.info(Sortable.serialize(ul)+'&allo=true');
		},
	initDrop : function(){
		Droppables.add('container_dev',{
			accept : 'container_item',
			onDrop: function(item){
//				Element.hide(item);
				item.style.left = '0';
				
				$('container_dev').appendChild(item);
			}
		});
	},
	
	initEvent : function(){
		if( $('container_gestion_div') )
		{
			this.initSortables();
	
		}
	}
	
}
Event.observe(window,'load',container.initEvent.bindAsEventListener(container), false);
