var actions = "1,2,3,4,5,6,7,8";
var created = "7";
var query = "";
var height = "140";
var width = "140";
var numResults = "2";

// The server the scripts are on.
var server = "http://os.adamaltemus.com/social-actions/";

// Generate the code for the user's page.
function generateCode(element)
{
	actions = getActions();
	if (document.getElementById('age').value.length > 0)
	{
		created = document.getElementById('age').value;
	}
	query = document.getElementById('queryText').value;
	hwArray = document.getElementById('dimensions').value.split('x');
	if (hwArray.length > 1)
	{
		width = hwArray[0];
		height = hwArray[1];
		numResults = hwArray[2];
	}

	var code = "<script type='text/javascript' src='" + server + "js/sa.js'></script><div id='saDisplay' style='width:" + width + "px;height:" + height + "px;'><script type='text/javascript'>callSa('" + query + "','" + created + "','" + actions + "','" + height +"','" + numResults + "' );</script></div>";
	
	document.getElementById('codeDiv').value = code;
}

// Get the actions from the list.
// Used for a multi select box.
function getActions()
{
	var actionsSelect = document.getElementById('actions');
	var actionsStr = "";
	for (var i = 0; i < actionsSelect.options.length; i++) 
	{
		if (actionsSelect.options[i].selected) 
		{
			actionsStr += actionsSelect.options[i].value + ",";
		}
	}
	// Remove trailing comma.  Preferred instead of counting the loop.
	actionsStr = actionsStr.substring(0, actionsStr.length - 1);
	// Debug
	//alert("Actions to be added: " + actionsStr);
	return actionsStr;
}

// Show a preview of the widget.
function showWidget()
{
	// JSON call.
	var widgetDiv = document.getElementById("previewWidgetDiv");
	getPreview(query, created, actions,height);
	if (widgetDiv.innerHTML.indexOf("Loading Preview") == -1)
	{
		widgetDiv.innerHTML = "<div class='smallHeader' style='margin-bottom:5px'>Loading Preview</div><img src='imgs/loading.gif' />";
	}
	if (parseInt(width) < 200 )
	{
		widgetDiv.style.marginLeft = "50px";
	}
	else
	{
		widgetDiv.style.marginLeft = "25px";
	}
	widgetDiv.style.height = height + "px";
	widgetDiv.style.width = width + "px";
	// Show the DIV.
	jQuery('#transparentDiv').fadeIn("slow");
}

// Close the preview window.
function closePreview()
{
	jQuery('#transparentDiv').fadeOut("slow");
}

// Place the query text and hide the helper div.
function placeText(element)
{
	var text = element.innerHTML;
	document.getElementById('queryText').value = text;
	removeHelperDiv();
	
}

function showHelperDiv()
{
	var display = "block";
	var el = document.getElementById('queryDiv');
	if (el.style.display == "block")
	{
		display = "none";
	}
	el.style.display = display;
}

function removeHelperDiv()
{
	document.getElementById('queryDiv').style.display = "none";
}
