/**
 * Video Player - by Duktee
 *
 * @package		Video Player
 * @version		Version 2.5
 * @author		Benjamin David
 * @copyright	Copyright (c) 2011 - Duktee
 * @license		http://duktee.com/addons/video-player/license
 * @link		http://duktee.com/addons/video-player/
 *
 */
 

// variable declaration

var videoplayerSelectedField = false;

// --------------------------------------------------------------------

/**
 * Initialize Videoplayer when DOM is ready
 *
 * @access	public
 * @return	void
 */
$(document).ready(function() {
	videoplayer_box_overlay();
	videoplayer_controls();
	videoplayer_search_results();
	videoplayer_timer_search();
	videoplayer_timer_url();
});

// --------------------------------------------------------------------

/**
 * Window Resize
 *
 * @access	public
 * @return	void
 */

$(window).resize(function() {
	videoplayer_resize();
});

// --------------------------------------------------------------------

/**
 * Box + Overlay
 *
 * @access	public
 * @return	void
 */
function videoplayer_box_overlay()
{

	$('body').prepend('<div id="videoplayer-overlay"></div>');
	$('#videoplayer-overlay').css('display', 'none');

	var boxUrl = VideoPlayer.box_ajax_url;	

	$.ajax({
	  url: boxUrl,
	  data: {
	  	
	  },
	  type: 'GET',
	  success: function(data){

	  	$('body').append(data);
  		$('#videoplayer-box').css('display', 'none');
		
		var defaultSearchField = "Search videos on the Internet";
		$('#videoplayer-search input').attr('value', defaultSearchField);
		
		$('#videoplayer-search input').focus(function() {
			if($(this).attr('value') == defaultSearchField) {
				$(this).attr('value', "");
			}
		});
		
		$('#videoplayer-search input').blur(function() {
			if($(this).attr('value') == "") {
				$(this).attr('value', defaultSearchField);
			}
		});
		
		videoplayer_resize();
	  }
	});
}

// --------------------------------------------------------------------

/**
 * Controls
 *
 * @access	public
 * @return	void
 */
function videoplayer_controls()
{


	$('#videoplayer-btn-use-video').live('click', function() {
	
		if($('#videoplayer-preview').html() != "") {
			$.ajax({
			  url: VideoPlayer.video_ajax_url,
			  data: {
			  	video_url: encodeURIComponent($('#videoplayer-controls input').attr('value')),
			  	XID:VideoPlayer.xid+"aeazea"
			  },
			  type: 'GET',
			  success: function(data){
			  	setTimeout(function() {
					loading('stop');
			  	}, 1000);
			  	
				videoplayerSelectedField.parent().parent().parent().find('.videoplayer-field-preview').html(data);
				videoplayerSelectedField.parent().find('.videoplayer-add').css('display', 'none');
				videoplayerSelectedField.parent().find('.videoplayer-remove').css('display', 'inline');
				videoplayerSelectedField.parent().parent().parent().find('input[type="hidden"]').attr('value', $('#videoplayer-controls input').attr('value'));
				videoplayerSelectedField = false;
				videoplayer_hidebox();
				
			  }
			  
			});
		}

		return false;
	});
	
	$('#videoplayer-btn-cancel').live('click', function() {
		videoplayer_hidebox();
		videoplayerSelectedField = false;
		return false;
	});
	
	
	
	$('.videoplayer-add').live('click', function() {
		videoplayerSelectedField = $(this);
		videoplayer_showbox();
		return false;
	});

	$('.videoplayer-remove').live('click', function() {
		var videoField = $(this).parent().parent().parent();
		videoField.find('.videoplayer-field-preview').html('');
		videoField.find('.videoplayer-add').css('display', 'inline');
		videoField.find('.videoplayer-remove').css('display', 'none');
		videoField.find('input[type="hidden"]').attr('value', '');

		return false;
	});
	
	
	// 	$('.videoplayer-remove').css('display', 'none');
	
	$('.videoplayer-field input[type="hidden"]').each(function() {
		if($(this).attr('value') != "") {
			$(this).parent().find('.videoplayer-add').css('display', 'none');
			$(this).parent().find('.videoplayer-remove').css('display', 'inline');			
		}
	});
}

// --------------------------------------------------------------------

/**
 * Search Results
 *
 * @access	public
 * @return	void
 */
function videoplayer_search_results()
{

	// filters
	
	$('#videoplayer-filters a').live('click', function() {
		$('#videoplayer-filters li').removeClass('selected');
		$(this).parent().addClass('selected');

		//var test = '#videoplayer-filters .videoplayer-results-'+$(this).attr('rel');
		//console.log(test);

		$('#videoplayer-search-results ul').css('display', 'none');
		$('#videoplayer-search-results .videoplayer-results-'+$(this).attr('rel')).css('display', 'block');
		return false;
	});
	
	
	// click on search results

	$('#videoplayer-search-results li').live('click', function() {

			var input = $(this);
			loading('start');
			$('#videoplayer-preview').html('');
			$('#videoplayer-search-results li.selected').removeClass('selected');
			input.addClass('selected');
			
			$('#videoplayer-controls input').attr('value', input.attr('rel'));
			
			//$('#videoplayer-preview').text($(this).attr('value'));
			
			$.ajax({
			  url: VideoPlayer.video_ajax_url,
			  data: {
			  	video_url: encodeURIComponent(input.attr('rel')),
			  	XID:VideoPlayer.xid+"aeazea"
			  },
			  type: 'GET',
			  success: function(data){
			  	setTimeout(function() {
					loading('stop');
			  	}, 1000);
				$('#videoplayer-preview').html(data);
			  }
			  
			});

		return false;
	});

}

// --------------------------------------------------------------------

/**
 * Timer URL
 *
 * @access	public
 * @return	void
 */
function videoplayer_timer_url()
{
	var timerUrl = false;
	
	$('#videoplayer-controls input').live('keyup', function(e) {
		var searchInput = $(this);
		
		if(e.keyCode != 13) {
			clearTimeout(timerUrl);
			timerUrl = setTimeout(function() {

					$.ajax({
					  url: VideoPlayer.video_ajax_url,
					  data: {
							video_url:encodeURIComponent(searchInput.attr('value'))
						},
					  type: 'GET',
					  success: function(data){
					  	try {
							$('#videoplayer-preview').html(data);
						} catch(err) {
							//console.log(err);
						}
					  }
					});
			
			}, 500);
		}
	});
	
	$('#videoplayer-controls input').live('keypress', function(e) {
		var searchInput = $(this);

        if(e.keyCode==13){
			var searchInput = $(this);
	
			clearTimeout(timerUrl);

			$.ajax({
			  url: VideoPlayer.video_ajax_url,
			  data: {
					video_url:encodeURIComponent(searchInput.attr('value'))
				},
			  type: 'GET',
			  success: function(data){
				$('#videoplayer-preview').html(data);
			  }
			});

        }
	});
}

// --------------------------------------------------------------------

/**
 * Timer Search
 *
 * @access	public
 * @return	void
 */
function videoplayer_timer_search()
{


	var timerSearch = false;
	
	$('#videoplayer-search input').live('keyup', function(e) {
		var searchInput = $(this);
		var searchUrl = VideoPlayer.search_ajax_url;
		
		
		if(e.keyCode != 13)
		{
			clearTimeout(timerSearch);
			timerSearch = setTimeout(function()
			{
				$.ajax({
				  url: searchUrl,
				  data: {
						q:encodeURIComponent(searchInput.attr('value'))
					},
				  type: 'GET',
				  success: function(data){
				  		
				  		
					  	if(data == "")
					  	{
					  		$('#videoplayer-search-results').html('<p class="no-videos">No videos to display</p>');
					  	}
					  	else
					  	{
							$('#videoplayer-search-results').html(data);
						}
						
				  		var current_service = $($('#videoplayer-filters li.selected a')[0]).attr('rel');

				  		$('#videoplayer-search-results ul').css('display', 'none');
				  		$('#videoplayer-search-results .videoplayer-results-'+current_service).css('display', 'block');
						//console.log(data.length);
				  }
				});
			
			}, 500);
		}
	});
	
	$('#videoplayer-search input').live('keypress', function(e) {
		var searchInput = $(this);
		var searchUrl = VideoPlayer.search_ajax_url;

		
		//when [enter] key is pressed
		
        if(e.keyCode==13)
        {
       		clearTimeout(timerSearch);
			$.ajax({
			  url: searchUrl,
			  data: {
					q:encodeURIComponent(searchInput.attr('value'))
				},
			  type: 'GET',
			  success: function(data){
				  	if(data == "")
				  	{
				  		$('#videoplayer-search-results').html('<p class="no-videos">No videos to display</p>');
				  	}
				  	else
				  	{
						$('#videoplayer-search-results').html(data);
					}
				
			  		var current_service = $($('#videoplayer-filters li.selected a')[0]).attr('rel');
			  		$('#videoplayer-search-results ul').css('display', 'none');
			  		$('#videoplayer-search-results .videoplayer-results-'+current_service).css('display', 'block');
			  }
			});
        }
	});
}


// --------------------------------------------------------------------

/**
 * Loading
 *
 * @access	public
 * @return	void
 */
function loading(action)
{


	if(action == "start") {
		$('#videoplayer-preview').addClass('loader');
	} else if(action == "stop") {
		$('#videoplayer-preview').removeClass('loader');
	
	}
}

// --------------------------------------------------------------------

/**
 * Show box
 *
 * @access	public
 * @return	void
 */
function videoplayer_showbox()
{
	$('#videoplayer-overlay').css('display', 'block');
	$('#videoplayer-box').css('display', 'block');
}

// --------------------------------------------------------------------

/**
 * Hide box
 *
 * @access	public
 * @return	void
 */
function videoplayer_hidebox()
{
	$('#videoplayer-overlay').css('display', 'none');
	$('#videoplayer-box').css('display', 'none');
	
	$('#videoplayer-preview').html('');
	$('#videoplayer-controls input').attr('value', '');
	$('#videoplayer-search-results li.selected').removeClass('selected');
}


// --------------------------------------------------------------------

/**
 * Resize the box and keeps it at the middle of the browser window
 *
 * @access	public
 * @return	void
 */
function videoplayer_resize()
{
	var width = 780;
	var height = 420;
	$('#videoplayer-overlay').css({
		'position' 		: 'fixed',
		'top'			: 0,
		'left'			: 0,
		'width'			: $(window).width(),
		'height'		: $(window).height(),
		'background'	: '#000',
		'opacity'		: 0.8,
		'z-index'		: '1000'
	});
	
	$('#videoplayer-box').css({
		'position' 		: 'fixed',
		'top'			: (($(window).height() / 2) - (height / 2)),
		'left'			: (($(window).width() / 2) - (width / 2)),
		'width'			: width,
		'height'		: height,
		'opacity'		: 1,
		'z-index'		: '10000'
	});
}

/* End of file videoplayer.js */
/* Location: ./themes/third_party/videoplayer/videoplayer.js */