Type.registerNamespace("Insp");
Type.registerNamespace("Insp.UI");


Insp.UI.YoutubePanel = function(element)
{
    Insp.UI.YoutubePanel.initializeBase(this,[element]);
    this._YTfeedSuccessDelegate  = null;
    this._feedYTFailureDelegate  = null;
    this._feedChangeDelegate = null;
    this._videoClickDelegate = null;
    this._popupCtl = null;
    this._playList = null;
    this._components = [];
    this._currentPage = 1;
    this._paginator = null;
    this._pageChangedDelegate = null;
    this._searchClickDelegate = null;
    this._YTCategorySuccessDelegate = null;
    this._searchKeypressDelegate = null;
    this._searchTextfocusDelegate = null;
    this._searchTextblurDelegate = null;
    this._currentCategory = null;
    this._savedSearchText = null;
    this._allCategories = null;
}

Insp.UI.YoutubePanel.prototype =
{
    get_currentCategory: function(){return this._currentCategory;},
    set_currentCategory: function(value){this._currentCategory = value;this.raisePropertyChanged('currentCategory');},
    get_savedSearchText: function(){return this._savedSearchText;},
    set_savedSearchText: function(value){this._savedSearchText = value;this.raisePropertyChanged('savedSearchText');},

    initialize: function()
    {
        Insp.UI.YoutubePanel.callBaseMethod(this,'initialize');
        
        if(this._YTfeedSuccessDelegate === null)
            this._YTfeedSuccessDelegate = Function.createDelegate(this, this._YTfeedSuccess);
        if(this._feedYTFailureDelegate === null)
            this._feedYTFailureDelegate= Function.createDelegate(this, this._YTfeedFailure);
            
        if(this._YTCategorySuccessDelegate=== null)
            this._YTCategorySuccessDelegate= Function.createDelegate(this, this._YTCategorySuccess);
        
        if(this._searchKeypressDelegate  ==null)
            this._searchKeypressDelegate = Function.createDelegate(this,this._searchKeypressSuccess);
            
         if(this._searchTextfocusDelegate  ==null)
            this._searchTextfocusDelegate = Function.createDelegate(this,this._searchKeyfocus)
            
               if(this._searchTextblurDelegate  ==null)
            this._searchTextblurDelegate = Function.createDelegate(this,this._searchKeyblur)
        
        this.addCssClass("ptModYoutube");
        //this._currentCategory
        
       
        
        $get('panelsKaBaap',this.get_element()).style.height="20px";
        $get('panelsKaBaap',this.get_element()).className = "NHLoading";
        
        if (this._feedChangeDelegate === null) {
            this._feedChangeDelegate = Function.createDelegate(this, this._feedChangeHandler);
        }
        
        $addHandler($get('videoCategory',this.get_element()),'change',this._feedChangeDelegate);
        if (this._pageChangedDelegate === null) {
            this._pageChangedDelegate = Function.createDelegate(this, this._pageChangedHandler);
        }
        
        if (this._searchClickDelegate === null) {
            this._searchClickDelegate = Function.createDelegate(this, this._searchClickHandler);
        }
        
        $addHandler($get('searchButton',this.get_element()),'click',this._searchClickDelegate);
        $addHandler($get('searchText',this.get_element()),'keypress',this._searchKeypressDelegate);
        $addHandler($get('searchText',this.get_element()),'focus',this._searchTextfocusDelegate);
        $addHandler($get('searchText',this.get_element()),'blur',this._searchTextblurDelegate);
        
    },
    _searchKeyfocus:function()
    {
        obj=$get('searchText',this.get_element());
        if(obj.getAttribute("modified") == "false")
	    {
		    obj.value = "";
		    obj.setAttribute("modified","true");
		    obj.style.color= "#000000";
		    obj.style.fontStyle= "normal";
	    }
	
    }
    ,
    _searchKeyblur:function(eve)
    {
        obj=$get('searchText',this.get_element());
        if(obj.value == "")
	    {
		    obj.value = strYoutube_orSearch;
		    obj.setAttribute("modified","false");
		    obj.style.color= "#808080";
		    obj.style.fontStyle= "italic";
	    }
	
    }
    ,
    _searchKeypressSuccess:function(eve)
    {
         getEvent=eve.charCode;
         if (getEvent == "13") {   
            this._searchClickHandler();
            return false;
         } else {
            return true;
        }
    },
    _cleanPanels:function()
    {
        var currentComps =this.getComponents(); 
        for(var i = 0;i<currentComps.length;i++)
        {
            this.removeComponent(currentComps[i]);
            currentComps[i].dispose();
        }
        $get('panelsKaBaap',this.get_element()).innerHTML='';   
    },
    _feedChangeHandler:function()
    {
        
        var selectedValue = $get('videoCategory',this.get_element()).value;
        
        if(selectedValue=="")
            return;
            
        this._cleanPanels();
        $get('panelsKaBaap',this.get_element()).style.height="20px";
        $get('panelsKaBaap',this.get_element()).className = "NHLoading";
        
        
            
        if(selectedValue.lastIndexOf("searchtext__")==0)
        {
            this._searchKeyfocus();
            $get('searchText',this.get_element()).value=selectedValue.split("__")[1];
            this.set_currentCategory(selectedValue);
            this._searchClickHandler();
        }
        else
        {
            $get('searchText',this.get_element()).value="";
            this._searchKeyblur();
            LogClient('955','portal module');
            Portal.YoutubeSvc.a(selectedValue,this.get_module().get_moduleID(),1,this._YTfeedSuccessDelegate, this._feedFailDelegate);
        }
    },
    _searchClickHandler:function()
    {
        if($get('searchText',this.get_element()).getAttribute("modified") == "false")
        {
            return;
        }
        newValue=$get('searchText',this.get_element()).value;
        if(newValue == "")
        {
            return;
        }
        newList = this.get_savedSearchText();
        var searchArray = newList.split("###");
      
        //$get('searchText',this.get_element()).value
        
        if(searchArray.length>2)
        {
            var newList='';
            for(var i=0;i<searchArray.length-1;i++)
            {
                searchArray[i]=searchArray[i+1];
                newList = newList + searchArray[i+1] + '###';
            }
        }
        else
        {
            newList = newList + '###';
        }
        
        
        this.set_savedSearchText(newList + newValue);
        
        this._cleanPanels();
        $get('panelsKaBaap',this.get_element()).style.height="20px";
        $get('panelsKaBaap',this.get_element()).className = "NHLoading";
        LogClient('954','portal module');
        Portal.YoutubeSvc.b(newValue,this.get_module().get_moduleID(),this._YTfeedSuccessDelegate, this._feedFailDelegate);
        
        this._YTCategorySuccess(this._allCategories);
    },
    updated: function()
    {
        Portal.YoutubeSvc.getCategories(this._YTCategorySuccessDelegate, this._feedFailDelegate);
        Portal.YoutubeSvc.a(this.get_currentCategory(),this.get_module().get_moduleID(),0,this._YTfeedSuccessDelegate, this._feedFailDelegate);
        Insp.UI.YoutubePanel.callBaseMethod(this,'updated');
    },
    _YTfeedSuccess:function(result)
    {
        $get('divKaBaap',this.get_element()).style.display="block";  
        $get('panelsKaBaap',this.get_element()).style.height="";
        $get('panelsKaBaap',this.get_element()).className = "";
        
        this.removeCssClass("NHLoading");
        this._playList=result;
        this._currentPage = 1;
        this._UpdatePanel();
        this.createPaginator();
    },
    _YTCategorySuccess:function(results)
    
    {
      this._allCategories = results;
      var elSel= $get('videoCategory',this.get_element());
      elSel.innerHTML="";
      //debugger;
      for(var i=0;i<results.length;i++)
      {
        
        var elOptNew = document.createElement('option');
        elOptNew.value=results[i].categoryURI;
        elOptNew.text=results[i].categoryName;
        
        if(elOptNew.value=="")
        {
            elOptNew.disabled=true;    
        }
  
        try {
        elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
        }
        catch(ex) {
            elSel.add(elOptNew); // IE only
        }

      }
      var elOptNew = document.createElement('option');
      
      elOptNew.text='-----'+strYoutube_Searches+'-----';
      elOptNew.disabled=true;
      try {
        elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
        }
        catch(ex) {
            elSel.add(elOptNew); // IE only
        }
      
      var searchArray = this.get_savedSearchText().split("###");
      
       for(var i=0;i<searchArray.length;i++)
      {
        
        var elOptNew = document.createElement('option');
        elOptNew.value="searchtext__"+searchArray[i];
        elOptNew.text=searchArray[i];
  
        try {
        elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
        }
        catch(ex) {
            elSel.add(elOptNew); // IE only
        }

      }
      
      elSel.value=this._currentCategory;
    },
    _UpdatePanel:function()
    {
        if(this._playList==null || this._playList.length==null || this._playList.length==0)
        {
            $get('panelsKaBaap',this.get_element()).style.height="20px"; 
            $get('panelsKaBaap',this.get_element()).innerHTML = strYoutube_NoVideos;
            return;
        }
        for(var i = 0;i<this._playList.length;i++)
            if(this._findVideoPanelCtl(i))this._findVideoPanelCtl(i).set_visible(false);
        
        for(var i= (this._currentPage-1)*4 ;i< this._currentPage*4 ;i++)
            if(this._findVideoPanelCtl(i))
            {   
                this._findVideoPanelCtl(i).set_visible(true);
            }
            else
            {
                if(i<this._playList.length)
                {
                    this._createVideoPanelCtl(i,this._playList[i]);
                    this._findVideoPanelCtl(i).set_visible(true);
                }
            }
            
    },
    _YTfeedFailure:function(result)
    {
        alert('Failure');
    },
    
    _createVideoPanelCtl:function(id,currentVideo)
    {
        var oSpan = document.createElement('SPAN');
		oSpan.id = "VideoPanel"+id;
		$get('panelsKaBaap',this.get_element()).appendChild(oSpan);   
		var e = $create(Insp.UI.YTVideoPanel,{currentVideo:currentVideo},null,{parent:this.get_id()},$get(oSpan.id,this.get_element()));
		this.addComponent(e); 
    },
    _findVideoPanelCtl:function(id)
    {
        return this.findComponent('VideoPanel' + id);
    },
    addComponent:function(e)
    {
        if(Sys.Component.isInstanceOfType(e))
            this._components[e.get_id()] = e;
    },
    getComponents:function()
    {
        var a=[];b= this._components;
        for(var c in b)
        {
            a[a.length] = b[c];
        }
        return a;
    },
    findComponent:function(id)
    {
        return this._components[id];
    },
    removeComponent:function(b)
    {
        var a = b.get_id();
        if(a){delete this._components[a];}
    },
    createPaginator:function()
    {
        if(this._paginator)
        {
            this._paginator.dispose();
            $get('paginationDivKaBaap',this.get_element()).innerHTML="";
        }
        if(this._playList==null || this._playList.length==null || this._playList.length==0)
            return;
        var e = $create(Insp.UI.Paginator,{currentPage:this._currentPage, itemsPerPage:4,totalItems:this._playList.length,numPageLinks:5},{pageChanged:this._pageChangedDelegate},{parent:this.get_id()},$get('paginationDivKaBaap',this.get_element()));
	    this.addComponent(e);
	    this._paginator = e;
    
    },
    _pageChangedHandler:function(sender,args)
    {
        this._currentPage = args.get_currentPage();
        this._UpdatePanel();
    }


}

Insp.UI.YoutubePanel.registerClass('Insp.UI.YoutubePanel',Insp.UI.ModuleContentCtl,Sys.IContainer);

Insp.UI.YTVideoPanel = function(element)
{
    
    Insp.UI.YTVideoPanel.initializeBase(this,[element]);
    this._currentVideo = null;
    this._videoClickDelegate = null;
    this._videoHoverDelegate = null;
    this._videoMouseOutDelegate = null;
}

Insp.UI.YTVideoPanel.prototype =
{
    get_currentVideo: function(){return this._currentVideo;},
    set_currentVideo: function(value){this._currentVideo = value;this.raisePropertyChanged('currentVideo');},

    initialize: function()
    {
        Insp.UI.YTVideoPanel.callBaseMethod(this,'initialize');
        this.addCssClass('YTVideoPanelBaap');
        this.set_visibilityMode(Sys.UI.VisibilityMode.collapse);
        this.get_element().innerHTML = '<span class="YTVideoPanel" id="YTVideoPanel" title="'+this._currentVideo.VideoTitle+'"><div style="background-image:url('+this._currentVideo.ThumbNailURL+')" class="videoThumbnail"><img src="Btn_videoplay.png"/></div><div class="videoTitle">'+this._currentVideo.VideoTitle+'</div><div class="videoDuration">'+this._currentVideo.Duration+'</div></span>';
        if (this._videoClickDelegate === null) {
            this._videoClickDelegate = Function.createDelegate(this, this._videoClickHandler);
        }
        
        if (this._videoHoverDelegate === null) {
            this._videoHoverDelegate = Function.createDelegate(this, this._videoHoverHandler);
        }
        
        if (this._videoMouseOutDelegate === null) {
            this._videoMouseOutDelegate = Function.createDelegate(this, this._videoMouseOutHandler);
        }

        $addHandler(this.get_element(),'click',this._videoClickDelegate);
        
        $addHandler($get('YTVideoPanel',this.get_element()),'mouseover',this._videoHoverDelegate);
        $addHandler($get('YTVideoPanel',this.get_element()),'mouseout',this._videoMouseOutDelegate); /* Had to do this stunt for it to work in IE 6*/
    },
    _videoMouseOutHandler:function()
    {
        Sys.UI.DomElement.removeCssClass($get('YTVideoPanel',this.get_element()),'YTVideoPanelhover');
    },
    _videoHoverHandler:function()
    {
        Sys.UI.DomElement.addCssClass($get('YTVideoPanel',this.get_element()),'YTVideoPanelhover');
    },
    updated: function()
    {
        Insp.UI.YTVideoPanel.callBaseMethod(this,'updated');
    },
    _videoClickHandler:function(a)
    {
        LogClient('953','portal module');
        var pop = this.get_PopupCtl();
        var oDiv = $get(pop.get_PopupControlID()+'_Title');
        var theDiv = $get(pop.get_PopupControlID()+'_PopupContent');
        theDiv.innerHTML = '<div style="border:2px solid white"><div id="ytapiplayer'+this.get_id()+'"></div></div>';
        var params = { allowScriptAccess: "always",allowFullScreen: "true"};
        var atts = { id: "myytplayer"+this.get_id() };
        swfobject.embedSWF("http://www.youtube.com/v/"+this._currentVideo.VideoID+"&enablejsapi=1&autoplay=1&fs=1&playerapiid=myytplayer"+this.get_id(), 
                       "ytapiplayer"+this.get_id(),  "425", "356", "8", null, null, params, atts);
        pop.set_OnCancelScript("swfobject.removeSWF('myytplayer"+this.get_id()+"');");
        pop.show();
    },
    get_PopupCtl: function(){
        if(!this._popupCtl){
            this._createPopupControl()
        }
        return this._popupCtl;
    },
    _createPopupControl:function()
    {
        var el = Insp.UI.PopupHelper.getYoutubePopupElement(this.get_parent().get_module().get_moduleID()+this.get_id()+'_Popup',431,356);
        el.className = 'Popup_Frame';
        this._popupCtl = $create(AjaxControlToolkit.ModalPopupBehavior,{"id":this.get_parent().get_module().get_moduleID()+this.get_id()+'_PopupBehavior',"BackgroundCssClass":"MBD","CancelControlID":el.id+'_PopupCancelImage',"PopupControlID":el.id}, null, null, $get(el.id+'_Title'));
    }
    
    
    
}

Insp.UI.YTVideoPanel.registerClass('Insp.UI.YTVideoPanel',Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

/*function onYouTubePlayerReady(playerId) {
//  ytplayer = $get(playerId);
  //ytplayer.playVideo();
}
*/