var buy_amount = 0;
var buy_method = 'paypal';
var response_str = '';
var dragObj = new Object();
//dragObj.zIndex = 1000;

// Simple follow the mouse script
var divName = 'mydiv'; // div that is to follow the mouse
                       // (must be position:absolute)
var offX = 15;          // X offset from mouse position
var offY = 15;          // Y offset from mouse position

function resetXY() {
	document.onmousemove = function() {
		mouseX = mouseX;
		mouseY = mouseY;
	};
}
	
function debug_obj(obj){
	alert_str = '';
	for (p in obj) {
		alert_str += '[x:' + p + ']';
		try {
			alert_str += p + '=' + obj[p] + "\n";
		} catch(e) {}
	}
	return alert_str;
}
	
ddMenu = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("template_nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
					resetXY();
				};
			  	node.onmouseout=function() {
			  		this.className=this.className.replace(" over", "");
						follow_mouse;
			   	};
			}
	  }

		nav_root = document.getElementById("nc_nav");
		for (i=0; i<nav_root.childNodes.length; i++) {
			nav_node = nav_root.childNodes[i];
			if (nav_node.nodeName == "LI" || node.nodeName == "UL") {
				nav_node.onmouseover=function() {
					this.className += " over";
					resetXY();
				}
			  nav_node.onmouseout=function() {
			  	this.className = this.className.replace(" over", "");
			  }
			}
		}
		
		nav_root = document.getElementById("nc_cat_nav");
		for (i=0; i<nav_root.childNodes.length; i++) {
			nav_node = nav_root.childNodes[i];
			if (nav_node.nodeName == "LI" || node.nodeName == "UL") {
				nav_node.onmouseover=function() {
					this.className += " over";
				}
				if (nav_node.getElementsByTagName("li")[0]) {
					nav_node.style.zIndex = "2001";
				}
				nav_node.onmouseout=function() {
					this.className = this.className.replace(" over", "");
				}
			}
		}
	}
}

// swap which previewer is showing
function swap_preview(prev_str, prop_id) {
	if (prop_id == null) prop_id = default_property;
	if (!prev_str) {
		cur_preview = (cur_preview == 'pet')? 'home' : 'pet';
		prev_str = cur_preview;
	}
	else {
		if (cur_preview == prev_str) {
			if (prop_id == cur_prop_id) return false;
			else cur_prop_id = prop_id;
		}
		else cur_preview = prev_str;
	}
	prev_link_left_tag = document.getElementById('prev_links_left');
	switch (prev_str) {
		case "pet":
			HTMLUtilities.tableDisplay()
			dojo.query('#prev_title_home').style('display','none');
			dojo.query('#prev_title_pet').style('display','inline');
			dojo.query('#pet_prev_footer').style('display',HTMLUtilities.tableDisplay());
			dojo.query('#showroom_footer').style('display','none');
			dojo.query('#reset_button').style('display','inline');
			dojo.query('#nc_pet_nav').style('display','inline');
			if (pet_is_old_style) {	dojo.byId('old_style_warning').style.display = 'block'; }
			dojo.query('#iframe_div').style('height','246px');
			dojo.query('#preview_frame')[0].src = 'pet_preview.phtml?id=CustomNeopetView&pet_name=' + cur_pet + '&size=100%';
			dojo.query('#showroom_clear_button').style('display','none');
			dojo.query('#clear_all_nh').style('display','none');

			dojo.query('#pet_prev_clear_button').style('display','inline');
			break;
		case "home":
			dojo.query('#prev_title_pet').style('display','none');
			dojo.query('#prev_title_home').style('display','inline');
			dojo.query('#nc_world_nav').style('display','none');
			dojo.query('#nc_pet_nav').style('display','none');
			dojo.query('#showroom_footer').style('display',HTMLUtilities.tableDisplay());
			dojo.query('#reset_button').style('display','none');
			dojo.query('#clear_all_nh').style('display','inline');
			
			dojo.query('#pet_prev_footer').style('display','none');
			dojo.query('#old_style_warning').style('display','none');
			dojo.query('#iframe_div').style('height','277px');
			var showroom_src = 'showroom.phtml?default_prop=' + default_property + '&id=showroom';
			if (prop_id) { showroom_src += '&prop_id=' + prop_id; }
			dojo.query('#preview_frame')[0].src = showroom_src;
			dojo.query('#showroom_clear_button').style('display','inline');
			dojo.query('#pet_prev_clear_button').style('display','none');
			break;
	}

	return true;
}

// Get info for displaying the items in the specified category.
// We have a function for this so that we don't have to build all this inline, down below.
function load_items_pane(type, cat, search_text) {
	var parms = {
		'type' : type
	}
	if (cat != undefined) { 
		parms.cat = cat;
		
		// make the omni call if it exists
		if(typeof omni_track_ajax == 'function') {
			omni_track_ajax('ajax/load_page.phtml','cat='+cat);
		}
	}
	if (search_text != undefined) {
		parms.text = search_text;
	}
    parms['lang'] = lang;
    if (type == 'free_nc') {
    	json_request('ajax/load_page.phtml', parms, 'get_items_json');
    } else {
    	json_request_get('ajax/load_page.phtml', parms, 'get_items_json');
    }
}

// AJAX call to load the FAQ into the main pane.
function load_faq() {
	json_request('ajax/faq.phtml', {}, 'set_main_div');
}

function show_nav(id){
	if (document.getElementById('nav_' + id) && document.getElementById('caret_' + id)) {
		if (document.getElementById('nav_' + id).style.display == 'none') {
			document.getElementById('nav_' + id).style.display = '';
			document.getElementById('caret_' + id).src = img_host + '/cash_shop/caret_dn.gif';
		}
		else {
			document.getElementById('nav_' + id).style.display = 'none';
			document.getElementById('caret_' + id).src = img_host + '/cash_shop/caret_up.gif';
		}
	}
}

function get_featured(type){
	switch (type) {
		case 'neopet':
			load_items_pane(type, 42);
			break;
		case 'neohome':
			load_items_pane(type, 27);
			break;
		default:
			//json_request('ajax/' + home_page, { id: user_id }, 'get_items_json');
			json_request_get('ajax/' + home_page, { id: user_id, 'lang' : lang }, 'get_items_json');
			break;
	}
}

function get_giveaway() {
	browse_type = 'featured';
	json_request('/games/giveaway/giveaway_game.phtml', { id: user_id }, 'set_main_div');
}

function promo_redeem(item, ck) {
	remove_rollover('item_roll');
	json_request(
		'/mall/ajax/promo_redeem.phtml',
		{ item: item, ck: ck },
		function(response) {
			if (response['error'] == 0) {
				dojo.query('.item_display_' + item).style('visibility', 'hidden');
				make_rollover('promo', response['html'], NCMallClientTrans.getTranslation(699));
			}
			else make_rollover('promo', response['error_msg'], NCMallClientTrans.getTranslation(216));
		}
	);
}

function set_main_div(content) {
	remove_rollover(0); // Remove previous rollovers before refreshing main content.
	if (typeof content['error'] != 'undefined' && content['error'] != 0) {
		make_rollover(content['error'], content['error_msg'], content['error_title'], 200, 100);
		return;
	}
	var div = dojo.byId('main_div');
	if (typeof div != 'undefined') div.innerHTML = content['html'];

	// Things that need to happen after the main_div gets changed...

	// The page that grants the newbie gift can remove the link.
	var free_nc_link = dojo.byId('free_nc_link');
	if (typeof free_nc_link != 'undefined' && typeof content['free_nc_link'] != 'undefined') {
		free_nc_link.style.display = content['free_nc_link'];
	}

	// The elite shop adds an additional footer note that is only displayed there.
	var elite_note = dojo.byId('elite_note');
	if (elite_note) {
		// If a directive was given for elite_note, follow it.
		if (content['elite_note']) {
			elite_note.style.display = content['elite_note'];
		}
		// Otherwise assumed to not show.
		else {
			elite_note.style.display = 'none';
		}
	}
}

function faq_answer(id) {
	faq_el = document.getElementById('q'+id);
	answer_el = document.getElementById('a'+id);
	faq_el.className = (faq_el.className == 'faq_up') ? 'faq_down' : 'faq_up';
	answer_el.style.display = (faq_el.className == 'faq_up') ? 'none' : 'block';
}

function kill_sound() {
	var IFrameObj = document.getElementById('preview_frame');
	if (IFrameObj) {
		IFrameObj.contentWindow.CNV_RemPreviewItem(1, '');
	}
}
var storeLastItem;
var state = {
		back: function() {
			console.log("Back was clicked!", state);
		},
		forward: function() {
			console.log("Forward was clicked!", state);
		},
		backButton: function() { this.back() },
		forwardButton: function() { this.forward() },
		handle: function(actionType) {
			if (actionType == 'back') {
				 this.back();
			} else if (actionType == 'forward') {
				this.forward();
			}
		},
		changeUrl : true
	};

dojo.addOnLoad(function() {
	// history manager
	dojo.back.setInitialState(state);
	var x = 0;
	dojo.query('.cat_links a').forEach(function(item){
		dojo.connect(item, 'onclick', function(e){
			console.log(item.style.color);
			var resetColor = (item.style.color!='#000000') ? item.style.color : null;
			console.log(resetColor);
			if (storeLastItem) { storeLastItem.style.color = resetColor; }
			storeLastItem = item;
			item.style.color = '#000000';
			dojo.stopEvent(e);
		});
	});
	dojo.query('.cat_links a, tr.nav_header td a').forEach(function(item){
		dojo.connect(item, 'onclick', function(e){
			state.changeUrl = encodeURI(item.innerHTML);
			dojo.back.addToHistory(state);
			console.log('History item added', state);
		});
	});
})


