(function ($) { $(function () {

if (window.location.href != 'http://worldtrade.jp/') {
	$('div#main').remove();
	return;
}

var count  = 1;

var tabs   = $('div#slideshow ul.tabs li');
var tabs_a = tabs.find('a');

loadXML();

tabs_a.click(function (){
	tabs.find('a.on').removeClass('on');
	$(this).addClass('on');
	$('div.info:visible').fadeOut(1000);
	$('div.introtext:visible').fadeOut(1000);
	$("div" + $(this).attr('href')).fadeIn(1000);
	$("div#introtext_" + $(this).attr('href').replace(/#info_/, '')).fadeIn(1000);
	count = parseInt($(this).attr('href').replace(/#info_/, ''));
	return false;
});

setInterval(function (){
	if (count >= 8) count = 0;
	$(tabs[count]).find('a').click();
},15000);

function loadXML () {
	$.ajax({
		url     : '/slideshow/slideshow.xml',
		dataType: 'xml',
		cache   : false,
		error   : function (r) {
			console.log('error');
		},
		success : function (r) {
			appendImg($(r).find('page'));
			appendTabs($(r).find('page'));
		}
	});
}

function appendImg (pages) {
	pages.each(function (i, page){
		$('div#info_' + (parseInt(i) + 1)).html(
			'<a class="png" href="' + $(page).find('link').text() + '" target="' + $(page).find('target').text() + '">' +
			'<img src="/slideshow/' + $(page).find('img').text() + '" />' +
			'</a>'
		);
		$('div#slideshow ul li').eq(i).find('div.thumbnail').html('<img src="/slideshow/' + $(page).find('img').text() + '" />');
	});
}

function appendTabs (pages) {
	pages.each(function (i, page) {
		$('div#introtext_' + (parseInt(i) + 1) + ' div.title').text($(page).find('title').text());
		$('div#introtext_' + (parseInt(i) + 1) + ' div.comment').text($(page).find('comment').text());
		$('div#introtext_' + (parseInt(i) + 1) + ' div.anchor').html(
			'<a class="png" href="' + $(page).find('link').text() + '" target="' + $(page).find('target').text() + '">' +
			'Read More' +
			'</a>'
		);
		$('div#slideshow ul li').eq(i).find('div.title').html('<p>' + $(page).find('title').text() + '</p>');
	});
}

})})(jQuery);
