/*
  Written by J. Carlos Nieto
  --------------------------
  (c) 2010 Astrata Software.
  http://astrata.mx
*/

var Red10={'options':{'token':'','barId':0,'registerEmail':true,'registerCellphone':false,'displayAds':true},'start':function(options){this.extend(this.options,options);},'login':function(){this.components.loginBox.toggle();return false;},'sync':function(token){this.options.token=token;location.href=this.createUrl('/users/sync');},'register':function(){this.components.registerBox.toggle();return false;},'logout':function(){location.href=this.createUrl('/users/logout');return false;},'createUrl':function(param){return'http://c.red10.com/'+param+(param.match(/\?/)?'&':'?')+'nocache='+Math.random()+'&bid='+this.options.barId+'&token='+this.options.token;},'createElement':function(name,attr){var el=document.createElement(name);if(attr){for(var name in attr){if(name=='class'){el.className=attr[name];}else{el.setAttribute(name,attr[name]);}}}
return el;},'centerElement':function(el){var y=this.pageScrollY()+(this.clientHeight()-el.offsetHeight)/2;var x=this.pageScrollX()+(this.clientWidth()-el.offsetWidth)/2;this.setStyles(el,{'top':(y>0?y:0)+'px','left':(x>0?x:0)+'px'});},'pageScrollX':function(){if(self.pageXOffset){return self.pageXOffset;}else if(document.documentElement&&document.documentElement.scrollLeft){return document.documentElement.scrollLeft;}else if(document.body){return document.body.scrollLeft;}},'pageScrollY':function(){if(self.pageYOffset){return self.pageYOffset;}else if(document.documentElement&&document.documentElement.scrollTop){return document.documentElement.scrollTop;}else if(document.body){return document.body.scrollTop;}},'clientHeight':function(){var heigth=0;if(window.innerHeight){height=window.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){height=document.documentElement.clientHeight;}else if(document.body){height=document.body.clientHeight;}
return height;},'clientWidth':function(){var width=0;if(window.innerWidth){width=window.innerWidth;}else if(document.documentElement&&document.documentElement.clientWidth){width=document.documentElement.clientWidth;}else if(document.body){width=document.body.clientWidth;}
return width;},'pageWidth':function(){var size;var clientsize=this.clientWidth();if(window.innerWidth&&window.scrollMaxX){size=window.innerWidth+window.scrollMaxX;}else if(document.body.scrollWidth>document.body.offsetWidth){if(this.ie6()){size=document.body.scrollWidth+(document.body.offsetWidth-document.body.offsetWidth);}else{size=clientsize;}}else{size=document.body.offsetWidth;}
return(size>clientsize?clientsize:size);},'pageHeight':function(){var size;var clientsize=this.clientHeight();if(window.innerHeight&&window.scrollMaxY){size=window.innerHeight+window.scrollMaxY;}else if(document.body.scrollHeight>document.body.offsetHeight){size=document.body.scrollHeight;}else{size=document.body.offsetHeight;}
return(size>clientsize?size:clientsize);},'setStyles':function(element,styles){for(var style in styles){element.style[style]=styles[style];}},'scanElement':function(parent,search){if(typeof parent.nodeType!=undefined&&parent.nodeType==1){if(search.substr(0,1)=='.'){var className=' '+search.substr(1)+' ';var testClass=' '+parent.className+' ';if(testClass.match(new RegExp(className))){return parent;}}else{if(parent.nodeName.toLowerCase()==search.toLowerCase()){return parent;}}
for(var i=0;i<parent.childNodes.length;i++){var node=this.scanElement(parent.childNodes[i],search);if(node){return node;}}}
return false;},'destroyChildren':function(element){if(element.childNodes!=undefined){while(element.childNodes.length>0){this.destroyElement(element.lastChild);}}},'destroyElement':function(element){if(element){this.destroyChildren(element);if(element.parentNode){element.parentNode.removeChild(element);}
element.deleted=true;delete element;}},'update':function(data){if(data.notices){for(var i=0;i<data.notices.length;i++){this.notices[i]=data.notices[i];}}
if(data.promo_button!=1){this.hidePromo();}
if(data.alert_button!=1){this.hideAlert();}
if(data.mobile_button!=1){this.hideMobile();}
if(data.profile_button!=1){this.hideProfile();}
if(data.stats_button!=1){this.hideStats();}
if(data.user){this.components.level.innerHTML=data.user.level;if(data.user.points!=undefined){if(data.user.points.length>6){this.components.points.innerHTML=data.user.points+'p';}else{this.components.points.innerHTML=data.user.points+'pts';}}
if(data.alerts!=undefined){this.getElement(this.components.buttonAlerts,'.r10-item1').innerHTML=data.alerts;}
if(data.messages!=undefined){this.getElement(this.components.buttonMessages,'.r10-item3').innerHTML=data.messages;}
if(data.user.token!=this.options.token){this.sync(data.user.token);}
if(this.isHidden<0){this.showBar();}
this.loggedIn=true;}else{if(this.options.token){this.logout();}
if(this.isHidden<0){this.hideBar();}
this.loggedIn=false;}},'hidePromo':function(){this.hide(this.components.buttonPromo);},'hideAlert':function(){this.hide(this.components.buttonAlerts);},'hideMobile':function(){this.hide(this.components.buttonSms);},'hideProfile':function(){this.hide(this.components.buttonProfile);},'hideStats':function(){this.hide(this.components.buttonPoints);},'ping':function(){if(!this.pokes){this.pokes=0;}
var rand=Math.random();var script=this.id('red10-toolbar-ping');if(script){this.destroyElement(script);}
var script=Red10.createElement('script',{'id':'red10-toolbar-ping'});script.src=this.createUrl('/notices/ajax_ping?poke='+this.pokes);document.body.appendChild(script);this.pokes++;},'getElement':function(parent,search){var search=search.split(' ');for(var i=0;i<search.length;i++){var chunk=search[i];var el=this.scanElement(parent,chunk);if(el){parent=el;}else{return false;}}
return el;},'addStylesheet':function(url){url='http://c.red10.com/1.0/css/'+url+'.css';this.documentHead().appendChild(this.createElement('link',{'rel':'stylesheet','type':'text/css','href':url}));},'build':function(){this.template=this.createElement('span');this.template.innerHTML='<div class="r10-content-box r10-active-box"><div class="r10-head"><a class="r10-btn-close" href="#" onclick="return false;">close</a><!-- title --><strong></strong></div><!-- content --><div class="r10-content-div"></div></div><div class="r10-spinner"><div class="r10-spinner-img"></div></div><div class="r10-event-box"><div class="r10-c"><div class="r10-ico"><div><!-- icon --><img /></div></div><div class="txt-hold"><!-- content --><p></p></div></div><div class="r10-b"></div></div><div class="r10-bar" id="r10-wrapper"><div class="r10-main"><!-- red10 main --></div><!--<div id="r10-hidden" class="r10-main"></div>--><div class="r10-footer-bar"><div id="r10-footar-nav"><ul class="r10-right"><li class="r10b-profile r10b-button"><a class="r10-footer-item" href="#" onclick="return false;"><span class="r10-hold"><span>Perfil</span></span></a></li><li class="r10b-promo r10b-button"><a class="r10-footer-item" href="#" onclick="return false;"><span class="r10-hold"><span>Promociones</span></span></a></li><li class="r10b-points r10b-button"><a class="r10-footer-item" href="#" onclick="return false;"><span class="r10-hold"><span class="r10-total-points">Puntos</span></span></a></li><li class="r10b-toggle"><a class="r10-footer-item" href="#" onclick="return false;"><em class="r10-btn-show"><em>Show</em></em></a></li></ul><ul class="r10-left"><li class="r10b-alerts r10b-button"><a class="r10-footer-item" href="#" onclick="return false;"><span class="r10-hold"><span class="r10-item1">0</span></span></a></li><li class="r10b-sms r10b-button"><a class="r10-footer-item r10b-sms" href="#" onclick="return false;"><span class="r10-hold"><span class="r10-item2"></span></span></a></li><li class="r10b-messages r10b-button"><a class="r10-footer-item" href="#" onclick="return false;"><span class="r10-hold"><span class="r10-item3">0</span></span></a></li><li class="r10b-level r10b-button"><a class="r10-footer-item" href="#" onclick="return false;"><span class="r10-hold"><span class="r10-item4 r10-current-level"></span></span></a></li></ul><span class="r10-logo"><a class="r10b-logo" href="http://red10.com" target="_blank"><span>Red10</span></a></span></div></div></div>';this.components={'main':this.getElement(this.template,'.r10-main'),'toggle':this.getElement(this.template,'.r10b-toggle a'),'level':this.getElement(this.template,'.r10-current-level'),'points':this.getElement(this.template,'.r10-total-points'),'buttonAlerts':this.getElement(this.template,'.r10b-alerts'),'buttonSms':this.getElement(this.template,'.r10b-sms'),'buttonMessages':this.getElement(this.template,'.r10b-messages'),'buttonLevel':this.getElement(this.template,'.r10b-level'),'buttonPoints':this.getElement(this.template,'.r10b-points'),'buttonPromo':this.getElement(this.template,'.r10b-promo'),'buttonProfile':this.getElement(this.template,'.r10b-profile'),'centerLogo':this.getElement(this.template,'.r10b-logo'),'spinner':this.getElement(this.template,'.r10-spinner'),'eventBox':this.getElement(this.template,'.r10-event-box'),'contentBox':this.getElement(this.template,'.r10-content-box')};this.element=this.getElement(this.template,'.r10-bar');this.components.loginBox=new Red10.popup(this,{'type':'r10-popup-login','title':'Bienvenido(a)','content':{'url':'/users/iframe_login'}});this.components.registerBox=new Red10.popup(this,{'type':'r10-popup-register','title':'Crea tu cuenta','content':{'url':'/users/iframe_register?register_cellphone='+(this.options.registerCellphone?1:0)+'&register_email='+(this.options.registerEmail?1:0)}});this.components.alertBox=new Red10.contentBox(this,{'attachTo':this.components.buttonAlerts,'type':'r10-store','title':'Alertas','content':{'url':'/notices/iframe_index'}});this.components.smsBox=new Red10.contentBox(this,{'attachTo':this.components.buttonSms,'type':'r10-sms-box','title':'Celular','content':{'url':'/mobiles/iframe_index'}});this.components.messagesBox=new Red10.contentBox(this,{'attachTo':this.components.buttonMessages,'type':'r10-message-box','title':'Mensajes privados','content':{'url':'/messages/iframe_index'}});this.components.levelBox=new Red10.contentBox(this,{'attachTo':this.components.buttonLevel,'type':'r10-nivel','title':'Celular','content':{'url':'/levels/iframe_index'}});this.components.pointsBox=new Red10.contentBox(this,{'attachTo':this.components.buttonPoints,'type':'r10-puntos','title':'Puntos','content':{'url':'/points/iframe_index'}});this.components.promoBox=new Red10.contentBox(this,{'attachTo':this.components.buttonPromo,'type':'r10-stock','title':'Promociones','content':{'url':'/promos/iframe_index'}});this.components.profileBox=new Red10.contentBox(this,{'attachTo':this.components.buttonProfile,'type':'r10-profile','title':'Perfil','content':{'url':'/profiles/iframe_index'}});this.buttons=[];for(name in this.components){if(name.match(/^button/)){this.buttons.push(this.components[name]);this.addEvent(this.components[name],'click',this.bindWithEvent(this.buttonClick,this));}}
this.addEvent(this.components.toggle,'click',this.bindWithEvent(this.toggle,this));this.addStylesheet('main');if(this.ie6()){this.addStylesheet('ie6');}
if(this.ie7()){this.addStylesheet('ie7');}
this.notices=[];document.body.appendChild(this.element);if(!this.options.displayAds){this.hide(this.components.buttonPromo);}},'closeAll':function(){var footer=this.getElement(this.element,'.r10-footer-bar');var items=footer.getElementsByTagName('li');for(var i=0;i<items.length;i++){this.removeClass(items[i],'r10-active');this.fireEvent(items[i],'unselect');}},'toggleMessagesBox':function(){this.closeAll();this.toggleButton(this.components.buttonMessages);},'toggleButton':function(el){if(this.hasClass(el,'r10-active')){this.removeClass(el,'r10-active');this.fireEvent(el,'unselect');}else{this.closeAll();this.addClass(el,'r10-active');this.fireEvent(el,'select');}},'overlay':function(){if(!this.__overlay){var overlay=this.createElement('div',{'class':'r10-overlay'});this.setStyles(overlay,{'top':'0px','left':'0px','width':this.pageWidth()+'px','height':this.pageHeight()+'px'});document.body.insertBefore(overlay,this.element);this.__overlay=overlay;}},'removeOverlay':function(){if(this.__overlay){this.destroyElement(this.__overlay);this.__overlay=false;}},'buttonClick':function(ev){var target=this.getEventTarget(ev);var parent=this.getParent(target,'li');this.toggleButton(parent);},'hideBar':function(){this.closeAll();this.addClass(this.element,'r10-mini');this.setOpacity(this.element,'0.7');this.isHidden=1;},'showBar':function(){this.closeAll();this.removeClass(this.element,'r10-mini');this.setOpacity(this.element,'1.0');this.isHidden=0;},'extend':function(origin,extension){if(origin.length==undefined){for(var i in extension){origin[i]=extension[i];}}else{for(var i=0;i<extension.length;i++){origin.push(extension[i]);}}
return origin;},'getEventTarget':function(ev){if(this.ie()){return window.event.srcElement;}else{return ev.target;}},'getParent':function(element,search){while(element){if(element.nodeName.toLowerCase()==search.toLowerCase()){return element;}
element=element.parentNode;}
return false;},'createFunction':function(options){var self=options.func;var options=options||{};if(!options.args){options.args=[];}
return function(event){var args=options.arguments;if(args==undefined){args=[];}
if(typeof args=='string'){args=new Array(args);}
if(options.event){args=Red10.extend([event||window.event],args);}
var returns=function(){return self.apply(options.bind||null,args);}
return returns();}},'bind':function(func,bind,args){return this.createFunction({'func':func,'bind':bind,'arguments':args});},'bindWithEvent':function(func,bind,args){return this.createFunction({'func':func,'bind':bind,'arguments':args,'event':true});},'toggle':function(){if(this.loggedIn==true){if(this.isHidden){this.showBar();}else{this.hideBar();}}else{this.login();}},'documentHead':function(){return document.getElementsByTagName('head')[0];},'initialize':function(){if(this.options.token=='-1'){this.options.token='';}
this.root='http://c.red10.com/1.0/';this.build();window.setInterval(this.bind(this.daemon,this),5000);window.setInterval(this.bind(this.ping,this),30000);this.hideBar();this.ping();this.isHidden=-1;this.loggedIn=-1;},'daemon':function(){if(this.notices.length>0){var notice=this.notices.pop();var ev=new Red10.eventBox(this,notice);ev.show();}},'attach':function(){if(document.body){this.initialize();}else{this.startTimer=window.setTimeout(function(){Red10.attach()},10);}},'id':function(element){if(typeof element=='string'){return document.getElementById(element);}else{return element;}},'hide':function(element){element.style.display='none';},'show':function(element){element.style.display='';},'getCoords':function(element){var coords={'x':0,'y':0};if(element.parentNode){if(element!=document.body){var coords=this.getCoords(element.parentNode);}}
coords.x+=element.offsetTop;coords.y+=element.offsetLeft;return coords;},'fadeOut':function(element){this.hide(element);},'fadeIn':function(element){this.show(element);this.setOpacity(element,1.0);},'set':function(element,name,value){if(element.__redAttr==undefined){element.__redAttr={};}
element.__redAttr[name]=value;},'get':function(element,name){if(element.__redAttr==undefined){element.__redAttr={};}
return(typeof element.__redAttr[name]==undefined)?null:element.__redAttr[name];},'fireEvent':function(element,ev,args){var events=this.get(element,'events');if(events!=undefined){if(events[ev]!=undefined){for(var i=0;i<events[ev].length;i++){events[ev][i](args);}}}},'setContent':function(element,content){Red10.destroyChildren(element);if(typeof content=='object'){var url=this.createUrl(content.url);var loading=this.components.spinner.cloneNode(true);var iframe=this.createElement('iframe',{'scrolling':'no','frameBorder':'0'});iframe.style.width=(element.offsetWidth)+'px';iframe.style.height=(element.offsetHeight)+'px';this.setOpacity(iframe,0);this.addEvent(iframe,'load',this.bind(function(el){this.setOpacity(this.get(el,'iframe'),1);this.show(this.get(el,'iframe'));this.destroyElement(this.get(el,'loading'));},this,[element]));element.appendChild(loading);element.appendChild(iframe);iframe.src=url;this.set(element,'iframe',iframe);this.set(element,'loading',loading);}},'setOpacity':function(element,opacity){if(opacity==0){element.style.visibility='hidden';}else{element.style.visibility='visible';}
element.style.opacity=opacity;},'ie':function(){if(navigator.userAgent.match(/MSIE/)&&!navigator.userAgent.match(/Opera/)){return true;}
return false;},'ie6':function(){if(navigator.userAgent.match(/MSIE\s6\./)){return true;}
return false;},'ie7':function(){if(navigator.userAgent.match(/MSIE\s7\./)){return true;}
return false;},'addEvent':function(element,ev,callback){var events=this.get(element,'events');if(!events){events={};}
if(!events[ev]){events[ev]=[];}
events[ev].push(callback);this.set(element,'events',events);if(this.ie()){return element.attachEvent('on'+ev,callback);}else{return element.addEventListener(ev,callback,false);}},'removeClass':function(element,className){var testClass=' '+element.className+' ';var className=' '+className+' ';var newClass=testClass.replace(new RegExp(className),' ');if(this.ie()){element.className=newClass;}else{element.setAttribute('class',newClass);}},'hasClass':function(element,className){var testClass=' '+element.className+' ';var className=' '+className+' ';return testClass.match(new RegExp(className));},'addClass':function(element,className){if(!this.hasClass(element,className)){if(this.ie()){element.className=element.className+' '+className;}else{element.setAttribute('class',element.className+' '+className);}}},'Class':function(params){var newClass=function(){var value=this.initialize?this.initialize.apply(this,arguments):this;return value;};for(var param in params){newClass.prototype[param]=params[param];}
return newClass;}};Red10.eventBox=Red10.Class({'options':{'icon':'http://c.red10.com/images/ico-event.png'},'initialize':function(parent,options){this.parent=parent;Red10.extend(this.options,options);this.element=this.parent.components.eventBox.cloneNode(true);var img=Red10.getElement(this.element,'img');if(this.options.icon){img.src=this.options.icon;}else{Red10.destroyElement(Red10.getElement(this.element,'.r10-ico'));}
Red10.getElement(this.element,'p').innerHTML=this.options.content;Red10.addClass(this.element,'r10-active-box');window.setTimeout(Red10.bind(this.close,this),5000);if(this.options.type=='message'){Red10.addEvent(this.element,'click',Red10.bind(Red10.toggleMessagesBox,Red10));}
Red10.addEvent(this.element,'click',Red10.bindWithEvent(this.close,this));Red10.hide(this.element);},'close':function(){Red10.fadeOut(this.element);},'show':function(){this.parent.components.main.appendChild(this.element);Red10.fadeIn(this.element);}});Red10.popup=Red10.Class({'initialize':function(parent,options){this.parent=parent;this.options=options;this.element=this.parent.components.contentBox.cloneNode(true);Red10.addClass(this.element,'r10-popup');Red10.addClass(this.element,options.type);Red10.getElement(this.element,'strong').innerHTML=this.options.title;Red10.addEvent(Red10.getElement(this.element,'.r10-btn-close'),'click',Red10.bindWithEvent(this.hide,this));this.attachedTo=Red10.id(this.options.attachTo);if(!this.attachedTo){this.attachedTo=Red10.createElement('div');}
Red10.hide(this.element);document.body.appendChild(this.element);Red10.addEvent(this.attachedTo,'click',Red10.bindWithEvent(this.toggle,this));},'toggle':function(){if(this.displayed){this.hide();}else{this.show();}},'hide':function(){Red10.fadeOut(this.element);Red10.removeClass(this.attachedTo,'r10-active');Red10.destroyChildren(Red10.getElement(this.element,'.r10-content-div'));Red10.removeOverlay();this.displayed=false;},'show':function(){Red10.overlay();Red10.setOpacity(this.element,0);Red10.show(this.element);Red10.centerElement(this.element);Red10.setContent(Red10.getElement(this.element,'.r10-content-div'),this.options.content);Red10.fadeIn(this.element);Red10.addClass(this.attachedTo,'r10-active');this.displayed=true;}});Red10.contentBox=Red10.Class({'initialize':function(parent,options){this.parent=parent;this.options=options;this.element=this.parent.components.contentBox.cloneNode(true);Red10.addClass(this.element,this.options.type);Red10.getElement(this.element,'strong').innerHTML=this.options.title;Red10.addEvent(Red10.getElement(this.element,'.r10-btn-close'),'click',Red10.bindWithEvent(this.hide,this));this.attachedTo=Red10.id(this.options.attachTo);Red10.hide(this.element);this.parent.components.main.appendChild(this.element);Red10.addEvent(this.attachedTo,'select',Red10.bindWithEvent(this.show,this));Red10.addEvent(this.attachedTo,'unselect',Red10.bindWithEvent(this.hide,this));},'hide':function(){Red10.fadeOut(this.element);Red10.removeClass(this.attachedTo,'r10-active');Red10.destroyChildren(Red10.getElement(this.element,'.r10-content-div'));Red10.removeOverlay();},'show':function(){Red10.overlay();Red10.setOpacity(this.element,0);Red10.show(this.element);Red10.setContent(Red10.getElement(this.element,'.r10-content-div'),this.options.content);Red10.fadeIn(this.element);Red10.addClass(this.attachedTo,'r10-active');}});Red10.attach();
