﻿/*global jenaServices, jCore, jForm, jPage, jServices */

function RomiLogin()
{
	this.initialized = false;
	
	var UserControlId = "ROMILogin.UserName", PassControlId = "ROMILogin.PassWord", WarningControlId = "ROMILogin.loginWarning";
		
	this.init = function ()
	{
		if (!this.initialized)
		{
			jenaServices.prototype.LoginService = function ()
			{
				this.host = "";
				this.base = "http://www.romidesigns.com/";
				this.url = "/App_Support/Services/Login.asmx";
				
				return this;
			};
			
			this.initialized = true;
		}
	};
	this.logIn = function ()
	{
		this.init();
		
		var userName = jForm().value(UserControlId), 
			passWord = jForm().value(PassControlId),
			soap = jServices().LoginService().call("UserLogin", {Username: userName, Password: passWord});
			
		if (soap.error !== "")
		{
			jCore("#" + WarningControlId).showError(soap.error);
		}
		else
		{
			jPage.navigate();
		}
	};
	this.logOut = function ()
	{
		this.init();
		
		jServices().LoginService().call("UserLogout");
		jPage.navigate();
	};
	
	return this;
}

var rdLogin = new RomiLogin();

function RomiContent()
{
	this.initialized = false;
	
	this.getItem = function (pUser, pMonth, pYear)
	{
		var response = this.loadReturn("GetItem", {User: pUser, Month: pMonth, Year: pYear}).parent.value;
		jCore("#dynContent").text(response);
	};
	
	this.getAllItems = function ()
	{
		this.loadDynamic("GetItems", "leftTop");
	};
	
	this.getItemDates = function (pUser, pItemDates)
	{
		pItemDates = pItemDates || true;
		
		var response = this.loadReturn("GetItemDates", {User: pUser}).parent.value,
			dates = new jXml("<data>" + response + "</data>").nodes("data/span/a");

		if (pItemDates) 
		{
			jCore("#leftCenterSubContent").text(response); 
		}

		if (dates.length > 0)
		{
			var last = dates[dates.length - 1],
				lastMonth = last.attribute("month"),
				lastYear = last.attribute("year");
					
			this.getItem(pUser, lastMonth, lastYear);
		}
	};
	
	this.init = function ()
	{
		if (!this.initialized)
		{
			jenaServices.prototype.ContentService = function ()
			{
				this.host = "";
				this.base = "http://www.romidesigns.com/";
				this.url = "/App_Support/Services/Content.asmx";
				
				return this;
			};
			
			this.initialized = true;
		}
	};
	
	this.loadBase = function (pScript, pId)
	{	
		this.init();
		
		this.loadDynamic("GetContent", pId, {Section: pScript});
	};
	
	this.loadDynamic = function (pMethod, pId, pArgs)
	{
		this.init();
		
		var soap = jServices().ContentService().call(pMethod, pArgs);
		jCore("#" + pId).text(soap.parent.value);
	};
	
	this.loadReturn = function (pMethod, pArgs)
	{
		this.init();
		
		return jServices().ContentService().call(pMethod, pArgs);
	};
	
	this.load = function (pScript)
	{
		this.loadBase(pScript, "dynContent");
	};
	
	this.loadSchedule = function (pLimit)
	{
		pLimit = pLimit || "0";
		
		this.loadDynamic("GetSchedule", "dynContent", {Limit: pLimit});
	};

	this.loadItems = function (pLoadOthers, pUser)
	{
		pLoadOthers = pLoadOthers || false;
		
		if (pLoadOthers)
		{
			this.getAllItems();
		}
		
		this.getItemDates(pUser);
	};
	
	return this;
}

var rdContent = new RomiContent();