/**
 * @author jeremy@eolas.fr
 */

YAHOO.namespace("YAHOO.ownFramePrehome");
YAHOO.ownFramePrehome.divId = 'ownFramePrehome';
YAHOO.ownFramePrehome.fermer = 'Fermer';
YAHOO.ownFramePrehome.imprimer = 'Imprimer';
YAHOO.util.Event.onDOMReady(function() {
	o = document.getElementById('cover');
	oDoc = document.getElementById('document');
	isFo = true;
	if (!oDoc) {
		isFo = false;
		oDoc = document.getElementById('document');
	}
	
	if (o && oDoc) 
		//YAHOO.util.Dom.setStyle(o, "height", oDoc.offsetHeight + 'px');

	YAHOO.ownFramePrehome.initialisation()
}); 


YAHOO.ownFramePrehome.initialisation = function() {
	//YAHOO.ownFramePrehome.showCover();
	YAHOO.util.Dom.getElementsByClassName('popup','a','document', function() {
	    var external = YAHOO.util.Dom.hasClass(this,'external');
	    var regON = new RegExp("(index.php)", "g");
	    var regOFF = new RegExp("(cms_pseudo.php)","g");
	    if(regON.test(this.href)) { 
		  this.setAttribute('rel', this.href.replace(regON,'indexPopup.php'));
		} else if (regOFF.test(this.href)) {
		  this.setAttribute('rel', this.href.replace(regOFF,'cms_pseudoPopup.php'));
		} else if(external) {
		  this.setAttribute('rel',this.href);
		} else {
		  this.setAttribute('rel', SERVER_ROOT+'indexPopup.php?url='+this.href);
		}
		
		this.href = 'javascript:void(0)';
		YAHOO.util.Dom.removeClass(this,'external');
		YAHOO.util.Event.purgeElement(this,false);
		
		YAHOO.util.Event.addListener(this, 'click', function() {
			YAHOO.ownFramePrehome.show(this.getAttribute('rel'),external);
			return false;
		});	
	});
	//YAHOO.ownFramePrehome.hideCover();
}



YAHOO.ownFramePrehome.coverFocus = function(evt) {
    YAHOO.util.Event.stopEvent(evt);
}

YAHOO.ownFramePrehome.show = function(url,external) {
	YAHOO.ownFramePrehome.showCover();
	setTimeout( YAHOO.ownFramePrehome.close, 22000) ;
	oBloc = document.getElementById(YAHOO.ownFramePrehome.divId);
	winH = YAHOO.util.Dom.getViewportHeight();
	winW = YAHOO.util.Dom.getViewportWidth();	
	if (!oBloc) {
		var objBody = document.getElementsByTagName("body").item(0);
		oBloc = document.createElement("div");
		oBloc.setAttribute('id', YAHOO.ownFramePrehome.divId);
		YAHOO.util.Dom.setStyle(oBloc, "position", "absolute");
		YAHOO.util.Dom.setStyle(oBloc, "z-index", 200);
		objBody.appendChild(oBloc);
	}

	oBloc.innerHTML = '<iframe id="frameEditor" src="" ALLOWTRANSPARENCY="true" frameborder="no" scrolling="no" style="width: 100%; min-height: 595px;"></iframe><!-- <div class="action"><a href="#" class="close" onclick="YAHOO.ownFramePrehome.close();return false;">'+YAHOO.ownFramePrehome.fermer+'</a><a href="#" class="close" onclick="Javascript:print();return false;">'+YAHOO.ownFramePrehome.imprimer+'</a></div> -->';	
	YAHOO.util.Dom.setStyle(oBloc, "display", "block");
	
	iFrame = document.getElementById('frameEditor');
    iFrame.setAttribute('src', url);
    iFrame.focus();
    if(!external) {
        YAHOO.util.Dom.setStyle(oBloc, "width", "1000px"); // 1000px = taille de la prehome
        YAHOO.ownFramePrehome.positionUpdate();
    
    	YAHOO.util.Event.addListener(iFrame, 'load', function() {  
    		o = this.contentWindow;
    		if (o) o = o.document;
    		if (o) o = o.getElementById('documentPopup');
    		if (o) YAHOO.ownFramePrehome.updateHeight(o.offsetHeight);
    	}); 
    	
    	// mise a defaut de la feuille de style de la popup
        //YAHOO.util.Event.addListener(iFrame, 'load', YAHOO.ownFramePrehome.loadPrintStyle);
        //YAHOO.util.Event.addListener(iFrame, 'unload', YAHOO.ownFramePrehome.unloadPrintStyle);
    	YAHOO.util.Event.addListener(window, 'scroll', YAHOO.ownFramePrehome.moveScroll);
    	YAHOO.util.Event.addListener(window, 'resize', YAHOO.ownFramePrehome.moveScroll);
    } else {
        YAHOO.util.Dom.setStyle(oBloc, "width", "1030px");
        YAHOO.util.Dom.setStyle(oBloc, "height", "818px");
        YAHOO.util.Dom.setStyle(iFrame, "height", "768px");
        winH = YAHOO.util.Dom.getViewportHeight();
    	winW = YAHOO.util.Dom.getViewportWidth();
    	try {
    		// Mise a jour positionnement
    		eleW = oBloc.offsetWidth;
    		if (winW >= eleW) YAHOO.util.Dom.setX(oBloc, YAHOO.util.Dom.getDocumentScrollLeft() + (winW - eleW) / 2); 
    		// on ne met pas a jour la hauteur et on la force a 10px du top.
            YAHOO.util.Dom.setY(oBloc, 10);
    	} catch(ex) {
            alert(ex);
        }
    	YAHOO.util.Event.addListener(iFrame, 'load', function() {  
    		o = this.contentWindow;
    		if (o) o = o.document;
    		if (o) o = o.getElementById('documentPopup');
    		if (o) YAHOO.ownFramePrehome.updateHeight(o.offsetHeight);		
    	}); 
    
    }
}
YAHOO.ownFramePrehome.loadPrintStyle = function() {
      var i, a, printIframe;
      var aDefault = new Array();
      for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            if(a.getAttribute('title') == 'default' && a.getAttribute('media') == 'print') {
              aDefault.push(a);
            }
            else if (a.getAttribute('title') == 'printIframe') {
              printIframe = a;
            }
        }
      }
      for(var i=0;aDefault[i];i++) {
        aDefault[i].disabled = true;
      }
      printIframe.disabled = false;
}
YAHOO.ownFramePrehome.unloadPrintStyle = function() {
      var i, a, printIframe;
      var aDefault = new Array();
      for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            if(a.getAttribute('title') == 'default' && a.getAttribute('media') == 'print') {
              aDefault.push(a);
            }
            else if (a.getAttribute('title') == 'printIframe') {
              printIframe = a;
            }
        }
      }
      for(var i=0;aDefault[i];i++) {
        aDefault[i].disabled = false;
      }
      printIframe.disabled = true;
}
YAHOO.ownFramePrehome.updateHeight = function(taille) {
	if(iFrame = document.getElementById('frameEditor')) {
    	oBloc = document.getElementById(YAHOO.ownFramePrehome.divId);
    	winH = YAHOO.util.Dom.getViewportHeight();
    	winW = YAHOO.util.Dom.getViewportWidth();	
    	// Ajout marge inner
    	taille += 40;
        oTemplatecover = document.getElementById('cover');
        if(YAHOO.util.Dom.getDocumentHeight() < taille) {
            YAHOO.util.Dom.setStyle(oTemplatecover, "height", (taille+100) + 'px');
        } else {
            YAHOO.util.Dom.setStyle(oTemplatecover, "height", YAHOO.util.Dom.getDocumentHeight() + 'px');
        }
    	//YAHOO.util.Dom.setStyle(oBloc, "height", (taille+20) + 'px');
       // YAHOO.util.Dom.setStyle(iFrame, "height", (taille) + 'px');
        var attributes1 = { height: { to: taille } };
        var myAnim1 = new YAHOO.util.Anim(iFrame, attributes1, 1, YAHOO.util.Easing.easeOut);
        myAnim1.animate();
        var attributes2 = { height: { to: taille+20 } };
        var myAnim2 = new YAHOO.util.Anim(oBloc, attributes2, 1, YAHOO.util.Easing.easeOut);        
        myAnim2.onComplete.subscribe(function(){window.scrollTo(0,0);});
        myAnim2.animate();	   
    	
        YAHOO.ownFramePrehome.positionUpdate();
    	iFrame.focus();
	}
}

YAHOO.ownFramePrehome.positionUpdate = function() {
	clearTimeout(YAHOO.ownFramePrehome.timeScroll);
	oBloc = document.getElementById(YAHOO.ownFramePrehome.divId);
	winH = YAHOO.util.Dom.getViewportHeight();
	winW = YAHOO.util.Dom.getViewportWidth();
	try {
		// Mise a jour positionnement
		eleW = oBloc.offsetWidth;
		if (winW >= eleW) YAHOO.util.Dom.setX(oBloc, YAHOO.util.Dom.getDocumentScrollLeft() + (winW - eleW) / 2); 
		// on ne met pas a jour la hauteur et on la force a 10px du top.
        
		YAHOO.util.Dom.setY(oBloc, 10);
	} catch(ex) {alert(ex);}
}

YAHOO.ownFramePrehome.timeScroll = 0;
YAHOO.ownFramePrehome.moveScroll = function(){
	YAHOO.ownFramePrehome.timeScroll = setTimeout(YAHOO.ownFramePrehome.positionUpdate, 200);
}

YAHOO.ownFramePrehome.close = function() {
	oBloc = document.getElementById(YAHOO.ownFramePrehome.divId);
	if (oBloc) YAHOO.util.Dom.setStyle(oBloc, 'display', 'none');
	iFrame = document.getElementById('frameEditor');
	oBloc.removeChild(iFrame);
	
	YAHOO.ownFramePrehome.hideCover();
	YAHOO.ownFramePrehome.showFlash();
    //YAHOO.ownFramePrehome.unloadPrintStyle();
	YAHOO.util.Event.removeListener(window, 'scroll', YAHOO.ownFramePrehome.moveScroll);
	YAHOO.util.Event.removeListener(window, 'resize', YAHOO.ownFramePrehome.moveScroll);
	
}
YAHOO.ownFramePrehome.showCover = function() {
    var oTemplatecover = document.getElementById('cover');
	if (!oTemplatecover) {
		var oTemplatecover = document.createElement('div');
		oTemplatecover.id = 'cover';
		document.getElementsByTagName('body')[0].appendChild(oTemplatecover);
	}
	oTemplatecover.style.display = "block";
	oTemplatecover.style.height = (YAHOO.util.Dom.getDocumentHeight() + 100) + 'px';
	oTemplatecover.style.width = YAHOO.util.Dom.getDocumentWidth() + 'px';
	
	YAHOO.ownFramePrehome.kpl1 = new YAHOO.util.KeyListener(document, {keys:27}, YAHOO.ownFramePrehome.close);
	YAHOO.ownFramePrehome.kpl1.enable();
	
	/** From ligthbox.js **/
    hideSelectBoxes();
	YAHOO.ownFramePrehome.hideFlash();
	
	/* on masque toutes les iframe */
    var cIframe = document.getElementsByTagName('iframe');
    for (i=0;i<cIframe.length;i++)
    {
    	if (cIframe[i].id != 'frameEditor') cIframe[i].style.display = 'none';
    }
}
YAHOO.ownFramePrehome.hideCover = function() {
	showSelectBoxes();
	var oTemplatecover = document.getElementById('cover');
	if (oTemplatecover) oTemplatecover.style.display = "none";
	
	/* on Réaffiche toutes les iframe */
	var cIframe = document.getElementsByTagName('iframe');
    for (i=0;i<cIframe.length;i++)
    {
    	cIframe[i].style.display = 'block';
    }
}
	
YAHOO.ownFramePrehome.showFlash = function() {
    var f = document.getElementById('CoverFlow');
    if(f) {
       f.style.display='';
    }   
}
YAHOO.ownFramePrehome.hideFlash = function() {
    var f = document.getElementById('CoverFlow');
    if(f) {
       f.style.display='none';
    }
}

