function Controller(){var ana=this;this.initialized=false;this.mods='';this.defaultTheme='Default';this.defaultLang='en';this.defaultView='startpage';this.currentTheme='';this.moduleListRetrials=3;this.moduleListPostpone=30;this.errorPage="/error.html";this.DEBUG=true;ana.sniffers={};ana.switchViewSniffers={};ana.onRenderHandlers={};ana.onUnloadHandlers={};ana.beforeUnloadHandlers={};ana.autoRefreshModules={};this.templatesDependancy={};this.onscrollListners=[];this.onresizeListners=[];this.onkeypressListners={};var index=document.location.href.indexOf("home.html");this.location=document.location.href.substring(0,index);this.registerOnscroll=function(fn){ana.onscrollListners.push(fn);}
this.registerOnresize=function(fn){ana.onresizeListners.push(fn);}
this.init=function(callback)
{try
{window.onunload=function(e){ana.unloadEvent();if(!info.isIE5_5)
dhtmlHistory.firstLoad=null;};window.onresize=function(){for(var i=0;i<ana.onresizeListners.length;i++)
if(ana.onresizeListners[i])ana.onresizeListners[i]();ajax.broadcast('clientRequest','windowResized',{});};document.onscroll=window.onscroll=function(){for(var i=0;i<ana.onscrollListners.length;i++)
if(ana.onscrollListners[i])ana.onscrollListners[i]();};if(typeof Forms!='undefined'&&ana.forms==undefined)
ana.forms=new Forms();if(typeof Results!='undefined'&&ana.results==undefined)
ana.results=new Results();if(typeof User!='undefined'&&ana.user==undefined)
ana.user=new User();if(typeof StaticForms!='undefined'&&ana.staticForms==undefined)
ana.staticForms=new StaticForms();ana.address=requestParameters.address;ana.domain=ana.address.substr(ana.address.indexOf('@')+1);ana.initModules();if(!info.isIE5_5){window.historyStorage.init();window.dhtmlHistory.create();dhtmlHistory.initialize();dhtmlHistory.addListener(ana.historyChange);}
blur();ana.initialized=true;if(callback)callback();}
catch(e)
{alert(e+'\n'+e.message);};};this.unloadEvent=function()
{var params={formName:ana.forms.inputs.formId};ajax.requestSilentPost('sessionFlow','applicationClose',params);}
this.loadEvent=function()
{ajax.requestInvisiblePost('sessionFlow','applicationStart');}
this.initModules=function(modules)
{if(modules)
{modules.eachAttrib(function(module){ana[module].init();});}
else
{if(controller.forms)controller.forms.init();if(controller.results)controller.results.init();if(controller.user)controller.user.init();if(controller.staticForms)controller.staticForms.init();}};this.complainWhileListingModule=function(response)
{ajax.error(T('re.ldngErr')+' ('+response.errorCode+(response.errorMessage?(': '+response.errorMessage):'')+')');}
ana.address=requestParameters.address;if(!ana.address)ana.address='dummy';this.retryFailedModule=function(module,trials)
{if(trials==undefined||trials==null)trials=ana.moduleListRetrials;if(!--trials)return false;setTimeout(function(){ana[module].init(trials);},module=='webmail'?1000:ana.moduleListPostpone*1000);return true;};this.notReady=function(modules)
{if(!modules)return false;if(modules.constructor==Array)
{if(modules.each(function(module){if(!ana[module]||!ana[module].initialized)return true;}))return true;}
else if(!ana[modules]||!ana[modules].initialized)
return true;return false;};this.getBrowser=function()
{if(info.isIE||info.isIE5_5)
return'Internet Explorer';else if(info.isFF)
return'Firefox';else if(info.Opera)
return'Opera';else if(info.Safari)
return'Safari';}
this.loadTheme=function(theme,view,callback,force,progressStep)
{if(!applicationLoaded)return;if(theme==ana.currentTheme&&!force)return;var templateDone=false;var viewsDone=false;var stylesheetDone=false;var done=function()
{if(templateDone&&viewsDone&&stylesheetDone)
{ana.currentTheme=theme;ana.currentView='';var v=view?view:ana.defaultView;if(callback)callback;if(ana.defaultNewView=='exist')
{controller.user.login(ana.username,ana.password,ana.login_type);ana.defaultNewView='';}
else if(ana.defaultNewView=='new')
{controller.forms.startNewbie();ana.defaultNewView='';}else if(ana.defaultNewView=='insurer'&&!controller.errorAuth)
{ajax.requestInvisiblePost('sessionFlow','forceInsurer',{id_insurer:ana.insurerId});ana.switchView(v,callback);}else if(ana.defaultNewView=='insurer'&&controller.errorAuth){alert("Error de autenticación");window.location="http://www.asesorseguros.com/entorno_test.html";}else if(ana.defaultNewView=='models'){ana.forms.goToSpecForm('A1','brand',ana.brandValue);}
else
{ana.switchView(v,callback);}};};var onTemplateDone=function()
{templateDone=true;done();};var onViewsDone=function(viewsJSON)
{ana.initThemeViews(viewsJSON);viewsDone=true;done();};var onStylesheetDone=function()
{stylesheetDone=true;done();};var preloadImages=function()
{var div=$('g_image_loader');if(!div){div=document.createElement('div');div.style.display='none';document.body.appendChild(div);}
var s='';for(var i=0;i<ana.preloadedImagesDir.length;i++)
s+="<img src='"+ana.dirImagesPath+ana.preloadedImagesDir[i]+"'>";for(var i=0;i<ana.preloadedImages.length;i++)
s+="<img src='"+ana.imagesPath+ana.preloadedImages[i]+"'>";div.innerHTML=s;};ana.dir=T('sys.dir');if(!theme)theme=ana.defaultTheme;ajax.resourceExists('views/themes/'+theme+'/'+ana.dir+'/template.html',function(found)
{if(!found)
theme=ana.defaultTheme;ana.imagesPath='views/themes/'+theme+'/images/';ana.dirImagesPath='views/themes/'+theme+'/'+ana.dir+'/images/';preloadImages();ajax.loadCSS('views/themes/'+theme+'/'+ana.dir+'/styles.css',onStylesheetDone,true,true);ajax.loadJSON('views/themes/'+theme+'/views.js',onViewsDone,true);ajax.loadPage('views/themes/'+theme+'/'+ana.dir+'/template.html','root',onTemplateDone);});};this.initThemeViews=function(viewsJSON)
{ana.views=viewsJSON;var copy=function(source,dest)
{var before={};source.eachAttrib(function(box)
{if(!dest[box])
{if(source[box]<0)
before[box]=1;else
dest[box]=source[box];}});if(before.size())
return copy(dest,before);else
return dest;};var all=ana.views.all;if(all)
{ana.views.eachAttrib(function(view)
{if(view.toLowerCase()!='all')
{all.eachAttrib(function(place)
{if(!ana.views[view][place])
ana.views[view][place]={};ana.views[view][place]=copy(all[place],ana.views[view][place]);});};});all=null;delete ana.views.all;};};this.setDocumentTitle=function(title)
{if(title)
document.title=title;};this.newLocationCounter=function()
{if(ana.locCtr==undefined)
ana.locCtr=1;else
ana.locCtr++;return ana.locCtr+"";};this.getMaxLocationCounter=function()
{return ana.locCtr;}
this.historyChange=function(locationCounter,historyData)
{if(!ana.views)return;if(!historyData);else
{switch(historyData.viewName)
{case'summary':ana.forms.gotoSummary(historyData.oldSection,historyData.newSection,true);break;case'saving':ana.user.changeEmailPass();break;case'results':ana.results.refreshResultss();break;case'forms':ana.forms.setVars(historyData);default:ana.switchView(historyData.viewName,historyData.callback,historyData.onNewView,true);}}
ajax.broadcast('clientRequest','historyChanged');};this.beforeUnload=function(evt)
{var err='Asegurate de GUARDAR tu comparativa antes de marcharte. Evitaras tener que volver a dar tus datos a la aseguradora.';var reason=ana.preventUnloadView(true);if(reason)
return reason+"\n"+err;return err;};this.switchView=function(viewName,callback,onNewView,noHistory)
{var log=$('_test_object_result');ana.switchingTo=viewName;if(!noHistory&&!info.isIE5_5)
{dhtmlHistory.add(ana.newLocationCounter(),{viewName:viewName,callback:callback,onNewView:onNewView},function(){ana.setDocumentTitle(ana.documentTitles[viewName]);});}
else
ana.setDocumentTitle(ana.documentTitles[viewName]);if(viewName==ana.currentView)
{ana.switchViewPending=false;if(callback)callback();return;}
if(ana.switchViewPending)return;ana.switchViewPending=true;if(ana.preventUnloadView(false))
{ana.switchViewPending=false;return;}
var view=ana.views[viewName];if(!view)
ana.connectionFailed();if(viewName=='results'){window.onbeforeunload=ana.beforeUnload;}
else{window.onbeforeunload=null;}
hideToolTip();if(viewName=='results')
$('_template_curvebox').className='';else
$('_template_curvebox').className='curvebox2';ana.sniffers={};ana.switchViewSniffers={};ana.onRenderHandlers={};var previousViewUnloadHandlers=ana.onUnloadHandlers;ana.onUnloadHandlers={};ana.beforeUnloadHandlers={};ana.autoRefreshModules={};var newlyLoadedBoxes={};var totalBoxes=0;var loadedBoxes=0;view.eachAttrib(function(place){if(place.charAt(0)!='_')
totalBoxes+=view[place].size();});var loadingDiv=$('theme_loading');if(loadingDiv)
{loadingDiv.innerHTML='cargando...';loadingDiv.style.display='';}
var placesRemaining=view.size();var allShowingDone=false;var allRemovingDone=false;var switchingDone=function()
{if(allShowingDone&&allRemovingDone)
{var remainingHandlers=0;var callingHandlersDone=false;var onRenderHandlerDone=function()
{if(!remainingHandlers&&callingHandlersDone)
{ana.switchViewPending=false;ana.currentView=viewName;if(loadingDiv)loadingDiv.style.display='none';if(view._topVisibleElement)
scrollToElementTop($(view._topVisibleElement));else
scrollTo(0,0);if(onNewView)onNewView();if(callback)callback();ana.switchViewSniffers.eachFunction(function(func){if(func.indexOf('box_')==0)
ana.switchViewSniffers[func](viewName);});view=null;previousViewUnloadHandlers=null;newlyLoadedBoxes={};loadingDiv=null;};};ana.onRenderHandlers.eachFunction(function(onRenderHandler)
{if(newlyLoadedBoxes[onRenderHandler]&&onRenderHandler.indexOf('box_')==0)
{remainingHandlers++;ana.onRenderHandlers[onRenderHandler]
(function()
{remainingHandlers--;onRenderHandlerDone();});};});callingHandlersDone=true;onRenderHandlerDone();};};var placeDone=function(placeName)
{placesRemaining--;if(placesRemaining<=0)
{allShowingDone=true;switchingDone();};};if(!placesRemaining)
placeDone();var advanceCurrentBox=function(currentBox,placeBoxes)
{if(!currentBox)
currentBox=placeBoxes[0];else
currentBox=currentBox.nextSibling;while(currentBox&&(currentBox.nodeType!=1||currentBox.id.indexOf('box_')!=0))
currentBox=currentBox.nextSibling;return currentBox;};var showBoxes=function(place,boxes)
{var placeDiv=$('theme_'+place);if(!placeDiv)
{alert('Warning: no place name '+'theme_'+place);placeDone(place);return 0;}
var boxesRemaining=boxes.size();var boxDone=function(boxDiv)
{if(boxDiv)
{boxesRemaining--;var installFunction=((boxDiv.id.indexOf('navigator')>=0)?'box_navigator':boxDiv.id)+'_install';eval('try {'+installFunction+'("'+boxDiv.id+'")} '+'catch(e){}');loadedBoxes++;if(loadingDiv)
loadingDiv.innerHTML=T('_.Ldng')+Math.ceil(loadedBoxes*100/totalBoxes)+'%';}
if(boxesRemaining<=0)
{placeDiv=null;placeBoxes=null;placeDone(place);}};var currentBox=null;var placeBoxes=placeDiv.childNodes;if(placeDiv.hasChildNodes())
currentBox=advanceCurrentBox(currentBox,placeBoxes);boxes.eachAttrib(function(box)
{var boxDiv=$('box_'+box);if(boxDiv&&boxDiv.getAttribute('loaded'))
{boxDiv.setAttribute('unwanted',0);if(currentBox==boxDiv)
currentBox=advanceCurrentBox(currentBox,placeBoxes);else
{boxDiv=placeDiv.removeChild(boxDiv);if(currentBox)
placeDiv.insertBefore(boxDiv,currentBox);else
placeDiv.appendChild(boxDiv);};boxDone(boxDiv);}
else
{boxDiv=document.createElement('DIV');boxDiv.setAttribute('unwanted',0);boxDiv.setAttribute('id','box_'+box);if(currentBox)
placeDiv.insertBefore(boxDiv,currentBox);else
placeDiv.appendChild(boxDiv);newlyLoadedBoxes['box_'+box]=1;if(box.indexOf('vnavigator')>=0)
ajax.loadJST('boxes/box_navigator.jst',boxDiv,{'orientation':'v','navlist':boxes[box]},boxDone,true);else if(box.indexOf('hnavigator')>=0)
ajax.loadJST('boxes/box_navigator.jst',boxDiv,{'orientation':'h','navlist':ana.views.maintabs[boxes[box]]},boxDone,true);else
ajax.loadJST('boxes/box_'+box+'.jst',boxDiv,{},boxDone,true);};boxDiv=null;});while(currentBox)
{currentBox.setAttribute('unwanted',1);currentBox=advanceCurrentBox(currentBox,placeBoxes);};if(!boxes.size())
{boxDone();}
return 1;};var removeUnwantedBoxes=function(place)
{var placeDiv=$('theme_'+place);if(!placeDiv)return 0;var currentBox=null;var placeBoxes=placeDiv.childNodes;if(placeDiv.hasChildNodes())
currentBox=advanceCurrentBox(currentBox,placeBoxes);if(currentBox)
{var boxes=[];while(currentBox)
{if(currentBox.getAttribute('unwanted')==1)
boxes.push(currentBox);currentBox=advanceCurrentBox(currentBox,placeBoxes);};for(var currentBoxId=0;currentBoxId<boxes.length;currentBoxId++)
{currentBox=boxes[currentBoxId];if(previousViewUnloadHandlers[currentBox.id])
previousViewUnloadHandlers[currentBox.id]();removeChildSafely(currentBox,placeDiv);};boxes=null;};placeDiv=null;currentBox=null;placeBoxes=null;return 1;};var delayedBlock=function(){var doSwitching=function()
{var result=0;view.eachAttrib(function(place)
{if(place.charAt(0)!='_')
{result+=showBoxes(place,view[place]);}
else
placeDone();});if(result>0)
{view.eachAttrib(function(place)
{if(place.charAt(0)!='_')
{removeUnwantedBoxes(place);}});allRemovingDone=true;switchingDone();};};var neededBoxes={};view.eachAttrib(function(place){if(place.charAt(0)!='_')
{view[place].eachAttrib(function(box){neededBoxes[box]=1;});}});var neededScripts={};neededBoxes.eachAttrib(function(box){var scripts=ana.templatesDependancy[box];if(scripts)scripts.each(function(script){neededScripts[script]=1;});});var remainingScripts=neededScripts.size();if(!remainingScripts)
doSwitching();else
neededScripts.eachAttrib(function(script){ajax.loadJS('scripts/'+script+'.js',function(){remainingScripts--;if(!remainingScripts)doSwitching();},true);});};controller.IE5_5Delay=500;if(info.isIE5_5&&controller.IE5_5Delay)
setTimeout(delayedBlock,controller.IE5_5Delay);else
delayedBlock();controller.IE5_5Delay=0;};this.connectionFailed=function(message)
{if(!message)
alert(controller.results.resultMsgs["_failConn"]);else alert(message);window.location="http://www.asesorseguros.com/comparador.php";}
this.preventUnloadView=function(silent)
{var reason;return controller.beforeUnloadHandlers.eachFunction(function(beforeUnload)
{if(beforeUnload.indexOf('box_')==0)
{reason=controller.beforeUnloadHandlers[beforeUnload](silent);if(reason)return reason;};});};this.documentTitles={startpage:'Asesor Seguros',results:'Resultados comparativa de seguros'}
this.themes={'names':{'1':'Default'},'displayNames':{'1':T('_.Dflt')}};this.preloadedImagesDir=[];this.preloadedImages=[];};
