jQuery(document).ready(function(){
    var current = 0;
    var array = new Array( '001', '002', '003', '005', '006', '007', '008', '009', '010');

    var pname = new Array( '高知県梼原町', '高知県中土佐町', '北海道下川町', '長野県小諸市',
			   '宮崎県諸塚村', '熊本県小国町', '大分県日田市', '新潟県新潟市', '岩手県住田町');

    var locale = new Array( '高知県梼原町', '高知県中土佐町', '北海道下川町', '長野県小諸市',
			    '宮崎県諸塚村', '熊本県小国町', '大分県日田市', '新潟県新潟市', '岩手県住田町');

    var span = new Array( '2007年11月～', '2008年8月～', '2009年4月～', '2009年9月～',
			  '2010年3月～', '2010年4月～', '2010年4月～', '2010年11月～', '2011年7月～');

    var area = new Array( '58.4ha', '70.23ha', '-', '104ha', '-', '-', '-', '-', '-');

    var latit = new Array( '33°25\’56.00”N,132°54\’24.00”E', '33°24\’35.00”N,133°07\’53.00”E',
			   '44°17\’12.00”N,142°40’32.00”E', '36°22\'41.00"N, 138°28\'10.00"E',
                           '32°34\'30.00"N,131°21\'33.00"E', '33°06\'18.00"N,131°09\'34.00"E',
			   '33°02\'10.00"N,130°57\'43.00"E', '-', '39°08’36.00”N, 141°34’29.00”E'); 

    var detail = new Array( '高岡郡梼原町東向　他', '高岡郡中土佐町大野見萩中', '上川支庁下川町渓和　他',
                            '小諸市甲字野馬取　他', '-', '熊本県阿蘇郡小国町上田　他',
                            '大分県日田市上津江村上野田シカキ石 他', '新潟市秋葉区', '岩手県気仙郡住田町世田米 他');

    var cont = new Array( '持続可能な森林管理', '持続可能な森林管理', '持続可能な森林管理',
			  '持続可能な森林管理', '持続可能な森林管理', '持続可能な森林管理',
			  '持続可能な森林管理', '持続可能な森林管理', '持続可能な森林管理');

    var assoc = new Array( '梼原町森林組合', '須崎地区森林組合', '下川町森林組合', '佐久森林組合',
			   'ウッドピア諸塚 耳川広域森林組合', '小国町森林組合', '株式会社トライ・ウッド',
			   '合同会社木質ペレット推進協議会', '住田町、松田林業、佐藤木材、気仙地方森林組合　他');

    var spec = new Array( 'スギ<br />ヒノキ', 'スギ<br />ヒノキ', 'トドマツ<br />カラマツ<br />エゾマツ<br />カンバ',
			  'カラマツ<br />アカマツ<br />他広葉樹', 'スギ<br />ヒノキ<br />他広葉樹',
			  'スギ<br />ヒノキ<br />他広葉樹', 'スギ<br />ヒノキ<br />他広葉樹', 'スギ', 'スギ');

    var iw = 250;

    var itvl = setInterval(function(){
	var slidestr = $('#action-slide img').attr('src');
	var photostr = $('#action-photo img').attr('src');

	if(current == array.length-1){
    	    $('#action-slide img').attr({'src':slidestr.replace(array[current],array[0])});
//	    $('#action-photo img').attr({'src':photostr.replace(array[current],array[0])});
	    $('#action-photo-grp').stop().animate({marginLeft:'0px'},750);
    	    $('#action-title').html('PJ:'+array[0]);
    	    $('#action-pname').html(pname[0]);
    	    $('#action-locale').html(locale[0]);
    	    $('#action-span').html(span[0]);
    	    $('#action-area').html(area[0]);
	    $('#action-latit').html(latit[0]);
	    $('#action-detail').html(detail[0]);
	    $('#action-cont').html(cont[0]);
	    $('#action-assoc').html(assoc[0]);
	    $('#action-spec').html(spec[0]);
	    current=0;
	}else{
	    $('#action-slide img').attr({'src':slidestr.replace(array[current],array[current+1])});
//	    $('#action-photo img').attr({'src':photostr.replace(array[current],array[current+1])});
    	    current++;
	    $('#action-photo-grp').stop().animate({marginLeft:-1*(iw*current)+'px'},750);
    	    $('#action-title').html('PJ:'+array[current]);
    	    $('#action-pname').html(pname[current]);
    	    $('#action-locale').html(locale[current]);
    	    $('#action-span').html(span[current]);
    	    $('#action-area').html(area[current]);
	    $('#action-latit').html(latit[current]);
	    $('#action-detail').html(detail[current]);
	    $('#action-cont').html(cont[current]);
	    $('#action-assoc').html(assoc[current]);
	    $('#action-spec').html(spec[current]);
	}
    },5000);


    $.ajax({type:'GET',url:'../news/atom.xml',dataType:'xml',success:function(xml) { parseXml_now(xml); }});
    $.ajax({type:'GET',url:'../blog/atom.xml',dataType:'xml',success:function(xml) { parseXml_update(xml); }});
//    console.log($.ajax);
});

function parseXml_now(xml){
    var list = $(xml).find('entry');

    for( var i=0; i<3; i++ ){
	var img = $($(list[i]).find('content').text()).find('img')[0];
	if( img != undefined){
	    var nimg = $('#now-'+(i+1)+' img').attr({'src':$(img).attr('src')});
	    var iw = $(img).attr('width');
	    var ih = $(img).attr('height');
	    var ia = iw / ih;
	    if( ia < 1.0 ){
		$(nimg).css({'width':100*ia+'px'});
		$(nimg).css({'height':'100px'});
	    }else{
		$(nimg).css({'width':'150px'});
		$(nimg).css({'height':150/ia+'px'});
	    }
	}else{
//	    $('#now-'+(i+1)+' img').attr({'src':'','width':'150px','height':'100px'});
	}
	$('#now-desc-'+(i+1)).html($(list[i]).find('title').text());
//	$('#now-desc-'+(i+1)+'-d').html($(list[i]).find('summary').text());
	$('#now-detail-'+(i+1)+' a').attr({'href':$($(list[i]).find('link')).attr('href')});
	if(parseFloat($(nimg).css('height').replace('px','')) < 90 ){
	    var margin = (100 - parseFloat($(nimg).css('height').replace('px',''))) / 2;
	    $(nimg).css({'margin-top':margin+'px'});
    	    $('#now-desc-'+(i+1)).css({'margin-top':margin+'px'});
    	    $('#now-detail-'+(i+1)).css({'margin-top':margin+'px'});
	}
    }
}
function parseXml_update(xml){
    var list = $(xml).find('entry');

    for( var i=0; i<3; i++ ){
		var img = $($(list[i]).find('content').text()).find('img')[0];
		if( img != undefined){
			var nimg = $('#webupdate-'+(i+1)+' img').attr({'src':$(img).attr('src')});
			var iw = $(img).attr('width');
			var ih = $(img).attr('height');
			var ia = iw / ih;
			if( ia < 1.0 ){
				$(nimg).attr({'width':100*ia+'px'});
				$(nimg).attr({'height':'100px'});
			}else{
				$(nimg).attr({'width':'150px'});
				$(nimg).attr({'height':150/ia+'px'});
			}
		}else{
	//	    $('#webupdate-'+(i+1)+' img').attr({'src':'','width':'150px','height':'100px'});
		}
		$('#webupdate-desc-'+(i+1)).html($(list[i]).find('title').text());
		$('#webupdate-desc-'+(i+1)+'-d').html($(list[i]).find('summary').text());
		$('#webupdate-detail-'+(i+1)+' a').attr({'href':$($(list[i]).find('link')).attr('href')});

		if( nimg == undefined ) continue;
		if(parseFloat($(nimg).attr('height').replace('px','')) < 90 ){
			var margin = (100 - parseFloat($(nimg).attr('height').replace('px',''))) / 2;
			$(nimg).css({'margin-top':margin+'px'});
				$('#webupdate-desc-'+(i+1)).css({'margin-top':margin+'px'});
				$('#webupdate-detail-'+(i+1)).css({'margin-top':margin+'px'});
		}
    }
 
}

