var scrollElem = null;

var ex_preview_x = 0;
var ex_preview_y = 0;

function pageWidth() {return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;} 

window.onscroll = function(){
	if (scrollElem == null)
		return false;
	if (document.all)
		{
		if (!document.documentElement.scrollTop)
			scrollY = document.body.scrollTop;
		else
			scrollY = document.documentElement.scrollTop;
		}   
	else
		scrollY = window.pageYOffset;

	if(scrollY <= ex_preview_y){
		scrollElem.style.top = ex_preview_y + "px";
	}else{
		scrollElem.style.top = 50 + scrollY + "px";
	}
	
}

window.onresize = function(){
	if (scrollElem == null)
		return false;
	//document.getElementById('ex_preview').style.left = (pageWidth() - 780) / 2 + 175 +"px";
}

window.onload = function(){
	if ($('preview_div'))
		scrollElem = $('preview_div');
	else if ('ex_preview')
		scrollElem = $('ex_preview');
	if (scrollElem == null)
		return false;
	ex_preview_x = parseInt(scrollElem.style.left);
	ex_preview_y = parseInt(scrollElem.style.top);
	//document.getElementById('ex_preview').style.left = (pageWidth() - 780) / 2 + 175 +"px";
}
function keyPreview(ev)
	{
	if (ev.keyCode)
		var keyCode=ev.keyCode;
	else
		var keyCode=ev.which;
	if (keyCode==13 && $('ex_preview'))
		{
		previewWidget();
		return false;
		}
	}
function previewWidget()
{
	if (document.getElementById('ex_preview'))
		{
		query = "/express/widgetPreview?"+getExpParams('config_form');
		new Ajax.Request(query, {method:'get', onSuccess:function(transport){document.getElementById('ex_preview').innerHTML=transport.responseText}});
		}

	return false;
}
function getExpParams(form_id)
	{
	var c 		= document.getElementById(form_id);
	var length 	= c.length;
	var query 	= '';
	for(var i=0; i<length; i++){
		value = escape(c[i].value);
		value = value.replace(/\+/, '%2b');
		query+= c[i].name+"="+value+"&";
	}
	return query;
	}
function setCheckbox(elem)
	{
	if (elem.checked)
		elem.value = 'true';
	else
		elem.value = 'false';

	previewWidget();
	}
function togglePreview()
	{
	$('ex_preview').style.display = $('ex_preview').style.display=='block' ? 'none' : 'block';
	}
function setSize()
	{
	elem = $('WidgetSize');
	sizes = elem.options[elem.selectedIndex].value.split('|');
	$('width').value = sizes[0];
	$('height').value = sizes[1];
	}