/*
*	HTMLArea Class
*	Requires jQuery
*	Born Interactive 2008
*/

function HTMLArea(params_obj)
{
	this.id = params_obj.id;
	this.params_obj = params_obj;
	
	this.createTag();
	this.showContent();
}

HTMLArea.prototype.createTag = function()
{
	// id same div ID exists update the content (no need to create the tag again)
	if($("#"+this.id).length) {
		this.div = $("#"+this.id);
		this.updateSizeAndPos();
		return;
	}
	this.relhost = addRelativeDiv();
	$(this.relhost).append('<div id="'+this.id+'"></div>')
	this.div = $("#"+this.id);
	this.div.css("position", "absolute");
	this.div.css("overflow", "hidden");
	if(getLang() == "ar") {					//handle arabic lang changes
		this.div.css("direction", "rtl");
		this.div.addClass("rtl");
	}
	if(typeof this.params_obj.scrollclass != "undefined") this.div.addClass(this.params_obj.scrollclass);	//custom scrollbars class
	this.updateSizeAndPos();
	this.saveReference();
}

HTMLArea.prototype.updateSizeAndPos = function()
{
	$(this.div).css("top", this.params_obj.top);
	$(this.div).css("left", this.params_obj.left);
	$(this.div).css("width", this.params_obj.width);
	$(this.div).css("height", this.params_obj.height);
}

HTMLArea.prototype.fillContent = function(str)
{
	this.showLoader(false);
	$(this.div).hide();
	$(this.div).append('<div class="scrollable-content">' + str + '</div>');
	
	if(str.indexOf("<form") != -1) this.setFormTarget();
	
	this.filterImageLinks();
	if(enable_animation) {
		this.showAnimated();
	} else {
		$(this.div).show();
	}
	
	this.addScrollers();
}

HTMLArea.prototype.showAnimated = function()
{
	$(this.div).fadeIn(300);
}

HTMLArea.prototype.addScrollers = function()
{
	window.setTimeout("addScrollersToArea('"+this.id+"')", 400)
}

HTMLArea.prototype.addScrollersNow = function(area)
{
	if(this.params_obj.scroll == "v"){
		$(this.div).find("div.scrollable-content").css("height", this.params_obj.height);
		$(this.div).find("div.scrollable-content").css("overflow", "hidden");
		var sleft = (getLang() == "ar");
		$(this.div).find("div.scrollable-content").jScrollPane({showArrows:true, scrollbarWidth: 14, scrollbarOnLeft: sleft });
	} else if(this.params_obj.scroll == "h") {
		$(this.div).addClass("horzpane");
		$(this.div).find("div.scrollable-content").css("width", this.params_obj.width);
		$(this.div).find("div.scrollable-content").css("float", "left");
		$(this.div).find("div.scrollable-content").css("height", function(){return $(this).height()+15;})
		$(this.div).find("div.scrollable-content").css("overflow", "hidden");
		$(this.div).find("div.scrollable-content").jScrollHorizontalPane({showArrows:true, scrollbarHeight: 12, animateTo:false });
	}
}

HTMLArea.prototype.filterImageLinks = function()		// convert href image links to showPhoto funtion
{
	var images_data   = this.getImagesDataArrays();
	var photos_group  = images_data[0];
	var photos_titles = images_data[1];
	
	$(this.div).find("a").each(function(){
		if(this.href.toLowerCase().indexOf(".jpg") != -1 && this.className != "externalimage") {
			this.oldhref = this.href;
			this.photos_group = photos_group;
			this.photos_titles = photos_titles;
			this.onclick = function(){ showPhoto(this.oldhref, this.photos_group, this.photos_titles); };
			this.target = "";
			this.href = "javascript:;";
		}
	});
}

HTMLArea.prototype.getImagesDataArrays = function()
{
	var paths  = [];
	var titles = [];
	$(this.div).find("a").each(function(){
		if(this.href.toLowerCase().indexOf(".jpg") != -1 && this.className != "externalimage") {
			paths.push(this.href);
			titles.push((typeof this.title == "string") ? this.title : "");
		}
	});
	return [paths, titles];
}

HTMLArea.prototype.getImagesGroupArray = function()
{
	var res = [];
	$(this.div).find("a").each(function(){
		if(this.href.toLowerCase().indexOf(".jpg") != -1 && this.className != "externalimage") {
			res.push(this.href);
		}
	});
	return res;
}

HTMLArea.prototype.updateContent = function(newurl, istext)		// update content of same area with new page
{
	this.params_obj.content = newurl;
	if(istext) this.params_obj.content_type = "text"; else this.params_obj.content_type = "url";
	
	this.showContent();
}

HTMLArea.prototype.showContent = function()
{
	// hide existing content and show loader
	this.empty();
	
	if(!this.params_obj.content.length || this.params_obj.content == "undefined") { return; }	//no content or URL was provided
	
	this.showLoader(true);

	// check type of content (url or text)
	if(this.params_obj.content_type == "text") {
		this.fillContent(this.params_obj.content);
	} else {
		this.loadURLContent(this.params_obj.content);	// Expecting a URL by default
	}
}

HTMLArea.prototype.setFormTarget = function()
{
	$(this.div).find("form").attr("target", "formiframe")
	//.submit(function(){ alert(this); $(this).html("Please wait..."); });
}

HTMLArea.prototype.empty = function()
{
	$(this.div).empty();
}

HTMLArea.prototype.showLoader = function(show)
{
	var loading_msg = "Loading...";
	if(typeof lang != "undefined") {
		if(lang == "ar") loading_msg = "تحميل...";
	}
	if(show){
		$(this.div).append('<div class="arealoader">' + loading_msg + '</div>');
		$(this.div).find(".arealoader").css("position", "absolute");
		$(this.div).find(".arealoader").css("left", Math.round(this.params_obj.width/2 - 40)+"px");
		$(this.div).find(".arealoader").css("top", Math.round(this.params_obj.height/2 - 10));
	} else {
		$(this.div).empty()
	}
}

HTMLArea.prototype.loadURLContent = function(url)
{
	var me = this;
	
	// Using jQuery AJAX capabilities, 
	// getting the HTML contents form a page
	$.get(url, function(data){
  		me.fillContent(data);
	});
}

HTMLArea.prototype.remove = function()
{
	$(this.div).remove();
	// remove from array
	deleteArrayItem(all_areas, this);
}

HTMLArea.prototype.saveReference = function()
{
	all_areas.push(this);
}


/******************************************************************************** 
	Related utitlities and helpers 
********************************************************************************/
var all_areas = [];		// array used to save reference IDs of all areas on stage
var enable_animation = true;

function deleteArrayItem (arr, item)
{
	for(var i=0; i<arr.length; i++)
	{
		if(arr[i] == item) {
			arr.splice(i, 1);
			return true;
		}
	}
	return false;
}

function addRelativeDiv()
{
	// if a relative div already exists do nothing
	if($("#areashost").length > 0) return $("#areashost");
	
	$("body").prepend('<div id="areashost"></div>');
	$("#areashost").css("position", "relative");
	return $("#areashost");
}

function hideALlAreas()
{
	while(all_areas.length) all_areas.pop().remove();
}

function getAreaById(id_str)
{
	for(area in all_areas) {
		if(all_areas[area].id == id_str) return all_areas[area];
	}
	return null;
}

function addScrollersToArea(area_id) 
{
	var area = getAreaById(area_id);
	if(area!=null) {
		area.addScrollersNow();
	}
}

function updateArea(area_id, newurl, istext)
{
	var area = getAreaById(area_id);
	
	if(area != null) {
		area.updateContent(newurl, istext);
	}
	
}
