// JavaScript Document
var mywin;
var vidID;
this.comment_window;
this.comments_panel;

playVideo = function (v,n,u){
	
	vidID = v;
	Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'under';

	if(mywin == undefined){

		/*****************************************
			 Rating Panel
		******************************************/ 
			this.rating_panel = new Ext.Panel({
				width       :530, //530
				height		: 30,
				collapsible : false,
				bodyStyle : 'background:#1F1F1F; padding:4px; border:0px; color:#FFF;',
				
				autoLoad:'/inc/star_rating.cfm?_vlink='+v
			}); 
			
		/*****************************************
			Comment Button
		******************************************/ 
			
			this.comment_btn = new Ext.Button({
				text: 'Add Comment',
				cellCls: 'comment_btn',
				 handler : function (){
					 open_comment_win(v);
				 }
			})
	
		/****************************************************
			Panel to hold ratings panel and comment button
		*****************************************************/ 
			this.rating_holding_panel = new Ext.Panel({
				width       : 640,
				height		: 30,
				collapsible : false,
				bodyCfg :{cls:'my-info-panels'},
				cellCls : 'rating-holding-panel',
				bodyStyle : 'padding:0px;',
				layout : 'table',
				layoutConfig : {columns:2},
				items:[this.rating_panel, this.comment_btn]
			}); 
			
				
		/*****************************************
			Comments Panel
		******************************************/ 
				this.comments_panel = new Ext.Panel({
					title       : 'Video Comments',
					width       : 640,
					height		: 176,
					collapsible : false,
					bodyCfg :{cls:'my-info-panels'},
					margins     : '10 10 10 10',
					cmargins    : '10 10 10 10',
					autoScroll : true,
					autoLoad: '/inc/video_comments.cfm?_vlink='+v
				}); 


		/*****************************************
			Flash Panel
		******************************************/ 
				this.vplayer = new Ext.ux.FlashPanel({
										xtype: 'flashpanel',
										//id:'vplayer',
										bodyStyle: 'backgroundColor:#000; margin:0px; padding:0px; border:0px',
										cellCls : 'vplayer-panel',
										width  : 640,
										height : 360,
										mediaMask: false,
										autoScroll : false,
										mediaCfg:{
													mediaType:'SWF',
													url:'/flash/vplayer.swf?Internal_Link',
													autoSize:true,
													controls:true,
													start:true,
													params:{wmode:'transparent' ,allowscriptaccess : 'always', allowFullScreen: 'true' ,scale:'exactfit', FlashVars: "_vlink="+v }
												 }
										
										
										
										});
				
		/*****************************************
			Video Panel
		******************************************/ 
		
			var vpanel_config = new Object();
				vpanel_config.region = "center";
				vpanel_config.split = false,
				//vpanel_config.title = n;
				//vpanel_config.collapsible = true;
				//vpanel_config.width = 640;
				//vpanel_config.height = 390;
				vpanel_config.border = false;
				vpanel_config.plain = true;
				vpanel_config.layout ='table';
				vpanel_config.layoutConfig = {columns:1};
				vpanel_config.bodyStyle = 'backgroundColor:#000; margin:0px; padding:0px; border-right:1px solid #333;';
				vpanel_config.cellCls = 'video-container-panel';
				vpanel_config.items = [this.vplayer, this.rating_holding_panel, this.comments_panel];
		
				vpanel_config.draggable = false;
				vpanel_config.resizable = false;
		
				// Create Video Panel Object
				this.vpanel = new Ext.Panel(vpanel_config); 
		
		/*****************************************
			More By: Panel 
		******************************************/ 
		
				this.moreBy_panel = new Ext.Panel({
					title       : 'More By: ' + u,
					split       : false,
					width       : 303,
					height		: 360,
					collapsible : false,
					cmargins    : '0 0 0 0',
					autoScroll : true,
					bodyCfg :{cls:'my-info-panels'},
					autoLoad: '/inc/more_by.cfm?_vlink='+ v +'&u=' + u
				}); 
				
		/*****************************************
			Related Videos Panel 
		******************************************/ 
		
				this.related_vid_panel = new Ext.Panel({
					title       : 'Related Videos',
					split       : false,
					width       : 303,
					height		: 360,
					collapsible : false,
					cmargins    : '0 0 0 0',
					autoScroll : true,
					bodyCfg :{cls:'my-info-panels'},
					autoLoad: '/inc/related_videos.cfm?_vlink='+v
				}); 

		/*****************************************
			Accordion 
		******************************************/ 
		
				this.sim_vid_panel = new Ext.Panel({
					layout:'accordion',
					layoutConfig: {
										// layout-specific configs go here
										titleCollapse: false,
										animate: true,
										activeOnTop: false
										
									},
					items:	[this.moreBy_panel, this.related_vid_panel],
					width       : 303,
					height		: 360,
					bodyCfg :{cls:'my-info-panels'}
				}); 
				
		
		
		/*****************************************
			ADS Panel
		******************************************/ 
		
				// Ads Panel
				this.ads_panel = new Ext.Panel({
					//title       : 'Sponsored Advertisement',
					split       : false,
					width       : 303,
					height		: 206,
					collapsible : false,
					cmargins    : '0 0 0 0',
					autoScroll : false,
					bodyCfg :{cls:'my-info-panels'},
					cellCls:'ads-panel',
					autoLoad: '/inc/ads.cfm?_vlink='+v
				}); 
		
		
		/*****************************************
			Side Panel
		******************************************/ 
			
			var side_vpanel_config = new Object();
				side_vpanel_config.region = "east";
				side_vpanel_config.split = false,
				//side_vpanel_config.title = n;
				//side_vpanel_config.collapsible = true;
				side_vpanel_config.width = 303;
				side_vpanel_config.height = 390;
				side_vpanel_config.border = false;
				side_vpanel_config.plain = true;
				side_vpanel_config.layout ='table';
				side_vpanel_config.layoutConfig = {columns:1};
				side_vpanel_config.bodyStyle = 'backgroundColor:#000; margin:0px; padding:0px;';
				side_vpanel_config.cellCls = 'video-container-panel';
				side_vpanel_config.items = [this.sim_vid_panel, this.ads_panel];
		
				side_vpanel_config.draggable = false;
				side_vpanel_config.resizable = false;
		
				// Create Video Panel Object
				this.side_vpanel = new Ext.Panel(side_vpanel_config); 
		
			
		
		/*****************************************
			The Window
		******************************************/ 
		
			var win_config = new Object();
				win_config.title = n;
				win_config.closable = true;
				win_config.width = 960;
				win_config.height = 600; //574
				win_config.plain = true;
				win_config.layout ='border';
				win_config.items = [this.vpanel, this.side_vpanel];
				win_config.modal = true;
				win_config.closeAction = 'hide';
				win_config.draggable = true;
				win_config.resizable = false;
				
				
				// Create Window Object
				mywin = new Ext.Window(win_config);
		
				mywin.addListener('hide', function(){close_mywin()});
				// Show the window
				mywin.show();
	


	}// end if
	else{
		this.rating_panel.load({url:'/inc/star_rating.cfm?_vlink='+v});
		this.comments_panel.load({url:'/inc/video_comments.cfm?_vlink=' + v});
		this.vplayer.load({url:'/inc/videowindow.cfm?_vlink='+v});
		this.moreBy_panel.load({url:'/inc/more_by.cfm?_vlink=' + v + '&u=' + u});
		this.related_vid_panel.load({url:'/inc/related_videos.cfm?_vlink=' + v});
		mywin.setTitle(n);
		mywin.show();
	}// end else
	
		if(isUserLoggedIn_fn()){
			this.comment_btn.enable();
			//this.comment_btn.setText('Leave a Comment');
		}else{
			this.comment_btn.disable();
			//this.comment_btn.setText('Log in to leave a comment.');
		}
	

}//end playVideo FN


/***********************************************
	Close The mywin window
************************************************/ 

close_mywin = function (){
	this.vplayer.load({url:'/inc/404.cfm'});
	
}

/***********************************************
	Refresh comments window will all comments
************************************************/ 

view_all_comments = function (v){
	this.comments_panel.load({url:'/inc/video_comments.cfm?_vlink=' + v + '&a=viewall'});
}

/***********************************************
	Open Add Comments Window
************************************************/ 
open_comment_win = function(v){
	if(this.comment_window == undefined){
				

		this.comment_txtarea = new Ext.form.TextArea({
								hideLabel :true,					 
								allowBlank :false,
								width:400,
								height:200
							});
						
		this.comment_window = new Ext.Window({
											 title: 'Add Comment',
											 closable: true,
											 closeAction:'hide',
											 width:380,
											 //height:240,
											 resizable :true,
											 modal : true,
											 layout:'form',
											 bodyStyle:'background:#1F1F1F; padding-left:3px;',
											 items:[new Ext.form.FormPanel({defaultType: 'textarea',
																		   defaults: {
																				// applied to each contained item
																				width: 360,
																				height:120
																			},
																			buttonAlign :'right',
																			bodyStyle :'background:#1F1F1F; border:none;',
																		   	labelAlign: 'top',
																			items:[this.comment_txtarea],
																		   
																		   	buttons:[
																					 {text:'Save',
																					  handler: function(){save_comment();}
																					 },
																					 {text:'Cancel',
																					  handler :function(){close_comment_win();}
																					 }
																					 ]
																		   })
													
													]
													
											 //autoLoad :'/inc/add_comment.cfm?_vlink='+v
											 });


		this.comment_window.addListener('hide', function(){comment_enable_disable()});
		this.comment_window.addListener('show', function(){comment_enable_disable()});
		
	}
	else{
		//this.comment_window.load({url:'/inc/add_comment.cfm?_vlink='+v});
	}
	
	this.comment_txtarea.setValue('');
	this.comment_window.show();
	
	
	//alert(v);		
	
}

/***********************************************
	Enable/Disable Comments Button
************************************************/ 

comment_enable_disable = function(){
	if(this.comment_btn.disabled){
		this.comment_btn.enable();
		
	}else{
		this.comment_btn.disable();
		
	}
	
}

/***********************************************
	Close Comments Window
************************************************/ 

close_comment_win = function (){
	this.comment_window.hide();
}


save_comment = function(){

	if(this.comment_txtarea.validate()){
		
		var _comment = new myCFC();
		
		// Setting a callback handler for the proxy automatically makes
        // the proxy's calls asynchronous.


			_comment.setCallbackHandler(comment_Handler);
			
			
			var form_data = new Object();
				form_data.vidID = vidID;
				form_data.comment = this.comment_txtarea.getValue();

				//alert(form_data.comment);
			_comment.saveUserComment(form_data);
	}else{
		this.comment_txtarea.markInvalid('The comment field is required.')
		//alert('The comment field is required.');	
	}
	
}




comment_Handler = function (result){
		
		switch(result.status){
			case true:
			//dump(this.comment_window.title);
				refresh_comments();
				close_comment_win();
				
			break;
			
			case false:
				alert(result.statMsg);
			break;
			
			case 'err':
				alert(result.statMsg);
			break;
				
			
		}
		
				
	}
	
refresh_comments = function (){
	this.comments_panel.load({url:'/inc/video_comments.cfm?_vlink=' + vidID});
}




/****************************************************
	Used to send messages to other users
****************************************************/ 
openSendMsgWin = function(uid){
	if(this.send_msg_win == undefined){	
		Ext.QuickTips.init();

		// turn on validation errors under the field globally
		Ext.form.Field.prototype.msgTarget = 'under';

		var msg = new Ext.form.TextArea({
											id:'msg',
											hideLabel :true,					 
											allowBlank :false,
											width:356,
											height:120,
											listeners: {
												'valid': function(fb, v){
													Ext.getCmp('send_msg_btn').enable();
												},
												'invalid': function(fb, v){
													Ext.getCmp('send_msg_btn').disable();
												}
												
											}
										});

		var msg_form = new Ext.form.FormPanel({
											bodyStyle :'background:#1F1F1F; border:none; padding-left:4px; padding-top:4px;',
											hideLabel:true,
											items:[msg],
											labelAlign: 'top'
										   });
		
		var send_msg_btn = new Ext.Button({
										  						disabled:true,
																text: 'Send Message',
																id:'send_msg_btn',
																formBind: true,
																handler: function(){
																				
																				var form_data = new Object();
																					form_data.msg = Ext.getCmp('msg').getValue();
																					form_data.userid = uid;
																					
																					
																					
																				/*	*/
																				Ext.Ajax.request({
																										waitMsg: 'Sending Message...',
																										url:'/cfc/youminister.cfc',
																										params:{method:'sendMessage',
																												returnFormat:'json',
																												argumentCollection: Ext.util.JSON.encode({form_data:form_data})
																										},
																												
																										success:function(result, action) {
																											var data = Ext.util.JSON.decode(result.responseText);
																											
																											switch(data.status){
																												case true:
																													this.msg('Confirmation', data.statMsg);
																													this.send_msg_win.hide();
																												break;
																												
																												case false:
																													this.msg('Warning', data.statMsg);
																												break;
																												
																												case 'err':
																													this.msg('System Error', data.statMsg);
																												break;
																													
																												
																											}
																											
																											
																										},
																										
																										failure: function(form, action) {
																										
																											alert('Oops the delete did not work out too well!');
																										
																										}
																								   
																								   });
																				

																	}//end of handler
																	



											   });
		

		/*****************************************
			The Send Message Window
		******************************************/ 
			
		
		
			var msg_win_config = new Object();
				msg_win_config.title = 'Send a Message';
				msg_win_config.closable = true;
				msg_win_config.closeAction = 'hide';
				msg_win_config.width = 380;
				//msg_win_config.height = 250; //574
				msg_win_config.resizable = true;
				msg_win_config.buttonAlign = 'right';
				msg_win_config.plain = true;
				msg_win_config.layout ='form';
				msg_win_config.items = [msg_form];
				msg_win_config.bbar = ['->',send_msg_btn];
				msg_win_config.bodyStyle = 'background:#1F1F1F;';
				msg_win_config.modal = true;
				msg_win_config.draggable = false;
				
				
				// Create Window Object
				this.send_msg_win = new Ext.Window(msg_win_config);
		
				//send_msg_win.addListener('hide', function(){close_mywin()});
				
	}
				// Show the window
				send_msg_win.show();
			
}