
var fi=fi||{};fi.component=fi.component||{};fi.component.ModalPopup=function($context){this.$context=$context;this.$container=$(".container",$context);this.$contentHolders=$(".body .content",$context);this.$head=$(".head",$context);this.$body=$(".body",$context);this.$title=$(".title",$context);this.$link=$(".link",$context);this.$subscribe=$(".subscribe",$context);this.$closeButton=$("a.close",$context);this.type=null;this.isOpen=false;this.scrollTop=0;this.videoPlayer=null;this.contentHolders={};this._onBackgroundClick=$.proxy(this.onBackgroundClick,this);this._onContainerClick=$.proxy(this.onContainerClick,this);this._onWindowScroll=$.proxy(this.onWindowScroll,this);};fi.component.ModalPopup.TYPE_VIDEO="type-video";fi.component.ModalPopup.TYPE_BLOG="type-blog";fi.component.ModalPopup.TYPE_HTML="type-html";fi.component.ModalPopup.prototype={init:function(){this.contentHolders[fi.component.ModalPopup.TYPE_VIDEO]=$(".content."+fi.component.ModalPopup.TYPE_VIDEO,this.$context);this.contentHolders[fi.component.ModalPopup.TYPE_BLOG]=$(".content."+fi.component.ModalPopup.TYPE_BLOG,this.$context);this.contentHolders[fi.component.ModalPopup.TYPE_HTML]=$(".content."+fi.component.ModalPopup.TYPE_HTML,this.$context);this.initVideoPlayer();this.$context.bind("click",this._onBackgroundClick);this.$container.bind("click",this._onContainerClick);this.$closeButton.bind("click",this._onBackgroundClick);},initVideoPlayer:function(){this.videoPlayer=fi.common.ComponentLoader.findInstanceById("popup-player");this.$videoPlayer=$(".video-player",this.$context);if(typeof(this.videoPlayer)!=="undefined"){this.videoPlayer.embedFlash();}},setType:function(type){this.$context.removeClass(this.type);$(".content",this.$context).hide();this.type=type;switch(type){case fi.component.ModalPopup.TYPE_VIDEO:if(typeof(this.videoPlayer)==="undefined"){this.initVideoPlayer();}
break;case fi.component.ModalPopup.TYPE_BLOG:break;case fi.component.ModalPopup.TYPE_HTML:break;default:this.type=null;return;}
$('.'+this.type,this.$context).show();this.$context.addClass(this.type);},populateBlogEntry:function(blogID,postID,data){this.$title.html("Article from:");this.$link.html(data.entry.author[0].name.$t).show();this.$link.attr("href","http://www.blogger.com/profile/"+blogID);var content=$("<div>").html(data.entry.content.$t);if(data.entry.category!=undefined){var labels=$("<p>Labels: </p>").addClass("categories");for(var i=0,n=data.entry.category.length;i<n;++i){labels.append($("<a>").attr("href","#").text(data.entry.category[i].term));}}
var date=new Date(data.entry.published.$t);content.prepend(labels);var minutes=new String(date.getMinutes());if(minutes.length==1){minutes='0'+minutes;}
content.prepend($("<p>").addClass("date").text(date.toDateString()+" | "+date.getHours()+":"+minutes));content.prepend($("<h1>").text(data.entry.title.$t));this.$subscribe.attr("href","http://www.blogger.com/feeds/"+blogID+"/posts/default/");this.$subscribe.unbind("click").bind("click",function(){window.open("http://www.blogger.com/feeds/"+blogID+"/posts/default/");}).show();this.contentHolders[this.type].html(content);$('.type-blog a').attr('target','_blank');},setContent:function(title,link,data){this.$link.unbind("click").bind("click",function(){window.open(link);});switch(this.type){case fi.component.ModalPopup.TYPE_VIDEO:this.$subscribe.hide();this.$title.html("Video from:");this.$link.html("YouTube").show();var youtubeID=link.match(/[\w-]+$/i);if(youtubeID){this.videoPlayer.play(youtubeID[0],true);}
break;case fi.component.ModalPopup.TYPE_BLOG:var self=this;fi.models.Blogger.getPost(data.blogid,data.postid,function(result){self.populateBlogEntry(data.blogid,data.postid,result);});break;case fi.component.ModalPopup.TYPE_HTML:this.$link.hide();this.$subscribe.hide();this.$title.text(title);this.contentHolders[this.type].html(data);break;default:}},open:function(type,title,link,data){if(type=="type-video"&&$('html.ipad').length>0){$('.video-player').html('');var videoID=link.replace("http://www.youtube.com/watch?v=","");var html='<iframe class="youtube-player" type="text/html" width="616" height="355" src="http://www.youtube.com/embed/'+videoID+'" frameborder="0"></iframe>';$('.video-player').html(html);}
this.setType(type);this.setContent(title,link,data);this.scrollTop=$(window).scrollTop();if($('html.ipad').length>0){this.$context.css('top',this.scrollTop);}
$(window).scroll(this._onWindowScroll);this.$context.stop().css({display:"block",opacity:0.0}).animate({opacity:1.0,duration:350});this.isOpen=true;},close:function(dispatchEvent){if($('html.ipad').length>0){$('.video-player').html('');}
if(typeof(dispatchEvent)==="undefined"){dispatchEvent=true;}
var self=this;$(window).unbind("scroll",this._onWindowScroll);this.$context.stop().animate({opacity:0.0},{duration:250,easing:"easeOutQuad",complete:function(){self.$context.stop().css({display:"none"});}});this.isOpen=false;if(dispatchEvent){this.$context.trigger("close");}},addEventListener:function(type,handler){this.$context.bind(type,handler);},onWindowScroll:function(event){if($('html.ipad').length>0){return;}
$(window).scrollTop(this.scrollTop);window.pageYOffset=0;if(event.stopImmediatePropagation){event.stopImmediatePropagation();}
if(event.stopPropagation){event.stopPropagation();}
if(event.preventDefault){event.preventDefault();}
return false;},onBackgroundClick:function(event){this.close();return false;},onContainerClick:function(event){event.stopPropagation();var clickedLink=$(event.target);if(clickedLink.is('.link, .subscribe')){return false;}}};fi.common.ComponentLoader.register("modal-popup",fi.component.ModalPopup);
