function $(elementId)
{
  return document.getElementById(elementId);
}

function listen(source, eventName, listener)
{
  var safeListener = function(eventArg) {
    try
    {
      listener.apply(this, new Array(eventArg));
    } catch(ex) { }
  };

  if(source.attachEvent)
  {
    source.attachEvent("on" + eventName, safeListener);
  }
  else
  {
    source.addEventListener(eventName, safeListener, false);
  }
}

function listenReady(callback){
  var done = false; // only fire once
  var init = function(){
    if (!done) {
      done = true;
      callback();
    }
  };

  /* for Mozilla */
  if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
  }

  /* for Internet Explorer */
  /*@cc_on @*/
  /*@if (@_win32)
  var ie_ready_script = "ie_ready_script" + Math.random();
  document.write('<scr' + 'ipt id="' + ie_ready_script + '" defer src="javascript:void(0)"><\/scr' + 'ipt>');
  var script = document.getElementById(ie_ready_script);
  script.onreadystatechange = function() {
  if (this.readyState == "complete") {
  init(); // call the onload handler
  }
  };
  @end @*/

  /* for Safari */
  if (/WebKit/i.test(navigator.userAgent)) { // sniff
  (function(){
    if (/loaded|complete/.test(document.readyState)) {
      init(); // call the onload handler
    }
    else
    setTimeout(arguments.callee, 50);
  })();
  }
  /* for other browsers */
  window.onload = init;
}

function getEventSource(eventArg)
{
  return eventArg.srcElement ? eventArg.srcElement : eventArg.target;
}

/* Popup
----------------------------------------*/
var isOverPopup = false;
var isOverAnchor = false;
var closeDelay = 100;
var currentPopup;
var delayTimer;
var g_anchorOutClassName;
var currentAnchor;

function prepareHiddenPopup(){
  if (delayTimer == null) {
    delayTimer = setTimeout(hiddenPopup, closeDelay);
  }
}

function hiddenPopup(){
  delayTimer = null;
  if (!isOverPopup && !isOverAnchor && currentPopup != null) {
    currentPopup.className = "hidden-div";
    if (g_anchorOutClassName != null) {
      currentAnchor.className = g_anchorOutClassName;
    }
    currentPopup = null;
    currentAnchor = null;
  }
}

function setHover(hoverTarget, overClassName, outClassName){
  listen(hoverTarget, "mouseover", function(){
    hoverTarget.className = overClassName;
  });

  listen(hoverTarget, "mouseout", function(){
    hoverTarget.className = outClassName;
  });
}

function setPopupEventHandler(anchorDiv, popupDiv, popupShowClassName, anchorOverClassName, anchorOutClassName){
  popupDiv.className = "hidden-div";
  listen(anchorDiv, "mouseover", function(){
    if (popupDiv.className != popupShowClassName) {
      if (delayTimer != null) {
        clearTimeout(delayTimer);
        delayTimer = null;
        if (g_anchorOutClassName != null) {
          currentAnchor.className = g_anchorOutClassName;
        }
        currentPopup.className = "hidden-div";
      }

      popupDiv.className = popupShowClassName;
      if (anchorOverClassName != undefined) {
        anchorDiv.className = anchorOverClassName;
      }
      if (anchorOutClassName != undefined) {
        g_anchorOutClassName = anchorOutClassName;
      }
      currentPopup = popupDiv;
      currentAnchor = anchorDiv;
    }
    isOverAnchor = true;
  });

  listen(anchorDiv, "mouseout", function(){
    isOverAnchor = false;
    prepareHiddenPopup();
  });

  listen(popupDiv, "mouseover", function(){
    isOverPopup = true;
  });

  listen(popupDiv, "mouseout", function(){
    isOverPopup = false;
    prepareHiddenPopup();
  });
}

/* Move Keyword Line
----------------------------------------*/
var playKeywordsMoveAnimationTimer;
var moveKeywordsTimer;
var keywordLines;
var keywordTotalLines;
var keywordContainer;
var keywordLineTop = 0;
var moveStopTop = -20;
var lineMove = 0;

function initKeywordContainer(){
  keywordContainer = $("keyword-container");
  keywordLines = keywordContainer.getElementsByTagName("div");
  keywordTotalLines = keywordLines.length;

  if(keywordTotalLines > 0)
  {
    listen(keywordContainer, "mouseout", function() {
      if(moveKeywordsTimer == null)
      {
        moveKeywordsTimer = setInterval(moveKeywords, 3000);
      }
    });

    listen(keywordContainer, "mouseover", function() {
      if(moveKeywordsTimer != null)
      {
        clearInterval(moveKeywordsTimer);
        moveKeywordsTimer = null;
      }
    });

    keywordLines[0].style.top = 0;
    moveKeywordsTimer = setInterval(moveKeywords, 3000);
  }
}

function moveKeywords(){
  if (playKeywordsMoveAnimationTimer == null) {
    playKeywordsMoveAnimationTimer = setInterval(playKeywordsMoveAnimation, 30);
  }
}

function playKeywordsMoveAnimation(){
  if (keywordLineTop != moveStopTop) {
    --keywordLineTop;
    keywordLines[lineMove].style.top = keywordLineTop + "px";
  }
  else
  {
    if(moveStopTop == 0)
    {
      moveStopTop = -20;
      clearInterval(playKeywordsMoveAnimationTimer);
      playKeywordsMoveAnimationTimer = null;
    }
    else
    {
      moveStopTop = 0;
      keywordLineTop = 20;
      lineMove = ++lineMove % keywordTotalLines;
    }
  }
}

/* Digg button
----------------------------------------*/
function setDigg()
{
  var digg = document.getElementById('digg');
  if(digg != null) {
    digg.innerHTML = '<iframe src="http://digg.com/tools/diggthis.php?&w=new&s=compact&c=business_finance&t='
    + escape(document.title)
    + '&b=' + escape(document.getElementsByTagName('meta')['description']['content'])
    + '&u=' + window.location.href
    + '" height="18" width="120" frameborder="0" scrolling="no"></iframe>';
  }
}

function setPrintButton()
{
  var print = $("printer");
  if(print != null)
  {
    listen(print, "mouseover", function() {
      print.style.cursor = 'pointer';
    });
    listen(print, "click", function() {
      window.print();
    });
  }
}

/* Show Top Page Selector
----------------------------------------*/
function showTopPageSelector()
{
  $('page-selector-top').style.display = 'block';
}

/* Default load
----------------------------------------*/
listenReady(function(){
  initKeywordContainer();
  setPrintButton();
  setDigg();
});

/* Search submit
----------------------------------------*/
function doSearch()
{
  var input = $('search-form-input');
  var value = input.value;
  if(value == '')
  {
    alert('Please input your query.');
    input.focus();
  }
  else
  {
    value = value.replace(/[^a-zA-Z0-9]/g, '-').replace(/--+/g, '-').replace(/-$/, '').replace(/^-/, '');
    window.location = 'http://q.china-trade-leads.net/' + value;
  }
  return false;
}

/* Google Analyse
----------------------------------------*/
function executeAnalyseRequest()
{
  var pageTracker = _gat._getTracker("UA-3087702-1");
  pageTracker._initData();
  pageTracker._trackPageview();
}

function analyseRequest()
{
  var element = document.createElement("script");
  element.src = "http://www.google-analytics.com/ga.js";
  element.type = 'text/javascript';
  if(window.XMLHttpRequest == undefined)
  {
    element.onreadystatechange = function() {
      var state = this.readyState;
      if (state == "loaded") {
        executeAnalyseRequest();
      }
    };
  } else {
    element.onload = function() {
      executeAnalyseRequest();
    };
  }
  document.getElementsByTagName("head").item(0).appendChild(element);
}
analyseRequest();