
getElement = function(aID)
{
	return (document.getElementById) ? document.getElementById(aID) : document.all[aID];
}

var __Cincopa__objects = Array();

function CincopaError(msg)
{
	alert(msg);
	for(var i=0; i<__Cincopa__objects.length; i++)
		__Cincopa__objects[i].onGeneralError(msg);
}

function CincopaCheckConnection()
{
	var ifrm = "<iframe style='visibility:hidden' width=1 height=1 src='http://api.cincopa.com/skins/api/checkconnection.aspx'></iframe>";
	document.write(ifrm);
}

function CincopaIFrame(APIKey, width, height)
{
	this.globalIndex = -1;
	
	this.APIKey = APIKey;
	this.width = width;
	this.height = height;
	this.name = "main";
	this.seed = Math.random(); //new Date().getTime();
	this.OnStatus = function(code, data) {};
	this.OnError = function(code, data) {};
	this.lastState = "";
//	this.site = "http://127.0.0.1";
	this.site = "http://api.cincopa.com";
	this.pullStatusSite = "http://data.cincopa.com";
	this.aid = "";

	this.init = function()
	{
		this.globalIndex = __Cincopa__objects.length;
		__Cincopa__objects[this.globalIndex] = this;
	}
	
	this.ifrm = function()
	{
		return getElement(this.name);
	}
	
	this.GetXMLHttpRequestObj = function()
	{
		// code for Mozilla, etc.
		if (window.XMLHttpRequest)
			return new XMLHttpRequest()
		// code for IE
		else if (window.ActiveXObject)
			return new ActiveXObject("Microsoft.XMLHTTP")
			
		return null;
	}

	///////////////////////////////////// check local  //////////////////////////////////////

	this.PullData = function()
	{
		var jsFile = this.pullStatusSite+"/skins/api/state.aspx?seed="+this.seed+"&callback=__Cincopa__objects["+this.globalIndex+"].onPullData";
		
		var headTag = document.getElementsByTagName('head')[0];
		script = document.createElement('script');
		script.id = 'hiddenScript';
		script.type = 'text/javascript';
		script.src = jsFile;
		headTag.appendChild(script);
	
//		document.write('<script type="text/javascript" src="'+ jsFile + '"></script>');
//		document.write('<h>'+ jsFile + '</h1>');
	}
	
	this.onGeneralError = function(msg)
	{
		this.OnStatus(msg, "");
		this.Hide();
	}
	
	this.onPullData = function(data)
	{
		if (data.response.ping == null)
			this.OnResponse(data);
		
		if (this.lastState != "applied")
			setTimeout("__Cincopa__objects["+this.globalIndex+"].PullData()", 0);
	}

	this.OnResponse = function(data)
	{
		if (data.response.error != null)
		{
			alert("error "+data.response.error.code+" - "+data.response.error.message);				
		}
		else
		{
			if (data.response.state != this.lastState)
			{
				this.lastState = data.response.state;

				if (this.lastState == "redirectlogin")
					self.location.href = "http://api.cincopa.com/skins/api/cincopalogin.aspx?serviceid="+this.APIKey+"&continue="+encodeURIComponent(self.location.href);
				else
					this.OnStatus(this.lastState, data);
			}
		}
	}

	this.Show = function()
	{
//		setInterval("__Cincopa__objects["+this.globalIndex+"].PullData()", 1000);

		var ifrm = "<iframe style='border-style:none;' scrolling=yes width="+this.width+" height="+this.height+" id="+this.name+"></iframe>";
		document.write(ifrm);
		this.ifrm().src = this.site+"/template.aspx?skin=api%2flogin&seed="+this.seed+"&aid="+this.aid+"&serviceid="+this.APIKey+"&ref="+encodeURIComponent(self.location.href);

		setTimeout("__Cincopa__objects["+this.globalIndex+"].PullData()", 0);
	}
	
	this.EditAlbum = function(guid)
	{
		this.aid = guid.split('.')[0];
	}
	
	this.Hide = function()
	{
//		clearInterval(this.timer);
		this.ifrm().src = "";
		
		(this.ifrm().parentNode).removeChild(this.ifrm());
	}
	
	this.init();
}
