var homepageAjax = function(type, obj)
{
	var ajax = new Ajax('/resources/rools/ajax.php?do=homepageAjax&type=' + type, {method: 'get', update: obj}).request();
}

var addRemoveFavorite = function(id, obj)
{
	var ajax = new Ajax('/resources/tools/ajax.php?do=addRemoveFavorite&id=' + id, {method: 'get', update: obj}).request();
}

var rateLayout = function(id, pm)
{
	var ajax = new Ajax(
					'/resources/tools/ajax.php?do=rating&lid=' + id + '&rate=' + ((pm == '+')?"1":"0"), 
					{
						method: 'get',
						onComplete: 
							function(data) {
								if(data == '5')
								{
									$('plus').className  = 'plus-hover';
									$('minus').className = 'minus';
								} else if (data == '0') {
									$('plus').className  = 'plus';
									$('minus').className = 'minus-hover';
								} else if (data == '-1') {
									$('ratingError').innerHTML = 'You must be logged in.';
								}
							}
					}
				).request();
}

var rateAnswer = function(id, pm)
{
	var ajax = new Ajax(
					'/resources/tools/ajax.php?do=rating&aid=' + id + '&rate=' + ((pm == '+')?"1":"0"), 
					{
						method: 'get',
						onComplete: 
							function(data) {
								type = data.substring(0, 1);
								answ = data.substring(1);
								if(type == '5')
								{
									$('plus-' + answ).className  = 'plus-hover';
									$('minus-' + answ).className = 'minus';
								} else if (type == '0') {
									$('plus-' + answ).className  = 'plus';
									$('minus-' + answ).className = 'minus-hover';
								} else if (data == '-1') {
									$('ratingError' + answ).innerHTML = 'You must be logged in.';
								}
							}
					}
				).request();
}

var addTags = function(id, obj)
{
		$(obj).innerHTML = "<textarea style=\"width: 95%;\" id=\"tagsToAdd" + id + "\"></textarea><br /><a href=\"javascript:;\" onclick=\"saveTags(" + id + ", '" + obj + "');\">Save Tags</a><br /><small>(seperated by commas)</small>";
}

var saveTags = function(id, obj)
{
	var ajax = new Ajax(
					'/resources/tools/ajax.php?do=addTags&layout=' + id +'&tags=' + $('tagsToAdd' + id).value, 
					{
						method: 'get',
						onComplete:
							function(data)
							{
								if (data != '-1' && data != '-2' && data != '-3' && data != '-4')
								{
									$('relatedTags').innerHTML = data;
									$('tagArea').innerHTML = '';
									$('tagErrorArea').innerHTML = '';
								} else if (data == '-2') {
									$('tagErrorArea').innerHTML = '<small>Only a-z and spaces are allowed in tags seperated by commas.</small>';
								} else if (data == '-3') {
									$('tagErrorArea').innerHTML = '<small>Please.  Be decent with your tags.</small>';
								} else if (data == '-4') {
									$('tagErrorArea').innerHTML = '<small>We limit users to submitting only 10 tags per layout.</small>';
								} else {
									$('tagErrorArea').innerHTML = '<small>You must be logged in to tag items.</small>';
								}
							}
					}
				).request();
}

var previewCode = function()
{
	code = $('layoutCode').value;
	popUpeditorv3('/myspace-layouts/preview/submitting/?code=' + code);
}

function popUpeditorv3(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=850,height=1500,left = 100,top = 300');");
}