if (typeof( HTMLString ) !== "undefined") {
	console.log("HTMLString ok");
} else {
	console.log("HTMLString must be included");
}

if (typeof( NEOPETS ) !== "undefined") {
	console.log("NEOPETS ok");
} else {
	console.log("NEOPETS must be included");
}

function prependChild(mom,new_kid) {
	var kids = mom.childNodes;
	var tmp_arr = new Array();
	tmp_arr[0] = new_kid;
	for (var i=1; i<kids.length; i++) {
		tmp_arr[i] = mom.childNodes[i];
		mom.removeChild(kids[i]);
	}
	for (var i=0; i<kids.length; i++) {
		try {
			mom.appendChild(tmp_arr[i]);
		} catch (e) {}
	}
}

function get_items_json(content) {
	// Import incoming data into registries.
	if (content.object_data != null) {
		for (var oii in content.object_data) desc_arr[oii] = content.object_data[oii];
	}
	if (content.lim_data != null) {
		for (var oii in content.lim_data) lim_arr[oii] = content.lim_data[oii];
	}
	if (content.html != null) {
		set_main_div(content);
	}
	// Update display to show which items in the listing are already being previewed.
	for (var i = 0; i < preview_id_arr.length; i++) {
		var cur_oii = preview_id_arr[i];
		var try_links = dojo.query('.try_link_' + cur_oii);
		try_links.forEach(
			function(try_link) {
				try_link.oii = cur_oii;
				try_link.onclick = function() { remove_from_preview(this.oii); }
				try_link.innerHTML = NCMallClientTrans.getTranslation(213);
				try_link.style.color = 'red';
			}
		);
	}
	for (var i = 0; i < showroom_id_arr[cur_view].length; i++) {
		var cur_oii = showroom_id_arr[cur_view][i];
		var obj_data = desc_arr[cur_oii];
		var try_links = dojo.query('.try_link_' + cur_oii);
		try_links.forEach(
			function(try_link) {
				if (obj_data['wall'] == 'true' || obj_data['floor'] == 'true' || obj_data['door'] == 'true' || obj_data['window'] == 'true' || obj_data['isSuperpack']) {
					try_link.oii = cur_oii;
					try_link.onclick = function() { clearNonFurnitureItem(this.oii); }
					try_link.innerHTML = NCMallClientTrans.getTranslation(661);
					try_link.style.color = 'red';
				}
			}
		);
	}
}

function show_item_rollover(item) {
	// Remove any previously opened item rollovers first.
	try {
		remove_rollover('item_roll');
	}
	catch(e) {}
	// The view is different for superpack and keyquest vs. normal item rollover.
	if (desc_arr[item]['isSuperpack'] || desc_arr[item]['isKeyquest']) {
		var rollover_width = 515;
		var rollover_height = 550;
	}
	else {
		var rollover_width = 375;
		var rollover_height = 485;
	}
	make_rollover('item_roll', make_item_rollover(item), NCMallClientTrans.getTranslation(514), rollover_width, rollover_height);
}

function make_item_rollover(item) {
	var item_info = desc_arr[item];
	var is_wearable = item_info['isWearable'];
	var is_neohome = item_info['isNeohome'];
	var is_superpack = item_info['isSuperpack'];
	var is_keyquest = item_info['isKeyquest'];
	var is_previewable = (is_wearable || is_neohome || is_superpack);
	var is_limited_time = (item_info['saleEnd'] > 0);
	var is_discounted = (item_info['discountPrice'] > 0);
	var is_collectible = (item_info['isCollectible'] > 0);
	var is_random = item_info['isInRandomWindow'];
	var is_promo = (item_info['ck'] != undefined);

	var item_img = img_url + item_info['imageFile'] + ".gif";
	var item_name = lang_fix_hyphenation(item_info['name']);
	var item_desc = item_info['description'];
	var item_price = item_info['price'];

	var info = new Array;
	var controls = new Array;
	var actions = new Array;

	// Display for promo items.
	if (is_promo) {
		var ck = item_info['ck'];
		actions[0] = "<a href='javascript:;' onclick=\"promo_redeem(" + item + ", '" + ck + "')\" style='color: green'><b>" + NCMallClientTrans.getTranslation(770) + "</b></a>";
	}
	else {
		var item_price_display = format_currency(item_price);
		// Check if a discount is in effect.
		if (is_discounted) {
			var item_discount_price = item_info['discountPrice'];
			item_price_display = "<del style='color: red'><span style='color: black'>" + item_price_display + "</span></del>&nbsp;" + format_currency(item_discount_price);
		}
		var item_uses = item_info['uses']? item_info['uses'] : NCMallClientTrans.getTranslation(199);
		//var item_duration = item_info['duration']? item_info['duration'] : NCMallClientTrans.getTranslation(200);

		// Build view components.
		info[0] = "<b><span style='color: black'>" + NCMallClientTrans.getTranslation(206) + "</span> " +
		          "<span style='color: red'>" + item_price_display + "&nbsp;" + NCMallClientTrans.getTranslation(163) + "</span></b>";
		info[1] = "<b><span style='color: black'>" + NCMallClientTrans.getTranslation(207) + "</span> " +
		          "<span style='color: red'>" + item_uses + "</span>";

		if (is_random) {
			controls[0] = "<b onclick='alert(\"" + NCMallClientTrans.getTranslation(204) + "\");' style='color: lightgrey'>" + NCMallClientTrans.getTranslation(166) + "</b>";
		}	else {
			controls[0] = "<a href='javascript:;' onclick='wish_list(" + item + ", \"add\", true, false)'><b>" + NCMallClientTrans.getTranslation(166) + "</b></a>";
		}
		// Tell a friend
		controls[1] = "<a href='javascript:;' onclick='send_link(" + item + ")'><b>" + NCMallClientTrans.getTranslation(205) + "</b></a>";

		//actions[0] = "<a href='javascript:;' onclick='update_cart(" + item + ", \"add\")' style='color: green'><b>" + NCMallClientTrans.getTranslation(198) + "</b></a>|||";
		actions[0] = HTMLUtilities.mouseOver("ncmall/popup/buttons/add-to-cart"+NEOPETS.langimg+".jpg","ncmall/popup/buttons/add-to-cart_ov"+NEOPETS.langimg+".jpg",{'border':0,'height':35,'width':175,'alt':NCMallClientTrans.getTranslation(198)},'javascript:;',"update_cart(" + item + ", 'add')");
		
		if (is_previewable) {
			// Make a reference to which preview array is active.
			var viewer_preview_arr = is_neohome? showroom_id_arr[cur_view] : preview_id_arr;
			var is_previewed = (viewer_preview_arr.length == 0)? false : in_array(item, viewer_preview_arr);

			// Preview link settings.
			if (is_neohome && is_previewed) {
				var preview_link_action = 'clearNonFurnitureItem';
				var preview_link_color = 'red';
				var preview_link_text = NCMallClientTrans.getTranslation(661);
				//actions[1] = "<a class='try_link_" + item + "' href='javascript:;' onclick='" + preview_link_action + "(" + item + ")' style='color: " + preview_link_color + "; font-weight: bold'>" + preview_link_text + "</a>";
				actions[1] = HTMLUtilities.mouseOver("ncmall/popup/buttons/take-off"+NEOPETS.langimg+".jpg","ncmall/popup/buttons/take-off_ov"+NEOPETS.langimg+".jpg",{'border':0,'height':35,'width':175, 'alt':preview_link_text},'javascript:;', preview_link_action + "(" + item + ")", try_link_class);
			}
			else if (!is_neohome && is_previewed) {
				var preview_link_action = 'remove_from_preview';
				var preview_link_color = 'red';
				var preview_link_text = NCMallClientTrans.getTranslation(213);
				var try_link_class = 'try_link_' + item;
				actions[1] = HTMLUtilities.mouseOver("ncmall/popup/buttons/take-off"+NEOPETS.langimg+".jpg","ncmall/popup/buttons/take-off_ov"+NEOPETS.langimg+".jpg",{'border':0,'height':35,'width':175, 'alt':preview_link_text},'javascript:;', preview_link_action + "(" + item + ")", try_link_class);
			}
			else if (is_neohome && !is_previewed) {
				var preview_link_action = 'showroom_click';
				var preview_link_color = 'green';
				var preview_link_text = NCMallClientTrans.getTranslation(675);
				//actions[1] = "<a class='try_link_" + item + "' href='javascript:;' onclick='" + preview_link_action + "(" + item + ")' style='color: " + preview_link_color + "; font-weight: bold'>" + preview_link_text + "</a>";
				actions[1] = HTMLUtilities.mouseOver("ncmall/popup/buttons/try-on"+NEOPETS.langimg+".jpg","ncmall/popup/buttons/try-on_ov"+NEOPETS.langimg+".jpg",{'border':0,'height':35,'width':175, 'alt':preview_link_text},'javascript:;', preview_link_action + "(" + item + ")", try_link_class);
			}
			else if (!is_neohome && !is_previewed) {
				var preview_link_action = 'preview_click';
				var preview_link_color = 'green';
				var preview_link_text = NCMallClientTrans.getTranslation(197);
				var try_link_class = 'try_link_' + item;
				actions[1] = HTMLUtilities.mouseOver("ncmall/popup/buttons/try-on"+NEOPETS.langimg+".jpg","ncmall/popup/buttons/try-on_ov"+NEOPETS.langimg+".jpg",{'border':0,'height':35,'width':175, 'alt':preview_link_text},'javascript:;', preview_link_action + "(" + item + ")", try_link_class);
			}

		}
	}

	// Begin building view output.
	var out = "<table cellpadding='0' cellspacing='0' border='0' style='width: auto'>";

		// Basic item info display.
		if (is_superpack) {
			var cols = 3; // Number of columns in this view.
			out += "<tr>";
				out += "<td width='80' valign='top'><img src='" + item_img + "' class='display_item' width='80' height='80'></td>";
				out += "<td width='310' align='center' valign='top'><div class='item_name_txt'>" + item_name + "</div><div class='item_desc_txt'>" + item_desc + "</div></td>";
				out += "<td width='125' align='center' valign='top'>";
					out += "<div style='white-space: nowrap'>" + info[0] + "</div>";
					//out += "<div style='white-space: nowrap'>" + info[2] + "</div>";
					for (var i in controls) out += "<div style='white-space: nowrap'>" + controls[i] + "</div>";
				out += "</td>";
			out += "</tr>";
			out += "<tr>";
				out += "<td colspan='3' align='center'><div align='center'>" + NCMallClientTrans.getTranslation(307) + "</div>";
				for (var i in item_info['packContents']) {
					var child_item = item_info['packContents'][i];
					var child_item_info = desc_arr[child_item];
					var child_item_img = img_url + child_item_info['imageFile'] + ".gif";
					var child_item_title = child_item_info['name'];

					out += "<img src='" + child_item_img + "' class='display_item' width='80' height='80' alt='" + child_item_title + "' title='" + child_item_title + "'> ";
				}
				out += "</td>";
			out += "</tr>";
		} else if (is_keyquest) {
			var cols = 3; // Number of columns in this view.
			out += "<tr>";
				out += "<td width='80' valign='top'><img src='" + item_img + "' class='display_item' width='80' height='80'></td>";
				out += "<td width='310' align='center' valign='top'><div class='item_name_txt'>" + item_name + "</div><div class='item_desc_txt'>" + item_desc + "</div></td>";
				out += "<td width='125' align='center' valign='top'>";
					out += "<div style='white-space: nowrap'>" + info[0] + "</div>";
					//out += "<div style='white-space: nowrap'>" + info[2] + "</div>";
					for (var i in controls) out += "<div style='white-space: nowrap'>" + controls[i] + "</div>";
				out += "</td>";
			out += "</tr>";
			out += "<tr>";
				out += "<td colspan='3' align='center'><div align='center'>" + NCMallClientTrans.getTranslation(927) + ":</div>";
				if(item_info['bonus']) {
					console.log(item_info['bonus']);
					var child_item_info = item_info['bonus'];
					var child_item_img = img_url + child_item_info['image'] + ".gif";
					var child_item_title = child_item_info['name'];

					out += "<img src='" + child_item_img + "' class='display_item' width='80' height='80' alt='" + child_item_title + "' title='" + child_item_title + "'> ";
					out += "<br/><b>" + child_item_title + "</b>";
				}
				out += "</td>";
			out += "</tr>";
        } else {
			var cols = 2; // Number of columns in this view.
			out += "<tr>";
				out += "<td width='110' valign='top'><img src='" + item_img + "' class='display_item' width='80' height='80'></td>";
				out += "<td width='375' align='center' valign='top'>";
					out += "<div class='item_name_txt'>" + item_name + "</div><div class='item_desc_txt'>" + item_desc + "</div>";
				out += "</td>";
			out += "</tr>";
			out += "<tr>";
				out += "<td align='center' valign='top' style='padding-top: 5px'>";
					for (var i in controls) out += "<div style='white-space: nowrap'>" + controls[i] + "</div>";
				out += "</td>";
				out += "<td align='left' valign='top' style='padding: 5px 0 0 5px'>";
					for (var i in info) out += "<div style='white-space: nowrap'>" + info[i] + "</div>";
				out += "</td>";
			out += "</tr>";
		}

		// Promo items have an extra little note.
		if (is_promo) {
			out += "<tr>";
				out += "<td colspan='" + cols + "' align='left' style='padding-top: 5px'><b>";
					out += NCMallClientTrans.getTranslation(873).replace('%itemName', item_name);
				out += "</b></td>";
			out += "</tr>";
		}

		var limited_display = '';
		var blank = "<img src='" + img_host + "/transparent_spacer.gif' width='12' height='10' style='margin-right: 3px'>";
		if (is_collectible) {
			var star = "<img src='" + img_host + "/ncmall/star_blue.gif' width='12' height='10' style='margin-right: 3px'>";
			var tmpStr = NCMallClientTrans.getTranslation(212);
			var tmpDate = new Date(item_info['saleEnd'] * 1000);
			var tmpDateDisp = (tmpDate.getMonth() + 1) + '/' + tmpDate.getDate();
			var limited_display = star + "<b>" + NCMallClientTrans.getTranslation(908) + "</b><br>" + blank + tmpStr.replace('%endSale', tmpDateDisp);
		}
		else if (is_discounted) {
			var star = "<img src='" + img_host + "/ncmall/star_red.gif' width='12' height='10' style='margin-right: 3px'>";
			var tmpStr = NCMallClientTrans.getTranslation(910);
			var tmpDate = new Date(item_info['discountEnd'] * 1000);
			var tmpDateDisp = (tmpDate.getMonth() + 1) + '/' + tmpDate.getDate();
			var limited_display = star + "<b>" + NCMallClientTrans.getTranslation(909) + "</b><br>" + blank + tmpStr.replace('%endDiscount', tmpDateDisp);
		}
		else if (is_limited_time) {
			var star = "<img src='" + img_host + "/ncmall/leso_star.gif' width='12' height='10' style='margin-right: 3px;'>";
			var tmpStr = NCMallClientTrans.getTranslation(212);
			var tmpDate = new Date(item_info['saleEnd'] * 1000);
			var tmpDateDisp = (tmpDate.getMonth() + 1) + '/' + tmpDate.getDate();
			var limited_display = star + "<b>" + NCMallClientTrans.getTranslation(211) + "</b><br>" + blank + tmpStr.replace('%endSale', tmpDateDisp);
		}

		if (limited_display.length > 0) {
			out += "<tr>";
				out += "<td colspan='" + cols + "' align='left' style='padding-top: 5px'>" + limited_display + "</td>";
			out += "</tr>";
		}

		// Action links (i.e. try on, add to cart)
		out += "<tr>";
			out += "<td colspan='" + cols + "' align='center' style='padding-top: 5px;'>";
				out += actions.join('');
			out += "</td>";
		out += "</tr>";
	out += "</table>";

	return out;
}

function show_item_rollover_delayed(obj_info_id, delay) {
	if (delay == undefined) var delay = 1000;
	if (obj_info_id != pop_obj_id) {
		pop_obj_id = obj_info_id;
		pop_id = setTimeout("show_item_rollover(pop_obj_id)", delay);
	}
}

function remove_rollover(id) {
	if( id == 0 ){
		// remove all
		for( var i in rollovers_arr ){
			container = document.getElementById(rollovers_arr[i]);
			try{
				if (container.id != 'ext_prev_div') container.parentNode.removeChild(container);
			}catch(e){}
		}
		return;
	}
	container = document.getElementById(id);
	try{
		container.parentNode.removeChild(container);
	}catch(e){}
}

function make_rollover(id,body,title,width,height,left,top,b_drag){
	rollovers_arr[id] = id;
	var ro_divs = new Array();

	if( browser.isIE ){
		winW = document.body.offsetWidth;
		winH = document.body.offsetHeight;
	}else{
		winW = window.innerWidth;
		winH = window.innerHeight;
	}
	if( width == null ) width = 350;
	if( height == null ) height = 200;
	if( left == null ) left = parseInt(winW/2) - parseInt(width/2);
	if( top == null ) top = parseInt(winH/2) - parseInt(height/2);
	if( title == null ) title = NCMallClientTrans.getTranslation(216);
	if( b_drag == null ) b_drag = true;
	if( ro_z_index == null ) ro_z_index = 11000; // Should already be set in globals.

	if (browser.isIE || browser.isOpera) {
		scrollx = document.body.scrollLeft;
		scrolly = document.body.scrollTop;
	}
	else  {
		scrollx = window.scrollX;
		scrolly = window.scrollY;
	}
	foot = "<input type='button' value='" + NCMallClientTrans.getTranslation(497) + "' onclick='remove_rollover(\"" + id + "\")'>";
	left += scrollx;
	top += scrolly;
	container = document.createElement("div");
	container.id = id;
	if( b_drag ) container.onmousedown = function(event){( window.event ) ? dragStart(window.event,id) : dragStart(event,id)};
	body_div = document.createElement("div");
	html_div = document.createElement("div");
	foot_div = document.createElement("div");
	html_div.id = id + '_html';
	body_div.id = id + '_body';
	shadow_div = document.createElement("div");
	shadow_div.id = id+'_shadow';
	header_div = document.createElement("div");
	iframe_hack = document.createElement("iframe");
	iframe_hack.id = 'iframe_hack';
	header_div.className = "sidebarHeader";
	header_div.innerHTML = "<table id='header_table' style=\"width:"+ width +"px;\"><tr><td style='width:100%;'>"+title+"</td><td style=\"text-align:right;\"><a href='javascript:;' onclick=\"remove_rollover('"+id+"')\">X</a></td><td>&nbsp;</td></tr></table>";
	shadow_div.innerHTML = "<img id='shadow_img' src='" + IMAGE_HOST + "/ncmall/shadow.png'>";
	html_div.style.position = 'relative';
	header_div.style.position = 'relative';
	body_div.style.position = 'absolute';
	shadow_div.style.position = 'absolute';
	html_div.innerHTML = (typeof(body) == 'string') ? body : body.innerHTML;
	foot_div.innerHTML = foot;
	html_div.style.margin = '10px';
	body_div.style.width = width + 'px';
	body_div.style.height = height + 'px';
	body_div.style.height = 'auto';
	body_div.style.zIndex = ro_z_index;
	body_div.style.left = '0px';
	body_div.style.top = '0px';
	body_div.style.border = '2px solid lightgrey';
	body_div.style.backgroundColor = 'white';
	container.style.position = 'absolute';
	container.style.left = left + 'px';
	container.style.top = top + 'px';
	container.style.zIndex = ro_z_index;
	container.appendChild(shadow_div);
	foot_div.style.marginBottom = '10px';
	body_div.appendChild(header_div);
	var html_element = body_div.appendChild(html_div);
	body_div.appendChild(foot_div);
	container.appendChild(body_div);
	document.body.appendChild(container);
	var body_element = document.getElementById(id + '_body');

	if (browser.isIE) {
		iframe_hack.style.height = body_element.offsetHeight;
		iframe_hack.style.width = body_element.offsetWidth;
		container.appendChild(iframe_hack);
	}
	var shadow_img = document.getElementById('shadow_img');
	var shadow_width = parseInt(width * 1.03);
	var shadow_height = body_element.offsetHeight;
	var shadow_left = parseInt(width*.05);
	var shadow_top = parseInt(height*.05);
	shadow_div.style.left = shadow_left + 'px';
	shadow_div.style.top = shadow_top + 'px';
	shadow_div.style.zIndex = ro_z_index - 1;
	shadow_img.style.width = shadow_width + 'px';
	shadow_img.style.height = shadow_height + 'px';

	// IE displays div with higher z-index below div with lower z-index without this
	// 2009-05-11 / ricejo / Apparently any subsequent rollover that can be fired from inside the item detail box needs this hack.
	// So if your rollover is appearing underneath it then add the id to this list. [BES-65]
	if (id == 'tell_a_friend' || id == 'find_pin' || id == 'cart_add' || id == 'wish_add') {
		try {
			document.getElementById('item_roll').style.zIndex = document.getElementById(id).style.zIndex - 1;
		} catch(e) {}
	}
}


function send_link(item_id){
	if( !user_id ){
		make_rollover("alert", NCMallClientTrans.getTranslation(217), NCMallClientTrans.getTranslation(216), 200, 100);
		return;
	}
	item_arr = desc_arr[item_id];
	body = NCMallClientTrans.getTranslation(693, {'name': '<b>'+item_arr['name']+'</b>'}) + "<br><br>";
	body += "<input type='hidden' id='item_id' value='" + item_id + "'>";
	body += NCMallClientTrans.getTranslation(218) + " <input type='text' id='taf_neofriend'><br><br><input type='button' value='" + NCMallClientTrans.getTranslation(694) + "' onclick='var nf=document.getElementById(\"taf_neofriend\").value; var item=document.getElementById(\"item_id\").value; json_request(\"ajax/tell_a_friend.phtml\", { item: item, neofriend: nf }, \"tell_a_friend\");'>";
	make_rollover("tell_a_friend", body, NCMallClientTrans.getTranslation(205), 250, 150);
}

function tell_a_friend(response){
	remove_rollover("tell_a_friend");
	if (response['error']) make_rollover('Alert', response['error_msg'], NCMallClientTrans.getTranslation(216), 200, 100);
	else make_rollover("friend_told", response['html'], NCMallClientTrans.getTranslation(205), 200, 150);
}


function lang_fix_hyphenation(str) {
	if (lang == 'de') { 
		return str.replace(/-/g, "<span style='font-size: 2px;'>&nbsp;</span>-<span style='font-size: 2px;'> </span>");
	} else return str;
}

function find_pin_popup() {
	make_rollover("find_pin", "<img src='http://images.neopets.com/ncmall/sample_backcard_anim.gif'>", NCMallClientTrans.getTranslation(727), 425, 400);
}
