relation_entry = new Object();
relation_entry.lang = (location.pathname.indexOf('/JP/') !== -1 ? 'JP' : 'EN');
relation_entry.init = function(){
	var self_uri = location.protocol + '//'+ location.host + location.pathname.replace(/\-[0-9]+\.html/, '.html');
	var mode = (location.pathname.indexOf('/MAGAZINE/') !== -1 ? 'magazine' : 'default');
	var tobj = $('div#relation_entry');
	var cat = tobj.attr('cat') || '';
	var keywords = tobj.attr('keywords') || '';
	if(keywords){
		var ids = keywords.split(',');
		var html = '';
		var items = new Array();
		var item_ids = new Array();
		var item_ids_magazine = new Array();
		var url_list = self_uri;
		var dateobj = new Date();
		for(var i = 0; i < ids.length; i++){
			var id = ids[i];
			var xml_url = '/' + relation_entry.lang + '/TECHNOLOGY/KEYWORDS/' + id + '.xml';
			$.ajax({
				url: xml_url,
				async: false,
				cache: true,
				dataType: 'xml',
				success: function(xml, textStatus){
					if(textStatus == 'success'){
						if($(xml).find('item').length > 0){
							var items_obj = $(xml).find('item');
							items_obj.each(function(i, obj){
								var title = $(this).find('title').text();
								var link = relation_entry.check_link($(this).find('link').text());
								var description = $(this).find("[nodeName=content:encoded]").text();
								var image = relation_entry.check_link($(this).find("[nodeName=xstep:image]").text());
								var catlabel = $(this).find("[nodeName=xstep:label][labelCategory='cat']").attr('labelId') || '';
								if(url_list.indexOf(link) === -1){
									url_list += ';' + link;
									var item_id = (i < 10 ? '0' : '') + i + link;
									if(link.indexOf('/MAGAZINE/') !== -1){
										item_ids_magazine.push(item_id);
									}
									else if(cat == '' || catlabel == cat){
										item_ids.push(item_id);
									}
									items[item_id] = {title:title, link:link, description:description, image:image, catlabel:catlabel};
								}
							});
						}
					}
				}
			});
		}
		if(mode == 'magazine'){
			html += relation_entry.get_magazine_contents2(mode, item_ids_magazine, items);
			html += relation_entry.get_contents(mode, item_ids, items);
		}
		else{
			html += relation_entry.get_contents(mode, item_ids, items);
			html += relation_entry.get_magazine_contents(mode, item_ids_magazine, items);
		}
		$(tobj).html(html);
		if(mode == 'magazine'){
			$(".post-thumb", tobj).hover(function(){
				$(".post-thumb").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(){
				$(".post-thumb").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();
			});
		}
	}
}
relation_entry.get_contents = function(mode, item_ids, items){
	var html = '';
	if(item_ids.length > 0){
		item_ids.sort();
		var count = (item_ids.length < 4 ? item_ids.length : 4);
		if(mode == 'magazine'){
			html += '<h2 class="tns-nt-h2">' + (relation_entry.lang == 'JP' ? 'NISSAN TECHNOLOGY 関連技術' : 'RELATED TECHNOLOGY') + '</h2>';
		}
		else{
			html += '<h3 class="content_ttl">' + (relation_entry.lang == 'JP' ? '関連技術' : 'RELATED TECHNOLOGY') + '</h3>';
		}
		html += '<div class="tns-nt clearfix">';
		for(var i = 0; i < count; i++){
			var item_id = item_ids[i];
			var item = items[item_id];
			html += '<div class="tn-nt"' + (i % 4 == 3 ? ' style="margin-right:0;"' : '') + '>';
			if(item.image){
				if(item.catlabel == 'future'){
					html += '<a href="' + item.link + '"><div class="xxx"><div class="yyy" style="background:url(' + item.image + ') no-repeat center center;"><img src="' + item.image + '" alt="" width="135" height="135" style="opacity: 0;" /></div></div></a>';
				}
				else{
					html += '<a href="' + item.link + '"><img src="' + item.image + '" alt="" width="135" height="135" /></a>';
				}
			}
			html += '<a href="' + item.link + '"><h3>' + item.title + '</h3></a>';
			if(item.description){
				html += '<p>' + item.description + '</p>';
			}
			html += '</div>';
		}
		html += '</div>';
	}
	return html;
}
relation_entry.get_magazine_contents = function(mode, item_ids_magazine, items){
	var html = '';
	if(item_ids_magazine.length > 0){
		item_ids_magazine.sort();
		var count = (item_ids_magazine.length < 3 ? item_ids_magazine.length : 3);
		html += '<h2 class="tns-ntm-header">NISSAN TECHNOLOGY MAGAZINE ' + (relation_entry.lang == 'JP' ? '関連記事' : 'RELATED ARTICLES') + '</h2>';
		html += '<div class="tns-ntm">';
		for(var i = 0; i < count; i++){
			var item_id = item_ids_magazine[i];
			var item = items[item_id];
			html += '<div class="tn-ntm"' + (i % 3 == 2 ? ' style="margin-right:0;"' : '') + '>';
			if(item.image){
				html += '<div class="tn-ntm-img"><a href="' + item.link + '"><img src="' + item.image + '" alt="" /></a></div>';
			}
			html += '<h4>' + item.title.slice(0,3) + '</h4><a href="' + item.link + '" target="_blank" ><h3>' + item.title.slice(4) + '</h3></a>';
			if(item.description){
				html += '<p>' + item.description + '</p>';
			}
			html += '</div>';
		}
		html += '</div>';
	}
	return html;
}
relation_entry.get_magazine_contents2 = function(mode, item_ids_magazine, items){
	var html = '';
	if(item_ids_magazine.length > 0){
		item_ids_magazine.sort();
		var count = (item_ids_magazine.length < 3 ? item_ids_magazine.length : 3);
		html += '<div id="single-related"><h2>' + (relation_entry.lang == 'JP' ? 'NISSAN TECHNOLOGY MAGAZINE 関連記事' : 'RELATED ARTICLES') + '</h2></div>';
		html += '<div class="post-thumb-div">';
		for(var i = 0; i < count; i++){
			var item_id = item_ids_magazine[i];
			var item = items[item_id];
			html += '<div class="post-thumb">';
			if(item.image){
				html += '<div class="post-thumb-image"><a href="' + item.link + '"><img src="' + item.image + '" alt="" /></a></div>';
			}
			html += '<div class="post-thumb-header">';
			html += '<div class="post-thumb-num"><h4>' + item.title.slice(0,3) + '</h4></div>';
			html += '<div class="post-thumb-title"><a href="' + item.link + '"><h3>' + item.title.slice(4) + '</h3></a></div>';
			html += '</div>';
			if(item.description){
				html += '<p>' + item.description + '</p>';
			}
			html += '</div>';
		}
		html += '</div>';
	}
	return html;
}
relation_entry.check_link = function(link){
	return link.replace(/http:\/\/www\.nissan\-global\.com\//, '/');
}

load_component = new Object();
load_component.init = function(){
	if($('.load_component').length){
		$('.load_component').each(function(i, component){
			var id = $(component).attr('id');
			var include_uri = '/JP/TECHNOLOGY/INCLUDE/'+id+'.html';
			$(component).hide();
			$(component).load(include_uri, function(){
				var items = $(component).children('div');
				var show = 0;
				if(items.length > 1){
					var include_uri,exclude_uri;
					for(var i = 0;i < items.length;i++){
						show = i;
						include_uri = $(items[i]).attr('include_uri');
						exclude_uri = $(items[i]).attr('exclude_uri');
						if(include_uri != '' && !(location.pathname.match(new RegExp(include_uri)))){ show = -1; }
						if(exclude_uri != '' && location.pathname.match(new RegExp(exclude_uri))){ show = -1; }
						if(show > -1){
							break;
						}
					}
					if(show >= 0){
						var html = $(items[show]).html();
						$(component).empty().html(html);
						$(component).show();
					}
				}
				if(typeof(news_list_load) == 'function'){
					news_list_load('#' + id);
				}
				if($('#sidebar-search-type dt a', component).length > 0){
					$('#sidebar-search-type dt a', component).click(function(){
						var dt = $(this).parent('dt');
						if(dt.next('dd').length != 0){
							if (dt.next('dd').is(':hidden')) {
								dt.next('dd').slideDown();
								dt.addClass('open');
							} else {
								dt.next('dd').slideUp();
								dt.removeClass('open');
							}
							return false;
						}
					});
				}
				if($('#sidebar-search-event li a').length > 0){
					$('#sidebar-search-event li a').click(function(){
						var target = $(this).attr('href');
						$('.sidebar-search-event-box').hide();
						$(target).show();
						return false;
					});
				}
			});
		});
	}
}

$(document).ready( function()
{	
	load_component.init();
	if($('div#relation_entry').length > 0){
		relation_entry.init();
	}
	if( $('.mainVisImage').length ){
		$('.mainVisImage').each( function(i, item){
			if( $('a', item).length ){
				var mainVisId = $(this).attr('id');
				$('a', item).click( function(){
					$(this).css({ display: 'none' });
					swfobject.embedSWF($(this).attr('href'), mainVisId, "565", "340", "9.0.0", {menu: "false",scale:"noscale",wmode:"transparent"}, {id: mainVisId} );
					return false;
				});
			}
		});
	}
    $("div.ct_image a[href$='.flv']").click(function(){
        var m = $(this).attr('href');
        var img = $('img', this).eq(0);
        var w = img.width();
        var h = img.height();
        var a = img.attr('class');
var html = '<div class="' + a + '" style="height:' + h + 'px;"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + w + '" height="' + h + '" id="FLVPlayer">\
<param name="movie" value="/JP/TECHNOLOGY/SWF/FLVPlayer_Progressive.swf" />\
<param name="salign" value="lt" />\
<param name="quality" value="high" />\
<param name="scale" value="noscale" />\
<param name="FlashVars" value="&MM_ComponentVersion=1&skinName=/JP/TECHNOLOGY/SWF/Clear_Skin_1&streamName=' + m + '&autoPlay=true&autoRewind=false" />\
<embed src="/JP/TECHNOLOGY/SWF/FLVPlayer_Progressive.swf" flashvars="&MM_ComponentVersion=1&skinName=/JP/TECHNOLOGY/SWF/Clear_Skin_1&streamName=' + m + '&autoPlay=true&autoRewind=false" quality="high" scale="noscale" width="' + w + '" height="' + h + '" name="FLVPlayer" salign="LT" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\
</object></div>';
        $(this).replaceWith(html);
        return false;
    });
});
