var ie = (function(){
    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');
    while (
        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );
    return v > 4 ? v : undef;
}());

function news_initCallback(carousel)
{
  // Disable autoscrolling if the user clicks the prev or next button.
  carousel.buttonNext.bind('click', function() {
    carousel.options.animation = 400;
    carousel.scroll(carousel.first+1);
    carousel.options.animation = 800;
    carousel.startAuto();
  });
  carousel.buttonPrev.bind('click', function() {
    carousel.startAuto(0);
  });
  // Pause autoscrolling if the user moves with the cursor over the clip.
  carousel.clip.hover(function() {
    carousel.stopAuto();
  }, function() {
    carousel.startAuto();
  });
};
function banners_initCallback(carousel)
{
  // Disable autoscrolling if the user clicks the prev or next button.
  carousel.buttonNext.bind('click', function() {
    carousel.options.animation = 400;
    carousel.scroll(carousel.first-1);
    carousel.options.animation = 800;
    carousel.startAuto();
  });
  carousel.buttonPrev.bind('click', function() {
    carousel.startAuto(0);
  });
  // Pause autoscrolling if the user moves with the cursor over the clip.
  carousel.clip.hover(function() {
    carousel.stopAuto();
  }, function() {
    carousel.startAuto();
  });
};
function dispCatch(xml) {
    $(xml).find("item").each(function () {
        target_str = $(this).find('target').text()==''?'':' target="'+$(this).find('target').text()+'"';
        if (ie<8) {
          $("#catch").append('<div class="catch_inner"><p class="catch_corp">' + $(this).find('corp').text() + '</p><p class="catch_text"><a href="' + $(this).find('link').text() + '"' + target_str + '>' + $(this).find('catch').text().replace(/\n/g, "<br />") + '</a></p><p class="catch_sentence"><a href="' + $(this).find('link').text() + '"' + target_str + '>' + $(this).find('sentence').text().replace(/\n/g, "<br />") + '</a></p></div>');
        } else {
          $("#catch").append('<div class="catch_inner"><p class="catch_corp">' + $(this).find('corp').text() + '</p><p class="catch_text">' + $(this).find('catch').text().replace(/\n/g, "<br />") + '</p><p class="catch_sentence">' + $(this).find('sentence').text().replace(/\n/g, "<br />") + '</p><p class="button"><a href="' + $(this).find('link').text() + '"' + target_str + '><img src="images/index/btn_catch.gif" alt="" /></a></p></div>');
        }
    });
    num = $(xml).find("item").length;
    current_num = 0;
    prev_num = num-1;
    nums = new Array();
    $("#catch").find('div').hide();
    $("#catch_wrap").hide();
    timerID = setInterval('dispCatch2()', 10000);
    dispCatch2();

}
function dispCatch2(){
    if(nums.length==0) {
      for(i=0;i<num;i++) {
        nums.push(i);
      }
    }
    current_num = nums.splice(Math.random()*nums.length,1);
//    if(jQuery.support.opacity){
      $("#catch_wrap").stop().fadeOut('slow',function() {
          $('div:eq('+prev_num+')',"#catch").hide();
          $('div:eq('+current_num+')',"#catch").show();
          prev_num = current_num;
          $("#catch_wrap").delay( 1000 ).fadeIn('slow');
      });
/*
    } else {
      $("#catch_wrap").stop().animate({opacity:'0.0',left:'-566px'},'','',function() {
          $('div:eq('+prev_num+')',"#catch").hide();
          $('div:eq('+current_num+')',"#catch").show();
          prev_num = current_num;
          $("#catch_wrap").css({opacity:'0.0',display:'block',left:'945px'});
          $("#catch_wrap").delay( 1000 ).animate({opacity:'1.0',left:'326px'});
      });
    }
*/
}
function dispBanners(xml) {
    repeatNum = Math.ceil(5 / $(xml).find("item").length);
    for(i=0;i<repeatNum;i++) {
      $(xml).find("item").each(function () {
          target_str = $(this).find('target').text()==''?'':' target="'+$(this).find('target').text()+'"';
          $("#banners").append('<li><a href="' + $(this).find('link').text() + '"' + target_str + '><img src="' + $(this).find('img').text() + '" alt="" /></a></li>');
      });
    }
    jQuery('#banners').jcarousel({
      rvs:true,
      animation:800,
      scroll:1,
      auto:1,
      wrap: 'circular',
      initCallback: banners_initCallback
    });
}
jQuery(document).ready(function() {
    $.ajaxSetup({ cache: false });
    $.ajax({
        type: "GET",
        url: "usrdata/catch.xml",
        dataType: "xml",
        success: dispCatch
    });
    $.ajax({
        type: "GET",
        url: "usrdata/banners.xml",
        dataType: "xml",
        success: dispBanners
    });
    jQuery('#news').jcarousel({
      animation:800,
      scroll:1,
      auto:2,
      wrap: 'circular',
      initCallback: news_initCallback
    });
  });
