var type = '';
var productType = '';
var filters = new Array();

function setProductType(pt) {
	productType = pt;
	
	/*
	if (pt != 'All Products') {
		var ind = filters.indexOf(pt);
		
		// Add the filter
		if (ind == -1) {
			filters.push(pt);
		}
		// Remove the filter
		else {
			filters.splice(ind, 1);
		}
	}
	// Reset the filter
	else {
		filters = new Array();
	}
	*/
	
	filters = new Array();
	
	if (pt != 'All Products') {
		filters.push(pt);
	}
	
	document.getElementById('products').innerHTML = '';
	importXML('product');
}

function getProductType() {
	return productType;
}

function importXML_original(xmlFile) {
	type = xmlFile;
	if (xmlFile.substring(0, 8) == 'featured') {
		type = 'featured';
	}
	
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = createTable;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) createTable()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	
	/*
	if (xmlFile == 'contact') {
		xmlDoc.load("./xml/contact.xml");
	}
	else if (xmlFile == 'product') {
		xmlDoc.load("./xml/product.xml");
	}
	else if (xmlFile == 'featured') {
		xmlDoc.load("./xml/featured.xml");
	}
	*/
	
	if (xmlFile != '') {
		xmlDoc.load("./xml/"+xmlFile+".xml");
	}
	else {
		alert('You do not have the right xml file.');
		return;
	}
}

function importXML(xmlFile) {
	type = xmlFile;
	if (xmlFile.substring(0, 8) == 'featured') {
		type = 'featured';
	}
	
	if (document.implementation && document.implementation.createDocument)
	{
		//xmlDoc = document.implementation.createDocument("", "", null);
		// xmlDoc.onload = createTable;
		
		xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange=function() {
  			if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    			xmlDoc = xmlhttp.responseXML;
    			createTable();
    		}
  		}
		if (xmlFile != '') {
			xmlhttp.open("GET","./xml/"+xmlFile+".xml",true);
			xmlhttp.send();
		}
		else {
			alert('You do not have the right xml file.');
			return;
		}
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) createTable()
		};
		if (xmlFile != '') {
			xmlDoc.load("./xml/"+xmlFile+".xml");
		}
		else {
			alert('You do not have the right xml file.');
			return;
		}
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	
	/*
	if (xmlFile == 'contact') {
		xmlDoc.load("./xml/contact.xml");
	}
	else if (xmlFile == 'product') {
		xmlDoc.load("./xml/product.xml");
	}
	else if (xmlFile == 'featured') {
		xmlDoc.load("./xml/featured.xml");
	}
	*/
	/*
	if (xmlFile != '') {
		xmlDoc.load("./xml/"+xmlFile+".xml");
	}
	else {
		alert('You do not have the right xml file.');
		return;
	}
	*/
}

function createTable(){	
	if (type == 'contact') {
		// Left Side
		var temp = xmlDoc.getElementsByTagName('page_title');
		document.getElementById('intro_title').innerHTML = temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('page_content');
		document.getElementById('intro_content').innerHTML = temp[0].firstChild.nodeValue;
		
		// Contacts
		temp = xmlDoc.getElementsByTagName('contact');
		
		// Go through all the contacts
		for (i = 0; i < temp.length; i++) {
			var mainContainer = document.createElement('DIV');
			mainContainer.setAttribute('class', 'contact');
			
			// Go through the info in each contact
			for (j = 0; j < temp[i].childNodes.length; j++) {
				if (temp[i].childNodes[j].nodeType != 1) continue;
				
				var container = document.createElement('DIV');
				var theData = document.createTextNode('');
				
				if  (temp[i].childNodes[j].nodeName == 'name') {
					container.setAttribute('class', 'contactName');
					theData = document.createTextNode(temp[i].childNodes[j].firstChild.nodeValue);
				}
				else if  (temp[i].childNodes[j].nodeName == 'title') {
					container.setAttribute('class', 'contactTitle');
					theData = document.createTextNode(temp[i].childNodes[j].firstChild.nodeValue);					
				}
				else if  (temp[i].childNodes[j].nodeName == 'email') {
					container.setAttribute('class', 'contactEmail');
					
					if (temp[i].childNodes[j].firstChild.nodeValue != '') {
						container.innerHTML = 'Email: <a href="mailto:' + temp[i].childNodes[j].firstChild.nodeValue + '">' + temp[i].childNodes[j].firstChild.nodeValue + '</a>';
					}
				}

				if (temp[i].childNodes[j].nodeName != 'email') {
					container.appendChild(theData);
				}
				
				mainContainer.appendChild(container);
			}
			
			document.getElementById('contacts').appendChild(mainContainer);
		}

		// Right Side
		temp = xmlDoc.getElementsByTagName('side_title');
		document.getElementById('contact_title').innerHTML = temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('side_street');
		document.getElementById('contact_address').innerHTML = temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('side_city');
		var temp2 = xmlDoc.getElementsByTagName('side_province');
		document.getElementById('contact_cityCountry').innerHTML = temp[0].firstChild.nodeValue + ', ' + temp2[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('side_postal');
		document.getElementById('contact_postal').innerHTML = temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('side_phone');
		document.getElementById('contact_phone').innerHTML = 'Phone: ' + temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('side_fax');
		document.getElementById('contact_fax').innerHTML = 'Fax: ' + temp[0].firstChild.nodeValue;
	}
	else if (type == 'product') {
		// Left Side
		var temp = xmlDoc.getElementsByTagName('page_title');
		document.getElementById('intro_title').innerHTML = temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('page_content');
		document.getElementById('intro_content').innerHTML = temp[0].firstChild.nodeValue;
		
		// Products
		temp = xmlDoc.getElementsByTagName('product');
		
		var productArray = new Array();
		
		// Go through all the product
		for (i = 0; i < temp.length; i++) {
			var prod = new Object();
			prod.name = "";
			prod.image = "";
			prod.desc = "";
			prod.download = "";
			prod.website = "";
			prod.types = null;
			
			var thisTypes = new Array();
			
			// Go through the info in each product
			for (j = 0; j < temp[i].childNodes.length; j++) {
				if (temp[i].childNodes[j].nodeType != 1) continue;
				
				if  (temp[i].childNodes[j].nodeName == 'name') {
					prod.name = temp[i].childNodes[j].firstChild.nodeValue;
				}
				else if  (temp[i].childNodes[j].nodeName == 'image') {
					prod.image = temp[i].childNodes[j].firstChild.nodeValue;
				}
				else if  (temp[i].childNodes[j].nodeName == 'desc') {
					prod.desc = temp[i].childNodes[j].firstChild.nodeValue;
				}
				else if  (temp[i].childNodes[j].nodeName == 'download') {
					prod.download = temp[i].childNodes[j].firstChild.nodeValue;
				}
				else if  (temp[i].childNodes[j].nodeName == 'website') {
					prod.website = temp[i].childNodes[j].firstChild.nodeValue;
				}
				else if (temp[i].childNodes[j].nodeName == 'types') {
					for (k = 0; k < temp[i].childNodes[j].childNodes.length;k++) {
						if (temp[i].childNodes[j].childNodes[k].nodeName == 'type') {
							if (temp[i].childNodes[j].childNodes[k].firstChild.nodeValue != '') {
								thisTypes.push(temp[i].childNodes[j].childNodes[k].firstChild.nodeValue);
							}
						}
					}
					
					prod.types = thisTypes;
				}
			}
			
			productArray.push(prod);
		}
		
		// Go through all the products
		for (i = 0; i < temp.length; i++) {
			var mainContainer = document.createElement('DIV');
			mainContainer.setAttribute('class', 'product');
			
			var thisType = '';
			var thisTypes = new Array();
			var productName = '';

			var container = document.createElement('DIV');
			var theData = document.createTextNode('');
				
			container.setAttribute('class', 'productName');
			theData = document.createTextNode(productArray[i].name);
			container.appendChild(theData);
			mainContainer.appendChild(container);

			container = document.createElement('DIV');
			theData = document.createTextNode('');
			container.setAttribute('class', 'productImage');
			container.innerHTML = '<a href="' + productArray[i].website + '" id="' + productArray[i].name + '" name="' + productArray[i].name +  '"><img src="' + productArray[i].image + '" alt="product image" class="images"/></a>';
			mainContainer.appendChild(container);

			container = document.createElement('DIV');
			theData = document.createTextNode('');
			container.setAttribute('class', 'productDesc');
			theData = document.createTextNode(productArray[i].desc);
			container.appendChild(theData);
			mainContainer.appendChild(container);
			
			/*
			container = document.createElement('DIV');
			theData = document.createTextNode('');
			container.setAttribute('class', 'productLink');
			container.innerHTML = 'Download <a href="' + productArray[i].download + '" target="_blank"><img src="./imgs/download.png" alt="download button" class="images"/></a>';
			mainContainer.appendChild(container);
			*/
			
			container = document.createElement('DIV');
			theData = document.createTextNode('');
			container.setAttribute('class', 'productLink');
			container.innerHTML = 'Website <a href="' + productArray[i].website + '" target="_blank"><img src="./imgs/website.png" alt="website button" class="images"/></a>';
			mainContainer.appendChild(container);
			
			
            
            //newly added div for hidden types of product used for filtering in jquery
            container = document.createElement('DIV');
            theData = document.createTextNode('');
            container.setAttribute('class', 'producttypes');
            container.setAttribute('style', 'display:none');   
            theData = document.createTextNode(productArray[i].types);
            container.appendChild(theData);
            mainContainer.appendChild(container);
			// filtering by product types
			// All Products
			if (filters.length == 0 || filters[0] == '') {
				document.getElementById('products').appendChild(mainContainer);
			}
			// Selected Product Types
			else {
				for (m = 0; m < filters.length; m++) {
					if (productArray[i].types.indexOf(filters[m]) != -1) {
						document.getElementById('products').appendChild(mainContainer);
						break;
					}
				}
			}
		}


		// Right Side
		temp = xmlDoc.getElementsByTagName('side_title');
		document.getElementById('product_title').innerHTML = temp[0].firstChild.nodeValue;
		
		// Product Types
		document.getElementById('product_types').innerHTML = '';
		temp = xmlDoc.getElementsByTagName('product_type');
		
		for (i = 0; i < temp.length; i++) {		
			
			var container = document.createElement('DIV');
			container.setAttribute('class', 'product_types');
            //newly added code
			if (filters.indexOf(temp[i].firstChild.nodeValue) != -1 || (temp[i].firstChild.nodeValue == 'All Products' && filters.length == 0)) {
                container.innerHTML = '<a alt="'+temp[i].firstChild.nodeValue+'" class="productTypeFilter active" href="javascript:void(0);"><img src="./imgs/arrow.png" alt="arrow" class="images"/>&nbsp;&nbsp;<span  >' + temp[i].firstChild.nodeValue + '</span></a>';
            }
            else {
                container.innerHTML = '<a alt="'+temp[i].firstChild.nodeValue+'" class="productTypeFilter" href="javascript:void(0);"><img src="./imgs/arrow.png" alt="arrow" class="images"/>&nbsp;&nbsp;<span>' + temp[i].firstChild.nodeValue + '</span></a>';
            }
            
            /*old code
			if (filters.indexOf(temp[i].firstChild.nodeValue) != -1 || (temp[i].firstChild.nodeValue == 'All Products' && filters.length == 0)) {
				container.innerHTML = '<a href="javascript:setProductType(' +  "'"  + temp[i].firstChild.nodeValue + "'" + ');" id="productTypeFilter" class="active"><img src="./imgs/arrow.png" alt="arrow" class="images"/>&nbsp;&nbsp;<span  >' + temp[i].firstChild.nodeValue + '</span></a>';
			}
			else {
				container.innerHTML = '<a href="javascript:setProductType(' +  "'"  + temp[i].firstChild.nodeValue + "'" + ');" id="productTypeFilter"><img src="./imgs/arrow.png" alt="arrow" class="images"/>&nbsp;&nbsp;<span>' + temp[i].firstChild.nodeValue + '</span></a>';
			}*/
			
			/*
			container.innerHTML = '<img src="./imgs/arrow.png" alt="arrow" class="images"/>';
			container.innerHTML += '&nbsp;&nbsp;';
			
			if (filters.indexOf(temp[i].firstChild.nodeValue) != -1 || (temp[i].firstChild.nodeValue == 'All Products' && filters.length == 0)) {
				container.innerHTML += '<input type="button" class="button" value="' + temp[i].firstChild.nodeValue + '" onClick="javascript:setProductType(' + "'" + temp[i].firstChild.nodeValue + "'" + ');" style="text-decoration:underline;color:#0000FF;"/>';
			}
			else {
				container.innerHTML += '<input type="button" class="button" value="' + temp[i].firstChild.nodeValue + '" onClick="javascript:setProductType(' + "'" + temp[i].firstChild.nodeValue + "'" + ');"/>';
			}
			*/
			
			document.getElementById('product_types').appendChild(container);
		}
	}
	else if (type == 'featured') {
		// Left Side
		var temp = xmlDoc.getElementsByTagName('page_title');
		document.getElementById('featured_main_title').innerHTML = temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('page_banner');
		document.getElementById('featured_banner').innerHTML = '<img src="' + temp[0].firstChild.nodeValue + '" alt="banner"/>';
		
		temp = xmlDoc.getElementsByTagName('page_content');
		document.getElementById('featured_content').innerHTML = temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('page_image');
		document.getElementById('featured_image').innerHTML = '<img src="' + temp[0].firstChild.nodeValue + '" alt="image"/>';
		
		// Features		
		// Right Side
		temp = xmlDoc.getElementsByTagName('side_title');
		document.getElementById('featured_side_title').innerHTML = temp[0].firstChild.nodeValue;
		
		temp = xmlDoc.getElementsByTagName('feature');
		var newTable = document.createElement('TABLE');
		
		var tmp = document.createElement('TBODY');
		newTable.appendChild(tmp);

		for (i = 0; i < temp.length; i++)
		{
			var row = document.createElement('TR');
			
			if (i % 2 == 0) {
				row.setAttribute('class', 'rowDark');
			}
			else {
				row.setAttribute('class', 'rowLight');
			}
			
			for (j = 0; j < temp[i].childNodes.length; j++)
			{
				if (temp[i].childNodes[j].nodeType != 1) continue;
				var container = document.createElement('TD');
				
				if (i == 0) {
					container.setAttribute('class', 'tableHeader');
				}
				else {
					container.setAttribute('class', 'tableCell');
				}
				
				var theData = document.createTextNode(temp[i].childNodes[j].firstChild.nodeValue);
				container.appendChild(theData);
				row.appendChild(container);
			}
			tmp.appendChild(row);
		}
		
		if (document.getElementById('features').lastChild) {
			document.getElementById('features').removeChild(document.getElementById('features').lastChild);
		}
		document.getElementById('features').appendChild(newTable);

		
		// Featured Side
		temp = xmlDoc.getElementsByTagName('side');
		var sideContainer = document.createElement('DIV');
		
		// Go through all the featured on the side
		for (i = 0; i < temp.length; i++) {
			var mainContainer = document.createElement('DIV');
			mainContainer.setAttribute('class', 'featured_side');
			
			var featuredLink = "";
			
			// Find the link
			for (j = 0; j < temp[i].childNodes.length; j++) {
				if (temp[i].childNodes[j].nodeType != 1) continue;
								
				if  (temp[i].childNodes[j].nodeName == 'link') {
					featuredLink = temp[i].childNodes[j].firstChild.nodeValue;
					break;
				}
			}
			
			
			// Go through the info in each featured
			for (j = 0; j < temp[i].childNodes.length; j++) {
				if (temp[i].childNodes[j].nodeType != 1) continue;
				
				var container = document.createElement('DIV');
				var theData = document.createTextNode('');
				
				if  (temp[i].childNodes[j].nodeName == 'image') {
					container.setAttribute('class', 'featured_side_image');
					container.innerHTML = '<a href="' + featuredLink + '" ><img src="' + temp[i].childNodes[j].firstChild.nodeValue + '" atl="featured product image" class="images" /></a>';
				}
				else if  (temp[i].childNodes[j].nodeName == 'text') {
					container.setAttribute('class', 'featured_side_text');
					container.innerHTML = '<a href="' + featuredLink + '" >' + temp[i].childNodes[j].firstChild.nodeValue + "</a>";
//					theData = document.createTextNode(temp[i].childNodes[j].firstChild.nodeValue);
//					container.appendChild(theData);
				}
				
				mainContainer.appendChild(container);
			}
			
			sideContainer.appendChild(mainContainer);
		}
		
		if (document.getElementById('featured_products').lastChild) {
			document.getElementById('featured_products').removeChild(document.getElementById('featured_products').lastChild);
		}
		document.getElementById('featured_products').appendChild(sideContainer);
	}
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );

}

function setHeight() {
	var inBrowserHeight = getDocHeight();
	return inBrowserHeight;
}


//newly added code for filtering using jquery
var $j = jQuery.noConflict();
$j(document).ready(function (){
   $j('.productTypeFilter').live('click',function(){
        var pt = $j(this).attr('alt');
        $j('.productTypeFilter').removeClass('active');
        $j(this).addClass('active');
        jQuery.expr[':'].Contains = function(a,i,m){
                return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
            };
       if(pt !== 'All Products'){    
            $j("#products").find("div.product:Contains(" + pt + ")").show();
            $j("#products").find("div.product:not(:Contains(" + pt + "))").hide();
       }else{
            $j("#products").find("div.product").show();    
       }
    });
});
