function menu_toggle(id) {
   var li  = document.getElementById('li_'+id);
   li.className = "a_expanded";

   var li_sub  = document.getElementById('li_'+id+'_sub');
   li_sub.className = "sub_expanded";
}

function top_tab_toggle(id_show, id_hide) {
  var div_show = document.getElementById(id_show);
  div_show.className = div_show.className.replace(" active-tab", "")+" active-tab";

  var div_hide = document.getElementById(id_hide);
  div_hide.className = div_hide.className.replace(" active-tab", "");

  var ul_show = document.getElementById(id_show.replace("div_", "ul_"));
  ul_show.style.display = '';

  var ul_hide = document.getElementById(id_hide.replace("div_", "ul_"));
  ul_hide.style.display = 'none';
}

var slide_more_interval = null;
var slide_less_interval = null;

function slide_move(direction, number) {
  var slider = document.getElementById('table_slider');
  var slider_left = parseInt(slider.style.left);
  var slider_width = parseInt(slider.offsetWidth);
  if (slider_width > 503) {
    switch (direction) {
      case "left":
        if (slider_left >= 0) {
          slider.style.left = "0px";
          if (slide_more_interval) window.clearTimeout(slide_more_interval);
          if (slide_less_interval) window.clearTimeout(slide_less_interval);
        } else {
          slider.style.left = (slider_left + 10) + "px";
          slide_more_interval = null;
          slide_less_interval = window.setTimeout("slide_move('left')", 30);
        }
        break;
      case "right":
        var magic = 503 - slider_width;
        if (slider_left <= magic) {
          slider.style.left = magic + "px";
          if (slide_more_interval) clearTimeout(slide_more_interval);
          if (slide_less_interval) clearTimeout(slide_less_interval);
        } else {
          slider.style.left = (slider_left - 10) + "px";
          slide_more_interval = window.setTimeout("slide_move('right')", 30);
          slide_less_interval = null;
        }
        break;
      case "stop":
        if (slide_more_interval) window.clearTimeout(slide_more_interval);
        if (slide_less_interval) window.clearTimeout(slide_less_interval);
        break;
    }
  }
}

var featured_pictures = new Array();

function featured_add(src) {
  var img = new Image();
  img.src = src;
  featured_pictures[featured_pictures.length] = img;
}
function featured_init() {
  var span_index = document.getElementById('span_featured_count');
  span_index.innerHTML = featured_pictures.length;

  var div_featured_pager = document.getElementById('div_featured_pager');
  if (featured_pictures.length > 0) {
    div_featured_pager.style.display = 'none';
  }

  featured_set(0);
}
function featured_set(index) {
  var a = document.getElementById('a_featured_photo');
  a.innerHTML = '<img src="'+featured_pictures[index].src+'" />';

  var span_index = document.getElementById('span_featured_index');
  span_index.innerHTML = index + 1;

  var a_prev = document.getElementById('a_featured_prev');
  if (index <= 0) {
    a_prev.href = "javascript: void(0);";
    a_prev.innerHTML = '<img src="images/ar_wh_l.gif" />';
  } else {
    a_prev.href = "javascript: featured_set("+(index-1)+");";
    a_prev.innerHTML = '<img src="images/ar_bl_l.gif" />';
  }
  var a_next = document.getElementById('a_featured_next');
  if (index >= featured_pictures.length - 1) {
    a_next.href = "javascript: void(0);";
    a_next.innerHTML = '<img src="images/ar_wh_r.gif" />';
  } else {
    a_next.href = "javascript: featured_set("+(index+1)+");";
    a_next.innerHTML = '<img src="images/ar_bl_r.gif" />';
  }
}

var rating_wrap = null;

function rating_init() {
  if (rating_wrap) {
    return true;
  } else {
    rating_wrap =  MauiWeekly.Controls.RatingControl;
    if (rating_wrap) {
      return true;
    } else {
      alert("rating_init(): rating UI not avaiable");
    }
  }
}
function rating_over(number) {
  var div_rating = document.getElementById('div_rating');
  div_rating.className += " ico_"+number;
}
function rating_out() {
  var div_rating = document.getElementById('div_rating');
  div_rating.className = div_rating.className
    .replace(" ico_0", "")
    .replace(" ico_1", "")
    .replace(" ico_2", "")
    .replace(" ico_3", "")
    .replace(" ico_4", "")
    .replace(" ico_5", "")
  ;
}
function rating_vote(story_id, number) {
  if (rating_init()) {
    var result = rating_wrap.AjaxRatingVote(story_id, number);
    if (result.error==null) {
      var parts = result.value.split(';');
      if (parts[0]=="OK") {
        // disable UI
        rating_over = function() { };
        rating_out = function() { };
        rating_vote = function() { };

        // update values
        var div_rating = document.getElementById('div_rating');
        div_rating.className = parts[1];
        var div_rating_votes = document.getElementById('div_rating_votes');
        div_rating_votes.innerHTML = parts[2]+" vote(s)";
        var span_rating_value = document.getElementById('span_rating_value');
        span_rating_value.innerHTML = parts[3];
      } else {
        alert("rating_vote(): failed with " + parts[0] + " response");
      }
    } else {
      alert("rating_vote(): " + result.error.Message);
    }
  }
}

var archive_wrap = null;

function archive_init() {
  if (archive_wrap) {
    return true;
  } else {
    archive_wrap =  MauiWeekly.Controls.ArchiveBoxControl;
    if (archive_wrap) {
      return true;
    } else {
      alert("archive_init(): archive UI not avaiable");
    }
  }
}
function archive_list_categories() {
  if (archive_init()) {
    var select_month = document.getElementById('select_archive_month');
   
    var result = archive_wrap.AjaxGetCategories(select_month.value);
    if (result.error==null) {
      var parts = result.value.split(';');
      if (parts[0]=="OK") {
        var html = '<select id="select_archive_category_id" name="archive_category_id">';
        for (var index = 1; index < parts.length; index++) {
          var pairs = parts[index].split('|');
          html += '  <option value="'+pairs[1]+'"'+(index==1?' selected="1"':'')+'>'+pairs[0]+'</option>';
        }
        html += '</select>';
      
        var div_categories = document.getElementById('div_archive_categories');
        div_categories.innerHTML = html;
      } else if (parts[0]=="NONE") {
        archive_disable_categories();
      } else {
        alert("archive_set_month(): failed with " + parts[0] + " response");
      }
    } else {
      alert("archive_set_month(): " + result.error.Message);
    }
  }
}
function archive_disable_categories() {
  var div_categories = document.getElementById('div_archive_categories');
  div_categories.innerHTML = 
    '<select id="select_archive_category_id" name="archive_category_id" disabled="1">'+
    '  <option>-- SECTION --</option>'+
    '</select>';
}

var etf_from_name = '';
var etf_from_email = '';
var etf_wrap = null;

function etf_init() {
  if (etf_wrap) {
    return true;
  } else {
    etf_wrap =  MauiWeekly.StoryPage;
    if (etf_wrap) {
      return true;
    } else {
      alert("etf_init(): email to friend UI not avaiable");
    }
  }
}
function etf_toggle(value) {
  if (etf_init()) {
    var a_link = document.getElementById('a_etf_link');
    a_link.href = 'javascript: etf_toggle('+(!value)+');';

    var div_form = document.getElementById('div_etf_form');
    div_form.style.display = value ? '' : 'none';
    var div_submit = document.getElementById('div_etf_submit');
    div_submit.style.display = 'none';

    var input_from_name = document.getElementById('input_etf_from_name');
    input_from_name.value = etf_from_name;
    var input_from_email = document.getElementById('input_etf_from_email');
    input_from_email.value = etf_from_email;
    var input_to = document.getElementById('input_etf_to_email');
    input_to.value = "";
    var input_message = document.getElementById('input_etf_message');
    input_message.value = "I think this story could make you interested."

    var button_submit = document.getElementById('button_etf_submit');
    button_submit.disabled = false;
  }
}
function etf_submit(story_id) {
  if (etf_init()) {
    var button_submit = document.getElementById('button_etf_submit');
    button_submit.disabled = true;

    var input_from_name = document.getElementById('input_etf_from_name');
    var input_from_email = document.getElementById('input_etf_from_email');
    var input_to_email = document.getElementById('input_etf_to_email');
    var input_message = document.getElementById('input_etf_message');

    etf_wrap.AjaxEtfSend(
      story_id,
      input_from_name.value, 
      input_from_email.value, 
      input_to_email.value, 
      input_message.value, 
      etf_submit_callback
    );
  }
}
function etf_submit_callback(result) {
  var button_submit = document.getElementById('button_etf_submit');
  button_submit.disabled = false;

  if (result.error==null) {
    if (result.value=="OK") {
      var a_link = document.getElementById('a_etf_link');
      a_link.href = 'javascript: etf_toggle(true);';

      var div_form = document.getElementById('div_etf_form');
      div_form.style.display = 'none';
      var div_submit = document.getElementById('div_etf_submit');
      div_submit.style.display = '';
    } else {
      alert("etf_submit_callback(): failed with " + result.value + " response");
    }
  } else {
    alert("etf_submit_callback(): " + result.error.Message);
  }
}

var voting_wrap = null;
var voting_answer_id = null;

function voting_init() {
  if (voting_wrap) {
    return true;
  } else {
    voting_wrap = MauiWeekly.Controls.VotingControl;
    if (voting_wrap) {
      return true;
    } else {
      alert("voting_init(): voting UI not avaiable");
    }
  }
}
function voting_set(answer_id) {
  voting_answer_id = answer_id;
}
function voting_vote(poll_id) {
  if (voting_init() && (voting_answer_id != null)) {
    var result = voting_wrap.AjaxVote(poll_id, voting_answer_id);
    if (result.error==null) {
      var parts = result.value.split(';;;');
      if (parts[0]=="OK") {
        var div_voting = document.getElementById('div_voting');
        div_voting.innerHTML = '';
        div_voting.className = 'results';
        div_voting.innerHTML = parts[1];
      } else {
        alert("voting_vote(): failed with " + parts[0] + " response");
      }
    } else {
      alert("voting_vote(): " + result.error.Message);
    }
  }
}
function voting_show(poll_id) {
  if (voting_init()) {
    var result = voting_wrap.AjaxShow(poll_id);
    if (result.error==null) {
      var parts = result.value.split(';;;');
      if (parts[0]=="OK") {
        var div_voting = document.getElementById('div_voting');
        div_voting.innerHTML = '';
        div_voting.className = 'results';
        div_voting.innerHTML = parts[1];
      } else {
        alert("voting_vote(): failed with " + parts[0] + " response");
      }
    } else {
      alert("voting_vote(): " + result.error.Message);
    }
  }
}

var admin_wrap = null;

function admin_init() {
  if (admin_wrap) {
    return true;
  } else {
    admin_wrap =  MauiWeekly.Controls.AdminScript;
    if (admin_wrap) {
      return true;
    } else {
      alert("admin_init(): admin UI not avaiable");
    }
  }
}
function admin_comment_delete(comment_id) {
  if (admin_init() && confirm("Are you sure to delete this comment?")) {
    var result = admin_wrap.AjaxCommentDelete(comment_id);
    if (result.error==null) {
      if (result.value=="OK") {
        var div_comment = document.getElementById('div_comment_'+comment_id);
        div_comment.style.display = "none";
      } else {
        alert("admin_comment_delete(): failed with " + result.value + " response");
      }
    } else {
      alert("admin_comment_delete(): " + result.error.Message);
    }
  }
}