var _videos = window.IPBoard;

_videos.prototype.videos = {
	
	init: function()
	{
		Debug.write("Initializing videos.js");
		
		document.observe("dom:loaded", function(){
			ipb.videos.initEvents();			
		
		});
	},
	initEvents: function()
	{		
		if( $('show_filter') )
		{
			$('show_filter').observe('click', ipb.videos.toggleCatFilter );
			$('filter_form').hide();
		}
        
        if( $('show_share') )
		{
			$('show_share').observe('click', ipb.videos.toggleShareBox );
            $('share_box').hide();
        }
        
		ipb.delegate.register(".view_media_sites", ipb.videos.viewMediaSites);
        ipb.delegate.register(".do_fav", ipb.videos.doFavorite);
        ipb.delegate.register(".change_status", ipb.videos.changeStatus);

		if( $('video_type') )
		{
			ipb.videos.initMediaType();
		}      
    
  		$$('.tab_toggle').each( function(elem){
		$(elem).observe('click', ipb.videos.changeTab );
	       });
	},
    
	toggleShareBox: function(e)
	{
        Effect.toggle( $('share_box'), 'blind', {duration: 0.2} );
	},	
	toggleCatFilter: function(e)
	{
		if( $('filter_form') )
		{
			Effect.toggle( $('filter_form'), 'blind', {duration: 0.2} );
			Effect.toggle( $('show_filter'), 'blind', {duration: 0.2} );
		}
	},
	initMediaType: function()
	{
		$('video_type').observe('change', ipb.videos.changeMediaType);
		
		if( $F('video_type') )
		{
			ipb.videos.changeMediaType();	
		}
	},
	
	changeMediaType: function(e)
	{
		media_type = $F('video_type');
		
		$('media_url').hide();
		$('media_embed').hide();
		
		$(media_type).show();
	},
	viewMediaSites: function(e)
	{
		Event.stop(e);
		
		media_type = $F('video_type');
		
		if( media_type == 'media_url' )
		{
			var _url = ipb.vars['base_url'] + '&app=videos&module=ajax&section=video&do=view_media_sites&secure_key=' + ipb.vars['secure_hash'];	
		}
		if( media_type == 'media_embed' )
		{
			var _url = ipb.vars['base_url'] + '&app=videos&module=ajax&section=video&do=view_media_sites&embed=1&secure_key=' + ipb.vars['secure_hash'];	
		}
		media_sites = new ipb.Popup( 'media_sites', {type: 'pane', modal: false, w: '700px', h: '500px', ajaxURL: _url, hideAtStart: false, close: '.cancel' } );
	},
	doFavorite: function( e, elem )
	{	
	    vid = elem.id.match('fav_id_([0-9a-z]+)')[1];
        
		if( Object.isUndefined(vid) ){ return; }
		
		if( parseInt(vid) == 0 ){
			return false;
		}        
       
		Event.stop(e);

		var url = ipb.vars['base_url'] + "app=videos&module=ajax&section=video&do=fav&video_id=" + vid + "&secure_key=" + ipb.vars['secure_hash'];
		
		new Ajax.Request( url.replace(/&amp;/, '&'),
		 				{
							method: 'get',
							evalJSON: 'force',
							onSuccess: function(t)
							{
								if( t.responseJSON['error'] )
								{
                                    alert(ipb.lang[ t.responseJSON['error'] ]);								    
				                }
                                else
                                {
								    if( t.responseJSON['status'] == 'added' )
                                    {
                                           $('fav_image').fade({ afterFinish: function(){
                                               $('fav_image').src = ipb.vars['fav_on'];
                                               $('fav_image').appear();
                                               $('fav_text').update(ipb.vars['fav_on_text']);           
                                             }
                                           }); 
                                    }
                                    else
                                    {
                                           $('fav_image').fade({ afterFinish: function(){
                                               $('fav_image').src = ipb.vars['fav_off'];
                                               $('fav_image').appear();
                                               $('fav_text').update(ipb.vars['fav_off_text']);           
                                             }
                                           });
                                    }
								}
								
							}
						});	
	},
    
 	changeStatus: function( e, elem )
	{	
	    vid = ipb.videos.videoID;
        
		if( Object.isUndefined(vid) ){ return; }
		
		if( parseInt(vid) == 0 ){
			return false;
		}        
       
		Event.stop(e);

		var url = ipb.vars['base_url'] + "app=videos&module=ajax&section=moderate&do=change_status&video_id=" + vid + "&secure_key=" + ipb.vars['secure_hash'];
		
		new Ajax.Request( url.replace(/&amp;/, '&'),
		 				{
							method: 'get',
							evalJSON: 'force',
							onSuccess: function(t)
							{
								if( t.responseJSON['error'] )
								{
                                    alert(ipb.lang[ t.responseJSON['error'] ]);								    
				                }
                                else
                                {
								    if( t.responseJSON['status'] == '1' )
                                    {
                                           $('status_text').fade({ afterFinish: function(){
                                               $('status_text').update(ipb.vars['status_1']);
                                               $('status_text').appear();
        
                                             }
                                           }); 
                                    }
                                    else
                                    {
                                           $('status_text').fade({ afterFinish: function(){
                                               $('status_text').update(ipb.vars['status_0']);
                                               $('status_text').appear();        
                                             }
                                           });
                                           
                                           $('unapproved_video').fade();
                                    }
								}
								
							}
						});	
	},   
    
	changeTab: function(e)
	{		
	    // Taken from profile.js, all credit goes to IPS.
        Event.stop(e);
		elem = Event.findElement(e, 'li');
		if( !elem.hasClassName('tab_toggle') || !elem.id ){ return; }
		id = elem.id.replace('tab_link_', '');
		if( !id || id.blank() ){ return; }
		if( !$('tab_content') ){ return; }
		
		if( ipb.videos.activeTab == id )
		{
			return;
		}
		
		oldTab = ipb.videos.activeTab;
		ipb.videos.activeTab = id;
		
		// OK, we should have an ID. Does it exist already?
	
		if( !$('tab_' + id ) )
		{
			new Ajax.Request( ipb.vars['base_url'] + 'app=videos&module=ajax&section=video&do=change_tab&video_id=' + ipb.videos.videoID + '&tab=' + id + '&secure_key=' + ipb.vars['secure_hash'],
							{
								method: 'post',
								onSuccess: function(t)
								{
								    if( t.responseText == 'nopermission' )
									{
										alert( ipb.lang['no_permission'] );
										return;
									}
									
									if( t.responseText != 'error' )
									{
										newdiv = new Element('div', { 'id': 'tab_' + id } ).hide();
										newdiv.update( t.responseText );
										
										$('tab_content').insert( newdiv );
										
										new Effect.Parallel( [
											new Effect.BlindUp( $('tab_' + oldTab), { sync: true } ),
											new Effect.BlindDown( $('tab_' + ipb.videos.activeTab), { sync: true } )
										], { duration: 0.4, afterFinish: function(){ 	// Re-execute JS for various things in posts
											ipb.videos.executeJavascript( $( newdiv ) ); } } );
										
										
									}
									else
									{
										alert( ipb.lang['action_failed'] );
										return;
									}
								}
							});
		}
		else
		{
			new Effect.Parallel( [
				new Effect.BlindUp( $('tab_' + oldTab), { sync: true } ),
				new Effect.BlindDown( $('tab_' + ipb.videos.activeTab), { sync: true } )
			], { duration: 0.4 } );
		}
		
		$$('.tab_toggle').each( function(otherelem){
			$(otherelem).removeClassName('active');
		});
		
		$(elem).addClassName('active');
		
	}     			
}
ipb.videos.init();
	
function check_boxes()
{
	var ticked = $('maincheckbox').checked;
	
	var checkboxes = document.getElementsByTagName('input');

	for ( var i = 0 ; i <= checkboxes.length ; i++ )
	{
		var e = checkboxes[i];
		
		if ( typeof(e) != 'undefined' && e.type == 'checkbox')
		{
			var boxname		= e.id;
			var boxcheck	= boxname.replace( /^(.+?)_.+?$/, "$1" );
			
			if ( boxcheck == 'tid' )
			{
				e.checked = ticked;
			}			
		}
	}
}
