var switch_var_element;
var switch_var_timeout;

function switch_menu_over(obj, fix_last, is_img_switch) {
	if (switch_var_element) {
		switch_menu_out(switch_var_element[0], switch_var_element[1], true);
	}
	if (obj) {
		switch_var_element = Array(obj, is_img_switch);
		obj.className = 'item-active';
		if (is_img_switch) {
			var img_obj = switch_get_img_obj(obj);
			if (img_obj) {
				img_obj.src = switch_get_img_src(img_obj.src);
			}
		}
		if (fix_last) {
			switch_fix_last_element(obj.parentNode);
		}
	}
}

function switch_menu_out(obj, is_img_switch, is_now, timeout) {
	clearTimeout(switch_var_timeout);
	if (!is_now) {
		switch_var_timeout = setTimeout(function() { switch_menu_out(obj, is_img_switch, true); }, (timeout) ? timeout : 700);
	} else {
		if (obj) {
			obj.className = 'item';
			if (is_img_switch) {
				var img_obj = switch_get_img_obj(obj);
				if (img_obj) {
					img_obj.src = switch_get_img_src(img_obj.src);
				}
			}
		}
		switch_var_element = false;
	}
}

function switch_get_img_obj(parent_obj) {
	for(var i = 0; i < parent_obj.childNodes.length; i++) {
		if (parent_obj.childNodes[i].nodeName == 'IMG') {
			return parent_obj.childNodes[i];
		} else {
			var cur_obj = switch_get_img_obj(parent_obj.childNodes[i]);
			if (cur_obj) {
				return cur_obj;
			}
		}
	}
	return false;
}

function switch_get_img_src(cur_src) {
	var src_ = cur_src.split('.');
	var ext = src_.pop();
	var path_and_name = src_.join('.');
	if (path_and_name.charAt(path_and_name.length - 1) == '_') {
		path_and_name = path_and_name.substr(0, path_and_name.length - 1);
	} else {
		path_and_name += '_';
	}
	return path_and_name + '.' + ext;
}

function switch_fix_last_element(parent_node) {
	var last_item = false;
	for(var i = parent_node.childNodes.length - 1; i >= 0; i--) {
		if (parent_node.childNodes[i].nodeName == 'DIV') {
			last_item = parent_node.childNodes[i];
			break;
		}
	}
	if (last_item) {
		for(var i = 0; i < last_item.childNodes.length; i++) {
			if (last_item.childNodes[i].nodeName == 'DIV' && last_item.childNodes[i].className == 'pop') {
				for(var ii = 0; ii < last_item.childNodes[i].childNodes.length; ii++) {
					if (last_item.childNodes[i].childNodes[ii].nodeName == 'DIV') {
						last_item.childNodes[i].childNodes[ii].style.left = (last_item.clientWidth - last_item.childNodes[i].clientWidth) + 'px';
						return true;
					}
				}
			}
		}
	}
	return false;
}

function get_nodes_by_node_name(node, name) {
	var output = new Array();
	for(var i = 0; i < node.childNodes.length; i++) {
		if (node.childNodes[i].nodeName == name) {
			output[output.length] = node.childNodes[i];
		}
	}
	return output;
}
