// JavaScript Document

//declare data
var data1=[
		{ChemClass:"Alkane", Group:"Alkyl", Formula:"RH", Prefix:"alkyl-", Suffix:"-ane", Example:"Ethane"},
		{ChemClass:"Alkene", Group:"Alkenyl", Formula:"R<sub>2</sub>C=CR<sub>2</sub>", Prefix:"alkenyl-", Suffix:"-ene", Example:"Ethylene<br><em>(Ethene)</em>"},
		{ChemClass:"Alkyne", Group:"Alkynyl", Formula:"RC≡CR'", Prefix:"alkynyl-", Suffix:"-yne", Example:"Acetylene<br><em>(Ethyne)</em>"},
		{ChemClass:"Benzene derivative", Group:"Phenyl", Formula:"RC<sub>6</sub>H<sub>5</sub> or RPh", Prefix:"phenyl-", Suffix:"-benzene", Example:"Cumene<br><em>(2-phenylpropane)</em>"},
		{ChemClass:"Toluene derivative", Group:"Benzyl", Formula:"RCH<sub>2</sub>C<sub>6</sub>H<sub>5</sub> or RBn", Prefix:"benzyl-", Suffix:"1-(substituent)toluene", Example:"Benzyl bromide<br><em>(1-Bromotoluene)</em>"}
		];

var data2=[
		{ChemClass:"haloalkane", Group:"halo", Formula:"RX", Prefix:"halo-", Suffix:"alkyl <b>halide</b>", Example:"Chloroethane<br><em>(Ethyl chloride)</em>"},
		{ChemClass:"fluoroalkane", Group:"fluoro", Formula:"RF", Prefix:"fluoro-", Suffix:"alkyl <b>fluoride</b>", Example:"Fluoromethane<br><em>(Methyl fluoride)</em>"},
		{ChemClass:"chloroalkane", Group:"chloro", Formula:"RCl", Prefix:"chloro-", Suffix:"alkyl <b>chloride</b>", Example:"Chloromethane<br><em>(Methyl chloride)</em>"},
		{ChemClass:"bromoalkane", Group:"bromo", Formula:"RBr", Prefix:"bromo-", Suffix:"alkyl <b>bromide</b>", Example:"Bromomethane<br><em>(Methyl bromide)</em>"},
		{ChemClass:"iodoalkane", Group:"iodo", Formula:"RI", Prefix:"iodo-", Suffix:"alkyl <b>iodide</b>", Example:"Iodomethane<br><em>(Methyl iodide)</em>"}
		];

var data3=[
		{ChemClass:"Acyl halide", Group:"Haloformyl", Formula:"RCOX", Prefix:"haloformyl-", Suffix:"-oyl halide", Example:"Acetyl chloride<br><em>(Ethanoyl chloride)</em>"},
		{ChemClass:"Alcohol", Group:"Hydroxyl", Formula:"ROH", Prefix:"hydroxy-", Suffix:"-ol", Example:"Methanol"},
		{ChemClass:"Ketone", Group:"Carbonyl", Formula:"RCOR'", Prefix:"keto-, oxo-", Suffix:"-one", Example:"Methyl ethyl ketone<br><em>(Butanone)</em>"},
		{ChemClass:"Aldehyde", Group:"Aldehyde", Formula:"RCHO", Prefix:"aldo-", Suffix:"-al", Example:"Acetaldehyde<br><em>(Ethanal)</em>"},
		{ChemClass:"Carbonate", Group:"Carbonate ester", Formula:"ROCOOR", Prefix:"", Suffix:"alkyl <b>carbonate</b>", Example:""},
		{ChemClass:"Carboxylate", Group:"Carboxylate", Formula:"RCOO<sup>−</sup>", Prefix:"carboxy-", Suffix:"-oate", Example:"Sodium acetate<br><em>(Sodium ethanoate)</em>"},
		{ChemClass:"Carboxylic acid", Group:"Carboxyl", Formula:"RCOOH", Prefix:"carboxy-", Suffix:"-oic acid", Example:"Acetic acid<br><em>(Ethanoic acid)</em>"},
		{ChemClass:"Ether", Group:"Ether", Formula:"ROR'", Prefix:"alkoxy-", Suffix:"alkyl alkyl <b>ether</b>", Example:"Diethyl ether<br><em>(Ethoxyethane)</em>"},
		{ChemClass:"Ester", Group:"Ester", Formula:"RCOOR'", Prefix:"", Suffix:"alkyl alkan<b>oate</b>", Example:"Ethyl butyrate<br><em>(Ethyl butanoate)</em>"},
		{ChemClass:"Hydroperoxide", Group:"Hydroperoxy", Formula:"ROOH", Prefix:"hydroperoxy-", Suffix:"alkyl <b>hydroperoxide</b>", Example:"Methyl ethyl ketone peroxide"},
		{ChemClass:"Peroxide", Group:"Peroxy", Formula:"ROOR", Prefix:"peroxy-", Suffix:"alkyl <b>peroxide</b>", Example:"Di-tert-butyl peroxide"}
		];

var data4=[
		{ChemClass:"Amide", Group:"Carboxamide", Formula:"RCONR<sub>2</sub>", Prefix:"carboxamido-", Suffix:"-amide", Example:"Acetamide<br><em>(Ethanamide)</em>"},
		{ChemClass:"Amines", Group:"Primary amine", Formula:"RNH<sub>2</sub>", Prefix:"amino-", Suffix:"-amine", Example:"Methylamine<br><em>(Methanamine)</em>"},
		{ChemClass:"Amines", Group:"Secondary amine", Formula:"R<sub>2</sub>NH", Prefix:"amino-", Suffix:"-amine", Example:"Dimethylamine"},
		{ChemClass:"Amines", Group:"Tertiary amine", Formula:"R<sub>3</sub>N", Prefix:"amino-", Suffix:"-amine", Example:"Trimethylamine"},
		{ChemClass:"Amines", Group:"4° ammonium ion", Formula:"R<sub>4</sub>N<sup>+</sup>", Prefix:"ammonio-", Suffix:"-ammonium", Example:"Choline"},
		{ChemClass:"Imine", Group:"Primary ketimine", Formula:"RC(=NH)R'", Prefix:"imino-", Suffix:"-imine", Example:""},
		{ChemClass:"Imine", Group:"Secondary ketimine", Formula:"RC(=NR)R'", Prefix:"imino-", Suffix:"-imine", Example:""},
		{ChemClass:"Imine", Group:"Primary aldimine", Formula:"RC(=NH)H", Prefix:"imino-", Suffix:"-imine", Example:""},
		{ChemClass:"Imine", Group:"Secondary aldimine", Formula:"RC(=NR')H", Prefix:"imino-", Suffix:"-imine", Example:""},
		{ChemClass:"Imide", Group:"Imide", Formula:"RC(=O)NC(=O)R'", Prefix:"imido-", Suffix:"-imide", Example:""},
		{ChemClass:"Azide", Group:"Azide", Formula:"RN<sub>3</sub>", Prefix:"azido-", Suffix:"alkyl <b>azide</b>", Example:"Phenyl azide<br><em>(Azidobenzene)</em>"},
		{ChemClass:"Azo compound", Group:"Azo (Diimide)", Formula:"RN<sub>2</sub>R'", Prefix:"azo-", Suffix:"-diazene", Example:"Methyl orange<br><em>(p-dimethylamino-azobenzenesulfonic acid)</em>"},
		{ChemClass:"Cyanates", Group:"Cyanate", Formula:"ROCN", Prefix:"cyanato-", Suffix:"alkyl <b>cyanate</b>", Example:"Methyl cyanate"},
		{ChemClass:"Cyanates", Group:"Isocyanide", Formula:"RNC", Prefix:"isocyano-", Suffix:"alkyl <b>isocyanide</b>", Example:""},
		{ChemClass:"Isocyanates", Group:"Isocyanate", Formula:"RNCO", Prefix:"isocyanato-", Suffix:"alkyl <b>isocyanate</b>", Example:"Methyl isocyanate"},
		{ChemClass:"Isocyanates", Group:"Isothiocyanate", Formula:"RNCS", Prefix:"isothiocyanato-", Suffix:"alkyl <b>isothiocyanate</b>", Example:"Allyl isothiocyanate"},
		{ChemClass:"Nitrate", Group:"Nitrate", Formula:"RONO<sub>2</sub>", Prefix:"nitrooxy-, nitroxy-", Suffix:"alkyl <b>nitrate</b>", Example:"Amyl nitrate<br><em>(1-nitrooxypentane)"},
		{ChemClass:"Nitrile", Group:"Nitrile", Formula:"RCN", Prefix:"cyano-", Suffix:"alkane<b>nitrile</b>, alkyl <b>cyanide</b>", Example:"Benzonitrile<br><em>(Phenyl cyanide)</em>"},
		{ChemClass:"Nitrite", Group:"Nitrosooxy", Formula:"RONO", Prefix:"nitrosooxy-", Suffix:"alkyl <b>nitrite</b>", Example:"Isoamyl nitrite<br><em>(3-methyl-1-nitrosooxybutane)</em>"},
		{ChemClass:"Nitro compound", Group:"Nitro", Formula:"RNO<sub>2</sub>", Prefix:"nitro-", Suffix:"", Example:"Nitromethane"},
		{ChemClass:"Nitroso compound", Group:"Nitroso", Formula:"RNO", Prefix:"nitroso-", Suffix:"", Example:"Nitrosobenzene"},
		{ChemClass:"Pyridine derivative", Group:"Pyridyl", Formula:"RC<sub>5</sub>H<sub>4</sub>N", Prefix:"4-pyridyl (pyridin-4-yl)|3-pyridyl (pyridin-3-yl)|2-pyridyl (pyridin-2-yl)", Suffix:"-pyridine", Example:"Nicotine"}
		];

var data5=[
		{ChemClass:"Phosphine", Group:"Phosphino", Formula:"R<sub>3</sub>P", Prefix:"phosphino-", Suffix:"-phosphane", Example:"Methylpropylphosphane"},
		{ChemClass:"Phosphodiester", Group:"Phosphate", Formula:"HOPO(OR)<sub>2</sub>", Prefix:"phosphoric acid di(substituent) ester", Suffix:"di(substituent) hydrogenphosphate", Example:"DNA"},
		{ChemClass:"Phosphonic acid", Group:"Phosphono", Formula:"RP(=O)(OH)<sub>2</sub>", Prefix:"phosphono-", Suffix:"substituent phosphonic acid", Example:"Benzylphosphonic acid"},
		{ChemClass:"Phosphate", Group:"Phosphate", Formula:"ROP(=O)(OH)<sub>2</sub>", Prefix:"phospho-", Suffix:"", Example:"Glyceraldehyde 3-phosphate"},
		{ChemClass:"Sulfide or thioether", Group:"Sulfide or thioether", Formula:"RSR'", Prefix:"", Suffix:"di(substituent) sulfide", Example:"Dimethyl sulfide"},
		{ChemClass:"Sulfone", Group:"Sulfonyl", Formula:"RSO<sub>2</sub>R'", Prefix:"sulfonyl-", Suffix:"di(substituent) sulfone", Example:"Dimethyl sulfone<br><em>(Methylsulfonylmethane)</em>"},
		{ChemClass:"Sulfonic acid", Group:"Sulfo", Formula:"RSO<sub>3</sub>H", Prefix:"sulfo-", Suffix:"substituent <b>sulfonic acid</b>", Example:"Benzenesulfonic acid"},
		{ChemClass:"Sulfoxide", Group:"Sulfinyl", Formula:"RSOR'", Prefix:"sulfinyl-", Suffix:"di(substituent) <b>sulfoxide</b>", Example:"Diphenyl sulfoxide"},
		{ChemClass:"Thiol", Group:"Sulfhydryl", Formula:"RSH", Prefix:"mercapto-, sulfanyl-", Suffix:"-thiol", Example:"Ethanethiol<br><em>(Ethyl mercaptan)</em>"},
		{ChemClass:"Thiocyanate", Group:"Thiocyanate", Formula:"RSCN", Prefix:"thiocyanato-", Suffix:"alkyl <b>thiocyanate</b>", Example:""},
		{ChemClass:"Disulfide", Group:"Disulfide", Formula:"RSSR'", Prefix:"", Suffix:"alkyl alkyl disulfide", Example:"Diphenyl disulfide<br><em>1,2-diphenyldisulfane</em>"}
		];

var fg=[
		{gp:"Hydrocarbons", cnt:"Functional groups that vary based upon the number and order of π bonds impart different chemistry. Each listing below contains C-H bonds, but each one differs in type (and scope) of reactivity."},
		{gp:"Groups containing halogens", cnt:"Haloalkanes are a class of molecule that is defined by a carbon-halogen bond. This bond can be relatively weak (in the case of an iodoalkane) or quite stable (as in the case of a fluoroalkane)."},
		{gp:"Groups containing oxygen", cnt:"Compounds that contain C-O bonds each possess differing reactivity based upon the location and hybridization of the C-O bond, owing to the electron-withdrawing effect of sp<sup>2</sup> hybridized oxygen and the donating effects of sp<sup>3</sup> hybridized oxygen."},
		{gp:"Groups containing nitrogen", cnt:"Compounds that contain Nitrogen in this category may contain C-O bonds, such as in the case of amides."},
		{gp:"Groups containing phosphorus &amp; sulfur", cnt:"Compounds that contain sulfur and phosphorus exhibit unique chemistry due to their ability to form more bonds than nitrogen and oxygen, their lighter analogues on the periodic table."}
		];

//declare HTML objects
var btn_back = document.getElementById('btn_back');
var dot1 = document.getElementById('dot1');
var dot2 = document.getElementById('dot2');
var dot3 = document.getElementById('dot3');
var cnt1 = document.getElementById('cnt1');
var cnt2 = document.getElementById('cnt2');
var list = document.getElementById('list');
var descr = document.getElementById('descr');
var title = document.getElementById('title');
var details = document.getElementById('details');
var ads = document.getElementById('ads');

//declare public variables
var cate ="";

//initiate the interface
function ini(){
	btn_back.style.visibility = 'hidden';
	btn_back.setAttribute("onclick", "");
	dot1.style.backgroundColor = '#ffffff';
	dot2.style.backgroundColor = '#336699';
	dot3.style.backgroundColor = '#336699';
	cnt1.style.visibility = 'visible';
	cnt2.style.visibility = 'hidden';
	
	cate = "";
	createDescr(descr, 0);
	createList(fg, list, "getSub");
}

//create a new list
function createList(dataSrc, lsObj, func){	
	//remove all items from the list
	lsObj.innerHTML = "";
	
	//add new items
	var itemNum = dataSrc.length;
	for (i = 0; i < itemNum; i++){
		var lsitem = document.createElement("div");
		if (i == 0){
			lsitem.setAttribute("class", "listtop");
		}else if (i == itemNum-1){
			lsitem.setAttribute("class", "listbottom");
		}else{
			lsitem.setAttribute("class", "list");
		}
		lsitem.setAttribute("onclick", func+"('"+String(i+1)+"')");
		
		switch (dataSrc){
			case fg:
				lsitem.innerHTML = fg[i].gp;
				break;
			default:
				lsitem.innerHTML = dataSrc[i].Group;
				break;
		}
		lsObj.appendChild(lsitem);
	}
}

//create category description
function createDescr(obj, id){
	//remove all child nodes from the obj
	obj.innerHTML = "";
	
	//add new description
	var cnt1title = document.createElement("div");
	cnt1title.setAttribute("class", "title");
	var cnt1descr = document.createElement("div");
	cnt1descr.setAttribute("class", "descr");
	if (id != 0){
		cnt1title.innerHTML = fg[id-1].gp;
		cnt1descr.innerHTML = fg[id-1].cnt;
	}else{
		cnt1title.innerHTML = "Functional group";
		cnt1descr.innerHTML = "In organic chemistry, functional groups are specific groups of atoms within molecules that are responsible for the characteristic chemical reactions of those molecules.";
	}
	obj.appendChild(cnt1title);
	obj.appendChild(cnt1descr);
}


//get sub-category list
function getSub(id){
	
	btn_back.style.visibility = "visible";
	btn_back.setAttribute("onclick", "goBack()");
	dot1.style.backgroundColor = '#336699';
	dot2.style.backgroundColor = '#ffffff';
	dot3.style.backgroundColor = '#336699';
	
	cate = id;
	createDescr(descr, Number(id));
	switch (id){
		case "1":
			createList(data1, list, "getDetails");
			break;
		case "2":
			createList(data2, list, "getDetails");
			break;
		case "3":
			createList(data3, list, "getDetails");
			break;
		case "4":
			createList(data4, list, "getDetails");
			break;
		case "5":
			createList(data5, list, "getDetails");
			break;
	}
	
}

//get details of final result
function getDetails(id){
	switch (cate){
		case "1":
			dataSrc = data1;
			break;
		case "2":
			dataSrc = data2;
			break;
		case "3":
			dataSrc = data3;
			break;
		case "4":
			dataSrc = data4;
			break;
		case "5":
			dataSrc = data5;
			break;
	}
	
	title.innerHTML = dataSrc[id-1].Group;
	var tmp = "";
	tmp = "<p><b>Chemical Class:</b> "+dataSrc[id-1].ChemClass+"</p>";
	tmp += "<p><b>Formula: </b>"+dataSrc[id-1].Formula+"</p>";
	tmp += "<p><b>Structural Formula: </b></p>";
	var tmparr = dataSrc[id-1].Prefix.split("|");
	if (tmparr.length == 3){
		tmp += "<p align='center'><img src='img/src/"+cate+String(id-1)+"0.png' /><br> "+tmparr[0]+"<br>";
		tmp += "<p align='center'><img src='img/src/"+cate+String(id-1)+"1.png' /><br> "+tmparr[1]+"<br>";
		tmp += "<p align='center'><img src='img/src/"+cate+String(id-1)+"2.png' /><br> "+tmparr[2]+"</p>";
		tmp += "<p><b>Prefix: </b>"+tmparr.join(", ")+"</p>";
	}else{
		tmp += "<p align='center'><img src='img/src/"+cate+String(id-1)+".png' /></p>";
		if (dataSrc[id-1].Prefix != ""){
			tmp += "<p><b>Prefix: </b>"+dataSrc[id-1].Prefix+"</p>";
		}
	}
	
	if (dataSrc[id-1].Suffix != ""){
		tmp += "<p><b>Suffix: </b>"+dataSrc[id-1].Suffix+"</p>";
	}
	if (dataSrc[id-1].Example != ""){
		tmp += "<p><b>Example: </b></p>";
		tmp += "<p align='center'><img src='img/src/"+cate+String(id-1)+"-0.png' /><br>";
		tmp += dataSrc[id-1].Example+"</p>";
	}
	
	details.innerHTML = tmp;
	if (dataSrc[id-1].Example != ""){
		tmparr = dataSrc[id-1].Example.split("<br>");
		ads.innerHTML = "<a href='http://www.google.com/cse?cx=partner-pub-5042102399833742%3Af5dcn3-8x3c&ie=ISO-8859-1&q="+tmparr[0]+"&sa=Search'  target='_blank'>See more on web ...</a>";
	}else{
		ads.innerHTML = "<a href='http://www.google.com/cse?cx=partner-pub-5042102399833742%3Af5dcn3-8x3c&ie=ISO-8859-1&q=chemical+agent&sa=Search' target='_blank'>See more on web ...</a>";
	}
	
	cnt1.style.visibility = 'hidden';
	cnt2.style.visibility = 'visible';
	dot1.style.backgroundColor = '#336699';
	dot2.style.backgroundColor = '#336699';
	dot3.style.backgroundColor = '#ffffff';
	list.innerHTML = "";
	window.location.href = "#1";
}

//go back button action
function goBack(){
	if (cate != ""){
		if (cnt2.style.visibility == "hidden"){
			ini();
			hideAddress();
		}else if (cnt2.style.visibility == "visible"){
			title.innerHTML = "";
			details.innerHTML = "";
			cnt1.style.visibility = 'visible';
			cnt2.style.visibility = 'hidden';
			dot1.style.backgroundColor = '#336699';
			dot2.style.backgroundColor = '#ffffff';
			dot3.style.backgroundColor = '#336699';
			getSub(cate);
			window.location.href = "#1";
		}
	}
}