////////////////////////////////////////////////////////////////////
////////Shortucuts vars/////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

var Dom = YAHOO.util.Dom;
var $ = YAHOO.util.Dom.get; //PROTOTYPE STYLE



///////////////////////////////////////////////////////////
//////////METHODS TO HANDLE DOM////////////////////////////
///////////////////////////////////////////////////////////	 
/* 
 * @deprecated
 */
 var getFrameWidth = function(){
     var h;
     if (document.all){h=document.body.clientWidth;}
     else{h=window.innerWidth;}
     return h;
}
 /* 
  * @deprecated
  */
var getFrameHeight = function(){
	var yScroll;
	if(window.innerHeight && window.scrollMaxY) {	
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if(document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
	}
	if(yScroll <= yDom.getViewportHeight()) {
		pageHeight = yDom.getViewportHeight();
	} else { 
		pageHeight = yScroll;
	}
	return pageHeight;
}
///////////////////////////////////////////////////////////
//////////METHOD TO ACTIVATE CSS ON THE FLY////////////////
///////////////////////////////////////////////////////////
function activateStyleSheet(title) {
      getStyleSheet(title).disabled = false;
}
function disableStyleSheet(title){
      getStyleSheet(title).disabled = true;
}
function getStyleSheet(title){
     var i, a;
     for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
	  if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
	       if(a.getAttribute("title") == title){
		    return a; } } } }
///////////////////////////////////////////////////////////
//////////METHOD TO ACTIVATE JAVASCRIPT ON THE FLY/////////
///////////////////////////////////////////////////////////
function syncIncludeJSTest(test, after){
     if (eval("window."+test+"!=undefined")) { debug("Javascript file loaded");eval(after); }
     else setTimeout("syncIncludeJSTest(\""+test+"\", '"+after+"')",500);
}
function syncIncludeJS(url, test, after){
     if (eval("window."+test+"===undefined")) {
	  debug("Including javascriptFile: "+url+"...");
	  includeJS(url);
	  setTimeout("syncIncludeJSTest(\""+test+"\", '"+after+"')", 500);
     }else eval(after);
}
function includeJS(url) {
     var head = document.getElementsByTagName('head')[0];
     var script = document.createElement('script');
     script.setAttribute('src',url);
     script.setAttribute('type', 'text/javascript');
     head.appendChild(script);
}
/* 
 * Update innercontent of a div, extracting and evaluating 
 * the javascript statements.
 */
var setInnerContents = function(targetObj, contents)
{
  targetObj.innerHTML = contents;

  var allJs = targetObj.getElementsByTagName('script');

  for(var i=0; i < allJs.length; i++)
  {
    if(allJs[i].src && allJs[i].src != '')
    includeJs(allJs[i].src);
    else
    globaleval(allJs[i].innerHTML);
  }
} 
///////////////////////////////////////////////////////////
//////////METHOD TO HANDLE LOG AND DEBUG///////////////////
///////////////////////////////////////////////////////////
/* 
 * @deprecated
 */
var debug = function(verb, val){
	debug("["+verb+"] "+val+"<br />");
}
/* 
 * @deprecated
 */
var debug = function(val){
     if ($('uc_debug_div')) $('uc_debug_div').innerHTML += val;
}
///////////////////////////////////////////////////////////
//////////METHOD TO HANDLE ASYNC POST AND ADMIN LOADING////
///////////////////////////////////////////////////////////
var includedModulesJs = new Array;
var loadAdminForm = function(idForm, id, blockId, frameTitle,params, jsonParams){
     frameName= "frame"+blockId;
     panel = loadAdminWindow(frameName, jsonParams, frameTitle);
     params= (params)?"&"+params:'';
     updateContainer(frameName+'_content', 'uc_blockload=11&uc_idForm='+idForm+'&id='+id+'&uc_blockId='+blockId+params);
}
var loadBlockWindow = function(frameTitle, params, jsonParams){
     frameName= "gallerieframe";
     panel = loadAdminWindow(frameName, jsonParams), frameTitle;
     updateContainer('gallerieframe_content', params);
}

var loadAdminFrame = function(frame, blockId, params){
     showAdminFrame('uc_admin_div');
     $('uc_admin_div').setAttribute('uc_id', blockId);
     if (params){ 
	  $('uc_admin_div').setAttribute('uc_params', params);
	  params="&"+params;
     } else params = ''
     updateContainer('uc_admin_div',  'uc_blockload='+blockId+params);
}
var loadinDiv = '<div style="text-align:center;"><img src="'+_uc_absurl+'template/theme_std/clip_loading_2.gif><br>loading</div>';

var loadAdminWindow = function(frameName, jsonParams, frameTitle){
     if (jsonParams == null) jsonParams = { y:YAHOO.util.Dom.getDocumentScrollTop(), width: "550px", fixedcenter: false, 
     constraintoviewport: false, visible: true , effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}} 
     //{ width : "550px", height:getFrameHeight()-70+"px", xy:[300,30], modal:false, visible : true, constraintoviewport : false, draggable: true,underlay:"none", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} };
     var panel = new YAHOO.widget.ResizePanel(frameName, jsonParams );
     panel.setBody('<div id="'+frameName+'_content"></div>');
     panel.render(document.body);
     YAHOO.example.container.manager.register(panel);
     return panel;
}
var CURRENTLOGFRAME = 'uc_admin_div';
var showAdminFrame = function (frameName){
     if (CURRENTLOGFRAME != frameName){
	  if (!($(CURRENTLOGFRAME) === null))$(CURRENTLOGFRAME).hide();
	  $(frameName).show();
	  CURRENTLOGFRAME = frameName;
	  YAHOO.example.container.uc_debug_win.show();
	  YAHOO.example.container.uc_debug_win.focus();
     }
}

     
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////METHODS FOR AJAX REQUESTS///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var globaleval =  function(script){
  if(window.execScript){
    return window.execScript(script);
  } else if(navigator.userAgent.indexOf('KHTML') != -1){ //safari, konqueror..
      var s = document.createElement('script');
      s.type = 'text/javascript';
      s.innerHTML = script;
      document.getElementsByTagName('head')[0].appendChild(s);
  } else {
    return window.eval(script);
  }
}                   
var handleFailure = function(o){
     var elem = $(o.argument.destination);
     if(o.responseText !== undefined){ 
		  elem.innerHTML = "<li>Transaction id: " + o.tId + "</li>"; 
		  elem.innerHTML += "<li>HTTP status: " + o.status + "</li>"; 
		  elem.innerHTML += "<li>Status code message: " + o.statusText + "</li>"; 
     } else elem.innerHTML = "Request failed";
}
var handleUpdate = function callBack(o) {
	
	setInnerContents($(o.argument.destination), o.responseText);
} 
var onMenuItemClick =function (p_sType, p_aArgs, p_oItem) {
     eval(p_oItem.value);
}                                                      
var onMenuButtonClick =function (p_sType, p_aArgs, p_oItem) {
     eval(this.get('value'));
}
var updateContainer = function(container, params){
	
     $(container).innerHTML = loadinDiv;
     var callback = { success: handleUpdate, argument: { destination: container} , failure : handleFailure };
     YAHOO.util.Connect.asyncRequest('POST','.',callback, params); 
}

/**
 * Detects if a form has a file input in it.
 * 
 * @param elmnt to check
 * @return true or false
 */
var hasFileInput = function(elmnt){
	var elements = Dom.getChildren(elmnt);
	for (var i = 0;i< elements.length; i++){
		 var item = elements[i];
		 if (item.tagName === "INPUT") {
			 if (item.type === "file") return true;
		 } else if (hasFileInput(item)) return true;
	} 
	return false;
}	
/**
 * 
 * @param formName
 * @param destination
 * @return void
 */
var postFormAsync = function(formName, destination){
     var callback = { success: handleUpdate, upload: handleUpdate, argument: { destination: destination } , failure : handleFailure };
     params = "uc_blockload="+$(destination).getAttribute('uc_id');
     var blockParams = $(destination).getAttribute('uc_params');
     if (blockParams) params += "&"+blockParams;
     form = $(formName);
     var hfi = hasFileInput(form)
     YAHOO.util.Connect.setForm(form, hfi);
     if (hfi) YAHOO.util.Connect.appendPostData(params);
     YAHOO.util.Connect.asyncRequest('POST','.',callback, params);  
    
//     form.action = "#";
//     form.target = "_blank";
//     form.submit();
//     return true;
     $(destination).innerHTML = loadinDiv;
     if (YAHOO.example.container.manager){
	     var panel = YAHOO.example.container.manager.find("frame"+destination);
	     if (panel !== null) panel.destroy();
     }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////FONCTIONS POUR XINHA//////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////   

var xinha_editors=null; 
var xinha_editors2=null;
var xinha_init2=null;
var xinha_config=null;
var xinha_plugins=null;
var xinha_init = function(dest){
     xinha_editors2 = null;
     syncIncludeJS(_uc_absurl+"javascript/Xinha/XinhaCore.js", "Xinha", "xinha_init2(\""+dest+"\")");
}
xinha_init2=xinha_init2?xinha_init2:function(dest){
     xinha_editors2=xinha_editors2?xinha_editors2:[dest];
     //xinha_plugins=xinha_plugins?xinha_plugins:["CharacterMap","ContextMenu","ListType","Stylist","Linker","SuperClean","TableOperations"];
     xinha_plugins=xinha_plugins?xinha_plugins:["CharacterMap","ContextMenu","DoubleClick","ListType","Stylist","TableOperations", "BackgroundImage"];
     if(!Xinha.loadPlugins(xinha_plugins,xinha_init2)){
	  return;
     }
     xinha_config=xinha_config?xinha_config:new Xinha.Config();
     xinha_config.stripScripts = false;
     xinha_config.height='400px';
     xinha_config.pageStyleSheets=[_editor_url+"examples/full_example.css"];
     debug("Creation of editor: "+xinha_editors2);
     xinha_editors=Xinha.makeEditors(xinha_editors2,xinha_config,xinha_plugins);
     Xinha.startEditors(xinha_editors);
};

function nothing(){}

function validemail(field) {var str = field.value;
     if (str != "") {
	  if (window.RegExp) {
	       var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
	       var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
	       var reg1 = new RegExp(reg1str);
	       var reg2 = new RegExp(reg2str);
	       if (!reg1.test(str) && reg2.test(str)) {
		    return true;
	       }
	       field.focus();field.select();
	       return false;
	  } else {
	       if(str.indexOf("@") >= 0) return true;
	       field.focus();field.select();
	       return false;
	  }
     }else {return false; }
}
var confirmDelete = function (){return confirm("Êtes-vous sur de vouloire détruire cet élement?"); }
///////////////DEPRECATED
function getSelectedRadioValue(radiobutton)
{
     var returnValue = "";
     if (radiobutton.length == 1){
	  returnValue = radiobutton.value;
     } else {
	  for (i=0;i<radiobutton.length;i++){
	       if (radiobutton[i].checked==true) {
		    returnValue=radiobutton[i].value;
	       }
	  }
     }
     return returnValue;
}

//=================================================================CHANGE LE TEXTE D'ID SPECIFIE ET LE VIDE
function changeText(id,texte){
     $(id).innerHTML=texte;
}
function checkRadio(form, radioName){
     var element=document.forms[form].elements[radioName];
     for (var i=0; i<element.length;i++) {
	  if (element[i].checked) {
	       return element[i].value;
	  }
     }
     return false;
}
//=================================================================FONCTION DE GESTION DES FORMULAIRES


//=================================================================FONCTION DE GESTION DES IMAGES
function makevisible(){
     if (cur.filters.alpha.opacity<70) cur.filters.alpha.opacity =70;
     if (cur.filters.alpha.opacity>100) cur.filters.alpha.opacity =100;
     cur.filters.alpha.opacity=cur.filters.alpha.opacity+which;
     vitessedefil = setTimeout("makevisible()", 10);
}

function makeopt(arg1,arg2){
     which=arg1;
     cur=arg2;
}
/*
This file is part of UC.

UC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

UC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with UC; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Author: Francois-Xavier Aeberhard
Contact: fx_aeb@yahoo.fr
*/
// BEGIN RESIZEPANEL SUBCLASS //

if (YAHOO.widget.Panel){
	YAHOO.widget.ResizePanel = function(el, userConfig) {
     if (arguments.length > 0) {
	  YAHOO.widget.ResizePanel.superclass.constructor.call(this, el, userConfig);
     }
}

YAHOO.widget.ResizePanel.CSS_PANEL_RESIZE = "yui-resizepanel";
YAHOO.widget.ResizePanel.CSS_RESIZE_HANDLE = "resizehandle";
YAHOO.extend(YAHOO.widget.ResizePanel, YAHOO.widget.Panel, {
	  
	  init: function(el, userConfig) {
	       
	       YAHOO.widget.ResizePanel.superclass.init.call(this, el);
	       
	       this.beforeInitEvent.fire(YAHOO.widget.ResizePanel);
	       
	       var Dom = YAHOO.util.Dom,
	       Event = YAHOO.util.Event,
	       oInnerElement = this.innerElement,
	       oResizeHandle = document.createElement("DIV"),
	       sResizeHandleId = this.id + "_resizehandle";
	       
	       oResizeHandle.id = sResizeHandleId;
	       oResizeHandle.className = YAHOO.widget.ResizePanel.CSS_RESIZE_HANDLE;
	       Dom.addClass(oInnerElement, YAHOO.widget.ResizePanel.CSS_PANEL_RESIZE);
	       this.resizeHandle = oResizeHandle;
	       function initResizeFunctionality() {
		    var me = this,
		    oHeader = this.header,
		    oBody = this.body,
		    oFooter = this.footer,
		    nStartWidth,
		    nStartHeight,
		    aStartPos,
		    nBodyBorderTopWidth,
		    nBodyBorderBottomWidth,
		    nBodyTopPadding,
		    nBodyBottomPadding,
		    nBodyOffset;
		    
		    oInnerElement.appendChild(oResizeHandle);
		    this.ddResize = new YAHOO.util.DragDrop(sResizeHandleId, this.id);
		    this.ddResize.setHandleElId(sResizeHandleId);
		    this.ddResize.onMouseDown = function(e) {
			 nStartWidth = oInnerElement.offsetWidth;
			 nStartHeight = oInnerElement.offsetHeight;
			 if (YAHOO.env.ua.ie && document.compatMode == "BackCompat") { nBodyOffset = 0;
			 }
			 else {
			      nBodyBorderTopWidth = parseInt(Dom.getStyle(oBody, "borderTopWidth"), 10),
			      nBodyBorderBottomWidth = parseInt(Dom.getStyle(oBody, "borderBottomWidth"), 10),
			      nBodyTopPadding = parseInt(Dom.getStyle(oBody, "paddingTop"), 10),
			      nBodyBottomPadding = parseInt(Dom.getStyle(oBody, "paddingBottom"), 10),
			      nBodyOffset = nBodyBorderTopWidth + nBodyBorderBottomWidth + nBodyTopPadding + nBodyBottomPadding;
			 }
			 me.cfg.setProperty("width", nStartWidth + "px");
			 aStartPos = [Event.getPageX(e), Event.getPageY(e)];
		    };
		    this.ddResize.onDrag = function(e) {
			 var aNewPos = [Event.getPageX(e), Event.getPageY(e)],
			 nOffsetX = aNewPos[0] - aStartPos[0],
			 nOffsetY = aNewPos[1] - aStartPos[1],
			 nNewWidth = Math.max(nStartWidth + nOffsetX, 10),
			 nNewHeight = Math.max(nStartHeight + nOffsetY, 10),
			 nBodyHeight = (nNewHeight - (oFooter.offsetHeight + oHeader.offsetHeight + nBodyOffset));
			 me.cfg.setProperty("width", nNewWidth + "px");
			 if (nBodyHeight < 0) {
			      nBodyHeight = 0;
			 }
			 oBody.style.height =  nBodyHeight + "px";
		    };
	       }
	       function onBeforeShow() {
		    initResizeFunctionality.call(this);
		    this.unsubscribe("beforeShow", onBeforeShow);
	       }
	       function onBeforeRender() {
		    if (!this.footer) {
			 this.setFooter("");
		    }
		    if (this.cfg.getProperty("visible")) {
			 initResizeFunctionality.call(this);
		    }
		    else {
			 this.subscribe("beforeShow", onBeforeShow);
		    }
		    this.unsubscribe("beforeRender", onBeforeRender);
	       }
	       this.subscribe("beforeRender", onBeforeRender);
	       if (userConfig) {
		    this.cfg.applyConfig(userConfig, true);
	       }
	       this.initEvent.fire(YAHOO.widget.ResizePanel);
	  },
	  toString: function() {
	       return "ResizePanel " + this.id;
	  }
});
}
