function example()
{
	document.getElementById('url').value = "http://www.dailymotion.com/video/x30dtn_as-i-lay-dying-nothing-left_music";
}

function utf8_encode(string) 
{
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}

function getLoadingHtml() {
	return '<br/><br/><br/><br/><br/><p align="center"><img src="/img/ajax-loader.gif" title="Loading..." alt="Loading..."/></p><br/><br/><br/><br/><br/>';
}

function getVideo()
{
	// disable button
	$('#shareButton').attr('disabled', 'true');	
	// get url
	var inputText = document.getElementById('url'); 
	var videoUrl = inputText.value;
	if (videoUrl === "") {
		alert('invalid url');
		inputText.focus();
		return;		
	}
	// encode url	
	videoUrl = utf8_encode(videoUrl);
	// set loading
	$('#videoContent').html(getLoadingHtml());
	// get data	
	$.ajax({
		type: "POST",
		url: "/api",
		data: {v: videoUrl},
		//timeout: 30000,
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			getVideoFailed(XMLHttpRequest, textStatus, errorThrown);
		},
		success: function (json) {
			getVideoCompleted(json);
		}
	});
}

function getVideoFailed(XMLHttpRequest, textStatus, errorThrown)
{
	// enable button
	$('#shareButton').removeAttr("disabled");
	$('#videoContent').html(getErrorHtml('Error', 'Unable to sharing video :('));
}

function getVideoCompleted(json)
{			
	// enable button
	$('#shareButton').removeAttr("disabled");
	if (json === '') { return; }
	var result = eval("(" + json + ")" );	
	
	if (result.title !== null && result.thumbnail != null) {
		$('#videoContent').html(getVideoHtml(result));
		document.getElementById('url').value = '';
	} else {
		$('#videoContent').html(getWarningHtml('Sorry', 'Unable to sharing video. No information found on that url :('));
	}
}

function getErrorHtml(label, msg)
{
	var html = "";	
		html += '<div class="ui-widget">';
		html += '<div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">'; 
		html += '<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>'; 
		html += '<strong>'+label+'</strong> '+msg+'</p>';
		html += '</div>';	
		html += '</div>';
	return html;
}

function getWarningHtml(label, msg)
{
	var html = "";	
		html += '<div class="ui-widget">';
		html += '<div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;">';
		html += '<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>';
		html += '<strong>'+label+'</strong> '+msg+'</p>';
		html += '</div>';
		html += '</div>';	
	return html;
}

function getVideoHtml(videoData)
{
	var html = "";	
		html += getWarningHtml('Done!', 'Your video is successfuly shared!');
		html += '<br/><div style="float:left">';
		html += '<table>';
		html += '<tr><td><b>Source</b></td><td><a href="'+videoData.source+'">'+videoData.source+'</a></td></tr>';
		html += '<tr><td><b>Video Url</b></td><td><a target="_blank" href="'+videoData.url+'">'+videoData.url+'</a></td></tr>';				
		html += '<tr><td><b>Title</b></td><td>'+videoData.title+'</td></tr>';
		html += '<tr><td><b>Description</b></td><td>'+videoData.description+'</td></tr>';
		html += '<tr><td><b>Keywords</b></td><td>'+videoData.keywords+'</td></tr>';					
		html += '<tr><td><b>Thumbnail</b></td><td><img src="'+videoData.thumbnail+'" alt="'+videoData.thumbnail+'" title="'+videoData.thumbnail+'"/></td></tr>';		
		if (videoData.code != null && videoData.code !== '') {
			html += '<tr><td><b>Size</b></td><td>'+videoData.width+' x '+videoData.height+' px</td></tr>';
			html += '<tr><td><b>Embed code</b></td><td><textarea readonly="readonly" rows="3" cols="50">'+videoData.code+'</textarea></td></tr>';
		}
		html += '</table>';
		html += '</div>';
		if (videoData.code != null && videoData.code !== '') {
			html += '<div style="float:left">'+videoData.code+'</div>';
		}
		html += '<div style="clear:both"></div>';
	return html;	
}

function watchVideo(hash)
{
	if (hash === '') { return; }
		
	$('#dialog').html(getLoadingHtml());	
	$('#dialog').dialog('open');
	
	// get data
	$.post('/api/watch', {h: hash}, function(json) { watchVideoCompleted(json); } );
}

function watchVideoCompleted(json)
{
	if (json === '' ) { return; }
	var result = eval("(" + json + ")" );
	
	if (result.code !== null) {		
		$('#dialog').dialog('option', 'title', result.title);
		$('#dialog').dialog('option', 'width', result.width + 50);
		$('#dialog').dialog('option', 'height', result.height + 50);
		$('#dialog').html("<center>" + result.code + "</center>");	
	} else {
		$('#dialog').html(getWarningHtml('Sorry', 'Unable to show video file :('));
	}
	
}
