/*  
Easy Video Sharing Script
Version: 1.0
Author: Webarti
Author URL: http://www.webarti.com/evss
*/
$(document).ready(function() {

	var defaultw = 600;
	var defaulth = 300;

	$('textarea.video').val(
		'Supported sites: Youtube, Vimeo, Yahoo Video, Break, Dailymotion, Metacafe, Viddler, Blip.tv, Myspace TV'
	);

	// This function calls data.php and inserts embed code into the preview div and textarea.video.
	$('#getVideo').click(function(){
		$.post("data.php",
		{ link: $('#link').val(), width:$('#width').val(), height:$('#height').val() },
		function(data){
			$('textarea.video').val(data);
			$('#preview')
			.width($('#width').val()+'px')
			.height($('#height').val()+'px')
			.fadeIn(200, function() {
				$(this).html(data);
				$(this).height('');
			});
		});
		$('#features').hide();
	});

	// This function shows a loader gif and disables "Get the video" button during the ajax call.
	$('#loading,#getVideo')
	.ajaxStart(function() {
		$('#loading').show();
		$('#getVideo').attr('disabled', true);
	})
	.ajaxStop(function() {
		$('#loading').hide();
		$('#getVideo').attr('disabled', false);
	});

	// UI functions for controlling the width and height of the video begin here
	$('#width').attr('value',defaultw);
	$('#height').attr('value',defaulth);
	
	$('#width,#height,#link').each(function() { // Changes the default text values of the input elements
	
		var default_value = this.value;
		
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
		
	});
	
	$('#link,textarea.video,#width,#height').focus(function(e) { // Selects the current value of the input elements
		var textfield = this;
		setTimeout(function(){$(textfield).select();},10);
		return false;
	});


	jQuery.fn.changeSize = function(){ // Creates a  jQuery  function which controls the width and height of the video

		$('textarea.video').val(
			$('textarea.video').val().replace('width="'+defaultw+'"', 'width="'+$('#width').val()+'"')
			.replace('width="'+defaultw+'"', 'width="'+$('#width').val()+'"')
		);
		$('textarea.video').val(
			$('textarea.video').val().replace('height="'+defaulth+'"', 'height="'+$('#height').val()+'"')
			.replace('height="'+defaulth+'"', 'height="'+$('#height').val()+'"')
		);

		defaultw = $('#width').val();
		defaulth = $('#height').val();

		$('#preview object,#preview embed').attr('width',$('#width').val()).attr('height',$('#height').val());
		$('#preview').width($('#width').val()+'px');

	}

	$('#width,#height').bind('keyup blur',function(){ 

		$(this).val(
			$(this).val().replace(/[^\d]/g, '') //This allows only numeric characters
		);

		$(this).changeSize();

	});

	$('#replace').click(function(){ 

		currentw = $('#width').val();
		currenth = $('#height').val();

		$('#width').val(
			$('#width').val().replace($('#width').val(), currenth)
		);		
		$('#height').val(
			$('#height').val().replace($('#height').val(), currentw)
		);

		$(this).changeSize();

	});
	
	$('.ribbon span').click(function(){
		$('#features').toggle();
		if ( $('#preview').html() != 0 ){
			$('#preview').toggle();
		}
	});
	//UI functions for controlling the width and height of the video end here
});
