/* 文字コード：UTF-8 */
jQuery(function($){
	var lang = (location.pathname.indexOf('/JP/') !== -1 ? 'JP' : 'EN');
	/* globalnavi */
	$('#GlobalNavi li.gmenu').hover(
		function(){
			if($("ul.fmenu:not(:animated)",this).length > 0){
				$(this).addClass('gmenu_on');
				$("ul.fmenu:not(:animated)",this).show();
			}
		},
		function(){
			if($("ul.fmenu:not(:animated)",this).length > 0){
				$(this).removeClass('gmenu_on');
				$("ul.fmenu",this).hide();
			}
		}
	);
	if(!window.XMLHttpRequest){
		$(document).ready(
			function(){
				$('#GlobalNavi a.gmenu').each(
					function(){
						var w = $(this).width();
						$(this).css('width', w);
						$(this).css('float', 'none');
					}
				);
			}
		);
	}
	/* rside */
	var naviImgOn = function(img){
		var src = img.attr('src');
		img.attr('src', src.replace(/_off/,'_on'));
	}
	var self_uri = location.pathname.replace(/index(-[0-9]+)?\.html/, '');
	$('div.rside_category_navi a').each(function(i, item){
		var link = $(item).attr('href');
		if(link == self_uri){
			naviImgOn($('img', item).eq(0));
		}
	});
	if($('div#Rside img.btn_model').length > 0){
		if(self_uri.indexOf('/' + lang + '/TECHNOLOGY/OVERVIEW/MODEL/') > -1){
			naviImgOn($('div#Rside img.btn_model').eq(0));
		}
	}
	if($('div#rsideRecentEntryBlock').length > 0){
		$(document).ready(
			function(){
				var html = '';
				var max_item_count = 4;
				var item_count = 0;
				$.ajax({
					url: '/' + lang + '/TECHNOLOGY/MAGAZINE/feed.xml',
					async: true,
					dataType: 'xml',
					success: function(xml, textStatus){
						if(textStatus == 'success'){
							if($(xml).find('item').length > 0){
								html += '<ul>';
								var items = $(xml).find('item');
								items.each(function(){
									var link = relation_entry.check_link($(this).find('link').text());
									var title = $(this).find('title').text();
									var leadimage = $(this).find('xstep\\:image').text() || $(this).find('image').text() || $(this).find('[nodename=xstep\\:image]').text();
									link = link.replace(/https?:\/\/[^\/]+/, '');
									leadimage = leadimage.replace(/https?:\/\/[^\/]+/, '');
									html += '<li><a href="' + link + '" title="' + title + '"><span class="image" style="background-image:url(' + leadimage + ');"></span><span class="title">' + (title.length > (lang == 'JP' ? 24 : 28) ? title.substring(0, (lang == 'JP' ? 24 : 28)) + '...' : title) + '</span></a></li>';
									item_count++;
									if(item_count >= max_item_count){
										return false;
									}
								});
								html += '</ul>';
							}
							$('div#rsideRecentEntryBlock').eq(0).append(html);
						}
					}
				});
			}
		);
	}
	/* label list */
	if($('div.label_list_load').length > 0){
		$('div.label_list_load').each(function(){
			var self = $(this);
			var cat = '';
			var event_year = '';
			var category = $(this).attr('rel');
			if(category.match(/^(present|future)_/)){
				cat = RegExp.$1;
			}
			else if(category.match(/^(event_year)/)){
				event_year = RegExp.$1;
			}
			var url = '/' + lang + '/TECHNOLOGY/KEYWORDS/list_' + category + '.js';
			$.getJSON(url, function(data){
				var ul = $('<ul></ul>').appendTo(self);
				$(data.list).each(function(i,item){
					var link = '/' + lang + '/TECHNOLOGY/';
					if(cat != ''){
						link += cat + '_keyword.html?keyword=' + item.id;
					}
					else if(event_year != ''){
						link += 'future_event.html?event_year=' + item.id;
					}
					else{
						link += 'KEYWORDS/' + item.id + '.html';
					}
					ul.append('<li><a href="' + link + '">' + item.name + '</a>(' + item.count + ') </li>\n');
				});
			});
		});
	}
	/* feed reader */
	if(typeof(news_list_load) == 'function'){
		news_list_load('.content');
	}
	/* related_technology */
	function related_technology_load(box, link){
		var loader = document.createElement('div');
		$(loader).load(link + " meta[property^='og:'],meta[property^='xstep:']", function(){
			var title = $("meta[property='og:title']", loader).attr('content');
			var image = $("meta[property='og:image']", loader).attr('content');
			var label = $("meta[property='xstep:label']", loader).attr('content');
			var lead = $("meta[property='xstep:lead']", loader).attr('content');
			var html = '';
			if(label.indexOf('future') !== -1){
				html += '<a href="' + link + '"><div class="xxx"><div class="yyy" style="background:url(' + image + ') no-repeat center center;"><img class="tn-nt-future" src="' + image + '" alt="thumb" width="135" height="135" style="opacity: 0; " /></div></div></a>';
			}
			else{
				html += '<a href="' + link + '"><img src="' + image + '" alt="thumb" width="135" height="135" /></a>';
			}
			html += '<a href="' + link + '"><h3>' + title + '</h3></a>';
			// html += '<p>' + lead + '</p>';
			$(box).html(html);
		});
	}
	if($('div.related_technology').length > 0){
		$('div.related_technology').each(
			function(){
				var self = this;
				$('<h3 class="content_stl">' + (lang == 'JP' ? '関連技術' : 'RELATED TECHNOLOGY') + '</h3>').appendTo(this);
				var list = $('<div class="tns-nt clearfix"></div>').appendTo(this);
				var links = $(this).attr('rel').split(',');
				var box = new Array();
				var j = 0;
				for(var i = 0; i < links.length; i++){
					var link = links[i];
					if(link){
						box[j] = $('<div class="tn-nt"></div>').appendTo(list);
						related_technology_load(box[j], link);
						j++;
					}
				}
			}
		);
	}
	/* related_magazine */
	function related_magazine_load(box, link){
		var loader = document.createElement('div');
		$(loader).load(link + " meta[property^='og:'],meta[property^='xstep:']", function(){
			var title = $("meta[property='og:title']", loader).attr('content');
			var image = $("meta[property='og:image']", loader).attr('content');
			var lead = $("meta[property='xstep:lead']", loader).attr('content');
			var num = '';
			if(title.match(/^([0-9]{3}) (.+)/)){
				num = RegExp.$1;
				title = RegExp.$2;
			}
			var html = '';
			html += '<div class="tn-ntm-img"><a href="' + link + '"><img src="' + image + '" /></a></div>';
			if(num){
				html += '<h4>' + num + '</h4>';
			}
			html += '<a href="' + link + '"><h3>' + title + '</h3></a>';
			html += '<p>' + lead + '</p>';
			$(box).html(html);
		});
	}
	if($('div.related_magazine').length > 0){
		$('div.related_magazine').each(
			function(){
				var self = this;
				$('<h2 class="tns-ntm-header">NISSAN TECHNOLOGY MAGAZINE ' + (lang == 'JP' ? '関連記事' : 'RELATED ARTICLES') + '</h2>').appendTo(this);
				var list = $('<div class="tns-ntm"></div>').appendTo(this);
				var links = $(this).attr('rel').split(',');
				var box = new Array();
				var j = 0;
				for(var i = 0; i < links.length; i++){
					var link = links[i];
					if(link){
						box[j] = $('<div class="tn-ntm"' + (j == 2 ? ' style="margin-right:0;"' : '') + '></div>').appendTo(list);
						related_magazine_load(box[j], link);
						j++;
					}
				}
			}
		);
	}
	$(".tn-ntm").hover(function(){
		$(".tn-ntm").not($(this)).animate({opacity: 0.3},{queue:false, duration:300});
		$(this).find("h3").animate({color: "#c71444"},{queue:false, duration:400});
		$(this).find("a").animate({color: "#c71444"},{queue:false, duration:400});
		$(this).find("p").show();
	}, function(){
		$(".tn-ntm").not($(this)).animate({opacity: 1},{queue:false, duration:300});
		$(this).find("h3").animate({color: "#444"},{queue:false, duration:400});
		$(this).find("a").animate({color: "#444"},{queue:false, duration:400});
		$(this).find("p").hide();
	});
});

