$(function() {
	// toggle sidebar
	$('.top .optbtn.wh a').toggle(
	 	function() {
      $('.top .sidebar').slideDown(250);
      $(this).text(SLIDEMENU_HIDE_TEXT);
      $(this).parent().removeClass('off').addClass('on');
    },
		function() {
      $('.top .sidebar').slideUp(250);
      $(this).text(SLIDEMENU_SHOW_TEXT);
      $(this).parent().removeClass('on').addClass('off');
    }
	);
  
  var replaceNode = function(target, newNode) {
    var pattern = '<\/?('+target.nodeName+')';
    var parentNode = $(target).parent();
    parentNode.html(parentNode.html().replace(new RegExp(pattern, 'gi'), function(str, p){return str.replace(p, newNode)}));
  };
  
  var tabSelectedIndex;
  var tabClickEvent = function() {
    $('div.top > div.sidebar > div.tab > ul > li').each(function(i) {
      if (typeof(tabSelectedIndex) === 'undefined') {
        if($(':first-child', this).is('span'))
          tabSelectedIndex = i;
      }
      $('a', this).click(function() {
        var span = $('li > span', $(this).parent().parent());
        replaceNode(span.get(0), 'a');
        replaceNode(this, 'span');
        $('div.top > div.sidebar > div.option > div.tabcontent:eq('+tabSelectedIndex+')').hide();
        tabSelectedIndex = i;
        tabClickEvent();
        return false;
      });
    });
    $('div.top > div.sidebar > div.option > div.tabcontent:eq('+tabSelectedIndex+')').show();
  };
  
  tabClickEvent();
  
  var regionSelectedIndex;
  $('#story_menu_location > div.city > ul > li > a').each(function(i) {
    if (typeof(regionSelectedIndex) === 'undefined') {
      if(this.className == 'on')
        regionSelectedIndex = i;
    }
    $(this).click(function() {
      if (this.className == 'on') return false;
      $('li > a:eq('+regionSelectedIndex+')', $(this).parent().parent()).removeClass('on');
			$(this).addClass('on');
			$('#story_menu_location > div.area > ul:eq('+regionSelectedIndex+')').fadeOut(300,
				function() {
					$('#story_menu_location > div.area > ul:eq('+i+')').fadeIn(300)
			});
			regionSelectedIndex = i;
			return false;
    });
  });
  if (typeof(regionSelectedIndex) === 'undefined')
      regionSelectedIndex = 0;
  $('#story_menu_location > div.area > ul:eq('+regionSelectedIndex+')').show();

});