﻿var xmlhttp = null;
JSPoster = function(){};
JSPoster.prototype.xmlHttp = null;
JSPoster.prototype.createHttpObject = function(){
	try {this .xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(ex1) { try { this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(ex2) { this.xmlHttp = null; } }

	if(this.xmlHttp == null && typeof XMLHttpRequest!='undefined') this.xmlHttp = new XMLHttpRequest();

	return this.xmlHttp;
};
JSPoster.prototype.GetHtml = function(element, url)
{
	if(this.xmlHttp==null) this.createHttpObject();
	this.element = element;
	this.xmlHttp.open("GET", url, false);
	this.xmlHttp.send();
	var realThis = this;
	this.timer = window.setInterval(function(){realThis.checkState();}, 100);
};
JSPoster.prototype.checkState = function()
{
	var realThis = this;
	if(this.xmlHttp.readyState==4)
	{
		hideLoading();
		if(!overlay) return;
		var currentLeft = parseInt(realThis.element.style.left) | 0;
		realThis.element.innerHTML = realThis.xmlHttp.responseText;
		realThis.element.fadeTween = Ekina.UI.Effects.Tween.alpha(
			overlay, 
			0, 
			{duration:tweenDuration, func:Ekina.UI.Effects.Tween.Regular.Out}
		);
		realThis.element.slideTween = new Ekina.UI.Effects.Tween(
				realThis.element.style, 
				"left", 
				currentLeft, 
				0, 
				.4, 
				Ekina.UI.Effects.Tween.Regular.Out, 
				"px"
		);
		realThis.element.fadeTween.start();
		realThis.element.slideTween.start();
		realThis.element.tweenStatus = "stopped";
		realThis.element.fadeTween.onTweenFinished = function()
		{
			if(overlay)
				realThis.element.parentNode.removeChild(overlay);
			overlay = null;
		}
		realThis.xmlHttp = null;
		window.clearInterval(realThis.timer);
		realThis.timer = null;
	}
}
var tweenDuration = .5;
var overlay;


function PutHtmlIntoElement(url, element,overlayColor, aTag)
{
	if(element.tweenStatus=="running") return;
	
	if(aTag)
	{
		if(currentActiveAtag)
			currentActiveAtag.className = "";
		aTag.className = "active";
		currentActiveAtag = aTag;
	}
	if(!overlayColor) overlayColor = "#ffffff";
	if(element.fadeTween) element.fadeTween.stop();
	if(element.slideTween) element.slideTween.stop();

	if(!element.tweenStatus || element.tweenStatus=="stopped")
		element.tweenStatus = "running";

	var currentLeft = parseInt(element.style.left) | 0;
	if(!overlay)
	{
		overlay = $("<div></div>").css({background:overlayColor,width:element.parentNode.offsetWidth+ "px",height:element.parentNode.offsetHeight + "px",position:"absolute",left:"0px",top:"0px"});
		Ekina.UI.Effects.SetAlpha(overlay, 0);
		element.parentNode.appendChild(overlay);
	}

	element.fadeTween = Ekina.UI.Effects.Tween.alpha(
		overlay, 
		100, 
		{duration:tweenDuration, func:Ekina.UI.Effects.Tween.Regular.In}
	);
	element.slideTween = new Ekina.UI.Effects.Tween(
		element.style, 
		"left", 
		currentLeft, 
		-(element.offsetWidth+25), 
		tweenDuration, 
		Ekina.UI.Effects.Tween.Regular.In, 
		"px"
	);
	element.fadeTween.start();
	element.slideTween.start();
	Mouse.getElementPos(element.parentNode);
	showLoading((element.parentNode.absoluteX + (element.parentNode.offsetWidth / 2)) - 20, 500);
	element.slideTween.onTweenFinished = function()
	{
		var newHtmlLoader = new JSPoster();
		newHtmlLoader.GetHtml(element, url);
	}
}


