
function backToBazaar(){location.href="/kidgetbox";}
function backToHomepage(){location.href=pageTabs.getBack();}
var FakeCardPageTabs=Class.create({initialize:function(userInfo){this.isBazaar=false;this.isCard=true;this.isNotification=false;this.kidget=null;this.kidgetData=null;this.hasVoted=null;this.pe=null;this.back="/page/back_to_homepage";this.user=new User();this.pageOwner=this.user;this.tabDialog=new TabDialog();this.communicator=new Communicator();this.justLoggedUser=false;this.user.setUserInfoFromServerData(userInfo);this.tabDialog.loaded(true);this.userManager=new UserManager(this,this.user,this.user,this.tabDialog,'card',this.communicator,true);},startPage:function(myPageHeaderBehaviour,myPageHeaderBehaviourAvoidFamous,showDialogAvvisiAtStartup){this.checkBrowser();this.userManager.checkUserStatusAtStartUp(myPageHeaderBehaviour,myPageHeaderBehaviourAvoidFamous);if(showDialogAvvisiAtStartup){this.userManager.showDialogAvvisi()}},checkBrowser:function(){if(!validateBrowser({skipBrowser:false})){var errori=detectBrowser({skipBrowser:false});if(errori['errore_cookie']){upperBar.showAlert({component:'browser_alert',alert:'cookies_not_enabled'});return false;}
if((errori['errore_browser'])&&(MPCookies.read('check_ok')!='true')){upperBar.showAlert({component:'browser_alert',alert:'browser_not_supported'});return false;}}else{}},loadKidget:function(tabItemId,specificKd){kidgetHeight=$('kd_'+tabItemId).getStyle('height').gsub('px','');if(kidgetHeight>0)
this.addDraggableByTabItemIdOnTabId(tabItemId,this.getCurrentTabId()).setSpecificKidget(specificKd);else
this.pe=new PeriodicalExecuter(this.checkHeight.bind(this,tabItemId,specificKd),0.5);},checkHeight:function(tabItemId,specificKd){kidgetHeight=$('kd_'+tabItemId).getStyle('height').gsub('px','');if(kidgetHeight>0){this.pe.stop();this.pe=null;this.addDraggableByTabItemIdOnTabId(tabItemId,this.getCurrentTabId()).setSpecificKidget(specificKd);}},addDraggableByTabItemIdOnTabId:function(tabItemId,tabId){kidgetWidth=$('kd_'+tabItemId).getStyle('width').gsub('px','');kidgetHeight=$('kd_'+tabItemId).getStyle('height').gsub('px','');h=((kidgetHeight<338)?338:kidgetHeight);$('istanza_kidget').setStyle({height:h+'px'});if(BrowserDetect.isIPad()||BrowserDetect.isIPhone()||BrowserDetect.isIPod()){kidgetObject=$('kd_'+tabItemId);kidgetObject.addEventListener("touchmove",function(e){e.preventDefault();},false);}
this.kidget=new Kidget('kd_'+tabItemId);return this.kidget;},getCurrentTabId:function(){return 0;},turnKidgetBack:function(kidget){kidId=kidget.id
new Effect.Opacity(document.getElementById(kidId).getElementsByClassName("kdfront")[0],{duration:0.5,from:1,to:0,afterFinish:function(effect){document.getElementById(kidId).getElementsByClassName("kdfront")[0].hide(),kidget.afterTurnBack();}});back=$(kidId).getElementsByClassName("kdback")[0]
back.setOpacity(0);back.show();new Effect.Opacity(document.getElementById(kidId).getElementsByClassName("kdback")[0],{duration:0.5,from:0,to:1});},restoreKidget:function(kidget){kidId=kidget.id
front=$(kidId).getElementsByClassName("kdfront")[0]
front.setOpacity(0);front.show();new Effect.Opacity(document.getElementById(kidId).getElementsByClassName("kdfront")[0],{duration:0.5,from:0,to:1,afterFinish:function(effect){kidget.afterRestore();}});new Effect.Opacity(document.getElementById(kidId).getElementsByClassName("kdback")[0],{duration:0.5,from:1,to:0,afterFinish:function(effect){document.getElementById(kidId).getElementsByClassName("kdback")[0].hide()}});},userCanModifyPage:function(){return false;},userCanDownloadMedia:function(){return this.user.canDownloadMedia();},userCanWriteText:function(){return this.user.canWriteText();},checkDeleteKidgetResponse:function(request,shortId){return false;},starsListener:function(){stellaImg=$$("#vota p img");for(i=0;i<5;i++){Event.observe(stellaImg[i],'mouseover',this.starOver.bindAsEventListener(this,i+1));Event.observe(stellaImg[i],'mouseout',this.starOut.bindAsEventListener(this));Event.observe(stellaImg[i],'click',this.starClicked.bindAsEventListener(this,i+1));}},starOver:function(pos,number_of_stars){if(this.hasVoted==null){stellaImg=$$("#vota p img");for(i=0;i<5;i++){if(i<number_of_stars)
stellaImg[i].src="/images/mypage/card/stella_on.gif";else
stellaImg[i].src="/images/mypage/card/stella.gif";}}},starOut:function(){if(this.hasVoted==null){stellaImg=$$("#vota p img");for(i=0;i<5;i++)
stellaImg[i].src="/images/mypage/card/stella.gif";}},starClicked:function(pos,number_of_stars){if(this.hasVoted==null){this.hasVoted=number_of_stars;new Ajax.Request('/card/vote_kidget/?card_tab_item_id='+this.kidget.getShortId()+'&vote='+number_of_stars,{asynchronous:true,evalScripts:false});stellaImg=$$("#vota p img");for(i=0;i<5;i++){if(i<number_of_stars)
stellaImg[i].src="/images/mypage/card/stella_on.gif";else
stellaImg[i].src="/images/mypage/card/stella.gif";stellaImg[i].style.cursor="default";}
grazieBalloon=$$("#vota #grazie")[0];grazieBalloon.src=i18n.imageLocale('/images/mypage/card/grazie_it.png');grazieBalloon.alt=i18n.stringLocale('grazie_voto_kidget');votaLabel=$$("#vota #label_vota")[0];votaLabel.src="/images/mypage/trasparente.gif";votaLabel.alt="";}},checkPageBottom:function(){null;},checkPageBottomAtStartUp:function(){null;},kidgetSponsorLogoClicked:function(kidId,sponsorId){if(sponsorId!='null'){window.open('/sponsor/leave_to/'+sponsorId);}},sendKidgetRequest:function(kidSenderId,url,requestObj,options){if(url.include('show')){url=url.gsub('show','card_show');this.communicator.sendRequest(url,requestObj,options);}else if(url.include('update')){this.kidgetData=requestObj;options.onComplete!=null?options.onComplete():null;}else{this.communicator.sendRequest(url,requestObj,options);}},addKidget:function(kidId){kidget_language=this.kidget.specificKidget.getLanguage()||'it';requestObj={'kidget_id':kidId,'kidget_data':this.kidget.getConfiguration({getEditingContents:true,clearContents:false}),'kidget_language':kidget_language};this.pasteKidget(requestObj);},callback:function(request){var error=false
if(request.status=='200'){response=request.responseJSON;if(response.status.action_result=='OK'){error=false;}else{error=true;}}else{error=true;}
if(!error){backToHomepage();return true;}else{alert(i18n.stringLocale('problema')+":\n"+response.status.message+"\n"+i18n.stringLocale('torna_hp_prova_ancora'))
backToHomepage();return false;}},showDialogAvvisi:function(options){this.userManager.showDialogAvvisi(options);},forceRefresh:function(){this.forcingRefresh=true;window.location.reload(true);},applyLoginToPage:function(){if((typeof args=='undefined')||(typeof args.hideDialog=='undefined')||(args.hideDialog==true)){this.tabDialog.hideDialog();}},performAction:function(){switch(this.actionToPerform.action){case'paste_kidget':this.completePasteKidget(this.actionToPerform.parameters[0])
break;case'notify_kidget':this.tabDialog.isVisible()?this.applyLoginToPage():null;this.sendKidget(this.actionToPerform.parameters[0],this.actionToPerform.parameters[1],this.actionToPerform.parameters[2],this.actionToPerform.parameters[3])
break;}
this.actionToPerform=null;},forceLogout:function(){this.user.logged_in=false;this.tabDialog.hideDialog();},forceRefresh:function(){window.location.reload(true);},pasteKidget:function(kidgetConfiguration){this.startPasteKidget(kidgetConfiguration);},startPasteKidget:function(kidgetConfiguration){if(this.user.isRegistered()){if(this.user.isLoggedIn()){this.completePasteKidget({parameters:kidgetConfiguration})}else{this.userManager.subscribeAction({event:'login',when:'after',callback:new EventCallback({callback:this.completePasteKidget.bind(this),callbackParams:kidgetConfiguration,executeOnce:true,asynchronous:false,blocksQueue:true})})
this.justLoggedUser=true;this.userManager.startLogin({message:i18n.stringLocale('inserisci_nick_e_pwd_per_incollare'),skipShowDialogAvvisi:true});}}else{this.completePasteKidget({parameters:kidgetConfiguration});}},completePasteKidget:function(){var callbackToContinue=arguments[0].callbackToContinue;var kidgetConfiguration=arguments[0].parameters;options={JSON:true,evalJS:true,onComplete:this.checkPasteKidgetResponse.bindAsEventListener(this,{callbackToContinue:callbackToContinue})};this.communicator.sendRequest('/kidget/paste_kidget/',kidgetConfiguration,options);},checkPasteKidgetResponse:function(request){var callbackToContinue=arguments[1].callbackToContinue;var error=false
var title=null;var actions=null;var description=null;var actionOK=null;var actionCancel=null;var size='big';if(request.status=='200'){var response=request.responseJSON;if(response.status.action_result=='OK'){error=false;}else{error=true;title='halt';switch(response.status.action_to_perform){case'register_first':description=response.status.message;actions='1button';actionOK=this.redirectToRegister.bindAsEventListener(this);break;case'already_pasted':description=response.status.message;actions='2buttons';actionOK=this.redirectToTab.bindAsEventListener(this,response.content);actionCancel=callbackToContinue;break;default:description=i18n.stringLocale('problema_server_prova_1_minuto');actionOK=this.afterInternalError.bindAsEventListener(this);size='small';}}}else{error=true;title='halt';description=i18n.stringLocale('problema_server_prova_1_minuto');actionOK=this.afterInternalError.bindAsEventListener(this);size='small';}
if(!error){if(this.justLoggedUser){this.justLoggedUser=false;location.href="/page/back_to_homepage?after=add_kidget&show_dialog_avvisi=true";}else{location.href="/page/back_to_homepage?after=add_kidget";}}else{this.tabDialog.displayMessage({title:title,description:description,actions:actions,actionOK:actionOK,actionCancel:actionCancel,size:size})
this.tabDialog.showDialog({zIndex:this.currentZIndex,dialogMaskHeight:this.newBottom});this.currentZIndex+=1;return false;}},redirectToRegister:function(){location.href="/user/register";},redirectToTab:function(){var tab=arguments[1];location.href="/tab/show/?tab="+tab+"&user="+this.user.nickname;},sendKidget:function(email,nickname,comment,kidgetConfiguration){this.startSendKidget(email,nickname,comment,kidgetConfiguration);},startSendKidget:function(email,nickname,comment,kidgetConfiguration){if((email.length>0)||(nickname.length>0)){var notificationData={'email':email,'nickname':nickname,'comment':comment,'kidget_data':kidgetConfiguration};if(this.user.isRegistered()){if(this.user.isLoggedIn()){this.completeSendKidget({parameters:notificationData})}else{this.userManager.subscribeAction({event:'login',when:'after',callback:new EventCallback({callback:this.completeSendKidget.bind(this),callbackParams:notificationData,executeOnce:true,asynchronous:false,blocksQueue:true})})
this.userManager.startLogin({message:i18n.stringLocale('inserisci_nick_e_pwd_per_invio')});}}else{this.completeSendKidget({parameters:notificationData})}
return true;}else{return false;}},completeSendKidget:function(){var notificationData=arguments[0].parameters;var callbackToContinueLogin=arguments[0].callbackToContinue;options={JSON:true,evalJS:true,onComplete:this.checkNotifyKidgetResponse.bindAsEventListener(this,callbackToContinueLogin)};this.communicator.sendRequest('/kidget/notify_kidget/',notificationData,options);},checkNotifyKidgetResponse:function(){var request=arguments[0];var callbackToContinueLogin=arguments[1];var error=false
if(request.status=='200'){response=request.responseJSON;if(response.status.action_result=='OK'){error=false;}else{error=true;}}else{error=true;}
if(error&&(request.status!='200')){this.tabDialog.displayMessage({title:'halt',description:i18n.stringLocale('problema_server_prova_1_minuto'),actionOK:this.afterInternalError.bindAsEventListener(this),size:'small'})
this.tabDialog.showDialog({zIndex:this.currentZIndex,dialogMaskHeight:this.newBottom});this.currentZIndex+=1;return false;}else{if(callbackToContinueLogin!=null){callbackToContinueLogin();}}},getUserLang:function(){return this.user.lang!=null?this.user.lang:'it';},afterInternalError:function(){this.tabDialog.hideDialog();generalReloadPage();},setBack:function(back){this.back=back},getBack:function(){return this.back;},registerUser:function(){location.href="/user/register/";}})