﻿// JScript File

/*_____________________________________________*/
/*RELATED COMPONENT*/
/*---------------------------------------------*/

         var RelatedFlicksComponent= 
         {
            //var me = this ;
            Items: new Array(),
            //Count: "",
              TargetPanel: "",
            Content: "",
            ContentView: "",
              Filter: 4,
            type:35,
            Sort:0,
            timespan:1,
            Adult:0,
            MaxResult:30,
            ToJson:1,
            ResultDisplay:16,
            Random:1,
            EnablePaging:false,
            ResultPerPage:4,
            PageCount:0,
            CurrentPage:0,
            isLastPage:false,
            isFirstPage:true,
            Ref:"",
            Pages: new Array(),
            
            GetQueryString: function(){
               //http://localhost/aniboom/Pages/Application/Feeds/Rss.aspx?sou=4&typ=35&sor=10&tim=1&adu=0&num=30&trg=1&itr=12&rnd=1
               return "sou="+this.Filter+"&typ="+this.type+"&sor="+this.Sort+"&tim="+this.timespan+"&adu="+this.Adult+"&num="+this.MaxResult+"&trg="+this.ToJson+"&itr="+this.ResultDisplay+"&rnd="+this.Random;
              
            },
            
            Reset : function(){
                this.Items = new Array();    
            },
            
            AddElement : function(elem){
            
                this.Items[this.Items.length] = elem;
                //Count++;
            },
        
            GetElementByName :function(elemName){
                for(i=0;i<this.Items.length;i++){
                    if(this.Items[i].Id==elemName)
                    return this.Items[i];
                }
            },
             RenderResultWithPaging:function()
            {
                   cnt=0;
                   this.PageCount=Math.ceil(this.Items.length/this.ResultPerPage)
                   this.CurrentPage=1;
                   this.Content = "";
                   this.TargetPanel = $(this.TargetPanel);
                   for(j=0;j<this.PageCount;j++)
                   {
                       page=j+1;
                       this.Content +="<div id='RelatedPage"+page+"' "
                       if(j==0)
                          this.Content += "style='display:block; margin-left:15px;' >"
                       else
                          this.Content += "style='display:none; margin-left:15px;' >"
                          
                       for(i=0;i<this.ResultPerPage && cnt<this.Items.length ;i++)
                       {
                            this.Content += this.RenderFlickItem(this.Items[cnt],cnt); 
                            cnt++;       
                       }
                       this.Content+="</div>";
                       this.Pages[j]="RelatedPage"+page;
                   }
                   
                   this.TargetPanel.innerHTML = this.Content;
                   

            },
            NextPage:function()
            {
              
              //alert(($(this.Pages[this.CurrentPage-1])).display)
              ($(this.Pages[this.CurrentPage-1])).style.display="none";
              this.CurrentPage++;
              ($(this.Pages[this.CurrentPage-1])).style.display="block";
              if(this.CurrentPage==this.PageCount)
                 this.isLastPage=true;
              else
                 this.isLastPage=false;
                 
                 this.isFirstPage=false;
              
              
            },
            PreviousPage:function()
            {
              //alert(($(this.Pages[this.CurrentPage-1])).display)
              ($(this.Pages[this.CurrentPage-1])).style.display="none";
              this.CurrentPage--;
              ($(this.Pages[this.CurrentPage-1])).style.display="block";
              if(this.CurrentPage==1)
                 this.isFirstPage=true;
              else
                this.isFirstPage=false;
                
                this.isLastPage=false;
                
              
            },
            
            RenderResult:function()
            {
               
              if(!this.EnablePaging)
              {
                   this.Content = "";
                   this.TargetPanel = $(this.TargetPanel);
                   for(i=0;i<this.Items.length;i++)
                   {
                        this.Content += this.RenderFlickItem(this.Items[i],i); 
                               
                   }
                   
                   this.TargetPanel.innerHTML = this.Content;
              }
              else
              {
               this.RenderResultWithPaging();
              }
               
            },
           
            
            PopulateImages: function()
            {
                
                for(m=0;m<this.Items.length;m++)
                {
                    
                        $("img"+m).src=this.Items[m].ThumbUrl;
                    
                }
            },
            GetRelatedAnimations:function(){
//                    relatedTimer=setTimeout("SwitchView('tabRecent','RadioheadRecent')",5000);
                    new Ajax.Request(baseUrl,
                    {   parameters: '',
                        method: 'post', 
                        onSuccess: function(transport)
                        {
                            s=transport.responseText.replace(/FlicksCollection/g,'RelatedFlicksComponent');
//                            clearTimeout(relatedTimer);  
                            RelatedFlicksComponent.Reset();
                            eval(s);
                            RelatedFlicksComponent.RenderResult(); 
                            RelatedFlicksComponent.PopulateImages();
                        },
                        
                        onFailure: function(){
                            alert("problem with ajax");
                            //window.status = "problem with ajax";
                        }
                        
                    });
            
            },
            switchToImg: function(fullRank)
            {
                fullRank=(fullRank % 0.5) > 0.25 ?
                    (fullRank - (fullRank % 0.5) + 0.5) : fullRank - (fullRank % 0.5);
                    
                    var str =new String(fullRank);
                    
                    return ("bbomb" + str.replace(".5","half") + ".gif");
            },
            RenderFlickItem: function(flick,pos)
            {
                var bombsImg = this.switchToImg(flick.Rating);
                flick.Url=flick.Url+this.Ref;
                fullTitle=flick.Title;
                if(flick.Title.length>10)
                {   
                    flick.Title = flickTitleLength(flick.Title);
                }
                
                        return "" 
                            + "<div title='"+fullTitle+"' style=\"display:inline;text-align:center;margin-top:7px; margin-left:6px;margin-right:6px;float:left;width:80px;height:100px;\">" 
                            + "<div onmouseover=\"relatedTabMouseOver('title"+pos+"')\" onmouseout=\"relatedTabMouseOut('title"+pos+"')\"><a href=\""+flick.Url+"\"><img onError=\"SetDefaultImage(this)\" id=\"img" + pos + "\" style=\"width:76px;height:58px;border:1px solid #78a8bb;\" src=\"" + defualtImg.src + "\""
                            + "class=\"RelatedImage\" /></div> "
                            + "<div onmouseover=\"relatedTabMouseOver('title"+pos+"')\" onmouseout=\"relatedTabMouseOut('title"+pos+"')\" style=\"margin-top:2px;\"><a class=\"relatedLink\" href=\""+flick.Url+"\"><span id=\"title" + pos + "\">" + flick.Title + "</span></a></div>"
                            + "<div style=\"margin-top:2px;\"><img src=\"http://www.aniboom.com/Media/Images/Buttons/" + bombsImg + "\" /></div>"
                            + "<div style=\"color:#ffffff;font-size:11px;margin-top:2px;\" ><span>Views: " + flick.TotalHits + "</span></div>"
                            + "</div>";
                            //style=\"width:75px; height:16px;\" 
                            //http://icons.ainboom.com/live/bombs/
                            //http://www.aniboom.com/Media/Images/Buttons/

            }           
         }
         var baseUrl;    
        var AnalyticsOnLoad=false;

    
    var defualtImg = new Image();
            defualtImg.src="http://icons.aniboom.com/animations/default.jpg";
    
    function SetDefaultImage( obj ) {
        obj.onError!=SetDefaultImage;
       
       obj.src = "http://icons.aniboom.com/animations/default.jpg"

    }
    
    function flickTitleLength(Title)
    {
        return Title.substr(0,10)+"...";
    }
     
    function relatedTabMouseOut(relTabId)
    {
        $(relTabId).style.cursor="default";
        $(relTabId).style.textDecoration="none";
    }
    function relatedTabMouseOver(relTabId)
    {
        $(relTabId).style.cursor="pointer";
        $(relTabId).style.textDecoration="underline";
    }
    
 
