var ServerName		= window.location.host;
var Protocol		= location.protocol.slice(0, -1);

function setPreviewState(inState)
{
	PreviewState = inState;
}

function setServerName(inName)
{
	ServerName = inName;
}

function getStyle(inName)
{
	if(inName.charAt(0) != ".")
	{
		inName = "." + inName;
	}

	var style = getStyleBySelector(inName);

	return style;
}

function getStyleBySelector(inName)
{
	for(var j = 0; j < document.styleSheets.length; j++)
	{
		var rules = document.styleSheets[j].rules;

		if(!rules)
		{
			rules = document.styleSheets[j].cssRules;
		}

		for(var i = 0; i < rules.length; i++)
		{
			var selector = rules[i].selectorText.toLowerCase();

			if(selector.charAt(0) == '*')
			{
				selector = selector.substring(1);
			}

			if(selector == inName.toLowerCase())
			{
				return rules[i].style;
			}
		}
	}
}

function getStyleAttribute(inName, inAttribute)
{
	var style = getStyle(inName);

	if(style)
	{
		return(style[inAttribute]);
	}
}

function setStyleAttribute(inName, inAttribute, inValue)
{
	var style = getStyle(inName);

	if(style)
	{
		style[inAttribute] = inValue;
	}
}

var HexValues = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");

function hexToInt(inHex)
{
	if((inHex >= 0) && (inHex <= 9))
	{
		return inHex * 1;
	}

	switch(inHex.toUpperCase())
	{
		case "A": return 10;
		case "B": return 11;
		case "C": return 12;
		case "D": return 13;
		case "E": return 14;
		case "F": return 15;
		default:  return 'X';
   }
}

function prepareColor(inValue)
{
	if(inValue.indexOf("#") == 0)
	{
		inValue = inValue.substring(1);
	}
	else if(inValue.indexOf("rgb") >= 0)
	{
		var parts = inValue.substring(4, inValue.length - 1).split(",");

		inValue =  HexValues[Math.floor(parts[0] / 16)];
		inValue += HexValues[parts[0] % 16];
		inValue += HexValues[Math.floor(parts[1] / 16)];
		inValue += HexValues[parts[1] % 16];
		inValue += HexValues[Math.floor(parts[2] / 16)];
		inValue += HexValues[parts[2] % 16];
	}

	return inValue;
}

function prepareFontSize(inValue)
{
	var position = inValue.indexOf("px");

	if(position > 0)
	{
		return inValue.substring(0, position);
	}

	position = inValue.indexOf("pt");

	if(position > 0)
	{
		return inValue.substring(0, position);
	}

	return inValue;
}

function prepareURL(inURL)
{
	if(inURL.substr(0,4) == "http")
	{
		return inURL;
	}

	if((PreviewState == "preview") && (inURL.substr(0,9) == "/members/"))
	{
		inURL = "/preview" + inURL;
	}

	inURL = Protocol + "://" + ServerName + inURL;

   return inURL;
}

function shadeColor(inColor, inAdjustment)
{
	if(!inColor)
	{
		return "ffffff";
	}

	if(inColor.length != 6)
	{
		return inColor
	}

	var red = 0;
	var green = 0;
	var blue = 0;

	red += hexToInt(inColor.charAt(0)) * 16;
	red += hexToInt(inColor.charAt(1));

	green += hexToInt(inColor.charAt(2)) * 16;
	green += hexToInt(inColor.charAt(3));

	blue += hexToInt(inColor.charAt(4)) * 16;
	blue += hexToInt(inColor.charAt(5));

	if(inAdjustment < 0)
	{
		inAdjustment = 0;
	}
	else if(inAdjustment > 1)
	{
		inAdjustment = 1;
	}

	var offset = ((red + green + blue) / 3 < 127) ? (inAdjustment * 255) : 0;

	red 	= Math.floor((red	* (1.0 - inAdjustment)) + offset);
	green	= Math.floor((green * (1.0 - inAdjustment)) + offset);
	blue	= Math.floor((blue	* (1.0 - inAdjustment)) + offset);

	inColor =  HexValues[Math.floor(red / 16)];
	inColor += HexValues[red % 16];
	inColor += HexValues[Math.floor(green / 16)];
	inColor += HexValues[green % 16];
	inColor += HexValues[Math.floor(blue / 16)];
	inColor += HexValues[blue % 16];

	return inColor;
}

function darkerColor(inColor, inAdjustment)
{
	if(!inColor)
	{
		return "ffffff";
	}

	if(inColor.length != 6)
	{
		return inColor
	}

	var red = 0;
	var green = 0;
	var blue = 0;

	red += hexToInt(inColor.charAt(0)) * 16;
	red += hexToInt(inColor.charAt(1));

	green += hexToInt(inColor.charAt(2)) * 16;
	green += hexToInt(inColor.charAt(3));

	blue += hexToInt(inColor.charAt(4)) * 16;
	blue += hexToInt(inColor.charAt(5));

	if(inAdjustment < 0)
	{
		inAdjustment = 0;
	}
	else if(inAdjustment > 1)
	{
		inAdjustment = 1;
	}

	red 	= Math.floor((red	* (1.0 - inAdjustment)));
	green	= Math.floor((green * (1.0 - inAdjustment)));
	blue	= Math.floor((blue	* (1.0 - inAdjustment)));

	inColor =  HexValues[Math.floor(red / 16)];
	inColor += HexValues[red % 16];
	inColor += HexValues[Math.floor(green / 16)];
	inColor += HexValues[green % 16];
	inColor += HexValues[Math.floor(blue / 16)];
	inColor += HexValues[blue % 16];

	return inColor;
}

function lighterColor(inColor, inAdjustment)
{
	if(!inColor)
	{
		return "ffffff";
	}

	if(inColor.length != 6)
	{
		return inColor
	}

	var red = 0;
	var green = 0;
	var blue = 0;

	red += hexToInt(inColor.charAt(0)) * 16;
	red += hexToInt(inColor.charAt(1));

	green += hexToInt(inColor.charAt(2)) * 16;
	green += hexToInt(inColor.charAt(3));

	blue += hexToInt(inColor.charAt(4)) * 16;
	blue += hexToInt(inColor.charAt(5));

	if(inAdjustment < 0)
	{
		inAdjustment = 0;
	}
	else if(inAdjustment > 1)
	{
		inAdjustment = 1;
	}

	var offset = inAdjustment * 255;

	red 	= Math.floor((red	* (1.0 - inAdjustment)) + offset);
	green	= Math.floor((green * (1.0 - inAdjustment)) + offset);
	blue	= Math.floor((blue	* (1.0 - inAdjustment)) + offset);

	inColor =  HexValues[Math.floor(red / 16)];
	inColor += HexValues[red % 16];
	inColor += HexValues[Math.floor(green / 16)];
	inColor += HexValues[green % 16];
	inColor += HexValues[Math.floor(blue / 16)];
	inColor += HexValues[blue % 16];

	return inColor;
}

function getImageURL(inTemplateURL, inAttributes)
{
	output = prepareURL(inTemplateURL);

	if(inAttributes)
	{
		output += "?"
		output += inAttributes;
	}

	return output;
}

function drawImage(inTemplateURL, inAttributes, inExtra)
{
	document.write("<img src='" + getImageURL(inTemplateURL, inAttributes) + "'");

	if(inExtra)
	{
		 document.write(" " + inExtra);
	}

	document.write(">");
}

function drawInputImage(inTemplateURL, inAttributes, inName, inExtra)
{
	document.write("<input type='image' src='" + prepareURL(inTemplateURL));

	if(inAttributes)
	{
		document.write("?" + inAttributes);
	}

	document.write("' name='" + inName + "'");

	if(inExtra)
	{
		 document.write(" " + inExtra);
	}

	document.write(">");
}

function resizeImage(inURL, inWidth, inHeight, inExtra,  inAttributes, inTemplateURL)
{
	if(!inTemplateURL)
	{
		inTemplateURL = "/images/site/common/en/image/imagewrap.img";
	}

	var attributes = "picture.image.url=" + prepareURL(inURL);

	if(inWidth)
	{
		attributes += "&picture.width.max="  + inWidth
	}

	if(inHeight)
	{
		attributes += "&picture.height.max="  + inHeight
	}

	if(inAttributes)
	{
		attributes += inAttributes;
	}

	drawImage(inTemplateURL, attributes, inExtra);
}

function colorizeImage(inURL, inColor, inExtra,  inAttributes, inTemplateURL)
{
	if(!inTemplateURL)
	{
		inTemplateURL = "/images/site/common/en/image/colorize.img";
	}

	var attributes = "picture.image.url=" + prepareURL(inURL);

	if(inColor)
	{
		attributes += "&picture.image.colorize.color=" + prepareColor(inColor)
	}

	if(inAttributes)
	{
		attributes += "&" + inAttributes;
	}

	drawImage(inTemplateURL, attributes, inExtra);
}

function colorizeStyleImage(inURL, inColor, inAttributes, inTemplateURL)
{
	if(!inTemplateURL)
	{
		inTemplateURL = "/images/site/common/en/image/colorize.img";
	}

	var attributes = "picture.image.url=" + prepareURL(inURL);

	if(inColor)
	{
		attributes += "&picture.image.colorize.color=" + prepareColor(inColor)
	}

	if(inAttributes)
	{
		attributes += "&" + inAttributes;
	}

	return "url(" + inTemplateURL + "?" + attributes + ")";
}

// ---------------------------------
// Elements

function drawImageElement(inURL, inName, inAlignment, inWidth, inHeight, inExtra, inAttributes, inTemplateURL)
{
	var extra = inExtra;

	if(inName)
	{
		var inNameA = unescape(inName);
		var inNameB = inNameA.replace(/\"/g,"");
		extra += (" alt=\"" + inNameB + "\"");
	}

	if(inAlignment == "center")
	{
		document.write("<center>");
	}
	else if(inAlignment == "left")
	{
		extra += " align=left";
	}
	else if(inAlignment == "right")
	{
		extra += " align=right";
	}

	resizeImage(inURL, inWidth, inHeight, extra,  inAttributes, inTemplateURL);

	if(inAlignment == "center")
	{
		document.write("</center>");
	}
}

// ---------------------------------
// Tabs

var Tabs		= new Array();
var TabCounter	= 0;

function Tab(inLabel, inLink, inContext, inSelected)
{
	this.Label = inLabel;
	this.Link = inLink;
	this.Context = inContext;
	this.Selected = inSelected;
	this.Alternative = false;
}

function clearTabs()
{
	Tabs = new Array();
}

function addTab(inLabel, inLink, inContext, inSelected)
{
	Tabs[Tabs.length] = new Tab(inLabel, inLink, inContext, inSelected);
}

function drawTabs(inOrientation, inWrapNumber, inWidth, inExtra,  inAttributes, inTemplateURL)
{
	if(Tabs.length == 0)
	{
		return;
	}

	if(inOrientation == "horizontal")
	{
		document.write("<table cellspacing='0' cellpadding='0' border='0' width='" + inWidth + "'>");
		document.write("<tr>");

		for(var i = 0; i < Tabs.length; i++)
		{
			Tabs[i].Context = Tabs[i].Context.toLowerCase();

			if((i % inWrapNumber) == 0)
			{
				Tabs[i].Context = 'n' + Tabs[i].Context.slice(1);
			}
			else if((i % inWrapNumber) == (inWrapNumber - 1))
			{
				Tabs[i].Context = Tabs[i].Context.slice(0, 2) + 'n';
			}

			var attributes = inAttributes;

			if((i / inWrapNumber) > 0)
			{
				attributes += "&style=alternate";
			}
				document.write("<td>");
			drawTab(Tabs[i], inExtra, inAttributes, inTemplateURL);
				document.write("</td>");
			if(((i % inWrapNumber) == (inWrapNumber - 1)) && (i < (Tabs.length - 1)))
			{
				document.write("</tr><tr>");
			}
		}

		document.write("</tr>");
		document.write("</table>");
	}
	else
	{
		for(var i = 0; i < Tabs.length; i++)
		{
			if(i > 0)
			{
				document.write("<br>");
			}

			drawTab(Tabs[i], inExtra, inAttributes, inTemplateURL);
		}
	}
}

function drawTab(inTab, inExtra, inAttributes, inTemplateURL)
{
	var imageName = "tab" + (TabCounter++);

	var imageURL  = prepareURL(inTemplateURL);

	imageURL += "?label.text=" + inTab.Label;
	imageURL += "&tab.context=" + inTab.Context;

	if(inAttributes)
	{
		imageURL += "&";
		imageURL += inAttributes;
	}

	imageURL += "&state=";

	if(inTab.Selected == "true")
	{
		imageURL += "selected";
	}

	var mouseOut  = "document.images." + imageName + ".src=\"" + imageURL + "\"";
	var mouseOver = "document.images." + imageName + ".src=\"" + imageURL + "over" + "\"";

	if(!inExtra)
	{
		inExtra = "";
	}
	newAlt = unescape(inTab.Label);
	newAlt2 = newAlt.replace(/\"/g,"");
	inExtra += " alt=\"" + newAlt2 + "\"";

	document.write("<a onmouseover='" + mouseOver + "' onmouseout='" + mouseOut + "' href='" + inTab.Link + "'>");
	document.write("<img src='" + imageURL + "' NAME='" + imageName + "' " + inExtra +">");
	document.write("</a>");
}

// ---------------------------------
// Buttons

function drawButton(inLinkURL, inLabel, inTemplateURL, inAttributes)
{
}

// ---------------------------------
// Header

function drawHeader(inImageDrawn, inExtra, inAttributes, inTemplateURL)
{
	var title = unescape(headerName + "\n" + headerByline + "\n" + headerText);

	var attributes = "";

	if(headerLayout == "2") // imageonly
	{
		attributes += "&name.visible=false&byline.visible=false&text.visible=false&state=imageonly";

		if(headerImage)
		{
			attributes += "&logo.image.url=";
			attributes += prepareURL(headerImage);
		}
		else
		{
			return;
		}
	}
	else
	{
		if(headerName)
		{
			attributes += "&name.text=";
			attributes += headerName;
		}
		else
		{
			attributes += "&name.visible=false";
		}

		if(headerByline)
		{
			attributes += "&byline.text=";
			attributes += headerByline;
		}
		else
		{
			attributes += "&byline.visible=false";
		}

		if(headerText)
		{
			attributes += "&text.text=";
			attributes += headerText;
		}
		else
		{
			attributes += "&text.visible=false";
		}

		if(inImageDrawn == false)
		{
			if(headerImage)
			{
				attributes += "&logo.image.url=";
				attributes += prepareURL(headerImage);
			}
			else
			{
				attributes += "&logo.visible=false";
			}
		}
		else
		{
			attributes += "&logo.visible=false";
		}

		if(headerLayout)
		{
			attributes += "&header.import.file=/site/common/en/header/layout";
			attributes += headerLayoutID;
			attributes += ".txt";

			attributes += "&logo.import.file=/site/common/en/header/logo";
			attributes += headerLogoID;
			attributes += ".txt";
		}
	}

	if(inAttributes)
	{
		attributes += "&";
		attributes += inAttributes;
	}

	if(attributes.charAt(0) == "&")
	{
		attributes = attributes.substring(1);
	}

	if(!inTemplateURL)
	{
		inTemplateURL = "/images/site/common/en/header/header.img";
	}

	if(!inExtra)
	{
		inExtra = "";
	}
	var newTitle = title.replace(/\"/g,"");
	inExtra += " alt=\"" + newTitle + "\"";

	drawImage(inTemplateURL, attributes, inExtra);
}

// ---------------------------------
// Borders

var borderWidth = "";
var borderAttributes = "";
var borderTemplateURL = "";

function drawBorderStart(inWidth, inAttributes, inTemplateURL, inCellAlign)
{
	borderWidth = inWidth;
	borderAttributes = inAttributes;
	borderTemplateURL = inTemplateURL;

	if(!borderTemplateURL)
	{
		borderTemplateURL = "/images/site/common/en/image/border.img";
	}

	// ---------------

	attributes = "state=top";
	attributes += "&width=" + borderWidth;

	if(borderAttributes)
	{
		attributes += "&";
		attributes += borderAttributes;
	}

	document.write("<table width=" + borderWidth + " cellpadding=0 cellspacing=0 border=0><tr><td colspan=3>");

	drawImage(borderTemplateURL, attributes);

	document.write("</td></tr>");

	// ---------------

	attributes = "state=left";
	attributes += "&width=" + borderWidth;

	if(borderAttributes)
	{
		attributes += "&";
		attributes += borderAttributes;
	}

	document.write("<tr><td background='" + getImageURL(borderTemplateURL, attributes) + "'>");

	drawImage(borderTemplateURL, attributes);

	document.write("</td><td align=" + inCellAlign + " width=" + borderWidth + ">");
}

function drawBorderEnd()
{
	attributes = "state=right";
	attributes += "&width=" + borderWidth;

	if(borderAttributes)
	{
		attributes += "&";
		attributes += borderAttributes;
	}

	document.write("</td><td background='" + getImageURL(borderTemplateURL, attributes) + "'>");

	drawImage(borderTemplateURL, attributes);

	document.write("</td></tr>");


	// ---------------

	attributes = "state=bottom";
	attributes += "&width=" + borderWidth;

	if(borderAttributes)
	{
		attributes += "&";
		attributes += borderAttributes;
	}

	document.write("<tr><td colspan=3>");

	drawImage(borderTemplateURL, attributes);

	document.write("</td></tr></table>");
}

// ---------------------------------
// Misc.

function pressButton(inName)
{
   myForm = document.forms[0];
   myForm.ACTION.name = inName;
   myForm.submit();
}

function drawBrowseMenu(inDropBox, inWidth, inMaxOptionLength)
{
	if((inDropBox == '') || (inMaxOptionLength == '') || (inWidth == ''))
	{
		return;
	}

	inWidth = Number(inWidth);

	if(!inMaxOptionLength)
	{
		inMaxOptionLength = 0;
	}
	else
	{
		inMaxOptionLength = Number(inMaxOptionLength);
	}

	inDropBox = unescape(inDropBox);
	inDropBox = inDropBox.slice(inDropBox.indexOf(">") + 1);
	inDropBox = inDropBox.slice(0, inDropBox.indexOf("</SELECT>"));

	menuOptions   = inDropBox.split("</OPTION>");

	inDropBox = '<SELECT NAME=EDIT_BROWSE WIDTH=' + inWidth + ' CLASS=browsemenu ONCHANGE="pressButton(\'NV-CMD-BROWSE\')">';

	for(var i in menuOptions)
	{
		var option = menuOptions[i];

		if(option != '')
		{
			components = option.split(">");
			components[0] = components[0] + ">";

			if(inMaxOptionLength >	0)
			if(components[1].length > inMaxOptionLength)
			{
				components[1] = components[1].slice(0, inMaxOptionLength) + "...";
			}

			inDropBox += components[0] + components[1] + "</OPTION>";
		}
	}

   inDropBox += "</SELECT>";

   document.write(inDropBox);
}

function drawFormTag(inProtocol, inServer, inPreview, inEntryPage)
{
	document.write("<FORM NAME='FORM_FORM' METHOD=POST ACTION=" + inProtocol + "://" + inServer + "/" + inPreview + inEntryPage + ">");
}

function pressEnter(myCall, event)
{
	var myKeyCode = event.keyCode;

	if(document.all) // Internet Explorer
	{
		event.cancelBubble = true;

		if(myKeyCode == 13)
		{
			event.returnValue = false;
		}
	}
	else // All Others
	{
		if(myKeyCode == 13)
		{
			event.preventDefault(true);
		}
	}

	if(myKeyCode == 13)
	{
		eval(myCall);
	}
}

function resizeWindow()
{
	var heightDiff = document.body.scrollHeight - document.body.clientHeight;
	var widthDiff  = document.body.scrollWidth - document.body.clientWidth;

	window.resizeBy(widthDiff, heightDiff);

	heightDiff = document.body.scrollHeight - document.body.clientHeight;
	widthDiff  = document.body.scrollWidth - document.body.clientWidth;

	window.resizeBy(widthDiff, heightDiff);

	if(window.screenTop + document.body.clientHeight > (screen.availHeight - 28))
	{
		window.resizeBy(0, screen.availHeight - 28 - window.screenTop - document.body.clientHeight);
	}
}

//pop up window
function OpenDaWindow(theURL,winName,features)
{
	window.open(theURL,winName,features);
}

//resize pop up window
function resizeWindowCvv()
{
	window.resizeTo(400, 475);
}

function recordStats(inAccount, inLocation)
{
	var proto	 = window.location.protocol.slice(0, -1);
	var host	 = window.location.host;
	var time	 = new Date();
	var referrer = window.document.referrer;
	var title	 = window.document.title;
	var location = inLocation  ? inLocation : window.location;
	var data	 = "";

	if(document.forms[0])
	if(document.forms[0].ST_DATA)
	if(document.forms[0].ST_DATA.value)
		data = document.forms[0].ST_DATA.value;

	var url 	 = proto + "://" + host + "/stats/" + inAccount + "/" + time.getTime() + "?" +
				  (PreviewState 		? "&p=" + PreviewState : "") +
				  (location 			? "&l=" + escape(location) : "") +
				  (referrer 			? "&r=" + escape(referrer) : "") +
				  (title				? "&t=" + escape(title) : "") +
				  ((data.length > 0)	? "&ST_DATA=" + data : "");

	document.write('<img src="'+ url +'" width=1 height=1 border=0>');
}
