// 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", {videouid: 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 maxcommentlen = 1000;
var commentajaxpage = 'getcomments.php';

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,mov) {
var aspectratio=2;
	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 colour = $("#colour").val();

var culoare = colour;
var latime = width;
var inaltime =height;
var locatie = "http://www.movshare.net/embed/" + mov;

var embedcode = "<iframe style='overflow: hidden; border: 0; width:" + latime + "px; height: " + inaltime +  "px;' " + "src='" +  locatie + "/?width=" + latime + "&height=" + inaltime +"&c=" + colour +"' scrolling='no'></iframe>";

	$("#embedtext").val(embedcode);

}

function updateembedcolour(colour,mov) {

	$("#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);

var culoare = colour;
var latime = width;
var inaltime =height;
var locatie = "http://www.movshare.net/embed/" + mov;

var embedcode = "<iframe style='overflow: hidden; border: 0; width:" + latime + "px; height: " + inaltime +  "px;' " + "src='" +  locatie + "/?width=" + latime + "&height=" + inaltime +"&c=" + colour +"' scrolling='no'></iframe>";

	$("#embedtext").val(embedcode);

	}

function addedtochan(data) {
	loading();
	if (data.success === true) {
		notice(data.message, '#090');
		$("#addto" + chanid).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", {uid: objectid, tag: tag}, done, "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
	});
	updateembedsize('width');
	updateembedsize($('#colour').val());
});
