// Requires loading(), notice(), vidouid, loggedin

var relatedon = true;
function togglerelated(related) {
	if (related == true) {
		$("#relatedvideos").show();
		$("#uservideos").hide();
	} else {
		$("#uservideos").show();
		$("#relatedvideos").hide();
	}
	if (relatedon != related) {
		relatedon = !relatedon;
		$('.rlink').toggleClass('rcurrent');
	}
}

var rateajaxpage = 'addrating.php';

function favourite() {
	function donefavourite(data, status) {
		if (data.message.length > 0) {
			if (data.success === true) notice(data.message, '#090');
			else notice(data.message, '#900');
		}
		loading();
	}
	loading();
	$.post(webpath + "/ajax/favourite.php", {id: objectid}, donefavourite, "json");
}
/*
function featuredvote() {
	function done(data, status) {
		if (data.message.length > 0) {
			if (data.success === true) {
				notice(data.message, '#090');
				$('#featurethis').css('display', 'none');
			} else notice(data.message, '#900');
		}
		loading();
	}
	loading();
	$.post(webpath + "/ajax/votefeatured.php", {uid: objectid}, done, "json");
}
*/
function toggleads() {
	//$("#showads").slideToggle("slow");
	$("#hideads").slideToggle("slow");
	$("#topad").slideToggle("slow");
	$(".ad").slideToggle("slow");
	$("#related").css('margin-top', 0);
	$("#related").animate({height: "830px"}, 500);
	$(".boxcontainer").animate({height: "785px"}, 500);
}

var type = 'video';

function reported(data) {
	loading();
	if (data.success == true) {
		notice(data.message);
		slidetoggle("report", '#090');
		$("#reportbox").val('');
		checklen("report", 200);
	} else {
		$("#reportmsg").html(data.message);
	}
}

var embedcolour = '000';
function updateembedsize(changed) {
	if (changed == 'width') $("#height").val(Math.round($("#width").val() / aspectratio));
	if (changed == 'height') $("#width").val(Math.round($("#height").val() * aspectratio));
	
	var width = Number($("#width").val());
	var height = Number($("#height").val());
	var background = $("#colour").val();
	
	$("#embedtext").val("<iframe style='overflow: hidden; border: 0; width: " + width + "px; height: " + (height + 56) + "px' src='http://stagevu.com/embed?width=" + width + "&amp;height=" + height + "&amp;background=" + embedcolour + "&amp;uid=" + objectuid + "' scrolling='no'></iframe>");
}

function updateembedcolour(colour) {
	$("#colour").val(colour);
	if (colour.match('([0-9a-fA-F]{3}|[0-9a-fA-F]{6})')) embedcolour = colour;
	
	var width = Number($("#width").val());
	var height = Number($("#height").val());
	$("#coloursample").css('background-color', "#" + colour);
	$("#embedtext").val("<iframe style='overflow: hidden; border: 0; width: " + width + "px; height: " + (height + 56) + "px' src='http://stagevu.com/embed?width=" + width + "&amp;height=" + height + "&amp;background=" + embedcolour + "&amp;uid=" + objectuid + "' scrolling='no'></iframe>");
}

function previewembed() {
	var embedcode = $('#embedtext').val();
	var divwidth = Number($('#width').val()) + 20;
	
	$('#sitemessage').width(divwidth).css('left', (960 - divwidth) / 2);
	$('#sitemessage').html(
		'<a href="#" id="hideoverlay" onclick="$(\'#sitemessage, #overlay\').fadeOut(\'slow\')" class="floatright">Hide</a>' +
		'<h2 style="border-bottom: 1px solid #555">Preview Embed Code</h2>' +
		embedcode
	);
	$('#sitemessage, #overlay').fadeIn('slow');
}

function addedtochan(data) {
	loading();
	if (data.success === true) {
		notice(data.message, '#090');
		var chanid = data.text;
		$('#addto' + chanid).remove();
	} else notice(data.message, '#900');
}


function sentnotice(data) {
	loading();
	if (data.success === true) {
		notice(data.message, '#090');
		var entityid = data.text;
		$('#takedown' + entityid).remove();
	} else notice(data.message, '#900');
}

function uptag(tag) {
	function done(data, status) {
		if (data.success === true) {
			notice(data.message, '#090');
			$('#tagscont').html(data.text);
		} else {
			notice(data.message, '#900');
		}
		loading();
	}
	loading();
	$.post(webpath + "/ajax/uptag.php", {id: objectid, tag: tag}, done, "json");
}

function sendnotice() {
	loading();
	$.post(webpath + "/ajax/takedownrequest.php", {action: 'add', uid: objectuid}, sentnotice, "json");
}

$(document).ready(function() {
	checkformlen("comment", maxcommentlen);
	checkformlen("report", 200);
	$('#commentform').ajaxForm({
		beforeSubmit: function() { return checkform('comment', maxcommentlen); },
		dataType: 'json',
		success: addcomment
	});
	$('#reportform').ajaxForm({
		beforeSubmit: function() { return checkform('report', 200); },
		dataType: 'json',
		success: reported
	});
	$('#addvidtochan').ajaxForm({
		beforeSubmit: function() { loading(); return true; },
		dataType: 'json',
		success: addedtochan
	});
	$('#takedownform').ajaxForm({
		beforeSubmit: function() { loading(); return true; },
		dataType: 'json',
		success: sentnotice
	});
	updateembedsize('width');
	updateembedsize($('#colour').val());
});

