Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}


var EditChannel = function(){
	var createChannel = false;
	var channelName = null;
	var privacySetting = 'Public';
	var tags = new Array();
	var existingContents = new Array();
	var channelContents = new Array();
	
	var channelContentObjects = new Array();
	
	var manualURL;
	var manualTitle;
	var manualUserName;
	
	return{
		init:function(){
			this.createChannel = false;
			this.channelName = '';
			this.privacySetting = 'Public';
			this.tags = null;
			this.tags = new Array();
			
			this.existingContents = null;
			this.existingContents = new Array();
			
			this.channelContents = null;
			this.channelContents = new Array();
			this.channelContentObjects = null;
			this.channelContentObjects = new Array();
			
		},
		
		setChannelName:function(channelName){
			if(jQuery.trim(channelName) == ''){
				this.createChannel = true;
			}
			this.channelName = jQuery.trim(channelName);
		},
		
		isCreateChannel: function(){
			return this.createChannel;
		},
		
		getChannelName:function(){
			return this.channelName;
		},
		
		setPrivacySetting:function(privacySettingValue){
			this.privacySetting = privacySettingValue;
		},
		
		getPrivacySetting:function(){
			return this.privacySetting;
		},
		
		setTags:function(tagsArray){
			this.tags = tagsArray;
		},
		
		addTag: function(tag){
			if(tags == null){
				tags = new Array();
			}
			tags.push(tag);
		},
		
		removeTag: function(tag){
			if(tags == null)
				return true;
			var pos = tags.indexOf(tag);
			if(pos > -1){
				tags.remove(pos);
			}
		},
		
		getTags: function(){
			return tags;
		},
		
		setExistingContents:function(existingContents){
			this.existingContents = existingContents;
		},
		
		addExistingContent: function(existingContent){
			if(existingContents == null){
				existingContents = new Array();
			}
			existingContents.push(existingContent);
		},
		
		removeExistingContent: function(existingContent){
			if(existingContents == null)
				return true;
			var pos = existingContents.indexOf(existingContent);
			if(pos > -1){
				existingContents.remove(pos);
			}
		},
		
		getExistingContents:function(){
			return this.existingContents;
		},
		
		setChannelContents:function(channelContents){
			this.channelContents = channelContents;
		},
		
		
		getChannelContents:function(){
			return this.channelContents;
		},
		
		
		addChannelContent: function(channelContent){
			if(this.channelContents == null){
				this.channelContents = new Array();
			}
			this.channelContents.push(channelContent);
			
			
			
			for(i=0;i<this.existingContents.length; i++){
				if(this.existingContents[i].contentId == channelContent){
					this.channelContentObjects.push(this.existingContents[i]);
					break;
				}
			}
			
		},
		
		removeChannelContent: function(channelContent){
			if(this.channelContents == null)
				return true;
			var pos = this.channelContents.indexOf(channelContent);
			if(pos > -1){
				this.channelContents.remove(pos);
			}
			
			
			for(i=0;i<this.channelContentObjects.length; i++){
				if(this.channelContentObjects[i].contentId == channelContent){
					this.channelContentObjects.remove(i);
					break;
				}
			}
		},
		
		getChannelContentObject: function(){
			return this.channelContentObjects;
		},
		
		getChannelContentHTML: function(){
			var html = '';
			
			for(i=0;i<this.channelContentObjects.length;i++){
				html += '<p style="padding: 0px; margin: 0pt 0pt 8px;">' + this.channelContentObjects[i].text +'</p>';
			}
			return html;
			
		},
		
		setManualURL: function(manualUrl){
			this.manualURL = manualUrl;
		},
		
		setManualTitle: function(manualTitle){
			this.manualTitle = manualTitle;
		},
		
		setManualUserName: function(manualUserName){
			this.manualUserName = manualUserName;
		},
		
		getChannelEmbedCode: function(){
			var embedCode = '';
			if(jQuery.trim(channelName) != ''){
				embedCode = '<script src="'+ArktanUtils.getSiteContext()+'FetchPopularChannel.action?widget=yes&accountName=web2.0helper&channelName=MicroBlog" type="text/javascript"></script>';
			}
			return embedCode;
		},
		
		getChannelLink: function(){
			var channelLink = '';
			if(channelName != ''){
				channelLink = ArktanUtils.getSiteContext()+ 'user/home/channels/'+channelName;
			}
			return channelLink;
		},
		
		submitUrl: function(){
			Ext.Ajax.request({
				   url: 'ajax/ManageUrl.action',
				   params: params,
				   	
				   scope: this,
				   success: function(resp){
				   		var c = resp.responseText;
				   		var resp = Ext.decode(c);
				   		if(resp.success == false){
				   			Ext.Msg.alert('Add Url', resp.error);
							return;
				   		}	
						
						if (params.redirect == 'true') {
							window.location.href = params.contentURL;
						}else {
							window.close();
						}	   				   		 			
				   },
				   
				   failure: function(resp){
				   		Ext.Msg.show("Unable to connect to the server");
				   }
				});
			}
		}
	
}();
