var data_array=[]
if(document.URL.indexOf("/en/")==-1){
	var lang="jp";	
}else{
	var lang="en";	
}



// HTTP通信用、共通関数
function createXMLHttpRequest(cbFunc)
{
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
	if (XMLhttpObject) XMLhttpObject.onreadystatechange = cbFunc;
	return XMLhttpObject;
}

// document.getElementById
function $(tagId){
	return document.getElementById(tagId);
}




function loadXMLFile(){
	var fName="/product/index/xml/product.xml"
	httpObj = createXMLHttpRequest(data_init);
	if (httpObj){
		httpObj.open("GET",fName,true);
		httpObj.send(null);
	}
	//document.location.href ="#"+fName.split(".")[0];
}
function data_init(){
	if ((httpObj.readyState == 4) && (httpObj.status == 200)){
		xmlData = httpObj.responseXML;
		itemListTags = xmlData.getElementsByTagName("item");
		office_product_ListTags = xmlData.getElementsByTagName("office_product");
		home_furniture_ListTags = xmlData.getElementsByTagName("home_furniture");
		nameListTags = xmlData.getElementsByTagName("name")
		imgListTags = xmlData.getElementsByTagName("img");
		
		linkListTags = xmlData.getElementsByTagName("link");
		//linkJpListTags = xmlData.getElementsByTagName("link_jp");
		//linkEnListTags = xmlData.getElementsByTagName("link_en");
		categoryListTags = xmlData.getElementsByTagName("category");
		designerListTags = xmlData.getElementsByTagName("designer");
		
		var itemLen = itemListTags.length; 
		//alert(linkJpListTags[0].childNodes[0].nodeValue);
		for(i=0; i<itemLen; i++){
			var data_obj={};
			name = nameListTags[i].childNodes[0].nodeValue;
			img=imgListTags[i].childNodes[0].nodeValue;
			office_product= office_product_ListTags[i].childNodes[0].nodeValue;
			home_furniture= home_furniture_ListTags[i].childNodes[0].nodeValue;
			
			link=linkListTags[i].childNodes[0].nodeValue;
			
			//link_jp=linkJPListTags[i].childNodes[0].nodeValue;
			//link_en=linkENListTags[i].childNodes[0].nodeValue;
			category=categoryListTags[i].childNodes[0].nodeValue;
			designer=designerListTags[i].childNodes[0].nodeValue;
			
			if(office_product=="1"){
				product_type="office product"
			}else if(home_furniture=="1"){
				product_type="home furniture"
			}
			data_obj.product_type=product_type;
			
			data_obj.office_product=office_product;
			data_obj.home_furniture=home_furniture;
			
			data_obj.name=name;
			data_obj.img=img;

			data_obj.link=link;
			data_obj.category=category;
			data_obj.designer=designer;
			
			data_array.push(data_obj);
			
		}
		//
		var url=document.URL
		
		if(url.indexOf("#?")!=-1){
			var view_str=url.split("#?view=")[1].split("&")[0]
			var sort_str=url.split("&sort=")[1]
		}
	
	    
		if(view_str!="all" && view_str!="office" && view_str!="home"){
			view_str="all"
		}
		if(sort_str!="category" && sort_str!="designer"){
			sort_str="category"
		}
		
		//alert(view_str+", "+sort_str)
		layout(sort_str,view_str)
	}else{
		$("result").innerHTML = "Loading...";
	}
}


function layout(theType,the_product_type){
	item_num=0
	//alert(theType+","+the_product_type)
	
	document.location.hash ="?view="+the_product_type+"&sort="+theType
	//document.location.hash ="a1"
	
	resultText = "";
	pre_h2_str="";
	
	var h2_count=0
	
	menuText = "<ul>";
	$("menu_category").innerHTML = "";	
	$("menu_designer").innerHTML = "";	

	var data_array_tmp=sortOn(data_array,theType)
	
	if(theType=="category"){
		$("product_type1").style.fontWeight="bold";
		$("product_type2").style.fontWeight="normal";
	}else if(theType=="designer"){
		$("product_type1").style.fontWeight="normal";
		$("product_type2").style.fontWeight="bold";
	}
	
	
	
	if(the_product_type=="all"){
		var headline1 = "All Product";

	}else if(the_product_type=="office"){
		var headline1 = "Office Product";

	}else if(the_product_type=="home"){
		var headline1="Home Furniture";

	}
	
	$("headline1").innerHTML = headline1
	
	for(i=1; i<=data_array_tmp.length; i++){
		var data_obj=data_array_tmp[i-1]
		
		var product_type=data_obj.product_type
		var office_product=data_obj.office_product;
		var home_furniture=data_obj.home_furniture;
	
		var name=data_obj.name;
		var	img=data_obj.img;
		var link=data_obj.link;
		var h2_str_orginal=data_obj[theType]
		var h2_str=txtConv(data_obj[theType]);
		
		
		/*
		if(product_type!=pre_product_type){
			resultText +="<h2>"+product_type+"</h2>"
		}
		*/
		
		if(lang=="jp"){
			var office_text="ハーマンミラーのプロダクトは、以下のグループに分類されます。人間工学に基づくチェアやデスク、オフィスシステム、そして家庭用のプロダクトまで。そのラインナップを御紹介しましょう。";
			var home_text="チャールズ＆レイ・イームズ、ジョージ・ネルソン、ビル・スタンフ等の著名なデザイナーとのコラボレーションによって生まれた数々のホームファニチャーをご覧いただけます。 スタイリッシュなインテリアとして、また機能的なSOHOスペースをつくり出すファニチャーとして、あなたのスペースクリエイトにお役立てください。";
		}else{
			var office_text="Herman Miller products fall into the following categories. These cover arange from chairs, desks and office systems from leading designers to ergonomic office systems and products for the home.";
			var home_text="Here is a complete collection of home furnishings, created by well-known designers, such as Charles and Ray Eames, George Nelson, and Bill Stumpf. <br /><br />We hope you find something in the collection that will impress you and match your tastes.";
		}

		
		
		if( the_product_type=="all"){
			$("headline1").innerHTML = "All Product";		
			set()
			$("ProductType_pulldown").innerHTML = "<select name='select' id='product_type_pulldown' onChange='product_type_Change()'><option value='all' selected='selected'>All Product</option><option value='office'>Office Product</option><option value='home'>Home Furniture</option></select>";
			
			$("description").innerHTML =office_text;
		
		}else if(the_product_type=="office"){
			
			$("ProductType_pulldown").innerHTML = "<select name='select' id='product_type_pulldown' onChange='product_type_Change()'><option value='all'>All Product</option><option value='office' selected='selected'>Office Product</option><option value='home'>Home Furniture</option></select>";			
			$("description").innerHTML =office_text;
			
			if(office_product=="1"){
				set();
			}
			
		}else if(the_product_type=="home"){
			$("ProductType_pulldown").innerHTML = "<select name='select' id='product_type_pulldown' onChange='product_type_Change()'><option value='all'>All Product</option><option value='office'>Office Product</option><option value='home' selected='selected'>Home Furniture</option></select>";
			$("description").innerHTML =home_text;
			
			if(home_furniture=="1"){
				set();
			}
		}
			
	}

	menuText += "</ul>";


	if(items_flg){
		//resultText +="</div>";
	}
	
	resultText +="<div class='dot_line'><a href='#pagetop' class='pagetop'>↑ page top</a></div>"
	
	$("menu_"+theType).innerHTML = menuText;

	$("result").innerHTML = resultText;		
	
	//alert(resultText);
	
	//document.write(resultText);
	
	nowProductType=the_product_type;	
	nowSortType=theType;	
	
	
	
	
	
	
	function set(){
		if(h2_str!=pre_h2_str){		
			if(pre_h2_str!=""){
				if(items_flg){
					//resultText +="</div>";
				}
				resultText +="<div class='dot_line'><a href='#pagetop' class='pagetop'>↑ page top</a></div>"
				
			}
			items_flg=false;
			item_num=0;
			
			h2_count++;
			
			resultText +="<a name='a"+h2_count+"' id='a"+h2_count+"'></a>"
			if(theType=="category"){
				resultText +="<h2>"+h2_str+"</h2>";
			}else{			
				resultText +="<h2>"+h2_designer_set(h2_str_orginal)+"</h2>";
			}
		
			menuText += "<li><a href='#a"+h2_count+"'>"+h2_str+"</a></li>";
		}
		resultText += resultTextSet(data_obj)
		pre_product_type=product_type;
		pre_h2_str=h2_str;
		
	
	}
	function resultTextSet(theObj){
		item_num++
		
		if(item_num%4==1 ){
			items_flg=true;
			//var resultText=	"<div class='items'>";
			var resultText=	"<div class='clear'></div>";
		}else{
			var resultText="";
		}
		
		
		var img=theObj.img;
		var name=theObj.name;
		var link=theObj.link;
	
		if(link!="null" && !linkCheck(lang,link)){
			//resultText += "<div class='item'><a href='/product/"+link+"'><img src='/product/index/imgs/"+img+"' width='99' height='99' border='0' /><br />"+name +"</a></div>";
			resultText += "<div class='item'><a href='"+link+"'><img src='/product/index/imgs/"+img+"' width='99' height='99' border='0' /><br />"+name +"</a></div>";
		}else{
			resultText += "<div class='item'><img src='/product/index/imgs/"+img+"' width='99' height='99' border='0' /><br />"+name +"</div>";
		}
		
		if(item_num%4==0 && items_flg){
			items_flg=false;
			//resultText+="</div>";		
		}
		
		return resultText
	
	}
	
}



function sortOn(the_data_array,theString){


	if(theString=="category"){
		var sort_array=["Office System","Seating","Accessories","Tables / Desks / Storage"];
	}else if(theString=="designer"){
		//var sort_array=["Charles and Ray Eames","George Nelson","Bill Stumpf and Don Chadwick","Bill Stumpf and Jack Kelley","Studio 7.5","Jerome Caruso","Eric Chan","Jeff Weber","Mark Goetz","Yves Behar","Tim Wallace","Bruce Burdick","Ayse Birsel","Others"];
		var sort_array=["Ayse Birsel","Bill Stumpf and Don Chadwick","Bill Stumpf and Jack Kelley","Bill Stumpf and Jeff Weber","BluDot",,"Bruce Burdick","Charles and Ray Eames","Daniel Korb","Eric Chan","George Nelson","Industrial Facility","Jeff Weber","Jerome Caruso","Kaiju Studios","Marc Fong","Mark Goetz","Studio 7.5","Tim Wallace","Yves Behar","Others"];
	}
	
	var arr=[]
	
	for(var i=1;i<=sort_array.length;i++){
		var sort_st=sort_array[i-1]
		
		for(var j=1;j<=the_data_array.length;j++){
			var data_obj=the_data_array[j-1]
			if(data_obj[theString]==sort_st){
				arr.push(data_obj)
			}
		}
	}
	
	
	//		
	return arr;
}

function txtConv(theString){
	if(theString=="Yves Behar"){
		var str="Yves B&eacute;har"
	}else{
		var str=theString
	}

	return str
}



function order_category(a,b){
  var arg1=a.category
  var arg2=b.category
  if( arg1 < arg2 )return -1;
　if( arg1 == arg2 )return 0;
　if( arg1 > arg2 )return 1;
}


function h2_designer_set(theString){

var h2_designer_array=[
{name:"Ayse Birsel",link:"<a href='../research/ayse_birsel.html'>Ayse Birsel</a>"},
{name:"Bill Stumpf and Don Chadwick",link:"<a href='../research/bill_stumpf.html'>Bill Stumpf</a> and <a href='../research/don_chadwick.html'>Don Chadwick</a>"},
{name:"Bill Stumpf and Jack Kelley",link:"<a href='../research/bill_stumpf.html'>Bill Stumpf</a> and Jack Kelley"},
{name:"Bill Stumpf and Jeff Weber",link:"<a href='../research/bill_stumpf.html'>Bill Stumpf</a> and <a href='../research/jeff_weber.html'>Jeff Weber</a>"},
{name:"BluDot",link:"BluDot"},
{name:"Bruce Burdick",link:"<a href='../research/bruce_burdick.html'>Bruce Burdick</a>"},
{name:"Charles and Ray Eames",link:"<a href='../research/charles_rayEames.html'>Charles and Ray Eames</a>"},
{name:"Daniel Korb",link:"<a href='../research/daniel_korb.html'>Daniel Korb</a>"},
{name:"Eric Chan",link:"<a href='../research/eric_chan.html'>Eric Chan</a>"},
{name:"George Nelson",link:"<a href='../research/george_nelson.html'>George Nelson</a>"},
{name:"Industrial Facility",link:"<a href='../research/industrial_facility.html'>Industrial Facility</a>"},
{name:"Jeff Weber",link:"<a href='../research/jeff_weber.html'>Jeff Weber</a>"},
{name:"Jerome Caruso",link:"<a href='../research/jerome_caruso.html'>Jerome Caruso</a>"},
{name:"Kaiju Studios",link:"<a href='../research/kaiju_studios.html'>Kaiju Studios</a>"},
{name:"Marc Fong",link:"Marc Fong"},
{name:"Mark Goetz",link:"<a href='../research/mark_goetz.html'>Mark Goetz</a>"},
{name:"Studio 7.5",link:"<a href='../research/studio_75.html'>Studio 7.5</a>"},
{name:"Tim Wallace",link:"<a href='../research/tim_wallace.html'>Tim Wallace</a>"},
{name:"Yves Behar",link:"<a href='../research/yves_behar.html'>Yves Behar</a>"},
{name:"Others",link:"Others"}]



/*
		if(theString.indexOf(" and ")!=-1){
			var st1=theString.split(" and ")[0];
			var st2=theString.split(" and ")[1];
			for(var i=1;i<=h2_designer_array.length;i++){
				var h2_designer=h2_designer_array[i-1]
				if(h2_designer.indexOf(st1)!=-1 && h2_designer.indexOf(st2)!=-1){
					break;
				}
			}
		}else{
			var st1=theString
			for(var i=1;i<=h2_designer_array.length;i++){
				var h2_designer=h2_designer_array[i-1]
				if(h2_designer.indexOf(st1)!=-1){
					break;
				}
			}
		}
*/

	
		for(var i=1;i<=h2_designer_array.length;i++){
			var designer_name=h2_designer_array[i-1].name;
			if(theString==designer_name){
				var designer_link=h2_designer_array[i-1].link;
				break;
			}
			
		}
		
		
		return designer_link;
	
}









function product_type_Change(){
	var obj = $("product_type_pulldown")
    var index = obj.selectedIndex;
    var p_type=obj.options[index].value;

	layout(nowSortType,p_type)
}

function linkCheck(theString1,theString2){
	
	var check_jp=[];
	var check_en=["eames_rafondaarmchair.html","eames_folding_table.html"];
	var arr=eval("check_"+theString1);
	//alert(arr);
	for(var i=1;i<=arr.length;i++){
		var link=arr[i-1];
		//alert(link);
		if(link==theString2){
			return true;
		}
	}
	return false;
	
}
