
function $(name)
{
	return document.getElementById(name);
}

function fw_trace(str)
{
	window.status = str;
}

function fw_empty_node(node)
{
	while (node && node.childNodes.length > 0) {
		node.childNodes[0].parentNode.removeChild(node.childNodes[0]);
	}
}

function fw_remove_node(node)
{
	node.parentNode.removeChild(node);
}

function fw_hide_node(node)
{
	node.style.display = 'none';
}

function fw_show_node(node)
{
		node.style.display = 'block';
}

function fw_toggle_node(node)
{
	if (node.style.display == 'block') {
		fw_hide_node(node);
	} else {
		fw_show_node(node);
	}
}

function fw_scrollTo(node)
{
	var x = node.offsetLeft;
	var y = node.offsetTop;
		
	if (window.pageYOffset == 0) {
		return true;
	}
	
	if (y < 200) {
		y = 0;
	}
	
	window.scrollTo(x, y);
}

function fw_popup(url, width, height, scrollbars, resizable)
{
	if (width == undefined || width == null) {
		width = 800;
	}
	if (height == undefined || height == null) {
		height = 600;	
	}
	if (scrollbars == undefined || scrollbars == null) {
		scrollbars = 'no';	
	}
	if (resizable == undefined || resizable == null) {
		resizable = 'no';	
	}
	var popup = window.open(url, '', 'width='+width+',height='+height+',scrollbars='+scrollbars+',resizable='+resizable);
	popup.focus();
}

function fw_popup_close()
{
	if (opener) {
		opener.focus();
		self.close();
	} else {
		fw_getPresenter().hide();
	}
}

function fw_search_popup(module, object, target)
{
	var presenter = fw_getPresenter();
	presenter.setOpener(target);
	presenter.present('/general/popup/object_search.ajax?module='+module+'&object='+object);
}

function fw_search_popup_close(id)
{
	var presenter = fw_getPresenter();
	presenter.getOpener().value = id;
	presenter.hide();
}

function fw_layout_table_refresh()
{
	if (!$('fw-layout-table')) {
		return false;
	}
	
	var n = $('fw-layout-table');

	if (window.innerWidth) {
		n.style.height = (window.innerHeight - n.offsetTop) + 'px';
	} else {
		// IE
		n.style.height = (document.documentElement.clientHeight - n.offsetTop - 60) + 'px';
	}
}

function fw_feedback_fade(timeout)
{
	if (!$('fw_feedback')) {
		return false;
	}
	if (timeout == undefined) {
		var log_icon = new Image();
		log_icon.src = '/general/images/icon_log.png';
		log_icon.setAttribute('id', 'fw_feedback_log');
		log_icon.onclick = function()
		{
			fw_present('/users/feedback_log.ajax');
		};
		fw_empty_node($('fw_feedback'));
		$('fw_feedback').appendChild(log_icon);
	} else {
		setTimeout('fw_feedback_fade();', timeout);
	}
}

function fw_edit_field(target, module, object, id, field, type)
{
	if (target.nodeName == 'DIV') {
		fw_get_input(target, module, object, id, field, type);
	} else if (target.nodeName == 'INPUT') {
		fw_send_input(target);
		fw_revert_input(target);
	}
}

function fw_create_input(container, module, object, id, field, type)
{
	var input = document.createElement('INPUT');
	input.setAttribute('type', 'text');
	input.setAttribute('name', 'a_'+module+'('+object+'.'+id+')_edit_'+field);
	input.setAttribute('value', container.innerHTML);
	input.setAttribute('class', 'input_'+type);
	input.onblur = container.onclick;
	input.onchange = container.onclick;
	
	container.parentNode.appendChild(input);
	container.parentNode.removeChild(container);
	
	input.focus()	;
}

function fw_get_input(container, module, object, id, field, type)
{
	fw_get('/'+module+'/'+object+'.'+id+'/edit_input_'+type+'.ajax', container.parentNode);
}

function fw_send_input(input)
{
	var content = input.getAttribute('name') + '=' + encodeURI(input.value);
	fw_post('/', null, content);
}

function fw_revert_input(input)
{
	var container = document.createElement('DIV');
	container.setAttribute('class', 'fw_object_field_editable');
	container.innerHTML = input.value;
	container.onclick = input.onblur;
	
	input.parentNode.appendChild(container);
	input.parentNode.removeChild(input);	
}

function fw_icon_toggle(anchor)
{
	var images = anchor.getElementsByTagName('IMG');
	for (i = 0; i < images.length; i++) {
		if (images[i].src.match(/_on\./)) {
			images[i].src = images[i].src.replace(/_on\./, '_off.');
		} else if (images[i].src.match(/_off\./)) {
			images[i].src = images[i].src.replace(/_off\./, '_on.');
		}
	}
}

function fw_tab_select(anchor, switch_tabs)
{
	var item = anchor.parentNode;
	var list = item.parentNode;
	var panel = list.parentNode;
	
	if (switch_tabs == undefined) {
		switch_tabs = true;
	}
	
	var tabIndex = 0;
	var j = 0;
	for (i = 0; i < list.childNodes.length; i++) {
		if (list.childNodes[i].nodeName == 'LI') {
			if (list.childNodes[i] == item) {
				item.className =  'active';
				tabIndex = j;
			} else {
				list.childNodes[i].className = '';
			}
			j++;
		}
	}
	
	if (switch_tabs) {
		var j = 0;
		for (i = 0; i < panel.childNodes.length; i++) {
			if (panel.childNodes[i].nodeName == 'DIV') {
				if (j == tabIndex) {
					fw_show_node(panel.childNodes[i]);
				} else {
					fw_hide_node(panel.childNodes[i]);
				}
				j++;
			}
		}
	}
}

function fw_fieldset_enable(fieldset)
{
	var fields = new Array();
	var inputs = fieldset.getElementsByTagName('input');
	for (i = 0; i < inputs.length; i++) {
		fields.push(inputs[i]);
	}	
	var selects = fieldset.getElementsByTagName('select');
	for (i = 0; i < selects.length; i++) {
		fields.push(selects[i]);
	}
	var textareas = fieldset.getElementsByTagName('textarea');
	for (i = 0; i < textareas.length; i++) {
		fields.push(textareas[i]);
	}
	for (i = 0; i < fields.length; i++) {
		field = fields[i];
		if (!(field.parentNode.nodeName == 'LEGEND' 
			|| field.parentNode.parentNode.nodeName == 'LEGEND')) {
			field.disabled = false;
		}
	}
}

function fw_fieldset_disable(fieldset)
{
	var fields = new Array();
	var inputs = fieldset.getElementsByTagName('input');
	for (i = 0; i < inputs.length; i++) {
		fields.push(inputs[i]);
	}	
	var selects = fieldset.getElementsByTagName('select');
	for (i = 0; i < selects.length; i++) {
		fields.push(selects[i]);
	}
	var textareas = fieldset.getElementsByTagName('textarea');
	for (i = 0; i < textareas.length; i++) {
		fields.push(textareas[i]);
	}
	for (i = 0; i < fields.length; i++) {
		field = fields[i];
		if (!(field.parentNode.nodeName == 'LEGEND' 
			|| field.parentNode.parentNode.nodeName == 'LEGEND')) {
			field.disabled = true;
		}
	}
}

function fw_input_focus(input)
{
	if (input.value == input.defaultValue) {
		input.value = '';
	}
}

function fw_input_blur(input)
{
	if (input.value == '') {
		input.value = input.defaultValue;
	}
}

function fw_search_asyoutype(input, module, object, path)
{
	var form = input.parentNode.parentNode;
	var results = form.lastChild;
	var query = input.value;
	if (path == undefined) {
		path = '/'+module+'/'+object+'/search/results-list.ajax';
	}
	fw_get(path+'?search='+query, results);
	fw_show_node(results);
}

function fw_search_asyoutype_select(link, value, value_display)
{
	if (value_display == undefined) {
		value_display = value;
	}
	form = link.parentNode.parentNode.parentNode.parentNode
	form.elements[0].value = value_display;
	form.elements[1].value = value;
	fw_empty_node(form.lastChild);
	fw_hide_node(form.lastChild);
} 

function fw_tab_activate(link, id)
{
	var li = link.parentNode;
	
	var ul = li.parentNode;
	var j = 0;
	var number = 0;
	for (i = 0; i < ul.childNodes.length; i++) {
		if (ul.childNodes[i] == li) {
			number = j;
		}
	}
	
	fw_node_activate(li);
	
	var path = link.href.replace(/\.html($|\?)/, '.ajax$1');
	
	var panel = ul.parentNode;
	var content = null;
	for (i = 0; i < panel.childNodes.length; i++) {
		if (panel.childNodes[i].className && panel.childNodes[i].className.search(/fw-tab-content/) != -1) {
			content = panel.childNodes[i];
		}
	}
	
	if (content) {
		fw_get(path, content);
	}
	
	if (id) {
		fw_action('a-users-setUserSession-var=tab-active-'+id+'&a-users-setUserSession-value='+number);
	}
}

function fw_node_activate(node)
{
	if (node.className.search(/active/) != -1) {
		return true;
	}
	if (node.nodeName == 'LI') {
		var ul = node.parentNode;
		for (i = 0; i < ul.childNodes.length; i++) {
			if (ul.childNodes[i].nodeName == 'LI') {
				ul.childNodes[i].className = ul.childNodes[i].className.replace(/\s?active\s?/, '');
			}
		}
	}
	node.className = node.className.replace(/\s?$/, 'active');	
}

function fw_object_info(node, module, object, id)
{
	if (node.lastChild.className == 'fw_object_info') {
		return false;
	}
	var url = '/admin/'+module+'/'+object+'.'+id+'/info.ajax';
	var container = document.createElement('DIV');
	container.className = 'fw_object_info';
	/*container.onmouseout = function() {
		fw_remove_node(this);
	}*/
	node.appendChild(container);
	fw_get(url, container);
}

function fw_set_datetime_date(target, value)
{
	if (target) {
		target.value = target.value.replace(/^[0-9\-]{10}/, value);
	}
}

function fw_set_datetime_time(target, value)
{
	if (target) {
		target.value = target.value.replace(/[0-9:]{8}/, value);
	}
}

/* date chooser */

var date_chooser_collection = new Array();

function fw_show_date_chooser(input)
{
	var dc = fw_get_date_chooser(input);
	if (dc) {
		dc.show();
		return true;
	}
	
	var date_chooser = new fw_date_chooser(input);	
	date_chooser.draw();
	date_chooser_collection.push(date_chooser);
}

function fw_hide_date_chooser(input, force)
{
	if (force == undefined) {
		force = false;
	}

	var dc = fw_get_date_chooser(input);
	if (dc) {
		dc.hide(force);
		return true;
	}
}

function fw_refresh_date_chooser(input)
{
	var dc = fw_get_date_chooser(input);
	if (dc) {
		dc.refresh();
		return true;
	}
}

function fw_remove_date_chooser(input)
{
	var dc= fw_get_date_chooser(input);
	if (dc) {
		dc.destroy();
		return true;
	}
}

function fw_get_date_chooser(input)
{
	for (i = 0; i < date_chooser_collection.length; i++) {
		if (date_chooser_collection[i].id == input.getAttribute('id')) {
			return date_chooser_collection[i];
		}
	}
	return null;
}

function fw_date_chooser(input)
{
	this.setTarget(input);
	this.getDate();
	this.mouseover = true;
}

fw_date_chooser.prototype.setTarget = function(input)
{
	this.id = input.getAttribute('id');
	this.input = input;
}

fw_date_chooser.prototype.getDate = function()
{
	/*this.month_days = [31,28,31,30,31,30,31,31,30,31,30,31];
	
	expr = /([0-9]{4})-([0-9]{2})-([0-9]{2})/;
	matches = expr.exec(this.input.value);
	
	if (matches.length == 4) {
		this.date = new Date(matches[1], matches[2], matches[3]);
	} else {
		this.date = new Date();
	}*/
}

fw_date_chooser.prototype.show = function()
{
	this.container.style.display = 'block';
}

fw_date_chooser.prototype.hide = function(force)
{
	if (this.mouseover == false || force) {
		this.container.style.display = 'none';
	}
}

fw_date_chooser.prototype.destroy = function()
{

}

fw_date_chooser.prototype.setValue = function(day)
{
}

fw_date_chooser.prototype.draw = function()
{
	this.container = document.createElement('DIV');
	this.container.setAttribute('class', 'fw_date_chooser');

	dc = this;

	this.container.onmouseover = function() {
		dc.mouseover = true;
	};
	this.container.onmouseout = function() {
		dc.mouseover = false;
	};

	this.refresh();
	
	this.input.parentNode.appendChild(this.container);
}


fw_date_chooser.prototype.refresh = function()
{
	fw_get('/general/util/datetime.'+this.id+'.ajax?date='+this.input.value, this.container);

	/*table = document.createElement('TABLE');
	table.setAttribute('cellspacing', '0');
	table.setAttribute('class', 'fw_calendar');
	
	tbody = document.createElement('TBODY');
	table.appendChild(tbody);
	
	tr = document.createElement('TR');
	tbody.appendChild(tr);
	
	th = document.createElement('TH');
	tr.appendChild(th);
	th = document.createElement('TH');
	th.setAttribute('colspan', '5');
	th.innerHTML = this.date.toLocaleString().substring(0, 15);
	tr.appendChild(th);
	th = document.createElement('TH');
	tr.appendChild(th);
	
	tr = document.createElement('TR');
	tbody.appendChild(tr);
	
	th = document.createElement('TH');
	th.innerHTML = 'M';
	tr.appendChild(th);
	th = document.createElement('TH');
	th.innerHTML = 'T';
	tr.appendChild(th);
	th = document.createElement('TH');
	th.innerHTML = 'W';
	tr.appendChild(th);
	th = document.createElement('TH');
	th.innerHTML = 'T';
	tr.appendChild(th);
	th = document.createElement('TH');
	th.innerHTML = 'F';
	tr.appendChild(th);
	th = document.createElement('TH');
	th.innerHTML = 'S';
	tr.appendChild(th);
	th = document.createElement('TH');
	th.innerHTML = 'S';
	tr.appendChild(th);
	
	for (week = 0; week < 7; week++) {
		tr = document.createElement('TR');
		tbody.appendChild(tr);
		for (day = 0; day < 7; day++) {
			td = document.createElement('TD');
			td.innerHTML = week+':'+day;
			tr.appendChild(td);
		}
	}
	
	this.container.appendChild(table);*/
}

/* time chooser */

var time_chooser_collection = new Array();

function fw_show_time_chooser(input)
{
	var tc = fw_get_time_chooser(input);
	if (tc) {
		tc.show();
		return true;
	}
	
	var time_chooser = new fw_time_chooser(input);	
	time_chooser.draw();
	time_chooser_collection.push(time_chooser);
}

function fw_hide_time_chooser(input, force)
{
	if (force == undefined) {
		force = false;
	}

	var tc = fw_get_time_chooser(input);
	if (tc) {
		tc.hide(force);
		return true;
	}
}

function fw_refresh_time_chooser(input)
{
	var tc = fw_get_date_chooser(input);
	if (tc) {
		tc.refresh();
		return true;
	}
}

function fw_get_time_chooser(input)
{
	for (i = 0; i < time_chooser_collection.length; i++) {
		if (time_chooser_collection[i].id == input.getAttribute('id')) {
			return time_chooser_collection[i];
		}
	}
	return null;
}

function fw_time_chooser(input)
{
	this.setTarget(input);
	this.mouseover = true;
}

fw_time_chooser.prototype.setTarget = function(input)
{
	this.id = input.getAttribute('id');
	this.input = input;
}

fw_time_chooser.prototype.show = function()
{
	this.container.style.display = 'block';
}

fw_time_chooser.prototype.hide = function(force)
{
	if (this.mouseover == false || force) {
		this.container.style.display = 'none';
	}
}

fw_time_chooser.prototype.draw = function()
{
	this.container = document.createElement('DIV');
	this.container.setAttribute('class', 'fw_time_chooser');

	var tc = this;

	this.container.onmouseover = function() {
		tc.mouseover = true;
	};
	this.container.onmouseout = function() {
		tc.mouseover = false;
	};

	this.refresh();
	
	this.input.parentNode.appendChild(this.container);
}

fw_time_chooser.prototype.refresh = function()
{
	fw_get('/general/util/time.'+this.id+'.ajax?time='+this.input.value, this.container);	
}

/* presenter */
var fw_prsnt = false;
function fw_present(path, confirm, abort, opener, evalScripts, shade, callback)
{
	if (shade == undefined) {
		shade = true;
	}
	fw_getPresenter().setOpener(opener);
	fw_getPresenter().setShade(shade);
	fw_getPresenter().present(path, confirm, abort, evalScripts);
	if (callback != undefined){
		fw_getPresenter().onResponse = callback;
	}
	return fw_getPresenter();
}

function fw_presenter_close()
{
	fw_getPresenter().hide();
	fw_getPresenter().destroy();
}

function fw_getPresenter()
{
	if (fw_prsnt == false) {
		fw_prsnt = new fw_presenter();
	}
	return fw_prsnt;
}

function fw_presenter()
{
	//this.return = null;
}

fw_presenter.prototype.setOpener = function(opener)
{
	this.opener = opener;
}

fw_presenter.prototype.getOpener = function()
{
	return this.opener;
}

fw_presenter.prototype.setShade = function(shade)
{
	this.shade = shade;
}

fw_presenter.prototype.present = function(path, confirm, abort, evalScripts)
{
	this.hide();
	
	//this.return = null;
	
	if (confirm != undefined || abort != undefined) {
		if (path.indexOf('?') == -1) {
			path += '?';
		} else {
			path += '&';
		}		
		if (confirm != undefined) {
			confirm = encodeURI(confirm);
			confirm = confirm.replace(/&/, '%26');
			path += 'confirm=' + confirm;
		}
		if (abort != undefined) {
			if (confirm != undefined) {
				path += '&';
			}
			abort = encodeURI(abort);
			abort = abort.replace(/&/, '%26');
			path += 'abort=' + encodeURI(abort);
		}
	}
	
	var request = new fw_ajaxrequest();
	request.setPath(path);
	request.setTarget(this.getContainer());
	request.setEvalScripts(evalScripts);
	
	var presenter = this;
	
	presenter.show();
	
	request.processAfterResponse = function()
	{
		presenter.getContainer().style.position = 'absolute';
		presenter.getContainer().style.display = 'block';

		var width = presenter.getContainer().clientWidth;
		var height = presenter.getContainer().clientHeight;

		if (window.innerWidth) {
			var posX = (window.innerWidth - width) / 2;
			var posY = (window.innerHeight - height) / 2;
		} else {
			var posX = (document.body.clientWidth - width) / 2;
			if (document.documentElement.clientHeight > document.body.clientHeight) {
				var posY = (document.body.clientHeight - height) / 2;
			} else {
				var posY = (document.documentElement.clientHeight - height) / 2;
			}
		}

		posX = Math.round(posX);
		posY = Math.round(posY);

		if (document.documentElement.scrollTop) {
			posY = posY + document.documentElement.scrollTop;
		} else if (window.pageYOffset) {
			posY = posY + window.pageYOffset;
		}

		if (posX < 0) { posX = 0; }
		if (posY < 0) { posY = 0; }

		presenter.getContainer().style.left = posX + 'px';
		presenter.getContainer().style.top = posY + 'px';

		if (window.innerWidth) {
			presenter.getBackground().style.width = '100%';
			if (window.innerHeight > getStyle(document.body, 'height').replace(/px/, '')) {
				presenter.getBackground().style.height = window.innerHeight;
			} else {
				presenter.getBackground().style.height = getStyle(document.body, 'height');
			}
		} else {
			presenter.getBackground().style.width = document.documentElement.clientWidth+'px';
			if (document.documentElement.clientHeight > document.body.clientHeight) {
				presenter.getBackground().style.height = document.documentElement.clientHeight;
			} else {
				presenter.getBackground().style.height = document.body.clientHeight;
			}
		}
		presenter.processAfterResponse();
		presenter.onResponse();
	};

	request.send();
}

fw_presenter.prototype.getContainer = function()
{
	if (this.container == undefined) {
		this.container = document.createElement('DIV');
		this.container.setAttribute('id', 'fw_presenter');
		document.body.appendChild(this.container);
	}
	
	return this.container;
}

fw_presenter.prototype.getBackground = function()
{
	if (this.background == undefined) {
		if ($('fw_presenter_background')) {
			this.background = $('fw_presenter_background');
		} else {
			this.background = document.createElement('DIV');
			this.background.setAttribute('id', 'fw_presenter_background');
		}
		document.body.appendChild(this.background);
	}
	
	return this.background;
}

fw_presenter.prototype.show = function()
{
	// inserted by bea (define height before display)
	var presenter = this;
	if (window.innerWidth) {
		presenter.getBackground().style.width = '100%';
		if (window.innerHeight > getStyle(document.body, 'height').replace(/px/, '')) {
			presenter.getBackground().style.height = window.innerHeight;
		} else {
			presenter.getBackground().style.height = getStyle(document.body, 'height');
		}
	} else {
		presenter.getBackground().style.width = document.documentElement.clientWidth+'px';
		if (document.documentElement.clientHeight > document.body.clientHeight) {
			presenter.getBackground().style.height = document.documentElement.clientHeight;
		} else {
			presenter.getBackground().style.height = document.body.clientHeight;
		}
	}

	if (this.shade !== false) {
		this.getBackground().style.display = 'block';
	}
	this.getContainer().style.display = 'block';
}

fw_presenter.prototype.hide = function()
{
	this.processBeforeClose();
	this.onClose(this.returnVar);
	this.getContainer().style.display = 'none';
	this.getBackground().style.display = 'none';
}

fw_presenter.prototype.destroy = function()
{
	fw_remove_node(this.getContainer());
	fw_prsnt = false;
}

fw_presenter.prototype.processAfterResponse = function()
{
}

fw_presenter.prototype.onResponse = function()
{
}

fw_presenter.prototype.processBeforeClose = function()
{
}

fw_presenter.prototype.onClose = function(returnVar)
{
}

/* banner */
function fw_banner(bannerNode, images)
{
	this.bannerNode = bannerNode;
	this.bannerNode.style.position = 'relative';
	this.bannerNode.style.overflow = 'hidden';
	this.images = images;
	this.imgNode = null;
	this.mode = 'swap';
	this.speed = 5000;
	this.hiddenNode = null;
}
fw_banner.prototype.start = function()
{
	this.i = 0;
	this.next();
}
fw_banner.prototype.next = function()
{
	if (this.mode == 'swap') {
		this.swap();
	} else if (this.mode == 'blend') {
		this.blend();
	}
	this.i++;
	if (this.i == this.images.length) {
		this.i = 0;
	}
	banner_temp = this;
	window.setTimeout('banner_temp.next()', this.speed);
}
fw_banner.prototype.swap = function()
{
	this.getImgNode().src = this.images[this.i];
}
fw_banner.prototype.blend = function()
{
	var oldNode = this.getImgNode();
	var imgNode = this.getImgNode(true);
	imgNode.src = this.images[this.i];
	if (oldNode.src)
	{
		imgNode.style.visibility = 'hidden';
		this.blendIn(imgNode, oldNode);
	}
}
fw_banner.prototype.blendIn = function(imgNode, oldNode)
{
	if(this.hiddenNode)
	{
		this.hiddenNode.parentNode.removeChild(this.hiddenNode);
	}
	
	imgNode.style.zIndex = 1;
	oldNode.style.zIndex = 2;

	imgNode.style.visibility = 'visible';
	
	Effect.Fade(oldNode);
	
	this.hiddenNode = oldNode;
}
fw_banner.prototype.setSpeed = function(speed)
{
	this.speed = speed;
}
fw_banner.prototype.setMode = function(mode)
{
	this.mode = mode;
}
fw_banner.prototype.getImgNode = function(force)
{
	if (this.imgNode == null || force) {
		this.imgNode = document.createElement('img');
		this.imgNode.style.position = 'absolute';
		this.bannerNode.appendChild(this.imgNode);
	}
	return this.imgNode;
}

function fw_hint_hide(hint, container)
{
	fw_action('a-general(hintmanager)-hide-hint='+hint);
	if (container) {
		fw_remove_node(container);
	}
}

/* -- */

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}
