var attributes = new Array();
var viewInfo = new Array();
var isExternalMag = false;
var baseAttributeDir = "attributes/";
var productDir = "";


/******************************************************************************************
/********************************* LOAD VIEW NAMES *************************************
/******************************************************************************************/
function getViewNames(){

//	pviewFile = baseURL + "customers/" + customerId + "/" + productId + "/" + productId + "_" + displayId + "/pview_" + productId + "_" + displayId + ".xml";
	pviewFile = baseURL + "customers/" + customerId + "/" + productId + "/" + productId + "_" + displayId + "/pview_" + productId + "_" + displayId + ".xml";
	xmldoc = makeSyncRequest(pviewFile);
	
	setIsInternalMag(xmldoc);
	//populateViewNames(xmldoc);
	populateViewAndVariationNames(xmldoc);
}

function setIsInternalMag(xmldoc){
	pviewElement = xmldoc.getElementsByTagName("product_view")[0];
	
	for(i=0; i < pviewElement.attributes.length; i++){

		if(pviewElement.attributes[i].name == "viewer_xml_path"){
			isTempateExternal(pviewElement.attributes[i].value);
			return;
		}
	}
}

//this assumes the person is using a standard template. This is a big assumption
function isTempateExternal(templatePath){
	templatePath = baseURL + "standard/v2/" + templatePath.substring(3);	

	templatexmldoc = makeSyncRequest(templatePath);
	imageViewerNode = templatexmldoc.getElementsByTagName("zoom_image_viewer");
	
	if(imageViewerNode == null || imageViewerNode.length == 0){
		return;
	}
	
	for(i=0; i < imageViewerNode[0].attributes.length; i++){
		if(imageViewerNode[0].attributes[i].name == "viewer_type"){
			if(imageViewerNode[0].attributes[i].value == "external"){
				isExternalMag = true;
			}
			else{
				return;
			}
		}
	}
}

function populateViewAndVariationNames( xmldoc ){
	categoryGroups = xmldoc.getElementsByTagName("category_group");	

	for(i=0; i < categoryGroups.length; i++){

		if(categoryGroups[i].attributes[0].value == "VIEW"){
			viewCategories = categoryGroups[i].getElementsByTagName("category");

			for(j=0; j < viewCategories.length; j++){
				var viewObject = new Object();

				for(k=0; k < viewCategories[j].attributes.length; k++){
					if(viewCategories[j].attributes[k].name == "inventory_id"){
						viewObject.inventoryId = viewCategories[j].attributes[k].value;
					}
					else if(viewCategories[j].attributes[k].name == "name"){
						viewObject.name = viewCategories[j].attributes[k].value;
					}
				}
				viewInfo[j] = viewObject;
			}		
		}

		if(categoryGroups[i].attributes[0].value == "VARIATION"){
			varCategories = categoryGroups[i].getElementsByTagName("category");

			for(j=0; j < varCategories[0].attributes.length; j++){
				if(varCategories[0].attributes[j].name == "inventory_id")
					varName = varCategories[0].attributes[j].value;
			}
		}
	}
}



/******************************************************************************************
/********************************* LOAD COLOR OPTIONS  **********************************
/******************************************************************************************/
function getColorOptions(productName){
	productDir = baseURL + productName + "/" + baseAttributeDir;
	xmldoc = makeSyncRequest(productDir + "attributeList.xml");
	populateLayerNames(xmldoc);
	populateColorOptions();
}

function populateLayerNames( xmldoc ){
	attributeNodes = xmldoc.getElementsByTagName("attribute");	

	for(i=0; i < attributeNodes.length; i++){
		var layerObject = new Object();
				
		for(j=0; j < attributeNodes[i].attributes.length; j++){
			switch(attributeNodes[i].attributes[j].name){
				case "name":
					layerObject.name = attributeNodes[i].attributes[j].value;
					break
				case "layername":
					layerObject.layername = attributeNodes[i].attributes[j].value;
					break;

			}
		}
		attributes[layerObject.layername] = layerObject;
	}
}

function populateColorOptions(){

	for (var layer in attributes){
		colorxmldoc = makeSyncRequest(productDir + layer + ".xml");
		getColors(colorxmldoc, attributes[layer].layername);
	}
}

function getColors( colorxmldoc, layername ){
	colorNodes = colorxmldoc.getElementsByTagName("color");

	var colorArray = new Array();
	for(i=0; i < colorNodes.length; i++){
		var colorObject = new Object();
		
		for(j=0; j < colorNodes[i].attributes.length; j++){

			switch(colorNodes[i].attributes[j].name){
				case "hex":
					colorObject.hex = colorNodes[i].attributes[j].value;
					break
				case "name":
					colorObject.name = colorNodes[i].attributes[j].value;
					break;
			}
		}
		colorArray[i] = colorObject;
	}
	attributes[layername].colorArray  = colorArray;
}



/******************************************************************************************
/******************************* XML REQUEST FUNCTIONS ********************************
/******************************************************************************************/
function makeSyncRequest(url){
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {
				alert("IE Request failure");
			}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.open('GET', url, false);
	http_request.send(null);

	if(http_request.status == 200) {
		var xmldoc = http_request.responseXML;
		return xmldoc;
	}
	else{
		alert("ERROR: TROUBLE WITH THE REQUEST: " + url);
	}

}
