var show = new Array();
show[1] = false;
show[2] = false;
show[3] = false;

function expand(which)
{
    if(!show[which])
    {
	document.getElementById("more" + which).style.display = 'block';
	document.getElementById("pm" + which).innerHTML = "-";
	show[which] = true;
    }
    
    else
    {
	document.getElementById("more" + which).style.display = 'none';
	document.getElementById("pm" + which).innerHTML = "+";
	show[which] = false;
    }
    
}

var currWidth = 0;
var currHeight = 0;
var currThich = 0;
var currProduct = '';

function loadMiniDesigner()
{
    var kw = '';
    var bg = 'url(/images/splash_default.jpg) top left no-repeat';

    kw = GetCookie("last_keyword");

    if(kw != '' && kw != null)
    {
	if(kw.match(/door/gi))
	{
	    document.getElementById('mini_product').selectedIndex = 1;
	    document.getElementById('mini_product').style.display = 'none';
	    document.getElementById('product_label').style.display = 'none';
	    bg = 'url(/images/splash_door.jpg) top left no-repeat';
	    setDynamicText('Door');
	}
	else if(kw.match(/cabinet/gi))
	{
	    document.getElementById('mini_product').selectedIndex = 2;
	    document.getElementById('mini_product').style.display = 'none';
	    document.getElementById('product_label').style.display = 'none';
	    bg = 'url(/images/splash_cabinet.jpg) top left no-repeat';
	    setDynamicText('Cabinet');
	}
	else if(kw.match(/shutter/gi))
	{
	    bg = 'url(/images/splash_shutter.jpg) top left no-repeat';
	    document.getElementById('mini_product').style.display = 'none';
	    document.getElementById('product_label').style.display = 'none';
	    setDynamicText('Shutter');
	}
    }
    
    document.getElementById('landing').style.background = bg;
    updateProduct();
}


function updateProduct()
{
    currProduct = document.getElementById('mini_product').options[document.getElementById('mini_product').selectedIndex].value;

    if(currProduct == 'shutter')
    {
	document.getElementById('mini_height').value = '46';
	document.getElementById('mini_width').value = '12';
	currThick = 1.3125;
	setDynamicText('Shutter');
    }
    
    else if(currProduct == 'door')
    {
	document.getElementById('mini_height').value = '84';
	document.getElementById('mini_width').value = '36';
	currThick = 1.375;
	setDynamicText('Door');
    }
    
    else if(currProduct == 'cabinet')
    {
	document.getElementById('mini_height').value = '30';
	document.getElementById('mini_width').value = '18';
	currThick = .75;
	setDynamicText('Cabinet');
    }
    
    updateDimensions();
}


function updateDimensions()
{
    document.getElementById('mini_height').value = document.getElementById('mini_height').value.replace(/[^\d|\.]/g , "");
    document.getElementById('mini_width').value = document.getElementById('mini_width').value.replace(/[^\d|\.]/g , "");
    currWidth = document.getElementById('mini_width').value;
    currHeight = document.getElementById('mini_height').value;
    updateMiniDesigner();
}



function updateMiniDesigner()
{
    //Update image
    document.getElementById('render').src = "/PANEL/panel.cgi?" + getRenderString();
    
    //Update description
    if(currProduct == 'shutter')
    {
	document.getElementById('mini_desc').innerHTML = 'Antebellum Asheville';
    }
    
    else if(currProduct == 'door')
    {
	document.getElementById('mini_desc').innerHTML = 'Cabana StJohn';
    }
    
    else if(currProduct == 'cabinet')
    {
	document.getElementById('mini_desc').innerHTML = 'Sonoma';
    }
    
    //Update price
    var JSONSpecs = eval("(" + RunCGI("/cgi-bin/NewSpecs.cgi?" + getSpecString(true)) + ")");
    var miniTable = document.getElementById('mini_price_table');

    //Clear price table
    if ( miniTable.rows[0].hasChildNodes() )
	while ( miniTable.rows[0].childNodes.length >= 1 )
	    miniTable.rows[0].removeChild( miniTable.rows[0].firstChild );
    
    if ( miniTable.rows[1].hasChildNodes() )
	while ( miniTable.rows[1].childNodes.length >= 1 )
	    miniTable.rows[1].removeChild( miniTable.rows[1].firstChild );

    //Build new price table
    var num = JSONSpecs.Prices.length;
    if(num > 4) num = 4;
    for(var i = 0; i < num; i++)
    {
	var matTH = document.createElement('th');
	matTH.setAttribute('class','md_mat');
	matTH.appendChild(document.createTextNode(JSONSpecs.Prices[i].Material));
	
	var priTD = document.createElement('td');
	priTD.setAttribute('class','md_pri');
	priTD.appendChild(document.createTextNode("$" + JSONSpecs.Prices[i].Price));

	var cartAnchor = document.createElement('a');
	cartAnchor.setAttribute('href','javascript:miniAddToCart("' + JSONSpecs.Prices[i].Material + '" , "' + JSONSpecs.Prices[i].Price + '")');

	var cartIMG = document.createElement('img');
	cartIMG.setAttribute('src','/images/buy.png');
	cartIMG.setAttribute('title','Add to Cart');
	cartIMG.className = "mini_cart";

	cartAnchor.appendChild(cartIMG);
	priTD.appendChild(document.createElement('br'));
	priTD.appendChild(cartAnchor);
	
	miniTable.rows[0].appendChild(matTH);
	miniTable.rows[1].appendChild(priTD);
    }
    
    //Update dimensions
    var dimens = currWidth + '" x ' + currHeight + '" ';
    if(currProduct == 'shutter')
    {
	dimens = '1 5/16" x ' + dimens;
    }
    
    else if(currProduct == 'door')
    {
	dimens = '1 3/8" x' + dimens;
    }
    
    else if(currProduct == 'cabinet')
    {
	dimens = '3/4" x' + dimens;
    }
    document.getElementById('mini_dimens').innerHTML = dimens;
    
}


function getSpecString(jsonFlag)
{
    var str = '';
    if(currProduct == 'shutter')
    {
	str = "-ST+1.3125+-CN+Antebellum+-DS+Asheville+-DL+" + 
	    currWidth + 
	    "+-DH+" + 
	    currHeight + 
	    "+-BGW+-DM+Cedar+-WW+170+-WH+525";
    }
    
    else if(currProduct == 'door')
    {
	str = "-ST+1.375+-CN+Cabana+-DS+StJohn+-DL+" + 
	    currWidth +
	    "+-DH+" +
	    currHeight +
	    "+-BGW+-DM+Mahogany+-WW+170+-WH+396";
    }
    
    else if(currProduct == 'cabinet')
    {
	str = "-ST+0.75+-CN+Sonoma+-DS+100+-DL+" +
	    currWidth +
	    "+-DH+" +
	    currHeight +
	    "+-BGW+-DM+Cherry+-WW+170+-WH+283";
    }
    
    if(jsonFlag) return str + "+-JSON";
    else return str;
    
}



function getRenderString()
{
    if(currProduct == 'shutter')
    {
	return "-OF+stdout+-ST+1.3125+-CN+Antebellum+-DS+Asheville+-DL+" + 
	    currWidth + 
	    "+-DH+" + 
	    currHeight + 
	    "+-BGW+-DM+Cedar+-WW+170+-WH+525+-BGW+1";
    }
    
    else if(currProduct == 'door')
    {
	return "-OF+stdout+-ST+1.375+-CN+Cabana+-DS+StJohn+-DL+" + 
	    currWidth +
	    "+-DH+" +
	    currHeight +
	    "+-BGW+-DM+Mahogany+-WW+170+-WH+396+-BGW+1";
    }
    
    else if(currProduct == 'cabinet')
    {
	return "-OF+stdout+-ST+0.75+-CN+Sonoma+-DS+100+-DL+" +
	    currWidth +
	    "+-DH+" +
	    currHeight +
	    "+-BGW+-DM+Cherry+-WW+170+-WH+283+-BGW+1";
    }
}



function setDynamicText(which)
{
    document.getElementById('dyn_prod_1').innerHTML = which.toUpperCase();
    document.getElementById('dyn_prod_2').innerHTML = which + ' ';
}



function continueDesign()
{
    var args = "?wid=" + currWidth + "&hei=" + currHeight;
    if(currProduct == 'shutter')
    {
	location = "/Exterior-Shutters.html" + args;
    }
    
    else if(currProduct == 'door')
    {
	location = "Louvered-Doors.html" + args;
    }
    
    else if(currProduct == 'cabinet')
    {
	location = "cabinet-doors.html" + args;
    }
}


function miniAddToCart(mat, pri)
{
    var itemcount = GetCookie("ITEM_COUNT") * 1;
    var SKU = '';
    var Quantity = 1;
    var Price = pri.replace('$','');
    var DesignString = getSpecString(false);

    //Fix mat in design string
    DesignString = DesignString.replace(/\+\-DM\+[A-Za-z]+\+\-/, "+-DM+" + mat.replace(/ /g, '') + "+-");

    var Description = document.getElementById('mini_desc').innerHTML + " " + 
	currProduct.substring(0,1).toUpperCase() + currProduct.substring(1) + " " + currThick + "-in.x" + currWidth + "-in.x" + currHeight + "-in in " + mat + " Unfinished";
    
    var itemstring = Description + "+PANEL_PARAM+" + DesignString + "+QUANTITY+" + Quantity + "+PRICE+" + Price + "+SKU+" + SKU;
    CreateCookie("ESTATE_MILLWORK_ITEM" + itemcount,itemstring);
    CreateCookie("ITEM_COUNT", ++itemcount);
    this.location="https://secure.estatemillwork.com/filtrexx.cgi?-P+ITEMS+" + itemcount + "+-P+IPRICE+" + Price + "+em_addtocart+order_cart.frm";
}

