var dynamic_list_size = 10;

// SUPPORT MULTIPLE INSTANCES IN ONE PAGE...
var dynamic_list_counters_array = new Array();
var dynamic_list_action_array = new Array();
var dynamic_list_func_array = new Array();
var dynamic_list_obj_result_array = new Array();
var dynamic_list_filter_array = new Array();
var dynamic_list_next_index_array = new Array();
var dynamic_list_module_array = new Array();
var dynamic_list_accumulate_index = dynamic_list_size;

var dynamic_list_last_id = "";
var dynamic_list_current_item = "";

var dynamic_list_next_index = dynamic_list_size;

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_current_item(current) {

	dynamic_list_current_item = current;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_get_current_item() {

	return dynamic_list_current_item;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_get_last_id() {

	return dynamic_list_last_id;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_last_id(say_id) {

	dynamic_list_last_id = say_id;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_module(module, item_name) {

	dynamic_list_module_array[item_name] = module;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_get_module(item_name) {

	return dynamic_list_module_array[item_name];
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_next_index(next_index, item_name) {

	dynamic_list_next_index_array[item_name] = next_index;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_filter(filter, item_name) {

	dynamic_list_filter_array[item_name] = filter;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_obj_result(obj_result, item_name) {

	dynamic_list_obj_result_array[item_name] = obj_result;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_func(func, item_name) {

	dynamic_list_func_array[item_name] = func;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_action(action, item_name) {

	dynamic_list_action_array[item_name] = action;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_more(item_name) {
	
	if (dynamic_list_counters_array[item_name] == null)
		dynamic_list_counters_array[item_name] = 0;

	if (dynamic_list_last_id == "")
		dynamic_list_last_id = document.getElementById("dynamic_list_save_say_id").value;

	dynamic_list_call_ajax(item_name);

	dynamic_list_next_index_array[item_name] =  dynamic_list_next_index_array[item_name] + 1;
}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_call_ajax(item_name) {

	// Call ajax

	var lsid_param;

	dynamic_list_set_current_item(item_name);

	if (dynamic_list_get_module(item_name) == "folder")
		lsid_param = "&val=";
	else
		lsid_param = "&lsid=";

	ajax_makeGETRequest(dynamic_list_action_array[item_name] + ".jsp?page=" + encodeURI(dynamic_list_next_index_array[item_name]) + "&" + dynamic_list_filter_array[item_name] + lsid_param + dynamic_list_get_last_id() + "&" + Math.random()
, null, dynamic_list_func_array[item_name], dynamic_list_obj_result_array[item_name]);

}

////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_add(item_name, index) {

	var newItem = document.getElementById(item_name + "_clone").cloneNode(true);
	var next_counter = dynamic_list_counters_array[item_name] + 1;
	newItem.id = item_name + "_append_" + next_counter;

	var list = document.getElementById(item_name + "_append_" + dynamic_list_counters_array[item_name]);
	list.appendChild(newItem);
	dynamic_list_counters_array[item_name]++;
	dynamic_list_accumulate_index++;

	return newItem;
}

///////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_pagerParser() {

	var result = document.getElementById("dynamic_list_result").value;
	//alert(result);
	var isQuote, buf, sep, say_id, photo_path, say_text, creator, creator_name, tags;
	var from = 0;
	buf = result;
	var index, sayObj;
	var decl;
	var tag1_val, tag1_type, tag1_name, tag1_impact;
	var tag2_val, tag2_type, tag2_name, tag2_impact;
	var tag3_val, tag3_type, tag3_name, tag3_impact;
	var lsid, votes, posting_time;

	var url = window.location;

	var s_url = url.toString();
	var base_url = s_url.substr(0, s_url.indexOf("/" + dynamic_list_get_module(dynamic_list_get_current_item())));

	sep = buf.indexOf("&$*@");		
	var limit = buf.substr(from, sep);
	buf = buf.substr(sep+4, buf.length);

	sep = buf.indexOf("&$*@");		
	lsid = buf.substr(from, sep);
	buf = buf.substr(sep+4, buf.length);

	if (limit == 0) {
		document.getElementById("dynamic_more_button").style.display = "none";
		document.getElementById("dynamic_no_more_records").style.display = "block";
		return;
	}

	for (i = 0; i < limit; i++) {
		index = i + 1;
		sep = buf.indexOf("&$*@");		
		
		isQuote = buf.substr(from, sep);
		isQuote = isQuote.replace(/^\s*|\s*$/g,''); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");
				say_id = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");
		//alert(say_id);

		dynamic_list_set_last_id(say_id);
		photo_path = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		say_text = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");
		//alert(say_text);
		creator = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");
		creator_name = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");
		tag1_val = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag1_type = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag1_name = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag1_impact = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag2_val = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag2_type = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag2_name = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag2_impact = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag3_val = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag3_type = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag3_name = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		tag3_impact = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		votes = parseFloat(buf.substr(from, sep));

		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");
		if (votes > 0)
			votes = "+" + votes;

		posting_time = buf.substr(from, sep); // done
		buf = buf.substr(sep+4, buf.length);
		sep = buf.indexOf("&$*@");

		var temp_plus_img = document.getElementById("plus_img_999999");
		var temp_minus_img = document.getElementById("minus_img_999999");
		var temp_plus = document.getElementById("plus_999999");
		var temp_minus = document.getElementById("minus_999999");
		var temp_vote_message = document.getElementById("vote_message_999999");

		var temp_votes = document.getElementById("post_item_votes_999999");
		var temp_say = document.getElementById("say_999999");
		var temp_photo = document.getElementById("photo_999999");
		var temp_user = document.getElementById("user_999999");

		var temp_creator_name = document.getElementById("creator_name_999999");
		var temp_usr = document.getElementById("usr_999999");
		var temp_say_mode = document.getElementById("say_mode_999999");
		var temp_say_mode_text = document.getElementById("say_mode_text_999999");

		var temp_tag1_name = document.getElementById("tag1_name_999999");
		var temp_tag1_link = document.getElementById("tag1_link_999999");
		var temp_tag1_img = document.getElementById("tag1_img_999999");

		var temp_tag2_name = document.getElementById("tag2_name_999999");
		var temp_tag2_link = document.getElementById("tag2_link_999999");
		var temp_tag2_img = document.getElementById("tag2_img_999999");

		var temp_tag3_name = document.getElementById("tag3_name_999999");
		var temp_tag3_link = document.getElementById("tag3_link_999999");
		var temp_tag3_img = document.getElementById("tag3_img_999999");

		var temp_creation_date = document.getElementById("creation_date_999999");

		var temp_tweet = document.getElementById("tweet_999999");

		var newItem = dynamic_list_add(dynamic_list_get_current_item(), i);

		temp_tweet.id = "xxxxx";
		document.getElementById("tweet_999999").removeAttribute("onclick");

		document.getElementById("tweet_999999").onclick = new Function("post_item_tweet('" + say_id + "')");

		document.getElementById("tweet_999999").id = "";
		temp_tweet.id = "tweet_999999";


		var logged_user_id = parseFloat(document.getElementById("dynamic_list_user_id").value);

		temp_vote_message.id = "xxxxx";
		document.getElementById("vote_message_999999").id = "vote_message_" + dynamic_list_accumulate_index;
		temp_vote_message.id = "vote_message_999999";

		temp_plus.id = "xxxxx";

		document.getElementById("plus_999999").removeAttribute("onclick");
		document.getElementById("plus_999999").removeAttribute("href");

		if (logged_user_id == -1) {
			document.getElementById("plus_999999").setAttribute("href", "javascript:void(0);");
			document.getElementById("plus_999999").onclick = new Function("post_item_plus_not_logged('" + dynamic_list_accumulate_index + "')");
		}
		else
		if (logged_user_id != creator) {
			document.getElementById("plus_999999").setAttribute("href", "javascript:void(0);");
			document.getElementById("plus_999999").onclick = new Function("post_item_plus_logged('" + say_id + "', '" + dynamic_list_accumulate_index + "')");
		}
		document.getElementById("plus_999999").id = "plus_" + dynamic_list_accumulate_index;
		temp_plus.id = "plus_999999";

		temp_minus.id = "xxxxx";
		if (logged_user_id == -1) {
			document.getElementById("minus_999999").setAttribute("href", "javascript:void(0);");
			document.getElementById("minus_999999").onclick = new Function("post_item_minus_not_logged('" + dynamic_list_accumulate_index + "')");
		}
		else
		if (logged_user_id != creator) {
			document.getElementById("minus_999999").setAttribute("href", "javascript:void(0);");
			document.getElementById("minus_999999").onclick = new Function("post_item_minus_logged('" + say_id + "', '" + dynamic_list_accumulate_index + "')");
		}
		document.getElementById("minus_999999").id = "minus_" + dynamic_list_accumulate_index;
		temp_minus.id = "minus_999999";

		temp_plus_img.id = "xxxxx";
		if (logged_user_id != -1 && logged_user_id == creator)
			document.getElementById("plus_img_999999").src = "main/img/general/green_hand_done.gif";
		else
			document.getElementById("plus_img_999999").src = "main/img/general/green_hand.gif";	
		document.getElementById("plus_img_999999").id = "plus_img_" + dynamic_list_accumulate_index;
		temp_plus_img.id = "plus_img_999999";

		temp_minus_img.id = "xxxxx";
		if (logged_user_id != -1 && logged_user_id == creator)
			document.getElementById("minus_img_999999").src = "main/img/general/red_hand_done.gif";
		else
			document.getElementById("minus_img_999999").src = "main/img/general/red_hand.gif";	
		document.getElementById("minus_img_999999").id = "minus_img_" + dynamic_list_accumulate_index;
		temp_minus_img.id = "minus_img_999999";

		temp_votes.id = "xxxxx";
		general_setSpanValue(document.getElementById("post_item_votes_999999"), votes);
		document.getElementById("post_item_votes_999999").id = "post_item_votes_" + dynamic_list_accumulate_index;
		temp_votes.id = "post_item_votes_999999";

		temp_say.id = "xxxxx";
		document.getElementById("say_999999").firstChild.nodeValue = say_text;
		document.getElementById("say_999999").id = "";
		temp_say.id = "say_999999";

		temp_photo.id = "xxxxx";
		document.getElementById("photo_999999").src = photo_path;
		document.getElementById("photo_999999").id = "";
		temp_photo.id = "photo_999999";

		temp_user.id = "xxxxx";
		var new_user_url = base_url + "/user.jsp?UserId=" + creator;
		document.getElementById("user_999999").removeAttribute("href");
		document.getElementById("user_999999").setAttribute("href", new_user_url);
		document.getElementById("user_999999").id = "";
		temp_user.id = "user_999999";

		temp_creator_name.id = "xxxxx";
		general_setSpanValue(document.getElementById("creator_name_999999"), creator_name);
		document.getElementById("creator_name_999999").id = "";
		temp_creator_name.id = "creator_name_999999";

		temp_usr.id = "xxxxx";
		document.getElementById("usr_999999").removeAttribute("href");
		document.getElementById("usr_999999").setAttribute("href", new_user_url);
		document.getElementById("usr_999999").id = "";
		temp_usr.id = "usr_999999";

		///////////////////////////////////////////////////

		temp_say_mode.id = "xxxxx";

		if (isQuote != "_") {
			document.getElementById("say_mode_999999").removeAttribute("target");
			document.getElementById("say_mode_999999").setAttribute("target", "_blank");
			document.getElementById("say_mode_999999").removeAttribute("class");
			general_setClassName("say_mode_999999", "general_link50");
			/*if (navigator.appName == "Netscape")
				document.getElementById("say_mode_999999").setAttribute("class","general_link50");
			else
				document.getElementById("say_mode_999999").setAttribute("className","general_link50");*/


			document.getElementById("say_mode_999999").removeAttribute("href");
			document.getElementById("say_mode_999999").setAttribute("href", isQuote);
		} else {
			document.getElementById("say_mode_999999").removeAttribute("target");
			document.getElementById("say_mode_999999").removeAttribute("class");
			if (navigator.appName == "Netscape")
				document.getElementById("say_mode_999999").setAttribute("class","general_link7");
			else
				document.getElementById("say_mode_999999").setAttribute("className","general_link7");
			document.getElementById("say_mode_999999").removeAttribute("href");
		}
		document.getElementById("say_mode_999999").id = "";
		temp_say_mode.id = "say_mode_999999";


		temp_say_mode_text.id = "xxxxx";
		if (isQuote != "_")
			general_setSpanValue(document.getElementById("say_mode_text_999999"), "quoted");
		else
			general_setSpanValue(document.getElementById("say_mode_text_999999"), "stated");
		document.getElementById("say_mode_text_999999").id = "";
		temp_say_mode_text.id = "say_mode_text_999999";


		/////////////////////////////////////////////////////

		temp_tag1_name.id = "xxxxx";
		if (tag1_name != "")
			general_setSpanValue(document.getElementById("tag1_name_999999"), tag1_name);
		document.getElementById("tag1_name_999999").id = "";
		temp_tag1_name.id = "tag1_name_999999";

		temp_tag1_link.id = "xxxxx";
		if (tag1_name != "") {
			var new_tag1_url = base_url + "/tag.jsp?type=" + tag1_type + "&val=" + tag1_val;
			document.getElementById("tag1_link_999999").removeAttribute("href");
			document.getElementById("tag1_link_999999").setAttribute("href", new_tag1_url);
		}
		document.getElementById("tag1_link_999999").id = "";
		temp_tag1_link.id = "tag1_link_999999";

		temp_tag1_img.id = "xxxxx";
		if (tag1_name != "") {
			var t_i = (tag1_impact * 1) - 5;
			if (tag1_impact <= 5)
				document.getElementById("tag1_img_999999").src = "main/img/impacts/bullish_" + tag1_impact + ".gif";
			else
				document.getElementById("tag1_img_999999").src = "main/img/impacts/bearish_" + t_i + ".gif";
		}
		document.getElementById("tag1_img_999999").id = "";
		temp_tag1_img.id = "tag1_img_999999";

		///////////////////////////////////////

		temp_tag2_name.id = "xxxxx";
		if (tag2_name != "") {
			document.getElementById("tag2_name_999999").style.display = "inline";
			general_setSpanValue(document.getElementById("tag2_name_999999"), tag2_name);
		}
		else
			document.getElementById("tag2_name_999999").style.display = "none";
		document.getElementById("tag2_name_999999").id = "";
		temp_tag2_name.id = "tag2_name_999999";

		temp_tag2_link.id = "xxxxx";
		if (tag2_name != "") {
			var new_tag2_url = base_url + "/tag.jsp?type=" + tag2_type + "&val=" + tag2_val;
			document.getElementById("tag2_link_999999").removeAttribute("href");
			document.getElementById("tag2_link_999999").setAttribute("href", new_tag2_url);
		}
		document.getElementById("tag2_link_999999").id = "";
		temp_tag2_link.id = "tag2_link_999999";

		temp_tag2_img.id = "xxxxx";
		if (tag2_name != "") {
			document.getElementById("tag2_img_999999").style.display = "inline";
			var t_i = (tag2_impact * 1) - 5;
			if (tag2_impact <= 5)
				document.getElementById("tag2_img_999999").src = "main/img/impacts/bullish_" + tag2_impact + ".gif";
			else
				document.getElementById("tag2_img_999999").src = "main/img/impacts/bearish_" + t_i + ".gif";
		}
		else
			document.getElementById("tag2_img_999999").style.display = "none";
		document.getElementById("tag2_img_999999").id = "";
		temp_tag2_img.id = "tag2_img_999999";

		///////////////////////////////////////

		temp_tag3_name.id = "xxxxx";
		if (tag3_name != "") {
			document.getElementById("tag3_name_999999").style.display = "inline";
			general_setSpanValue(document.getElementById("tag3_name_999999"), tag3_name);
		}
		else
			document.getElementById("tag3_name_999999").style.display = "none";

		document.getElementById("tag3_name_999999").id = "";
		temp_tag3_name.id = "tag3_name_999999";

		temp_tag3_link.id = "xxxxx";
		if (tag3_name != "") {
			var new_tag3_url = base_url + "/tag.jsp?type=" + tag3_type + "&val=" + tag3_val;
			document.getElementById("tag3_link_999999").removeAttribute("href");
			document.getElementById("tag3_link_999999").setAttribute("href", new_tag3_url);
		}
		document.getElementById("tag3_link_999999").id = "";
		temp_tag3_link.id = "tag3_link_999999";

		temp_tag3_img.id = "xxxxx";
		if (tag3_name != "") {
			document.getElementById("tag3_img_999999").style.display = "inline";
			var t_i = (tag3_impact * 1) - 5;
			if (tag3_impact <= 5)
				document.getElementById("tag3_img_999999").src = "main/img/impacts/bullish_" + tag3_impact + ".gif";
			else
				document.getElementById("tag3_img_999999").src = "main/img/impacts/bearish_" + t_i + ".gif";
		}
		else
			document.getElementById("tag3_img_999999").style.display = "none";

		document.getElementById("tag3_img_999999").id = "";
		temp_tag3_img.id = "tag3_img_999999";

		///////////////////////////////////

		temp_creation_date.id = "xxxxx";
		general_setSpanValue(document.getElementById("creation_date_999999"), posting_time);
		document.getElementById("creation_date_999999").id = "";
		temp_creation_date.id = "creation_date_999999";
	}
	dynamic_list_set_height();
}

///////////////////////////////////////////////////////////////////////////////////////////

function dynamic_list_set_height() {

	var obj = parent.window.document.getElementById("dynamic_list_frame");
	if (obj != null) {
		var current_height = parseFloat(obj.height);
		var new_height = current_height + 1000;
		obj.height = new_height + "px";
	}

}

///////////////////////////////////////////////////////////////////////////////////////////

