var asahi = asahi || new Object();
/**************************************************************************
 * NewsCustomizer
 **************************************************************************/
asahi.NewsCustomizer = function()
{
	this.initialize.apply(this, arguments);
}

asahi.NewsCustomizer.prototype =
{
	initialize: function()
	{
		this.initContents();
	},
	
	
	initContents: function()
	{
		jQuery('#Genre').find('ul.BoxUtility').prepend('<li class="customize"></li>');
		var self = this;
		jQuery('#CustomizeInfo')
			.append('<li class="OptItm">もとの表示に戻す<a href="#"><img src="http://www.asahicom.jp/images08/common/btn_return.gif" alt="" width="25" height="17" /></a></li>')
			.find('li.OptItm a').each
			(
				function()
				{
					this.onclick = function()
					{
						self.resetOrder();
						return false;
					}
				}
			);
		this.initOrder();
	},
	
	
	initOrder: function()
	{
		var _cookie = asahi.cookie.read('newsOrder');
		_cookie = asahi.cookie.string2Array(_cookie);
		
		this.order = (!!_cookie) ? _cookie : this.getDefaultOrder();
		
		this.setMytown();
		this.setOrder();
	},
	
	setMytown: function()
	{
		jQuery('#MyTown').find('ul.FollowLnk').append('<li class="OptItm"><a href="/customize.html">地域を選択</a></li>');
		
		var _cookie = asahi.cookie.read('utilityData');
		if(!_cookie) return;
		_cookie = asahi.cookie.string2Object(_cookie);
		var src = _cookie.mytown;
		if(!!src)
		{
			var url = '/custom/' + src + '.xml' + '?' + (new Date()).getTime();
			jQuery.get(url,
				function(xml, status)
				{
					if(status != 'success') return;
					var content = jQuery('content', xml).text();
					jQuery('#MyTown').find('div.BoxContents').html(content);
				});
		}
	},
	
	setOrder: function()
	{
		for(var i=0, l=this.order.length; i<l; i++)
		{
			// order
			jQuery('#' + this.order[i].genre).appendTo("#Genre");
			
			
			var liHtml = '';
			// up, down btn
			if(i==0)
			{
				liHtml += '<a href="#" class="down"><img src="http://www.asahicom.jp/images08/common/btn_down.gif" alt="1つ下に移動する" width="24" height="17" /></a><img src="http://www.asahicom.jp/images08/common/btn_up_off.gif" alt="1つ上に移動する" width="23" height="17" />';
			}
			else if(i==l-1)
			{
				liHtml += '<img src="http://www.asahicom.jp/images08/common/btn_down_off.gif" alt="1つ下に移動する" width="24" height="17" /><a href="#" class="up"><img src="http://www.asahicom.jp/images08/common/btn_up.gif" alt="1つ上に移動する" width="23" height="17" /></a>';
			}
			else
			{
				liHtml += '<a href="#" class="down"><img src="http://www.asahicom.jp/images08/common/btn_down.gif" alt="1つ下に移動する" width="24" height="17" /></a><a href="#" class="up"><img src="http://www.asahicom.jp/images08/common/btn_up.gif" alt="1つ上に移動する" width="23" height="17" /></a>';
			}
			
			
			// open, close btn
			if(this.order[i].status == 'open')
			{
				jQuery('#' + this.order[i].genre).find('div.BoxContents').show();
				liHtml += '<a href="#" class="close"><img src="http://www.asahicom.jp/images08/common/btn_close.gif" alt="記事を表示しない" width="24" height="17" /></a>';
			}
			else if(this.order[i].status == 'close')
			{
				jQuery('#' + this.order[i].genre).find('div.BoxContents').hide();
				liHtml += '<a href="#" class="open"><img src="http://www.asahicom.jp/images08/common/btn_open.gif" alt="記事を表示する" width="24" height="17" /></a>';
			}
			
			jQuery('#' + this.order[i].genre).find('ul.BoxUtility li.customize').html(liHtml);
		}
		
		var self = this;
		jQuery('#Genre').find('ul.BoxUtility li.customize a').each
		(
			function()
			{
				this.onclick = function()
				{
					self.changeOrder(this);
					return false;
				}
			}
		);
	},
	
	changeOrder: function(target)
	{
		var genre = target.parentNode.parentNode.parentNode.id;
		var num;
		for(var i=0, l=this.order.length; i<l; i++)
		{
			num = i;
			if(this.order[i].genre == genre) break;
		}
		
		var type = target.className;
		switch(type)
		{
			case 'up':
				this.order.splice(num-1, 2, this.order[num], this.order[num-1]);
				break;
			
			case 'down':
				this.order.splice(num, 2, this.order[num+1], this.order[num]);
				break;
			
			case 'open':
				//jQuery(genre + ' .Lnk').show('slow');
				this.order[num].status = 'open';
				break;
			
			case 'close':
				//jQuery(genre + ' .Lnk').hide('slow');
				this.order[num].status = 'close';
				break;
			
		}
		
		this.setCookie();
		this.setOrder();
	},
	
	resetOrder: function()
	{
		this.order = this.getDefaultOrder();
		this.setCookie();
		this.setOrder();
	},
	
	setCookie: function()
	{
		var _data = asahi.cookie.array2String(this.order);
		asahi.cookie.create('newsOrder', _data, 365);
	},
	
	getDefaultOrder: function()
	{
		var _defaultOrder = 
		[
			{genre:"National", status:"open"},
			{genre:"Business", status:"open"},
			{genre:"Politics", status:"open"},
			{genre:"International", status:"open"},
			{genre:"Culture", status:"open"},
			{genre:"Science", status:"open"},
			{genre:"MyTown", status:"open"},
			{genre:"Obituaries", status:"open"}
		];

		return _defaultOrder;
	}
	
}

if(asahi.agent.getTarget())
{
	jQuery(function()
	{
		asahi.newsCustomizer = new asahi.NewsCustomizer();
	});
}



