/**
 * @author jeremy@eolas.fr
 */

YAHOO.namespace("YAHOO.ownFrame");
YAHOO.ownFrame.divId = 'ownFrame';
YAHOO.ownFrame.fermer = 'Fermer';
YAHOO.ownFrame.imprimer = 'Imprimer';
YAHOO.util.Event.onDOMReady(function() {
	var o = document.getElementById('cover');
	var oDoc = document.getElementById('document');
	var isFo = true;
	if (!oDoc) {
		isFo = false;
		var oDoc = document.getElementById('document');
	}
	
	if (o && oDoc) 
		YAHOO.util.Dom.setStyle(o, "height", oDoc.offsetHeight + 'px');

	YAHOO.ownFrame.initialisation()
}); 


YAHOO.ownFrame.initialisation = function() {
	YAHOO.ownFrame.showCover();
	YAHOO.util.Dom.getElementsByClassName('popup','a','document', function() {
		if(this.href!='javascript:void(0)'){
			this.setAttribute('rel', this.href);
			this.href = 'javascript:void(0)';
			YAHOO.util.Event.addListener(this, 'click', function() {
				YAHOO.ownFrame.show(this.getAttribute('rel'));
				return false;
			});
		}	
	});
	YAHOO.ownFrame.hideCover();
}


YAHOO.ownFrame.coverFocus = function(evt) {
    YAHOO.util.Event.stopEvent(evt);
}

YAHOO.ownFrame.show = function(url) {
	YAHOO.ownFrame.showCover();
	window.scroll(0,0);

	/** From ligthbox.js **/
	hideSelectBoxes();
	YAHOO.ownFrame.hideFlash();

	oBloc = document.getElementById(YAHOO.ownFrame.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.ownFrame.divId);
		YAHOO.util.Dom.setStyle(oBloc, "position", "absolute");
		YAHOO.util.Dom.setStyle(oBloc, "z-index", 2000);
		objBody.appendChild(oBloc);
	}


	oBloc.innerHTML = '<div id="hautProduit"><a href="javascript:YAHOO.ownFrame.close();" class="close"><img src="'+SERVER_ROOT+'images/produiFermer.gif" id="FermerProduit" alt="Fermer"></a></div><iframe id="frameEditor" src="" ALLOWTRANSPARENCY="true" marginwidth="0" marginheight="0" frameborder="no" scrolling="no" style="width:100%;"></iframe><div id="basProduit"></div>';	
	YAHOO.util.Dom.setStyle(oBloc, "display", "block");
	YAHOO.util.Dom.setStyle(oBloc, "width", "719px");
    
	YAHOO.ownFrame.positionUpdate();
	
    
				
	iFrame = document.getElementById('frameEditor');
	iFrame.setAttribute('src', url);
	iFrame.style.width = '100%';

	YAHOO.util.Event.addListener(iFrame, 'load', function() {  
		o = this.contentWindow;
		if (o) o = o.document;
		if (o) o = o.getElementById('documentPopup');
		if (o) YAHOO.ownFrame.updateHeight(o.offsetHeight-40);		
	}); 
	
	iFrame.focus();
	
	// mise a defaut de la feuille de style de la popup
    //YAHOO.util.Event.addListener(iFrame, 'load', YAHOO.ownFrame.loadPrintStyle);
    //YAHOO.util.Event.addListener(iFrame, 'unload', YAHOO.ownFrame.unloadPrintStyle);
	/*YAHOO.util.Event.addListener(window, 'scroll', YAHOO.ownFrame.moveScroll);*/
	YAHOO.util.Event.addListener(window, 'resize', YAHOO.ownFrame.moveScroll);
}
YAHOO.ownFrame.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.ownFrame.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.ownFrame.updateHeight = function(taille) {
	if(iFrame = document.getElementById('frameEditor')) {
    	oBloc = document.getElementById(YAHOO.ownFrame.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.animate();
    	
        YAHOO.ownFrame.positionUpdate();
    	iFrame.focus();
	}
}

YAHOO.ownFrame.positionUpdate = function() {
	clearTimeout(YAHOO.ownFrame.timeScroll)
	oBloc = document.getElementById(YAHOO.ownFrame.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) {}
}

YAHOO.ownFrame.timeScroll = 0;
YAHOO.ownFrame.moveScroll = function(){
	YAHOO.ownFrame.timeScroll = setTimeout(YAHOO.ownFrame.positionUpdate, 200);
}

YAHOO.ownFrame.close = function() {
	oBloc = document.getElementById(YAHOO.ownFrame.divId);
	if (oBloc) YAHOO.util.Dom.setStyle(oBloc, 'display', 'none');
	YAHOO.ownFrame.hideCover();
	/** From ligthbox.js **/
	showSelectBoxes();
	YAHOO.ownFrame.showFlash();
    //YAHOO.ownFrame.unloadPrintStyle();
	YAHOO.util.Event.removeListener(window, 'scroll', YAHOO.ownFrame.moveScroll);
	YAHOO.util.Event.removeListener(window, 'resize', YAHOO.ownFrame.moveScroll);
}
YAHOO.ownFrame.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() + 'px';
	oTemplatecover.style.width = YAHOO.util.Dom.getDocumentWidth() + 'px';
}
YAHOO.ownFrame.hideCover = function() {
	var oTemplatecover = document.getElementById('cover');
	if (oTemplatecover) oTemplatecover.style.display = "none"; 
}
	
YAHOO.ownFrame.showFlash = function() {
    var f = document.getElementById('CoverFlow');
    if(f) {
       f.style.display='';
    }   
}
YAHOO.ownFrame.hideFlash = function() {
    var f = document.getElementById('CoverFlow');
    if(f) {
       f.style.display='none';
    }
}
