var topSWFPath = "/flash/main20100226.swf";

if ( swfobject.hasFlashPlayerVersion("8.0.0") ) {
	if ( ( $.browser.msie && $.browser.version < 7 ) || ( jQuery.browser["opera"] && jQuery.browser.version<9 ) ) {
		$(document).ready(function() {
			$("#HEAD-BG").before('<div id="VISUAL-WRAPPER" style="position:absolute; width:970px; height:398px;">&nbsp;</div>');
			$("#HEAD-BG").css("background-image","none");
			$("#VISUAL").empty();
			swfobject.embedSWF(topSWFPath, "VISUAL-WRAPPER", "970", "398", "8.0.0", false, {}, {wmode:"transparent"}, {id:"TopSWF"});
		});
	} else {
		swfobject.embedSWF(topSWFPath, "VISUAL", "970", "398", "8.0.0", false, {}, {wmode:"transparent"}, {id:"TopSWF"});
		$(document).ready(function() {
			$("#VISUAL, #TopSWF").css("width","970px");
		});
	}
}

//---------------------------------------------

$(function($){
	
	var items = [];
	var numD = 6;
	var indexNext = 0;
	var interval = 7000;
	var intervalOffset = 200;
	var hideDuration = 150;
	var showDuration = 150;
	var timer;
	
	function onLoad(xml){
		var $wrapper = $("div#ITEM-LIST");
		var $listLink = $("div#ITEM-LIST div#BTN-NEW").clone();
		$wrapper.empty();
		$("item",xml).each(function(i,n){
			var title = $(this).attr("title");
			var name = $(this).attr("name");
			var thum_src = $(this).attr("thum_src");
			var link = $(this).attr("link");
			var date = $(this).attr("date");
			var shopurl = $(this).attr("shopurl");
			var shopname = $(this).attr("shopname");
			var shopfloorurl = $(this).attr("shopfloorurl");
			var shopfloorname = $(this).attr("shopfloorname");
			var bodytext = $(this).text();
			var item = '<div class="list-l"><div class="clearfix"><div class="list-l-img"><a href="'+link+'"><img src="'+thum_src+'" width="64" height="64" alt="'+name+'" /></a></div><div class="list-l-data"><span>'+date+' <a href="'+shopfloorurl+'">'+shopfloorname+'</a> - <a href="'+shopurl+'">'+shopname+'</a></span><strong><a href="'+link+'">'+title+'</a></strong><p>'+bodytext+'</p></div></div></div>';
			var img = new Image();
			img.src = thum_src;
			items.push(item);
		});
		
		var i;
		for ( i=0; i<numD; i++ ) {
			$wrapper.append('<div id="ITEM-LIST-'+i+'">'+items.shift()+'</div>');
			indexNext++;
		}
		$wrapper.append($listLink);
		timer = setInterval(lotate,interval);
	}
	
	function lotate() {
		if ( indexNext > numD-1 ) indexNext = 0;
		$("div#ITEM-LIST-"+indexNext).fadeOut(hideDuration,replaceItem);
		clearInterval(timer);
		time = intervalOffset;
		if ( indexNext >= numD -1 ) time = interval;
		clearInterval(timer);
		timer = setInterval(lotate,time);
	}
	
	function replaceItem() {
		items.push($(this).html());
		$(this).empty();
		$(this).append(items.shift());
		$(this).fadeIn(showDuration);
		indexNext++;
		if ( indexNext > numD-1 ) indexNext = 0;
	}
	
	$.ajax({
		type:"GET",
		url:"/tmpl/top_item.xml",
		dataType:"xml",
		success:onLoad
	});
});


