﻿/* Aardstorm Images Script Library */
/**** Global Variables ****/

var printSize = 600;
var printHeight = 600;
var printWidth = 480;

var printName = "Dahlia";
var printIndex = "1";
var printTitle = "Dahlia Flower";
var printSubtitle = "Tally Ho";

/**** ParseParams Function ****/

var aData=new Array();
function ParseParams()
{
   var sLocation=  location.href;
   
   var queryLoc = sLocation.indexOf("?");
   
   if (queryLoc > 0)
   {
	   var sData = sLocation.substring(queryLoc + 1,sLocation.length);
	   
		sData = decodeURI(sData);
	   aData = sData.split("&");
	   
	   for(var i = 0;i < aData.length;i++)
	   {
	      var sName = aData[i].substring(0,aData[i].indexOf("="));
	      var sValue = aData[i].substring(aData[i].indexOf("=")+1,aData[i].length);
	      
	      switch (sName)
	      {
	      case "size":
				printSize = parseInt(sValue);
				break;
	      case "index":
				printIndex = sValue;
				break;
	      }
	   }
	}
}

function ReplaceImage(imgName, newIndex, newSize)
{
	var element = document.all[imgName];
	
	var srcName = element.src;
	var pathLoc = srcName.lastIndexOf("/");
	var pathName = srcName.substring( 0, pathLoc+1 );
	var fileName = srcName.substring( pathLoc+1, srcName.length );
	
	var part = fileName.split(/[_.]/);

	if ( part.length == 4 )
	{
		if ( newIndex != "" )
			part[1] = newIndex;

		if ( newSize != "" )
			part[2] = newSize;

		fileName = part[0] + "_" + part[1] + "_" + part[2] + "." + part[3];
		srcName = pathName + fileName;
		
		element.src = srcName;
		element.alt = part[0] + " " + part[1];
	}
}

function UpdatePrevNextSizes(newSize)
{
	var allLinks = document.links;
	
	for ( var i = 0; i < allLinks.length; i++ )
	{
		var thisLink = allLinks[i];
		
		var text = thisLink.innerText;
		if ( ( text.indexOf("Prev") >= 0 ) || ( text.indexOf("Next") >= 0 ))
		{
			var srcName = thisLink.href;
			var pathLoc = srcName.lastIndexOf("/");
			var pathName = srcName.substring( 0, pathLoc+1 );
			var fileName = srcName.substring( pathLoc+1, srcName.length );
			
			var part = fileName.split(/[?&=]/);
			
			for( var j=0; j < part.length; j++ )
			{
				if ( part[j] == "size" )
				{
					if ( newSize != "" )
						part[j+1] = newSize;
			    }
			    
			    switch ( j )
			    {
			    case 0:
			    	fileName = part[j];
			    	break;
			    	
			    case 1:
			    	fileName = fileName + "?" + part[j];
			    	break;

			    case 3:
			    case 5:
			    	fileName = fileName + "&" + part[j];
			    	break;

			    case 2:
			    case 4:
			    case 6:
			    	fileName = fileName + "=" + part[j];
			    	break;
			    }
			}

			srcName = pathName + fileName;
			thisLink.href = srcName;
		}
	}
}

function UpdateFormat()
{
	var newFormat = formatSelect.options[formatSelect.selectedIndex].value;
	
	if ( newFormat == "1" )
	{
		displayTitle.style.display = "none";
		printDiv.style.padding = "0";
		mountDiv.style.width = printWidth + 10 + "px";
	}
	else
	{
		displayTitle.style.display = "";
		printDiv.style.padding = "5px";
		mountDiv.style.width = printWidth + 20 + "px";
	}
}

function UpdateMount()
{
	var newValue = mountSelect.options[mountSelect.selectedIndex].value;
	
	switch(newValue)
	{
	case "UM":
		mountDiv.style.backgroundColor="#FFFFFF";
		break;
	case "SW":
		mountDiv.style.backgroundColor="#FFFAFA";
		break;
	case "AW":
		mountDiv.style.backgroundColor="#FFFFF8";
		break;
	case "SB":
		mountDiv.style.backgroundColor="#FAFAFA";
		break;
	case "IV":
		mountDiv.style.backgroundColor="#FFFFF0";
		break;
	}
}

function UpdateOrder()
{
	orderCode.innerText = " -" + sizeSelect.options[sizeSelect.selectedIndex].value
	   + " -" + mountSelect.options[mountSelect.selectedIndex].value
	   + " -" + formatSelect.options[formatSelect.selectedIndex].value;
	   
	var priceValue = 6;
	
	if (parseInt(sizeSelect.options[sizeSelect.selectedIndex].value) < 80)
	{
		if (mountSelect.options[mountSelect.selectedIndex].value != "UM")
			priceValue += 1;
	}
	else
	{
		priceValue += 3;
		
		if (mountSelect.options[mountSelect.selectedIndex].value != "UM")
			priceValue += 2;
	}
	
	printPrice.innerText = priceValue + ".95 (or " + (priceValue - 2) + ".95";
}

function InitLinks()
{
	// Iterate over links of class 'mlink'
	
	var pfix1 = "mai";
	var pfix2 = "lto";
	var dom = "aardstorm-images.com";
	var all_links = document.links;
	
	for (i=0; i < all_links.length; i++ )
	{
		var lnk = all_links[i];
		if ( lnk.className == "mlink" )
		{
			// Convert each link to generate an email request
			
			lnk.href = pfix1 + pfix2 + ":" + lnk.innerText + "@" + dom;
		}
	}
}
