// JavaScript Document

var validURL;
var videoID;
var properFileName;
var firstHidden = false;

function convert(){
	
		var url = document.getElementById('url').value;
		var valueNumber = document.getElementById('numberid').value;
	
	 $.ajax({
	   type: "GET",
	   url: "validate.php",
	   data: 'url=' + url +'&valuee=' + valueNumber,
	   success: function(reply){
		  
		if(reply==1 || reply==2){
			alert('Invalid URL - Please use a correct Youtube URL');
			document.getElementById('getclip').disabled=false;
		}
		else{
	
		$('#w_span_auto').html("Please wait <br /><br /><img src=\"images/load.gif\" alt=\"loading\" /><br /><br />");
		
		videoID = reply;
		getVideoDetails(videoID);
		
		}   
	   
	   }//function
	   
	 });//ajax
	 
}

function getVideoDetails(vid){
	
	   $.ajax({
	   type: "GET",
	   url: "getvideo.php",
	   data: 'videoid=' + vid,
	   success: function(found){
		  
		if(found==0){
			alert('Youtube video not found - check URL');
			window.location.reload();
		}
		else{
		var splitfound = found.split('160687');
		validURL = splitfound[0];
		properFileName = splitfound[0];
		convertNow('http://www.youtube.com/watch?v='+videoID, splitfound[1]);
		}   
	   
	    }//function
	   
	 });//ajax
	
	
}

function convertNow(validURL, thumbimage){
	
	var result;
	
	 $.ajax({
	   type: "GET",
	   url: "yourtube.php",
	   data: 'url=' + validURL,
	   success: function(msg){
		
		$('#videotitle').html(properFileName);
		
		$('#links').html(msg);
		
		$('#getanother').html("<a href=\"index.php\">Get Another Video</a>");

		$('#thumb').html("<img src=\""+ thumbimage +"\" alt=\"Youtube Tumbnail\" />");

		$('#w_span_auto').hide('slow');	
		$('#clip1').show('slow');
	   
	   }//function
	   
	 });//ajax
	 
}//function convertNow

function problem(){

	var value = document.getElementById('problemdescription').value;
	var email = document.getElementById('email').value;
	
	 $.ajax({
	   type: "GET",
	   url: "feedback.php",
	   data: 'feedback=' + value + '&email=' + email,
	   success: function(){
		$("#quickgame").html("<p style='color:green;'><b>Thank you!</b></p>");
	   }
	 });
}

function clearText(field){

    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;

}

function helpfunc(){
	$('#helpme').toggle('slow');
}
