// JavaScript Document
var ServerProxy = {};

ServerProxy.requestPost = function(postId, successFunction)
{
	$.getJSON(Ali.settings.baseUrl + "?json=get_post&post_id=" + postId + "&custom_fields=backgroundColor,backgroundImage,textColor,description", function(data)
	{
		var elements = new Array();
		for(var i = 0; i < data.post.attachments.length; i++)
		{
			var a = data.post.attachments[i];
			if(a.images == undefined) // Only grab attachments added by the attachments plugin
				elements.push(
				{
					id: postId + "_" + i,
					title:"",
					width: a.width,
					height: a.height,
					url: a.url,
					excerpt:""
				});
		}
		var cf = data.post.custom_fields;
		successFunction(
		{
			title: data.post.title,
			backgroundColor: cf.backgroundColor != undefined ? cf.backgroundColor[0] : Ali.settings.defaultBackgroundColor,
			backgroundImage: cf.backgroundImage != undefined ? "url('" + cf.backgroundImage[0] + "')" : Ali.settings.defaultBackgroundImage,
			description: cf.description != undefined ? cf.description[0] : "",
			textColor: cf.textColor != undefined ? cf.textColor[0] : Ali.settings.defaultTextColor,
			elements: ServerProxy.loadBoxes(elements, Ali.settings.elementsLayoutSettings)
		});
	});
};
ServerProxy.requestPosts = function(page, successFunction)
{
	$.getJSON(Ali.settings.baseUrl + "?json=get_recent_posts&page=" + (1 + page) + "&custom_fields=backgroundColor,backgroundImage,textColor", function(data)
	{
		var posts = new Array();
		for(var i = 0; i < data.posts.length; i++)
		{	
			var rawPost = data.posts[i];
			posts.push(ServerProxy.preparePost(rawPost.id, rawPost.title, rawPost.excerpt, rawPost.content, rawPost.custom_fields));
		}
		//window.atest = ServerProxy.loadBoxes(posts);
		successFunction(ServerProxy.loadBoxes(posts, Ali.settings.postsLayoutSettings));
	});
	//window.setTimeout(function(){successFunction(ServerProxy.loadBoxes(testPosts));	}, 2000);
};
ServerProxy.preparePost = function(id, title, excerpt, content, customFields)
{
	//Ali.log("Preparing " + title);
	var img = $("img", content);//.prevObject;));
	return {
		id: id,
		title:title,
		width:img.attr("width"),
		height:img.attr("height"),
		url:img.attr("src"),
		backgroundColor: customFields.backgroundColor != undefined ? customFields.backgroundColor [0] : undefined,
		backgroundImage: customFields.backgroundImage != undefined ? customFields.backgroundImage[0] : undefined,
		textColor: customFields.textColor != undefined ? customFields.textColor[0] : undefined
		//excerpt:excerpt
	};
};
ServerProxy.requestPage = function(pageId, successFunction)
{
	jQuery.getJSON(
			Ali.settings.baseUrl + "?json=get_page&id=" + pageId, function(data)
		{
			var page = {};
			page.content = data.page.content;
			page.title = data.page.title;
			successFunction(page); //testPage
		}
	);
};
ServerProxy.loadBoxes = function(posts, settings)
{
	var boxes = new Array();
	var tileSize = settings.gridTileSize;
	for (var i = 0; i < posts.length; i++)
	{
		var post = posts[i];
		var w = Math.floor(post.width / tileSize) *  tileSize;
		var h = Math.floor(post.height / tileSize) *  tileSize;
		var box = new AutoLayout.Box(post.id, w, h, post.title, post.excerpt, post.url, post.textColor, post.backgroundColor, post.backgroundImage, settings.borderWidth);
		
		if(w > 0 && h > 0)
			boxes.push(box);
	}
	return boxes;
};
