function addStart(o) {
    if ('\v' == 'v') {
        o.style.behavior = 'url(#default#homepage)';
        o.setHomePage(o.href);
        return false;
    } else return true;
}

function initNews() {
  $(".news .detail").mouseover(function() {
    $(".date .d", $(this).parent().parent()).css("background-position", "0 -32px");
  }).mouseout(function() {
    $(".date .d", $(this).parent().parent()).css("background-position", "0 0");
  });
  $(".news .detailimg").mouseover(function() {
    $(".date .d", $(this).parent()).css("background-position", "0 -32px");
  }).mouseout(function() {
    $(".date .d", $(this).parent()).css("background-position", "0 0");
  });
}

function carouselMove() {
	if (!window.carouselSpeedX) return;
	if (window.carouselSpeedX * window.carouselSpeed < 0) {
		window.carouselSpeed = 0;
	} else if (Math.abs(window.carouselSpeedX - window.carouselSpeed) > 0.01) {
		window.carouselSpeed = window.carouselSpeed + (window.carouselSpeedX - window.carouselSpeed) * 0.1;
	} else {
		window.carouselSpeed = window.carouselSpeedX;
	}
	var $carousel = window._carousel;
	var $children = window.carouselChild;
	var speed = window.carouselSpeed;
	var width = $carousel.width();
	var left = parseInt($children.css("margin-left"));
	var full = window.carouselWidth;
	var delta = speed * 10;
	if (width > full) return;
	if (!left) left = 0;
	if (left <= 0 && left + delta > 0) left = 0;
	else if (left >= -full + width && left + delta < -full + width) left = -full + width;
	else left = left + delta;
	$children.css("margin-left", left);
}

function initCarousel() {
	var $carousel = $("#carousel");
	if ($carousel.parent().offset().left > 0)
	$carousel.css("left", - $carousel.parent().offset().left);
	$carousel.css("width", document.body.clientWidth - 1);
	
	if ($carousel.width() > $carousel.children().children().width()) return;
	window.setTimeout(function() {
	$("#carousel").mouseover(function(e) {
		if (window.carouselTimeout) {
			clearTimeout(window.carouselTimeout);
			window.carouselTimeout = null;
		}
		if (!window.carouselInterval) {
			window.carouselInterval = setInterval(carouselMove, 20);
		}
	}).mouseout(function() {
		if (window.carouselTimeout) clearTimeout(window.carouselTimeout);
		window.carouselTimeout = setTimeout(function() {
			if (window.carouselInterval) {
				clearInterval(window.carouselInterval);
				window.carouselInterval = null;
				window.carouselSpeed = 0;
			}
		}, 100);
	}).mousemove(function(e) {
		var $this = $(this);
		var left = e.pageX - $this.offset().left;
		var width = $this.width();
		if (left < width / 3) {
			window.carouselSpeedX = (width / 3 - left) / width * 3;
		} else if (left > width * 2 / 3) {
			window.carouselSpeedX = - (left - width * 2 / 3) / width * 3;
		} else {
			window.carouselSpeedX = 0;
		}
	}).click(function() {
		$.cookie("carousel-left", window.carouselChild.css("margin-left"), { path: "/" });
	});
	}, 200);
	window._carousel = $("#carousel");
	window.carouselChild = window._carousel.children();
	window.carouselWidth = window.carouselChild.children().width();
	var x = $.cookie("carousel-left");
	var l = location.href;
	l = l.substr(l.indexOf("://") + 3);
	l = l.substr(l.indexOf("/") + 1);
	if (x && l && document.referrer) {
		window.carouselChild.css("margin-left", x);
	} else {
		window.carouselChild.css("margin-left", - (window.carouselWidth - document.body.clientWidth) / 2);
	}
}

function initBanner() {
	var $ad = $("div.ad");
	$("td:first", $ad).css("text-align", "left");
	$("td:last", $ad).css("text-align", "right");
}

function init() {
    if (typeof($) === "undefined") return setTimeout(arguments.callee, 100);
    $(document).ready(function() {
        initNews();
		initBanner();
		initCarousel();
    });
}

init();

window.onresize = function() {
	var $carousel = $("#carousel");
	$carousel.css("left", 0);
	$carousel.css("width", "100%");
	if ($carousel.parent().offset().left >= 0)
	$carousel.css("left", - $carousel.parent().offset().left);
	$carousel.css("width", document.body.clientWidth - 1);
};

