function setProgress(id) {
	var posx = $('#'+ id).width()/2 - 20;
	var posy = $('#'+ id).height()/2 - 20;
	$('#'+ id)
		.html('<img src="/usma/images/ajax_progress.gif" style="margin:'+ posy +'px 0 0 '+ posx +'px"/>');
}

function formatDate(str){
    var myDate = new Date(str);
    var YYYY = myDate.getFullYear();
    var MM = myDate.getMonth()+1;
    if(MM < 10){ MM = "0"+ MM; }
    var DD = myDate.getDate();
    if(DD < 10){ DD = "0"+ DD; }
    var date = YYYY +"/"+ MM +"/"+ DD;
    return date;
}

function formatDate2(str){
	var values = str.split(' ');
    var myDate = new Date(values[3], values[2].substring(0, values[2].length-1)-1, parseInt(values[1])+1);
    var YYYY = myDate.getFullYear();
    var MM = myDate.getMonth()+1;
    if(MM < 10){ MM = "0"+ MM; }
    var DD = myDate.getDate();
    if(DD < 10){ DD = "0"+ DD; }
    var date = YYYY +"/"+ MM +"/"+ DD;
    return date;
}


function loadInformation() {
	jQuery.getFeed({url:'/jp/proxy.php?url=http://feed.dowbe.com/information.xml', success:onComplete});
    function onComplete(feed) {
        var temp = '';
        
        var count = (feed.items.length > 5)? 5 : feed.items.length;
        for(var i = 0; i < count; i++) {
            var item = feed.items[i];
            temp += '<li class="list">'+ formatDate(item.updated) +' : ';
            if(item.link) {
	            temp += '<a href="' + item.link + '">' + item.title + '</a>';
            } else {
				temp += item.title;
            }
            if(item.id) {
            	temp += '<img src="res/image/new2.gif" />';
            }
            temp += '</li>';
        }
        jQuery('#info > ul').append(temp);
    }
}

function loadBlog() {
	jQuery.getFeed({url:'/jp/proxy.php?url=http://i-deal-inc.com/wp/?feed=rss2', success:onComplete});
    function onComplete(feed) {
        var temp = '';
        
        var count = (feed.items.length > 5)? 5 : feed.items.length;
        for(var i = 0; i < count; i++) {
            var item = feed.items[i];
            temp += '<li class="list">'+ formatDate(item.updated) +' : ';
            if(item.link) {
	            temp += '<a href="' + item.link + '" target="about:blank">' + item.title + '</a>';
            } else {
				temp += item.title;
            }
            if(i == 0) {
            	temp += '<img src="res/image/new2.gif" />';
            }
            temp += '</li>';
        }
        jQuery('#blog > ul').append(temp);
    }
}

function loadEnglishNote() {
	jQuery.getFeed({url:'/jp/proxy.php?url=http://notes.dowbe.com/7-culture/?format=feed&type=atom', success:onComplete});
    function onComplete(feed) {
        var temp = '';
        
        var count = (feed.items.length > 5)? 5 : feed.items.length;
        for(var i = 0; i < count; i++) {
            var item = feed.items[i];
            temp += '<li class="list">'+ formatDate2(item.updated) +' : ';
            if(item.link) {
	            temp += '<a href="' + item.link + '">' + item.title + '</a>';
            } else {
				temp += item.title;
            }
            if(i == 0) {
            	temp += '<img src="res/image/new2.gif" />';
            }
            temp += '</li>';
        }
        jQuery('#notes > ul').append(temp);
    }
}


function loadHeadlines() {
	$.ajax({
		dataType: "json",
		cache: true,
		url: "/data/headlines.json",
		success: function(data) {
			var headlineObj = $('#headline > .body');
			headlineObj.empty();

			var articleDate = data.date;
			var dateObj = $('<div/>').addClass('articledate').text(articleDate);
			headlineObj.append(dateObj);

//			var tableObj = $('<table width="100%" border="0" cellspacing="0" cellpadding="3"/>');
			var tableObj = $('<ul/>');
			$.each(data.headlines, function(i, item) {
//				var trObj = $('<tr/>');
//				trObj.append('<td><div class="en">'+ item.en +'</div><div class="ja">'+ item.ja +'</div></td>');
				tableObj.append('<li><div class="en">'+ item.en +'</div><div class="ja">'+ item.ja +'</div></li>');
//				tableObj.append(trObj);
			});
			headlineObj.append(tableObj);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}

function loadHeadlines2() {
	jQuery.getFeed({url:'/jp/proxy.php?url=http://feed.dowbe.com/article', success:onComplete});
    function onComplete(feed) {
        var temp = '';
        
        var count = 1;
        for(var i = 0; i < count; i++) {
            var item = feed.items[i];
            temp += '<div class="articledate">'+ formatDate(item.updated) +'</div>';
			var titles = item.title.split(' - ');
            temp += '<h4>'+ titles[0].substring(titles[0].indexOf(' ')) +'</h4>';
            temp += '<h4>'+ titles[1] +'</h4>';
            //temp += '<p>'+ item.description +'</p>';
        }
        jQuery('#headline > .body').append(temp);
    }

//	$('#headline > .body').rssfeed('http://feed.dowbe.com/article', {
//    	limit: 1,
//    	header: false,
//    	date: false
//  	});
}

function loadWhatsNews() {
	$.ajax({
		dataType: "json",
		cache: true,
		url: "/data/whatsnews.json",
		success: function(data) {
			var wnObj = $('#whatsnews > .contents');
			wnObj.empty();

			var articleDate = data.date;
			wnObj.append('<div id="articleDate">'+ articleDate +'</div>');

			var articleId = data.articleId;

			// build what's news
			$.each(data.articles, function(i, item) {
				var arnum = i + 1;

				var articleDiv = $('<div/>').attr('id', 'article'+ arnum).addClass('warticle');
				if(i > 0) articleDiv.hide();

				// create article part
				articleDiv.append($('<div/>').addClass('artext').addClass('radius5')
					.append('<img src="/jp/res/image/icon_article.gif" style="margin-right:5px;" class="radius5" />')
					.append(item.articleText));

				// create dialog part
				articleDiv.append($('<div/>').addClass('ditext').addClass('radius5').
					append('<img src="/jp/res/image/icon_question.gif" style="margin-right:5px;" class="radius5" />')
					.append(item.dialogText));

				// create answers part
				var answersDiv = $('<div/>').addClass('answers');
				var tableObj = $('<table border="0" cellpadding="5" cellspacing="7" />');

				$.each(item.distractor, function(j, dt) {
					var trObj = $('<tr/>');

					var dstnum = j + 1;
					var inputId = 'dst'+ arnum +'-'+ dstnum;
					var labelId = 'lbl'+ arnum +'-'+ dstnum;

					// setup values for radio button.
					var inputObj = $('<input/>').attr({
						'type': 'radio',
						'name': 'choise'+ arnum, 
						"id": inputId,
						"arnum": arnum,
						"dstnum": dstnum,
						"correct": item.answer
					});

					// setup event when radio button is clicked.
					inputObj.click(function(evnet) {
						var own = $(this);
						var correctObj = $('#correct'+ own.attr('arnum'));
						if(own.attr('dstnum') == own.attr('correct')) {
							correctObj.text('Correct').css('color', '#ff0000');
						} else {
							correctObj.text('Incorrect').css('color', '#0000ff');
						}
						$('lbl'+ own.attr('arnum') +'-'+ own.attr('correct')).addClass('highlight');
					});

					// create label
					var labelObj = $('<label/>').attr({'for': inputId, 'id': labelId}).css('margin','0').text(dt);

					var td1Obj = $('<td valign="top" />').append(inputObj);
					var td2Obj = $('<td valign="top" />').append(labelObj);

					trObj.append(td1Obj);
					trObj.append(td2Obj);

					tableObj.append(trObj);

					//answersDiv.append(inputObj);
					//answersDiv.append($('<label/>').attr('for', inputId).text(dt));
					//answersDiv.append('<br/>');

				})

				answersDiv.append(tableObj);
				articleDiv.append(answersDiv);

				// create correct indication part
				articleDiv.append($('<div/>').attr('id','correct'+ arnum).addClass('wcorrect'));

				wnObj.append(articleDiv);

			});

			// handle pager action
			$('.pager > a').click(function(evnet) {
				$('.pager > a').removeClass();
				var current = $(this);
				var text = current.text();
				if(-1 < text.indexOf('next') || -1 < text.indexOf('prev')) {
					text = current.attr('pagenum');
				}
				text = parseInt(text);

				$('#pg'+ text).addClass('current');
				$('#pgp').attr('pagenum', (((text - 1) < 1)? 4 : text - 1));
				$('#pgn').attr('pagenum', (((text + 1) > 4)? 1 : text + 1));

				$('.warticle').hide();
				$('#article'+ text).show();

				return false;
			});

		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			//alert(textStatus);
		}
	});
}


$(document).ready(function(){
//	loadHeadlines();
	loadInformation();
	loadBlog();
	loadHeadlines2();
///	loadWhatsNews();
	loadEnglishNote();
});

