var project, totalVideos = 0, totalAudios = 0;
var inProgress = 'В настоящее время файл перекодируется';
var httpHost = 'http://virtuzor.ru';

function embedVideo(video, fname, w, h) {
	if (parseInt(w) && parseInt(h)) {
		var videoURI = httpHost + "/data/projects/flv/";
		var vars = {
			file:       videoURI + fname,
			image:      videoURI + fname + '.jpg',
			logo:       httpHost + '/img/logo.video.png',
			controlbar: 'over',
			type:       'video',
			showstop:   'true'
		};
		var params = {
			allowfullscreen: 'true',
			wmode: 'transparent'
		};
		swfobject.embedSWF(httpHost + '/player.swf', 'swf' + video, w, h, '9.0.0', httpHost + '/expressInstall.swf', vars, params);
		var html = '<embed width="' + w + '" height="' + h + '" flashvars="config=' + httpHost + '/video.' + video + '.xml" wmode="transparent" allowfullscreen="true" quality="high" id="mp' + video + '" src="' + httpHost + '/player.swf" type="application/x-shockwave-flash"></embed>';
		var link = '<br><a href="' + $('#spec4codeURL').val() + '?utm_source=informer&utm_medium=informer4" style="font-size:85%;color:#666666;font-style:italic;text-decoration:none;padding-top:5px;display:inline-block">' + $('#spec4codeTitle').val() + '</a>';
		$('#fEmbedBlogs' + video).val(html + link);
		$('#fEmbedLJ' + video).val(document.location.href.replace(/#[a-z\-]+/i, ''));
		$('#fEmbedVirtuzor' + video).val('[video=' + video + ']');
		$('#fEmbedBlogs' + video + ', #fEmbedLJ' + video + ', #fEmbedVirtuzor' + video).focus(function(){ this.select() });
	} else {
		$('#infEmbed' + video).remove();
	}
	return;
}

function embedAudio() {
	if (totalAudios > 0) {
		$('#infEmbedPlaylist').show();
		var h = 18 + (24 * totalAudios);
		var vars = {
			file:          httpHost + '/playlist.' + $('#project').val() + '.xml',
			backcolor:     '0xffffff',
			frontcolor:    '0x000000',
			lightcolor:    '0x0099cc',
			displayheight: 0,
			playlist:      'bottom',
			repeat:        'list',
			type:          'sound',
			showstop:      'true'
		};
		var params = {
			allowfullscreen: 'false',
			wmode: 'transparent'
		};
		$('#playlist').html('<span id="swfPlaylist"/>');
		swfobject.embedSWF(httpHost + '/mp3player.swf', 'swfPlaylist', 480, h, '9.0.0', httpHost + '/expressInstall.swf', vars, params);
		var html = '<embed width="480" height="' + h + '" flashvars="file=' + httpHost + '/playlist.' + $('#project').val() + '.xml&backcolor=0xffffff&frontcolor=0x000000&lightcolor=0x0099cc&displayheight=0&playlist=bottom&repeat=list&type=sound" wmode="transparent" id="playlist' + $('#project').val() + '" src="' + httpHost + '/mp3player.swf" type="application/x-shockwave-flash"></embed>';
		var link = '<br><a href="' + $('#spec4codeURL').val() + '?utm_source=informer&utm_medium=informer5" style="font-size:85%;color:#666666;font-style:italic;text-decoration:none;padding-top:5px;display:inline-block">' + $('#spec4codeTitle').val() + '</a>';
		$('#fEmbedBlogs').val(html + link);
		$('#fEmbedLJ').val(document.location.href.replace(/#[a-z\-]+/i, ''));
		$('#fEmbedVirtuzor').val('[playlist=' + $('#project').val() + ']');
		$('#fEmbedBlogs, #fEmbedLJ, #fEmbedVirtuzor').focus(function(){ this.select() });
	} else {
		$('#infEmbedPlaylist').hide();
		$('#playlist').empty();
	}
	return;
}

function embedCode(id) {
	if ($('#embed' + id).hasClass('invis')) {
		$('#embed' + id).removeClass('invis');
	} else {
		$('#embed' + id).addClass('invis');
	}
	return;
}

$(document).ready(function(){
	$.ajaxSetup({
		type:       'post',
		url:        '/ajax/user.projects.ajax.php',
		cache:      false,
		timeout:    tout,
		beforeSend: function() {
			$('#run').show();
		},
		complete:   function() {
			$('#run').hide();
		},
		error: function(xhr, status) {
			if (status == 'timeout') {
				alert(errTimeout);
			}
		}
	});

	project = parseInt($('#project').val());

	if ($('a[id^="photosItem"]').length) {
		$('a[id^="photosItem"]').fancybox();
	}

	flash = flashVersion();
	if (flash < 9) {
	} else {
		$.each($('div[id^="swf"]'), function() { $(this).html(inProgress) });

		if ($('div[id^="video"]').length) {
			postData = ['act=getProjectVideos', 'project=' + project];
			$.ajax({
				data: postData.join('&'),
				dataType: 'json',
				success: function(data) {
					$.each(data, function() { embedVideo(this.video, this.filename, this.width, this.height); });
				}
			});
		}
	
		postData = ['act=getProjectAudios', 'project=' + project];
		$.ajax({
			data: postData.join('&'),
			dataType: 'json',
			success: function(data) {
				totalAudios = parseInt(data.count);
				embedAudio();
			}
		});
	}
});
