Type.registerNamespace('AjaxControls');
var MediaTabAjaxObjectId = null;

AjaxControls.UserContext = function (contextVal, updateMediaPlayer)
{
    this.contextValue = contextVal;
    this.updateMediaplayer = updateMediaPlayer;
}

// Define the ContactControl constructor
AjaxControls.MediaTabAjaxBehavior= function(element) {
	
	// Calling constructor of base class
	AjaxControls.MediaTabAjaxBehavior.initializeBase(this, [element]);
	this._ids = null;	
	this.cachedData = new Array();
	this._skeletonobject = null;
	this._mediaobject = null;
		
}

AjaxControls.MediaTabAjaxBehavior.prototype = 
{
	initialize: function() {
		
		AjaxControls.MediaTabAjaxBehavior.callBaseMethod(this, 'initialize');		
		if (this._clickDelegate == null)
		    this._clickDelegate = Function.createDelegate(this, this._clickHandler);
		if ( this.get_SkeletonObject() && this.get_SkeletonObject().ClientHandlers ){ 
		    var contactargs =  null;		    			    
		    for (var i=0; i<this._ids.length; i++){		        
		        contactargs = new AjaxControls.EventArgs.ContactEventArgs(i);		        
		        $addHandler($get(this._ids[i]), "click", Function.createCallback(this._clickDelegate, contactargs ))
		    }
		}
		MediaTabAjaxObjectId = this.get_id();
		//MAKE THE DEFAULT CALL
		this.get_element().style.display = "none";
		this.ShowUpdateProgress();
		this.set_UserContext(0);		
		if (this.get_MediaObject()){
		    eval(this.get_SkeletonObject().ServiceNamespaces[0])(this.get_MediaObject().RequestItem, this.get_MediaObject().MediaID, 
		        this.get_SkeletonObject().ClientHandlers[this._ids[0]], 
		        this.OnCompleteOwnerMediaCall, this.OnErrorOwnerMediaCall, new AjaxControls.UserContext(this.get_UserContext(), false));
		} 
		else if (this.get_MODownloadsObject()){
		    var needmedia = false;
		    if (this.get_MODownloadsObject().PublisherCategoryID == 15 || this.get_MODownloadsObject().PublisherCategoryID == 16) needmedia = true
		    eval(this.get_SkeletonObject().ServiceNamespaces[0])(this.get_MODownloadsObject().PublisherCode, this.get_MODownloadsObject().PublisherCategoryID, 
		    this.get_MODownloadsObject().CategoryID, this.get_MODownloadsObject().ArtistID, 
		    this.OnCompleteOwnerMediaCall, this.OnErrorOwnerMediaCall, new AjaxControls.UserContext(this.get_UserContext(), needmedia ));
		}
		            
	},
	
	// Override dispose function on the Sys.UI.Control class.
	dispose: function() {        
        $clearHandlers(this.get_element());        
        var items = new Array();
        for (var i=0;i<this._ids.length; i++){            
            $clearHandlers($get(this._ids[i]));
        }
        // Call base dispose function
        AjaxControls.MediaTabAjaxBehavior.callBaseMethod(this, 'dispose');
    }, 
    
    get_UserContext : function(){
        return this._requestType;
    },
    
    set_UserContext : function(value){
        this._requestType = value;
    },    
    _clickHandler: function(event, args){  
        this.get_element().style.display = "none";  
        this.ShowUpdateProgress();
        this.set_UserContext(args.get_data());
        var items = new Array();
        var index = args.get_data();          
        Sys.UI.DomElement.addCssClass($get(this._ids[index]), 'selected');                
        for (var i=0; i<this._ids.length; i++){               
            if (i != index){                
                Sys.UI.DomElement.removeCssClass($get(this._ids[i]), 'selected');
            }
        }        
        //MAKE THE SERVICE CALL 
         if (!this.cachedData[index]){   
            if (this.get_MediaObject()){            
               eval(this.get_SkeletonObject().ServiceNamespaces[index])(this.get_MediaObject().RequestItem, 
               this.get_MediaObject().MediaID, this.get_SkeletonObject().ClientHandlers[this._ids[index]], 
               this.OnCompleteOwnerMediaCall, this.OnErrorOwnerMediaCall, new AjaxControls.UserContext(this.get_UserContext(), false));
            }
            if (this.get_MODownloadsObject()){ 
                var needmedia = false;
                if (this.get_MODownloadsObject().PublisherCategoryID == 15 || this.get_MODownloadsObject().PublisherCategoryID == 16) needmedia = true;               
                eval(this.get_SkeletonObject().ServiceNamespaces[index])(this.get_MODownloadsObject().PublisherCode, this.get_MODownloadsObject().PublisherCategoryID, 
                    this.get_MODownloadsObject().CategoryID, this.get_MODownloadsObject().ArtistID, 
                    this.OnCompleteOwnerMediaCall, this.OnErrorOwnerMediaCall, new AjaxControls.UserContext(this.get_UserContext(), needmedia));                
                }               
         }
         else{
               $get(MediaTabAjaxObjectId).style.display = "inline";    
                this.HideUpdateProgress();
                $get(MediaTabAjaxObjectId).innerHTML = this.cachedData[index];
                //only in fulltracks and mp3
                if (this.get_MODownloadsObject() && (this.get_MODownloadsObject().PublisherCategoryID == 15 || this.get_MODownloadsObject().PublisherCategoryID == 16))
                    initAudioPlayer();                    
         }                                      

    },
    
    //properties
    
    get_SkeletonObject : function(){
        return this._skeletonobject;
    },
    
    set_SkeletonObject : function(value){
        if (this._skeletonobject !== value){
            this._skeletonobject = value;
            this.raisePropertyChanged('SkeletonObject');
        }
    },
    
    get_MediaObject : function(){
        return this._mediaobject;   
    },
    
    set_MediaObject : function(value){       
        if (this._mediaobject !== value){
            this._mediaobject = value;
            this.raisePropertyChanged('MediaObject');
        }
    },
        
    get_MODownloadsObject : function(){ 
        return this._modownloadobject;
    },
    
    set_MODownloadsObject : function(value){
        if (this._modownloadobject !== value){
            this._modownloadobject = value;
            this.raisePropertyChanged('MODownloadsObject');
        }
    },
    
    get_IDs : function(){
        return this._ids;
    },
    
    set_IDs : function(value){        
        if (this._ids !== value){
            this._ids = value;
            this.raisePropertyChanged('IDs');
        }
    },
    
    ShowUpdateProgress : function(){    
        var progress= $get(this.get_SkeletonObject().AjaxLoaderID);     
        if (progress) {            
            progress.style.display= "inline";
        }        
    },
        
    HideUpdateProgress : function(){
        var progress= $get(this.get_SkeletonObject().AjaxLoaderID);
        if (progress) {            
            progress.style.display= "none";
        }
    },
            
    OnCompleteOwnerMediaCall : function(result, userContext){       
        var html = result.Html;
        var message = result.Message;
        
        if (userContext.contextValue == $find(MediaTabAjaxObjectId).get_UserContext() &&
            !$find(MediaTabAjaxObjectId).cachedData[userContext.contextValue]){
            
            $find(MediaTabAjaxObjectId).cachedData[userContext.contextValue] = html;               
            $get(MediaTabAjaxObjectId).style.display = "inline";    
            $find(MediaTabAjaxObjectId).HideUpdateProgress();      
            if (!message)                            
                $get(MediaTabAjaxObjectId).innerHTML = html;            
            else
                $get(MediaTabAjaxObjectId).innerHTML = "<div style='position:relative; top:45%; left:32% ' width='100%'  ><strong>" + message +"</strong></div>";
            
        } 
        if (userContext.updateMediaplayer)               
            initAudioPlayer();
    },       
    
    OnErrorOwnerMediaCall : function(){
            
    }
        
}

AjaxControls.MediaTabAjaxBehavior.registerClass('AjaxControls.MediaTabAjaxBehavior', Sys.UI.Control);
Sys.Application.notifyScriptLoaded();
