// -----------------------------------------------------------------------------
// Ads

var member_group = 0;
var ads_disabled = true;
var right_visible = false;
var bottom_visible = false;
var right_width = 200;
var bottom_height = 160;
var right_interval = 10000;
var right_click_delay = 3;
var bottom_interval = 29000;
var bottom_click_delay = 3;

var list_of_blocks = [];

var right_last_app_click_date = null;
var right_int_id = null;
var bottom_last_app_click_date = null;
var bottom_int_id = null;
var blockRightArea = false;
var blockBottomArea = false;

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

function updateAds()
{
	if (ads_disabled) return;

	var right_time_diff = 1000000;
	if (right_last_app_click_date!=null)
	{
		var now = new Date();
		right_time_diff = (now.getTime()-right_last_app_click_date.getTime())/1000;
	}

	if (right_time_diff>right_click_delay)
	{
		right_last_app_click_date = new Date();

		update_ads("right");
		
		blockRightArea = true;
		setTimeout(function(){ blockRightArea=false; }, right_interval);
	}

	var bottom_time_diff = 1000000;
	if (bottom_last_app_click_date!=null)
	{
		var now = new Date();
		bottom_time_diff = (now.getTime()-bottom_last_app_click_date.getTime())/1000;
	}

	if (bottom_time_diff>bottom_click_delay)
	{
		bottom_last_app_click_date = new Date();

		update_ads("bottom");

		blockBottomArea = true;
		setTimeout(function(){ blockBottomArea=false; }, bottom_interval);

	}

	if (right_time_diff>right_click_delay)
	{
		updateAdsStatus("update right ads by click");
	}
	if (bottom_time_diff>bottom_click_delay)
	{
		updateAdsStatus("update bottom ads by click");
	}
}

function showAds(obj,publisher,shared,membergroup)
{
	if (location.href.indexOf('doNotShowAds')!=-1) return;
	var displayAds = false;

	member_group = membergroup?membergroup:0;

	for (var i in obj)
	{
		var o = obj[i];
		var area_name = String(o.name);
		var area_visible = o.visible;
		var area_width = parseInt(o.width);
		var area_height = parseInt(o.height);
		var area_refreshInterval = parseInt(o.refreshInterval)*1000;
		var area_clickDelayInterval = parseInt(o.clickDelayInterval);
		if (!(area_clickDelayInterval>0))
			area_clickDelayInterval = 3;

		if (area_name=="Application Right" && area_visible)
		{
			right_width = area_width;
			right_interval = area_refreshInterval;
			right_click_delay = area_clickDelayInterval;
			for (var j in o.blocks)
			{
				var b = o.blocks[j];
				list_of_blocks[list_of_blocks.length] = { id: b.id, p:"right", w:b.width, h:b.height};
				displayAds = true;
				right_visible = true;
				document.getElementById("area_right").parentNode.style.width = document.getElementById("area_right").style.width = b.width + "px";
				document.getElementById("rightColumn").width = b.width;
			}
		}
		if (area_name=="Application Bottom" && area_visible)
		{
			bottom_height = area_height;
			bottom_interval = area_refreshInterval;
			bottom_click_delay = area_clickDelayInterval;
			for (var j in o.blocks)
			{
				var b = o.blocks[j];
				list_of_blocks[list_of_blocks.length] = { id: b.id, p:"bottom", w:b.width, h:b.height};
				displayAds = true;
				bottom_visible = true;
				document.getElementById("area_bottom").parentNode.style.height = document.getElementById("area_bottom").style.height = b.height + "px";
			}
		}
	}

	if (!displayAds) return;

	ads_disabled = false;

	update_ads();

	if (right_int_id>0)
		clearInterval(right_int_id);

	right_int_id = setInterval(function(){

		update_ads("right");

		updateAdsStatus("update right ads by interval");

	}, right_interval);

	if (bottom_int_id>0)
		clearInterval(bottom_int_id);

	bottom_int_id = setInterval(function(){

		update_ads("bottom");

		updateAdsStatus("update bottom ads by interval");

	}, bottom_interval);

	updateAdsStatus("show ads");

}

function hideAds()
{
	ads_disabled = true;
	var rightArea = document.getElementById("area_right");
	var bottomArea = document.getElementById("area_bottom");
	var rCol = document.getElementById("rightColumn");

	if (right_visible)
	{
		while (rightArea.firstChild)
		{
			rightArea.firstChild.id = "";
			rightArea.removeChild(rightArea.firstChild)
		}
		var rightImg = document.createElement("img");
		rightImg.id = "right_iframe";
		rightImg.width = 1;
		rightImg.height = 1;
		rightImg.src = "/images/spacer.gif";
		rightArea.appendChild(rightImg);
		rightArea.parentNode.style.width = rightArea.style.width = "0px";
		try{
			rCol.width = 0;
		}catch(e){
			var err = String(e);
		}
	}
	if (bottom_visible)
	{
		while (bottomArea.firstChild)
		{
			bottomArea.firstChild.id = "";
			bottomArea.removeChild(bottomArea.firstChild)
		}
		var bottomImg = document.createElement("img");
		bottomImg.id = "bottom_iframe";
		bottomImg.width = 1;
		bottomImg.height = 1;
		bottomImg.src = "/images/spacer.gif";
		bottomArea.appendChild(bottomImg);
		bottomArea.parentNode.style.height = bottomArea.style.height = "0px";
	}

	updateAdsStatus("hide ads");

}

function update_ads(position)
{
	if (ads_disabled/* || (blockRightArea && position=="right") || (blockBottomArea && position=="bottom")*/) return;
	window.status = new Date()+ " - update "+position;
	var rightIframe = document.getElementById("right_iframe");
	var bottomIframe = document.getElementById("bottom_iframe");
	var rightArea = document.getElementById("area_right");
	var bottomArea = document.getElementById("area_bottom");

	var hasRightBlocks = false;
	var hasBottomBlocks = false;
	for (var i=0; i<list_of_blocks.length; i++)
	{
		if (right_visible && list_of_blocks[i].p=="right" && (!position || (position && position=="right")))
		{
			hasRightBlocks = true;
		}

		if (bottom_visible && list_of_blocks[i].p=="bottom" && (!position || (position && position=="bottom")))
		{
			hasBottomBlocks = true;
		}
	}

	if (hasBottomBlocks)
	{
		loadAdsIFrame(bottomIframe, "/app/advertisement?areaId=2"+"&mg="+member_group);
	}
	if (hasRightBlocks)
	{
		loadAdsIFrame(rightIframe, "/app/advertisement?areaId=4"+"&mg="+member_group);
	}

}

function loadAdsIFrame(element, newUrl)
{
	var elem = element;
	if (!elem.parentNode || ads_disabled) return;

	var newelem = createAdsIFrame(elem.id+(new Date()).getTime());
	newelem.width = 0;
	newelem.height = 0;
	elem.parentNode.appendChild(newelem);

	if (!window.adsZIndex)
		window.adsZIndex = 2;

	var adsZIndexShift = elem.id=="right_iframe"?10:0;
	$(elem).css('z-index', String(window.adsZIndex+adsZIndexShift)).css('position', 'absolute');
	$(newelem).css('z-index', String(window.adsZIndex+adsZIndexShift-1)).css('position', 'absolute').hide();
	window.adsZIndex += 2;

	newelem.src = newUrl;

	if (!window.adsFrames)
	{
		window.adsFrames = [];
		window.adsFrames[elem.id] = 0;
	}
	if (window.adsFrames[elem.id]!=0)
		clearTimeout(window.adsFrames[elem.id]);

	window.adsFrames[elem.id] = setTimeout("replaceAdsIFrame('"+elem.id+"')", 5000);

	updateAdsStatus("load "+(elem.id=="right_iframe"?"right":"bottom")+" ads iframe("+newUrl+")");
}

function replaceAdsIFrame(id) {
	var elem = $("#"+id)[0];
	var newelem;
	$(elem).parent().children().each(function(i){
		var a = 1;
		if (this!=elem)
			newelem = this;
	});
	if (!newelem) return;

	if (window.adsFrames[id]!=0)
		clearTimeout(window.adsFrames[id]);

	window.adsFrames[elem.id] = 0;
	newelem.id = elem.id;
	elem.id = "";

	$(elem).fadeOut('slow', function() {
		removeTempAdsIFrames(this.parentNode);
	});

	newelem.width = newelem.id=="right_iframe"?right_width:"100%";
	newelem.height = newelem.id=="bottom_iframe"?bottom_height:"100%";

	$(newelem).fadeIn('slow', function() {
		removeFilter($(this)[0]);
		removeTempAdsIFrames(this.parentNode);
	});

	updateAdsStatus("loaded "+(newelem.id=="right_iframe"?"right":"bottom")+" ads iframe("+newelem.src+")");
}

function removeTempAdsIFrames(parent)
{
	if (parent)
	{
		var iCount = parent.childNodes.length;
		for (var i=iCount-1; i>=0; i--)
			if (parent.childNodes[i].id!="right_iframe" && parent.childNodes[i].id!="bottom_iframe")
			{
				parent.childNodes[i].src = "about:blank";
				parent.removeChild(parent.childNodes[i]);
			}
	}
}

function createAdsIFrame(id)
{
	var ifrm = document.getElementById(id);
	if (ifrm) return ifrm;
	ifrm = document.createElement("iframe");
	ifrm.id = id;
	ifrm.width = "100%";
	ifrm.height = "100%";
	ifrm.frameBorder = 0;
	ifrm.scrolling = "no";
	return ifrm;
}

function updateAdsStatus(message)
{
	try{
		document.getElementById("firecat").adsStatusFromJavaScript(message);
	}catch(e){}
}
// -----------------------------------------------------------------------------