﻿$(function () {
  var instance = $('#sp-announce');
  var timeout;
  instance.css({ top: $(window).height() + 'px' });
  instance.hover(function () { clearTimeout(timeout) }, function () { timeout = setTimeout(function () { instance.fadeOut() }, 10000) });

  setTimeout(function () {
    instance.animate({ top: ($(window).height() - instance.height()) + 'px' }, 1000, 'swing', function () {
      instance.css({ top: 'auto', bottom: 0 });
      timeout = setTimeout(function () { instance.fadeOut() }, 10000);
    })
  }, 500);
  
    $.fn.fullBg = function(){
    var bgImg = $(this);		
 
    function resizeImg() {
      var imgwidth = bgImg.width();
      var imgheight = bgImg.height();
 
      var winwidth = $(window).width();
      var winheight = $(window).height();
 
      var widthratio = winwidth / imgwidth;
      var heightratio = winheight / imgheight;
 
      var widthdiff = heightratio * imgwidth;
      var heightdiff = widthratio * imgheight;
 
      if(heightdiff>winheight) {
        bgImg.css({
          width: winwidth+'px',
          height: heightdiff+'px'
        });
      } else {
        bgImg.css({
          width: widthdiff+'px',
          height: winheight+'px'
        });		
      }
    } 
  
    resizeImg();
	$(window).resize(function() {
	  resizeImg();
	}); 
 
  };
  
  	var msg_count;
	msg_count = $("#msg img").length;
	$("#msg img").hide();
	$("#msg img").each(function(index) {
		$(this).attr("id", "msg-"+index);
		if (index == (msg_count-1)) {
			$(this).show().attr("class","active");
		}
	});
  $("#main-background").cycle({ fx: "fade", speed: 1000, timeout: 8000, timeoutFn: showhidemsg(msg_count) });
  //$("#msg").cycle({ fx: "fade", speed: 2000 });



  $('#news > ul a').each(function (i) {
    $(this).mouseenter(function () {
      if ($('.show-news:visible').index() != i) {
        $('.show-news').stop(true);
        if ($('.show-news:visible').length) {
          $('.show-news:visible').animate({ height: 0 }, function () {
            $(this).hide();
            $('.show-news').eq(i).show().animate({ height: '999px' },1000);
          });
        } else {
          $('.show-news').eq(i).show().animate({ height: '999px' },1000);
        }
      }
    });
  });
  
   $('.show-news').mouseleave(function(e){
     $('.show-news:visible').animate({ height: 0 }, function () { $(this).hide() });
   });
});

 $(window).load(function() {
	$("#main-background img").fullBg();
});

 function showhidemsg(msg_count) {
	var next_id = parseInt($("#msg .active").attr('id').substring(4))+1;
	if (next_id >= msg_count) {
		next_id = 0;
	}
	setTimeout(function() {
		$("#msg .active").hide().attr("class","");
		$("#msg #msg-"+next_id).show().attr("class","active");
	}, 500);
	setTimeout(function() {
		showhidemsg(msg_count);
	}, 8000)
 }
