// JavaScript Document
//requires prototype.js
var CPS_Query = Class.create();
	CPS_Query.prototype = {
			initialize:function(pos){
				this.position = pos;
				this.get_module_content();
				this.eventManager = new EventPublisher();
			},
			get_module_content:function(){
				new Ajax.Request(
					'/cc-common/js/ajax_services/cps.php',
					{
						method:'post',
						parameters:{pos:this.position},
						requestHeaders: {"Cache-control":'no-cache'},
						on200:this.return_module_content.bind(this)
					}
				);	
			},
			return_module_content:function(transport){
				eval(transport.responseText);
				if(Object.keys(o).length != 0){
					//takeover program id is 21
					if(o.content_type_id == 21){
						this.type = 'takeover';
						if(o.module_content.content.substr(0,1).match(/[0-9a-zA-Z]/)){
							//automated module substrings
							//by comma-separated list
							//VOD: 'vod',track_id
							//NEW: 'new',genre_id,artist_id,media_id
							//NEWS: 'news', news_id
							//these will build out modules with the item featured and a list of other content of that type in the module
							var content_array = o.module_content.content.split(',');
							if(content_array[0].match(/^vod$/)){
								this.takeover_type = 'vod';
								this.track_id = content_array[1];
								this.pic = content_array[2];
								this.teaser = '';
								//put commas back into teaser text since we split on ','
								for(var i =3; i<content_array.length; i++){
									if(i==3){
										this.teaser = this.teaser+content_array[i];	
									}else{
										this.teaser = this.teaser + ',' + content_array[i];
									}
								}
							}else if(content_array[0].match(/^new$/)){
								this.takeover_type = 'new';
								this.artist_id = content_array[1];
								this.loc_id = content_array[2];
							}else if(content_array[0].match(/^news$/)){
								this.takeover_type = 'news';
							}else if(content_array[0].match(/^highlights$/)){
								this.takeover_type = 'highlights';
								this.identifier = content_array.shift();
								this.highlights = {};
								for(var i = 0; i<content_array.length; i+=2){
									this.highlights['artist_'+Object.keys(this.highlights).length] = content_array.slice(i, i+2);
								}
								
							}else if(content_array[0].match(/^cover$/)){
								this.takeover_type = 'cover';
								this.identifier = content_array.shift();
								if(content_array.length>1){
									this.takeover_program = false;
									this.apid = content_array[0];
									this.mid = content_array[1];
								}else{
									this.takeover_program = content_array[1];	
									this.apid = false;
									this.mid = false;
								}
							}
						}else{
							this.content = o.module_content.content;
						}
					}else{
						//it is a NCDB program
						//define the type to be used for the module
						//define the artist id for the module
						//define the media id for the module
						switch(o.content_type_id){
							case '3':
								this.type = 'inconcert';
							break;
							case '9':
								this.type = 'mod';
							break;
							case '4':
								this.type = 'sneak_peek';
							break;
							case '10':
								this.type = 'stripped';
							break;
							case '54':
								this.type = 'video_6_pack';
							break;
							case '59':
								this.type = 'upcoming';
								this.program_id = 59;
							break;
						}
						this.artist_program_id = o.content_id;
						this.media_id = o.feature_id;
					}
					this.eventManager.fireEvent('module_loaded');
				}else{
					this.eventManager.fireEvent('module_null');
				}
				
			},
			_toString:function(){
				var keys = Object.keys(this);
				var values = Object.values(this);
				var string_ref = '';
				for(var i = 0;i<keys.length;i++){
					if(typeof(values[i]) != 'function'){
						string_ref = string_ref + keys[i] +' : '+values[i] +', ';
					}
				}
				var ret = string_ref.substr(0, string_ref.length-2);
				return ret;
			}
	};