(function(D){D.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(F,E){D.fx.step[E]=function(G){if(G.state==0){G.start=C(G.elem,E);G.end=B(G.end)}G.elem.style[E]="rgb("+[Math.max(Math.min(parseInt((G.pos*(G.end[0]-G.start[0]))+G.start[0]),255),0),Math.max(Math.min(parseInt((G.pos*(G.end[1]-G.start[1]))+G.start[1]),255),0),Math.max(Math.min(parseInt((G.pos*(G.end[2]-G.start[2]))+G.start[2]),255),0)].join(",")+")"}});function B(F){var E;if(F&&F.constructor==Array&&F.length==3){return F}if(E=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(F)){return[parseInt(E[1]),parseInt(E[2]),parseInt(E[3])]}if(E=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(F)){return[parseFloat(E[1])*2.55,parseFloat(E[2])*2.55,parseFloat(E[3])*2.55]}if(E=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(F)){return[parseInt(E[1],16),parseInt(E[2],16),parseInt(E[3],16)]}if(E=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(F)){return[parseInt(E[1]+E[1],16),parseInt(E[2]+E[2],16),parseInt(E[3]+E[3],16)]}return A[D.trim(F).toLowerCase()]}function C(G,E){var F;do{F=D.curCSS(G,E);if(F!=""&&F!="transparent"||D.nodeName(G,"body")){break}E="backgroundColor"}while(G=G.parentNode);return B(F)}var A={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);var cloneInstance=function(A){function B(){}B.prototype=A;return new B};var newInstance=function(B){console.log(B);var A=cloneInstance(B);var D=[];for(var C=1;C<arguments.length;C++){D.push(arguments[C])}A.init.apply(A,D);return A};var firebugFix=function(){if(!window.console||!console.firebug){var B=0,A=["log","debug","info","warn","error","assert","dir","dirxml","group","groupEnd","time","timeEnd","count","trace","profile","profileEnd"];window.console={};for(B=0;B<A.length;++B){window.console[A[B]]=function(){}}}};firebugFix();$.fn.yellowFade=function(){return this.each(function(){var A;var B=$(this)[0];attr="backgroundColor";do{A=jQuery.curCSS(B,attr);if(A!=""&&A!="transparent"||jQuery.nodeName(B,"body")){break}attr="backgroundColor"}while(B=B.parentNode);if(A=="rgba(0, 0, 0, 0)"){A="rgb(255, 255, 255)"}$(this).css("backgroundColor","#ffff99").animate({backgroundColor:A},1500)})};String.format=function(C){if(arguments.length<=1){return C}var B=arguments.length-2;for(var A=0;A<=B;A++){C=C.replace(new RegExp("\\{"+A+"\\}","gi"),arguments[A+1])}return C};var getKeys=function(E){var D=[];for(var A in E){D.push(A)}var C={};for(var B in D){C[D]=B}return{keys:D,rKeys:C}};var Settings={Ajax:{type:"post"},SubmitPhotoController:{id:"#pictureForm",textArea:"#pictureForm textarea",form:"#pictureForm form",displayCant:"#pictureForm h5",submitBtn:"#pictureForm input[type=submit]",LimitList:[{name:"MainAlert",value:10},{name:"alert",name:20}],maxChars:140},CommentsController:{id:"#commentForm",moreButton:{$html:".more a",$box:".more"},SubmitCommentController:{textArea:"#commentForm textarea",form:"#commentForm form",displayCant:"#commentForm h5",submitBtn:"#commentForm input[type=submit]",LimitList:[{name:"MainAlert",value:10},{name:"alert",name:20}],maxChars:140},CommentList:{id:"#comments",deleteLink:".delete a",commentId:".comment",CommentContent:".CommentContent"}},PhotosController:{MainPhoto:".MainPost",SidePhotoList:{id:"#SideGallery",deleteLink:".delete a",elementId:".PictureFrame",list:"side",maxCant:2},CenterPhotoList:{id:"#CenterGallery",deleteLink:".delete a",elementId:".PictureFrame",list:"center",maxCant:0},}};var Class={name:"Class",init:function(){return this},extend:function(B){var A=cloneInstance(this);console.log("loading "+B.name,B);return $.extend(cloneInstance(this),B,{parent:A})}};var HtmlUtils=Class.extend({name:"HtmlUtils",renderComment:function(A){var B="";B+='<div class="comment">';B+='	<div class="photo"><a href="/'+A.screen_name+'"><img src="'+A.profile_image_url+'" alt="thumb_normal" width="48" height="48"/></a></div><!-- ends photo -->\r\n';if(A["reply"]&&A["reply"]=="on"){B+='<div class="PictureFrame"><div class="photo"><a href="/'+A.screen_name+"/"+A.reply_photo_id+'"><img src="'+A.reply_photo_url+'" title="'+A.screen_name+'" /></a></div></div>'}B+='	<div class="CommentContent">\r\n';B+='		<p><a href="/'+A.screen_name+'">'+A.screen_name+"</a> "+A.text+"</p>\r\n";B+='		<p class="small"><a href="#">'+A.age+"</a></p>\r\n";B+="	</div><!-- CommentContent -->\r\n";if(A["delete"]&&A["delete"]=="on"){B+='	<ul class="tools">\r\n';B+='		<li class="icon delete"><a href="/guestbook/delete/'+A.photo_id+"/"+A.db_index+"/"+A.gid+'">Delete</a></li>\r\n';B+="	</ul>\r\n"}B+="</div><!-- ends comment -->\r\n";return B},renderMainPhoto:function(A){photo+='<div class="module MainPost">\r\n';photo+='<div id="MainPhoto">\r\n';photo+='<img src="http://'+A.server+"."+A.domain+"/photos/"+A.dir+"/m"+A.photo_id+"."+A.ext+'" alt="'+A.description+'" title="'+A.description+'"  />\r\n';photo+="</div><!-- ends mainphoto -->\r\n";photo+="<p>"+A.description+"</p>\r\n";photo+='<p class="small">'+A.age+' <span class="icon delete"><a href="/photos/delete/'+A.photo_id+'">Delete</a></span></p>\r\n';photo+="</div><!-- ends module -->\r\n";return photo},renderPhoto:function(B){var A="";A+='<div class="PictureFrame" id="ajaxGenerated/'+B.photo_id+'">\r\n';A+='<div class="photo">\r\n';A+='<a href="/'+B.screen_name+"/"+B.photo_id+'">\r\n';A+='<img src="http://'+B.server+"."+B.domain+"/photos/"+B.dir+"/t"+B.photo_id+"."+B.ext+'" alt="'+B.screen_name+'" title="'+B.screen_name+'" />\r\n';A+="</a></div><!-- ends photo -->\r\n";A+="<ul>\r\n";A+="<li>"+B.age+"</li>\r\n";if(!B["delete"]||B["delete"]!="off"){A+='<li class="icon delete"><a href="/'+B.screen_name+"/photos/delete/"+B.photo_id+'">Delete</a></li>\r\n'}A+="</ul>";A+="</div><!-- ends picture -->";return A}});var Ajax=Class.extend({name:"Ajax",doRequest:function(C){var B=this;var A=$.extend({},Settings.Ajax,{dataType:"json",type:"post",success:function(D){if(B.validateData(D)!="ok"){A.error();return }A.successCallback(D)},error:function(D){alert("error en ajax!");A.errorCallback()}},C);console.log("request",A);return $.ajax(A)},validateData:function(A){if(A&&A.head){if(A.head.result&&A.head.result=="ok"){return"ok"}else{if(A.head.msg){return A.head.msg}else{return"Error 101"}}}else{return"Error 100"}}});var Element=Class.extend({name:"Element",$html:null,init:function(C,A){var B=this;this.$html=$(C.$html);this.controller=A;if(this.click){this.$html.click(function(D){return B.click(D)})}if(this.submit){this.$html.submit(function(D){return B.submit(D)})}if(this.keyup){this.$html.keyup(function(D){B.keyup(D)})}}});var Numerator=Class.extend({name:"Numerator",id:null,init:function(){this.id=0},get:function(){if(id==null){throw"Numerator no iniciado!"}return ++this.id}});var Controller=Class.extend({name:"Controller"});var LimitList=Class.extend({name:"LimitList",limits:null,init:function(A){this.limits=[];if(A){for(var B in A){this.addLimit(A[B])}}},addLimit:function(B){var A;for(A in this.limits){if(this.limits[A].name==B.name){return }}A=0;while(A<this.limits.length){if(B.value<this.limits[A].value){break}A++}this.limits.splice(A,0,B)},getLimit:function(D,C){var B=0;var A=null;while(B<this.limits.length&&A==null){if(D<this.limits[B].value){A=this.limits[B].name}B++}if(A==null){A=(arguments.length>1)?C:""}return A}});var ElementList=Class.extend({name:"ElementList",$html:null,elements:null,elementType:null,id:null,init:function(A){this.elements={};this.elementType=A},size:function(){return getKeys(this.elements).keys.length},getElement:function(A){if(this.elements[A]){return this.elements[A]}else{return null}},addElement:function(A,C){var B=newInstance(this.elementType,A,this);this.elements[C]=B},removeElement:function(B,A){if(this.elements[B]){this.elements[B].remove(A);delete this.elements[B]}}});var CommentList=ElementList.extend({name:"CommentList",deleteLink:null,commentContent:null,init:function(B,A){this.elementType=Comment;this.elements={};this.id=B.id;this.controller=A;this.deleteLink=B.deleteLink;this.$html=$(this.id);this.commentContent=B.CommentContent;this.loadElements(B.commentId)},loadElements:function(A){var B=this;$(A,this.$html).each(function(){var F=$(B.commentContent,this).attr("id").split("/");var E=Number(F[F.length-1]);var D=Number(F[F.length-2]);var C=Number(F[F.length-3]);B.addElement({$html:this,deleteLink:B.deleteLink,gid:E,db_index:D,photo_id:C},E)});console.log("Elements",this.elements)},addElement:function(A,D){var B=this;var A=$.extend(A,{deleteLink:this.deleteLink});var C=newInstance(this.elementType,A,this);this.elements[D]=C;if(A.insert&&A.insert=="top"){console.log("where to insert",B.$html,this.elements[D].$html);this.elements[D].$html.prependTo(B.$html).yellowFade()}else{if(A.insert&&A.insert=="bottom"){this.elements[D].$html.appendTo(B.$html)}}}});var MoreButton=Element.extend({name:"MoreButton",posting:false,page:"",db_index:"",photo_id:"",$box:null,init:function(){this.parent.init.apply(this,arguments);var B=this;var A=$(this.$html).attr("href").split("/");this.$box=$(arguments[0].$box);this.id=A[A.length-1];this.page=A[A.length-2];this.db_index=A[A.length-3];this.photo_id=A[A.length-4];this.waitOff()},click:function(){if(!this.posting){this.controller.moreComments()}return false},waitOn:function(){this.posting=true;this.$box.addClass("loader")},waitOff:function(){this.posting=false;this.$box.removeClass("loader")}});var SubmitCommentController=Controller.extend({name:"SubmitCommentController",textArea:null,form:null,submitBtn:null,displayCant:null,maxChars:null,counter:null,disabled:false,posting:false,limitList:null,lastClass:null,controller:null,submitButton:null,submitMode:null,init:function(A){this.submitMode="ajax";this.limitList=newInstance(LimitList,A.LimitList);this.maxChars=A.maxChars;this.controller=A.controller;this.form=newInstance(SubmitCommentForm,{$html:$(A.form)},this);this.displayCant=newInstance(Element,{$html:$(A.displayCant)},this);this.textArea=newInstance(SubmitCommentTextArea,{$html:$(A.textArea)},this);this.submitButton=newInstance(SubmitCommentButton,{$html:$(A.submitBtn)},this);this.waitOff()},doSubmit:function(A){if(this.disabled){alert("Please enter a comment of 140 caracters or less")}if(this.form.mode=="comment"){if(!this.disabled&&!this.posting){this.controller.submitComment(A)}return false}else{if(this.form.mode=="photo"){if(!this.disabled&&!this.posting&&$("input[type=file]",this.form.$html).val()){return true}else{if(!$("input[type=file]",this.form.$html).val()){alert("Please select a photo")}return false}}}},waitOn:function(){var A=this;this.posting=true;$(".SubmitLine",A.form.$html).addClass("loader");this.textArea.waitOn();this.submitButton.waitOff()},waitOff:function(){var A=this;this.posting=false;$(".SubmitLine",A.form.$html).removeClass("loader");this.textArea.waitOff();this.submitButton.waitOff()},updateContChars:function(D){var A=this.maxChars-D;var C=this.limitList.getLimit(A);this.displayCant.$html.text(A);if(C!=this.lastClass){var B=this;this.displayCant.$html.removeClass(B.lastClass).addClass(C);this.lastClass=C}this.disabled=(A<0||D==0);return true}});var SubmitCommentButton=Element.extend({name:"SubmitCommentButton",waitOn:function(){this.$html.attr("disabled","disabled")},waitOff:function(){this.$html.removeAttr("disabled")}});var SubmitCommentTextArea=Element.extend({name:"SubmitCommentTextArea",keyupTimer:false,init:function(){this.parent.init.apply(this,arguments);this.keyup()},keyup:function(B){var A=this;if(!this.keyupTimer){this.keyupTimer=true;setTimeout(function(){A.keyupTimer=false;A.controller.updateContChars(A.$html.val().length)},200)}},waitOn:function(){this.$html.attr("disabled","disabled")},waitOff:function(){this.$html.removeAttr("disabled")}});var SubmitCommentForm=Element.extend({name:"SubmitCommentForm",addPhotoLink:null,mode:null,init:function(){this.parent.init.apply(this,arguments);this.addPhotoLink=this.renderJsControls();this.mode="comment"},toggleMode:function(){if(this.mode=="comment"){$("a",this.addPhotoLink.$html).text("Send a text-only comment");$("input[type=file]",this.$html).removeAttr("disabled").parents("li:first").show();this.mode="photo"}else{if(this.mode=="photo"){$("a",this.addPhotoLink.$html).text("Add a photo to your comment");$("input[type=file]",this.$html).attr("disabled","disabled").parents("li:first").hide();this.mode="comment"}}return false},renderJsControls:function(){$("input[type=file]",this.$html);addPhotoLink=newInstance(AddPhotoLink,{$html:$('<li><a href="#">Add a photo to your comment</a></li>')},this);$("ol:first",this.$html).prepend(addPhotoLink.$html);$("input[type=file]",this.$html).attr("disabled","disabled").parents("li:first").hide();return addPhotoLink},submit:function(){return this.controller.doSubmit(this.getData())},getData:function(){var A={};$("input",this.$html).add("textarea",this.$html).each(function(){if(!$(this).attr("disabled")){A[$(this).attr("name")]=$(this).val()}});return A}});var CommentsController=Controller.extend({name:"CommentsController",commentList:null,moreButton:null,submitCommentController:null,ajax:null,lastRequest:null,conf:null,init:function(A){this.commentList=newInstance(CommentList,A.CommentList,this);this.submitCommentController=newInstance(SubmitCommentController,$.extend({},A.SubmitCommentController,{controller:this}));this.ajax=newInstance(Ajax);this.conf=A;console.log(A.moreButton.$html,$(A.moreButton.$html).size());if(A.moreButton.$html&&$(A.moreButton.$html).size()>0){this.moreButton=newInstance(MoreButton,A.moreButton,this)}},moreComments:function(){var A=this;this.moreButton.waitOn();this.ajax.doRequest({type:"get",url:"/guestbook/get/"+A.moreButton.photo_id+"/"+A.moreButton.db_index+"/"+A.moreButton.page+"/"+A.moreButton.id,successCallback:function(D){for(var C in D.data.comments){console.log(D.data.comments[C],D.data.comments[C].gid,A.commentList.getElement(D.data.comments[C].gid));if(!A.commentList.getElement(D.data.comments[C].gid)){var B=$(HtmlUtils.renderComment(D.data.comments[C]));A.commentList.addElement($.extend({$html:B,insert:"bottom"},D.data.comments[C]),D.data.gid)}}A.moreButton.waitOff();if(!D.data.page||D.data.page=="end"){A.moreButton.$box.hide()}else{A.moreButton.page=D.data.page}},errorCallback:function(){A.moreButton.waitOff()}})},submitComment:function(A){console.log("inciandoAjax");var B=this;this.submitCommentController.waitOn();this.lastRequest=this.ajax.doRequest({url:"/guestbook/postajax",data:A,successCallback:function(D){$.resp=D;console.log(D,HtmlUtils.renderComment(D.data.comments[0]));B.submitCommentController.textArea.$html.val("").keyup();var C=$(HtmlUtils.renderComment(D.data.comments[0]));B.commentList.addElement($.extend({$html:C,insert:"top"},D.data.comments[0]),D.data.comments[0].gid);B.submitCommentController.waitOff()},errorCallback:function(){B.submitCommentController.waitOff()}})},deleteComment:function(A){var B=this;var C=A.gid;this.ajax.doRequest({url:"/guestbook/deleteajax/"+A.photo_id+"/"+A.db_index+"/"+A.gid,successCallback:function(D){B.commentList.removeElement(D.data.gid)},errorCallback:function(){B.commentList.elements[C].working=false}})}});var DeleteLink=Element.extend({name:"DeleteLink",click:function(){return this.controller.del()}});var Comment=Element.extend({name:"Comment",deleteLink:null,gid:null,db_index:null,photo_id:null,working:false,init:function(C,A){console.log("init Comment",C,A);var B=this;this.$html=$(C.$html);this.controller=A;this.gid=C.gid;this.photo_id=C.photo_id;this.db_index=C.db_index;this.deleteLink=newInstance(DeleteLink,{$html:$(C.deleteLink,this.$html)},this)},del:function(){if(!this.working){this.working=true;this.controller.controller.deleteComment({photo_id:this.photo_id,gid:this.gid,db_index:this.db_index},this.gid)}return false},remove:function(){this.$html.fadeOut("normal",function(){$(this).remove()})}});var Photo=Element.extend({name:"Photo",photo_id:null,working:false,deleteLink:null,init:function(B,A){var C=this;this.$html=$(B.$html);this.controller=A;this.photo_id=B.photo_id;this.deleteLink=newInstance(DeleteLink,{$html:$(B.deleteLink,this.$html)},this)},del:function(){if(!this.working){if(!confirm("Are you sure?")){return false}if(this.controller.controller.getMainPhotoId()==this.photo_id){return true}this.working=true;this.controller.controller.deleteElement({photo_id:this.photo_id,list:this.controller.conf.list},this.photo_id)}return false},remove:function(A){this.$html.fadeOut("normal",function(){$(this).remove();if(A){A()}})}});var PhotosController=Controller.extend({name:"PhotosController",sidePhotoList:null,centerPhotoList:null,conf:null,init:function(A){this.sidePhotoList=newInstance(PhotoList,A.SidePhotoList,this);this.centerPhotoList=newInstance(PhotoList,A.CenterPhotoList,this);this.ajax=newInstance(Ajax);this.conf=A},getMainPhotoId:function(){if($(this.conf.MainPhoto).size()>0){var A=$(this.conf.MainPhoto).attr("id");A=A.split("/");if(A.length>0){return A[A.length-1]}}return null},deleteElement:function(B){var C=this;var D=B.photo_id;var A=B.list;this.ajax.doRequest({url:"/photos/deleteajax/"+B.photo_id,successCallback:function(E){C.centerPhotoList.removeElement(D);C.sidePhotoList.removeElement(D,function(){if(C.sidePhotoList.size()<C.sidePhotoList.conf.maxCant){for(var G in E.data.photos){if(!C.sidePhotoList.getElement(E.data.photos[G].photo_id)){console.log("incoming",E.data.photos[G]);var F=$(HtmlUtils.renderPhoto(E.data.photos[G]));console.log("new",F);C.sidePhotoList.addElement($.extend({$html:F,insert:"bottom"},E.data.photos[G]),E.data.photos[G].photo_id)}}}})},errorCallback:function(){C.sidePhotoList.elements[D].working=false}})}});var PhotoList=ElementList.extend({name:"PhotoList",conf:null,deleteLink:null,init:function(B,A){this.conf=B;this.elementType=Photo;this.deleteLink=B.deleteLink;this.controller=A;this.elements={};this.id=B.id;this.$html=$(this.id);this.loadElements(B.elementId)},loadElements:function(A){var B=this;$(A,this.$html).each(function(){var D=$(this).attr("id").split("/");var C=D[D.length-1];B.addElement({$html:this,deleteLink:B.deleteLink,photo_id:C},C)})},addElement:function(A,D){var B=this;var A=$.extend(A,{deleteLink:this.deleteLink});var C=newInstance(this.elementType,A,this);this.elements[D]=C;if(A.insert&&A.insert=="top"){this.elements[D].$html.hide().prependTo(B.$html).fadeIn("fast")}else{if(A.insert&&A.insert=="bottom"){this.elements[D].$html.hide().appendTo(B.$html).fadeIn("fast")}}}});var SubmitPhotoController=SubmitCommentController.extend({name:"SubmitPhotoController",init:function(A){this.submitMode="ajax";this.limitList=newInstance(LimitList,A.LimitList);this.maxChars=A.maxChars;this.controller=A.controller;this.form=newInstance(SubmitPhotoForm,{$html:$(A.form)},this);this.displayCant=newInstance(Element,{$html:$(A.displayCant)},this);this.textArea=newInstance(SubmitCommentTextArea,{$html:$(A.textArea)},this);this.submitButton=newInstance(SubmitCommentButton,{$html:$(A.submitBtn)},this);this.waitOff()},doSubmit:function(A){if(this.disabled){alert("Please enter a comment of 140 caracters or less")}if(!this.disabled&&!this.posting&&$("input[type=file]",this.form.$html).val()){return true}else{if(!$("input[type=file]",this.form.$html).val()){alert("Please select a photo")}return false}}});var SubmitPhotoForm=Element.extend({name:"SubmitPhotoForm",submit:function(){return this.controller.doSubmit(this.getData())},getData:function(){var A={};$("input",this.$html).add("textarea",this.$html).each(function(){A[$(this).attr("name")]=$(this).val()});return A}});var AddPhotoLink=Element.extend({name:"AddPhotoLink",click:function(){return this.controller.toggleMode()}});$(function(){if($(Settings.CommentsController.id).size()==1){var B=newInstance(CommentsController,Settings.CommentsController)}if($(Settings.SubmitPhotoController.id).size()==1){var A=newInstance(SubmitPhotoController,Settings.SubmitPhotoController)}if($(Settings.PhotosController.SidePhotoList.id).size()>0){var C=newInstance(PhotosController,Settings.PhotosController)}})