/* Comm.Common*/
function GetQueryParam( param ){
    var regexS = "[\\?&]"+param+"=([^&#]*)";
    var regex = new RegExp ( regexS );
    var tmpURL = window.location.href;
    var results = regex.exec( tmpURL );
    if( results == null ){
        return "";
    }else{
        return results[1];
    }
}
function GetQueryHashParam( param ){
    var regexS = "[\\#&]"+param+"=([^&#]*)";
    var regex = new RegExp ( regexS );
    var tmpURL = window.location.hash;
    var results = regex.exec( tmpURL );
    if( results == null ){
        return "";
    }else{
        return results[1];
    }
}
function GetQuery(url){
    // Searh + Hash    
    return (url.lastIndexOf('?')!=-1?url.substring(url.lastIndexOf('?')+1):""); 
}
function GetQueryHash(url){
    // Hash    
    return (url.lastIndexOf('#')!=-1?url.substring(url.lastIndexOf('#')+1):""); 
}

function GetFullPath(url){
    return url.protocol+"//"+url.host+url.pathname;
}

function id2IFrame(id) {  
    var result = null;
    switch (id) {
        case "c0": result = "_inmofactory_Ib2c_Escritorio"; break;
        case "c1": result = "_inmofactory_Ib2c_BuscadorCompacto"; break;
        case "c2": result = "_inmofactory_Ib2c_BuscadorRefMini"; break;
        case "c3": result = "_inmofactory_Ib2c_Destacadas"; break;
        default: result = "ComponenteIb2cDesconocido";
    }
    return result;
}
function ResizeElement (id,w,h){   
   element = document.getElementById(id2IFrame(id));
   if (w!=""){element.width = w;element.style.width = w;}
   if (h!=""){element.height = h;element.style.height = h;}
} 

function Op2Ib2c(op){    
    if( GetFullPath(window.location).toLowerCase() == inmofactory_Ib2c_HostUrlEscritorio.toLowerCase() ) {
        document.getElementById(id2IFrame("c0")).src = ib2cEscritorio.getIb2cUrl() +"&"+ op;
    }else{
        window.location = inmofactory_Ib2c_HostUrlEscritorio + "#" + op;    
    }
}         

/* Componente Ib2c comm */
ib2cEscritorio=null;
Ib2c = function(ib2cComponente, ib2cUrl, hostUrlRoot, hostUrlEscritorio) {
        this.initialize(ib2cComponente, ib2cUrl, hostUrlRoot, hostUrlEscritorio);        
};
Ib2c.prototype = {        
        _inmofactory_Ib2c_Componente : "",
        _inmofactory_Ib2c_Url : "",      
        _inmofactory_Ib2c_HostUrlRoot : "",
        _inmofactory_Ib2c_HostUrlEscritorio : "",        
        _componente : {
            Escritorio:{containerId:"inmofactory_Ib2c_Escritorio",iframeId:"_inmofactory_Ib2c_Escritorio",src:"/Escritorio.aspx",lookForParam:"firstop"},
            BuscadorCompacto:{containerId:"inmofactory_Ib2c_BuscadorCompacto",iframeId:"_inmofactory_Ib2c_BuscadorCompacto",src:"/sinescritorio/BuscadorCompacto.aspx"},
            BuscadorRefMini:{containerId:"inmofactory_Ib2c_BuscadorRefMini",iframeId:"_inmofactory_Ib2c_BuscadorRefMini",src:"/sinescritorio/BuscadorRefMini.aspx"},
            Destacadas:{containerId:"inmofactory_Ib2c_Destacadas",iframeId:"_inmofactory_Ib2c_Destacadas",src:"/sinescritorio/OfeDestacadasRd.aspx"}                      
            
        },                          
        initialize : function (ib2cComponente, ib2cUrl, hostUrlRoot, hostUrlEscritorio) {            
            this._inmofactory_Ib2c_Componente = ib2cComponente;
            this._inmofactory_Ib2c_Url = ib2cUrl;
            this._inmofactory_Ib2c_HostUrlRoot =  hostUrlRoot;
            this._inmofactory_Ib2c_HostUrlEscritorio = hostUrlEscritorio;
            this.writePersistableVar("inmofactory_Ib2c_HostUrlEscritorio", this._inmofactory_Ib2c_HostUrlEscritorio);
            this.createComponent(); 
            if (this._inmofactory_Ib2c_Componente=="Escritorio"){ib2cEscritorio=this;}
        },
        createComponent :  function (){            
            this.writeIFrame(this.writeContainer(this._componente[this._inmofactory_Ib2c_Componente].containerId),
                             this.createIFrame(this._componente[this._inmofactory_Ib2c_Componente].iframeId),
                             this.getIFrameSrc()                 
                             );                
        },        
        createIFrame : function (id) {    
           ib2cIframe = document.createElement("iframe");             
           ib2cIframe.id = id;   
           ib2cIframe.name = id;
           ib2cIframe.setAttribute("src", ""); 
           ib2cIframe.style.padding = "0";
           ib2cIframe.style.margin = "0";
           ib2cIframe.style.border = "0";
           ib2cIframe.style.overflow = "hidden";
           ib2cIframe.scrolling = "no";
           ib2cIframe.frameBorder="0";
           ib2cIframe.frameSpacing="0";
           ib2cIframe.marginHeight="0";
           ib2cIframe.marginWidth="0";                      
           switch (id) {
                case "_inmofactory_Ib2c_Escritorio": ib2cIframe.height=1; ib2cIframe.width=1; break;
                case "_inmofactory_Ib2c_BuscadorCompacto": ib2cIframe.height=1; ib2cIframe.width=1; break;
                case "_inmofactory_Ib2c_BuscadorRefMini": ib2cIframe.height=1; ib2cIframe.width=1; break;
                case "_inmofactory_Ib2c_Destacadas": ib2cIframe.height=1; ib2cIframe.width=1; break;
                default: ib2cIframe.height=1; ib2cIframe.width=1;
           }
           return ib2cIframe;
        },                                
        writeContainer : function (id){
            document.writeln('<div id="'+ id +'" style="padding:0px; margin:0px; border:none;"><\/div>');
            return id;                                    
        },
        writeIFrame : function(containerId, iFrame, src) {
            iFrame.src = src;
            document.getElementById(containerId).appendChild(iFrame);
            return iFrame;
        },
        writePersistableVar : function(variable,value){
            document.writeln('<sc'+'ript language="javascript" type="text\/javascript">\r\n' + variable + '="'+ value +'";\r\n<\/sc'+'ript>');
        },
        resetHostLocation : function(){
            url = document.location.href;
            document.location.href = url.substring(0,(url.lastIndexOf('#')<0?url.length:url.lastIndexOf('#')))+"#";                                
        },
        getIFrameSrc : function(){                        
            result =  this.getIb2cUrl();
            
            if (this._componente[this._inmofactory_Ib2c_Componente].lookForParam){                
                firstop = GetQueryHashParam( 'FIRSTOP' );
                if (firstop!=undefined && firstop!=null && firstop!="" ){      
                   result+= "&" + GetQueryHash(document.location.href); 
                   this.resetHostLocation(); 
                }
            }                        
            return result;
        },
        getIb2cUrl : function() {                
            tmpQuery = GetQuery(this._inmofactory_Ib2c_Url);  
            tmpInmofactory_Ib2c_HostUrlRoot = (this._inmofactory_Ib2c_HostUrlRoot.indexOf("http://")!=-1)?this._inmofactory_Ib2c_HostUrlRoot.substring(this._inmofactory_Ib2c_HostUrlRoot.indexOf("http://")+7):this._inmofactory_Ib2c_HostUrlRoot;
            if ( tmpQuery.length>0 ){
                tmpHostAndPath = this._inmofactory_Ib2c_Url.substring(0,this._inmofactory_Ib2c_Url.lastIndexOf('?'));
                if ( tmpHostAndPath.length != tmpHostAndPath.lastIndexOf('/')+1){tmpHostAndPath+='/'}
                result = tmpHostAndPath.substring(0,tmpHostAndPath.lastIndexOf('/')) + this._componente[this._inmofactory_Ib2c_Componente].src + "?HRoot=" + tmpInmofactory_Ib2c_HostUrlRoot + "&" + tmpQuery;
            }else{
                result = this._inmofactory_Ib2c_Url + this._componente[this._inmofactory_Ib2c_Componente].src + "?HRoot=" + tmpInmofactory_Ib2c_HostUrlRoot;                        
            }            
            return result;
        }                
};
