// Disable caching of AJAX
$.ajaxSetup ({
    cache: false
});
/**
 *
 */
function submitEnter(myfield, e) {
	var keycode;
	
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	}

	if (keycode == 13) {
		myfield.form.submit();
		return false;
	}
	
	return true;
}
/**
 * 
 */
function setFocus(id) {
	setTimeout(function() {	$(id).focus() }, 1);
}


/**
 *
 */
function getSelectedText() {
    if (window.getSelection) {
        return window.getSelection();
    } else if (document.selection) {
        return document.selection.createRange().text;
    }

    return '';
}

/**
 *
 */
function check(selector, flag) {
	$(selector + ':checkbox').attr('checked', flag);
}

/**
 *
 */
function toggleExpander(domain, body, trigger) {
	if (typeof toggleExpander._params == 'undefined') {
		toggleExpander._params = {};
	}
	if (typeof toggleExpander._params[domain] == 'undefined') {
		toggleExpander._params[domain] = {
			state: 0,
			body: {
				a: $(body['selector']).html(),
				b: body['text']
			},
			trigger: {
				a: $(trigger['selector']).html(),
				b: trigger['text']
			}
		};
	}
	
	var q = toggleExpander._params[domain];
 
	$(body['selector']).css({ 'height': 'auto', 'overflow': 'hidden' });

	var o = $(body['selector']).height();

	$(body['selector']).html((q['state'] == 1) ? q['body']['a'] : q['body']['b']);
	$(trigger['selector']).text((q['state'] == 1) ? q['trigger']['a'] : q['trigger']['b']);

	var n = $(body['selector']).height();

	if (q['state'] == 1) {
		$(body['selector']).html(q['body']['b']);
	}

	$(body['selector'])
		.css({ 'height': o, 'overflow': 'hidden' })
		.animate({ height: n }, 150, 'linear', function() {
			if (q['state'] == 1) {
				$(body['selector']).html(q['body']['a']);
			}
			toggleExpander._params[domain]['state'] = !q['state'];
		});
}

function superClone2(domain, src, dest, index) {
	if (typeof superClone2._params == 'undefined') {
		superClone2._params = {};
	}
	if (typeof superClone2._params[domain] == 'undefined') {
		superClone2._params[domain] = index;
	}
	
	var count = superClone2._params[domain];
	var element = $(src).clone(true).removeAttr('id').removeAttr('style');
	
	element.find(':input').each(function() {
		var _attr = {
			id: $(this).attr('id').replace('{n}', count)
		};
		
		switch ($(this).attr('type')) {
			case 'text':
				_attr['name'] = $(this).attr('name').replace('{n}', count);
				break;
			
			case 'textarea':
				_attr['name'] = $(this).attr('name').replace('{n}', count);
				break;
			
		    case 'file':
				_attr['name'] = $(this).attr('name').replace('{n}', count);
				break;
			
		    case 'checkbox':
			    _attr['name'] = $(this).attr('name').replace('{n}', count);
				_attr['value'] = $(this).attr('value').replace('{n}', count);
				break;
			
    		case 'hidden':
    			_attr['name'] = $(this).attr('name').replace('{n}', count);
				break;
			
			case 'radio':
				break;
		}
		
		$(this).attr(_attr);
	});
	element.find('label').each(function() {
	    var _for = $(this).attr('for').replace('{n}', count);

		$(this).attr({'for': _for});
	});
	element.find('select').each(function() {
	    var _id = $(this).attr('id').replace('{n}', count);
	    var _name = $(this).attr('name').replace('{n}', count);

		$(this).attr({'id': _id, 'name': _name});
	});
	
	$(dest).append(element);
	
	superClone2._params[domain]++;
}

/*
 * 
 */
function superClone(domain, selector, index) {
	if (typeof superClone._params == 'undefined') {
		superClone._params = {};
	}
	if (typeof superClone._params[domain] == 'undefined') {
		superClone._params[domain] = index;
	}

	var count = superClone._params[domain];
	var element = $(selector).clone(true);

	element.find(':input').each(function() {
		var _attr = {};

		switch ($(this).attr('type')) {
			case 'text':
			case 'textarea':
		    case 'file':
				_attr['id'] = $(this).attr('id').replace(count, count + 1);
				_attr['name'] = $(this).attr('name').replace(count, count + 1);
				_attr['value'] = '';
				break;

		    case 'checkbox':
				_attr['checked'] = false;
    		case 'hidden':
			case 'radio':
				_attr['id'] = $(this).attr('id').replace(count, count + 1);
				_attr['value'] = $(this).attr('value').replace(count, count + 1);
				break;
		}

		$(this).attr(_attr);
	});

	element.find('label').each(function() {
	    var _for = $(this).attr('for').replace(count, count + 1);

		$(this).attr({'for': _for});
	});
	element.find('select').each(function() {
	    var _id = $(this).attr('id').replace(count, count + 1);
	    var _name = $(this).attr('name').replace(count, count + 1);

		$(this).attr({'id': _id, 'name': _name});
	});

	$(selector).after(element);

	superClone._params[domain]++;
}

function dialogOpener2(href, cls, post) {
	$('#dialogWrapper')
		.removeClass()
		.addClass(cls);

	$('#dialogContent').hide();
	$('#dialog, #dialogSpinner, #dialogOverlay').show();

	$('#dialogWrapper, #dialogOverlay').bind('mousewheel', function(event, delta) { return false; });

	if (post) {
		$.post(href, $('#' + cls).serialize(), function(response) {
			$('#dialogContent').html(response);
			$('#dialogSpinner').hide();
			$('#dialogContent').show();
		});
	} else {
		$.get(href, {}, function(response) {
		    $('#dialogContent').html(response);
			$('#dialogSpinner').hide();
			$('#dialogContent').show();
		});
	}
}

/*
 * 
 */
function dialogOpener(object, cls, post) {
	$('#dialogWrapper')
		.removeClass()
		.addClass(cls);

	$('#dialogContent').hide();
	$('#dialog, #dialogSpinner, #dialogOverlay').show();

	$('#dialogWrapper, #dialogOverlay').bind('mousewheel', function(event, delta) { return false; });

	if (post) {
		$.post($('#' + cls).attr('action'), $('#' + cls).serialize(), function(response) {
			$('#dialogContent').html(response);
			$('#dialogSpinner').hide();
			$('#dialogContent').show();
		});
	} else {
		$.get(object.href, {}, function(response) {
		  $('#dialogContent').html(response);
			$('#dialogSpinner').hide();
			$('#dialogContent').show();
		});
	}
}
/**
 *
 */
function forum_quote(src, dst) {
	// to keep names hidden - use post number instead of author name
	//var alias = $(src + ' .fieldUserName').text();
	var alias = $(src + ' .fieldPostNumber').text();
	var body = $(src + ' .fieldBody').html();
	// get top position of the containing form and scroll there
	var y = Math.round($(dst).parents('form').position().top);

	$(dst).append('<blockquote><strong><em>' + alias + '</em></strong>' + body + '</blockquote><p></p>');
	$('html,body').scrollTop(y);
	//$('html,body').animate({scrollTop: y}, 500);

}

/**
 *
 */
function cloner(e, src, dest, i) {
	
	if (typeof $(e).data('cloner') == 'undefined') {
		$(e).data('cloner', i);
	}
	
	var count = $(e).data('cloner');
	var element = $(src).clone(true).removeAttr('id').removeAttr('style');
	
	element.find(':input').each(function() {
		var _attr = {
			id: $(this).attr('id').replace('{n}', count)
		};
			

		switch ($(this).attr('type')) {
			case 'text':
				_attr['name'] = $(this).attr('name').replace('{n}', count);
				break;
			
			case 'textarea':
				_attr['name'] = $(this).attr('name').replace('{n}', count);
				break;
			
		    case 'file':
				_attr['name'] = $(this).attr('name').replace('{n}', count);
				break;
			
		    case 'checkbox':
			    _attr['name'] = $(this).attr('name').replace('{n}', count);
				_attr['value'] = $(this).attr('value').replace('{n}', count);
				break;
			
    		case 'hidden':
    			_attr['name'] = $(this).attr('name').replace('{n}', count);
				break;
			
			case 'radio':
				break;
		}
		
		$(this).attr(_attr);
	});
	element.find('label').each(function() {
	    var _for = $(this).attr('for').replace('{n}', count);

		$(this).attr({'for': _for});
	});
	element.find('select').each(function() {
	    var _id = $(this).attr('id').replace('{n}', count);
	    var _name = $(this).attr('name').replace('{n}', count);

		$(this).attr({'id': _id, 'name': _name});
	});
	
	$(dest).append(element);
	
	$(e).data('cloner', ++count);
}

/**
 *
 */
function relation_locker(e, v) {
	e = $(e);
	
	if (typeof e.data('relation_locker') == 'undefined') {
		e.data('relation_locker', {state: true, val: e.html()});
	}
	
	var q = e.data('relation_locker');
	
	e.addClass('spinner');
	// LP - no need to send .json suffix
	// $.get(e.attr('href') + '.json', {}, function(r) {
	$.get(e.attr('href'), {}, function(r) {
		
		e.removeClass('spinner');
		if (r == 200) {
			e.toggleClass('lock');
			
			if (q['state']) {
				e.html(v);
			}
			else {
				e.html(q['val']);
			}
			
			q['state'] = !q['state'];
			
			e.data('relation_locker', q);
		}
	}, 'json');
}

/**
 *
 */
function dialog_load(object, cls, post) {
	$('#dialogWrapper')
		.removeClass()
		.addClass(cls);
	
	$('#dialogContent').hide();
	$('#dialog, #dialogSpinner, #dialogOverlay').show();
	
	if (post) {
		var href = null;
		var form = $(object).parents('form')
		
		if (typeof object.href != 'undefined') {
			var href = object.href;
		}
		else {
			var href = form.attr('action');
		}
		
		$.post(href, form.serialize(), function(response) {
			$('#dialogContent').html(response);
			$('#dialogSpinner').hide();
			$('#dialogContent').show();
		});
	} else {
		$.get(object.href + '.dialog', {}, function(response) {
		    $('#dialogContent').html(response);
			$('#dialogSpinner').hide();
			$('#dialogContent').show();
		});
	}
}

/**
 *
 */
function dialog_close() {
	$('#dialog, #dialogOverlay').hide()
	
	$('#dialogWrapper').removeClass($('#dialogWrapper').attr('class'));

	$('#dialogContent textarea').each(function (e) {
		var q = (typeof tinyMCE != 'undefined') ? tinyMCE.get($(this).attr('id')) :undefined;
		
		if (typeof q != 'undefined') {
			q.remove();
		}
	});

	$('#dialogContent').empty();
}

/**
 * function to auto remove objects if link succeed
 */
function element_remover(e, s, callback) {
	if (typeof s == 'undefined' || s == false || confirm(s)) {
		if (typeof callback == 'undefined' || callback(e)) {
			$(e).remove();
		}
	}
	
	return false;
}

/**
 * function to auto remove objects if link succeed
 */
function remover(e, o, s, callback) {
	if (typeof s == 'undefined' || s == false || confirm(s)) {
		$.get($(e).attr('href') + '.json', {}, function(r) {
			if (r == 200 && (typeof callback == 'undefined' || callback(e))) {
				$(o).remove();
			}
		}, 'json');
	}
	
	return false;
}

/**
 * add .trunc to strings
 */
String.prototype.trunc =
	function (n, word_boundary) {
		var to_long = this.length > n,
			s_ = to_long ? this.substr(0, n - 1) : this;
			s_ = word_boundary && to_long ? s_.substr(0, s_.lastIndexOf(' ')) : s_;
		return to_long ? s_ + '...' : s_;
	};


/**
 * function to toggle a link
 */
function toggle_link(e, c, u, v, s) {
	e = $(e);
	
	if (typeof e.data('toggle_link') == 'undefined') {
		e.data('toggle_link', {state: true, val: e.html(), href: e.attr('href')});
	}
	
	if (typeof s == 'undefined' || confirm(s)) {
		var q = e.data('toggle_link');
		
		e.addClass('spinner');
		$.get(e.attr('href') + '.json', {}, function(r) {
			e.removeClass('spinner');
			if (r == 200) {
				e.toggleClass(c);
				
				if (q['state']) {
					e
						.html(v)
						.attr('href', u);
				} else {
					e
						.html(q['val'])
						.attr('href', q['href']);
				}
				
				q['state'] = !q['state'];
				
				e.data('toggle_link', q);
			}
		}, 'json');
	};
	
	return false;
}


/**
 * function to toggel text (html)
 *
 */
function toggle_text(select, length, trigger, toggle, duration) {
	if(typeof duration === 'undefined') duration = 150;
	
	var data = $(select).data('toggle_text');
	if (typeof data == 'undefined') {
		data = {
			_short : false,
			_text : $(select).html(),
			_toggle : $(trigger).html()
		};
		/*toggle_text._short = false;
		toggle_text._text = $(select).html();
		toggle_text._toggle = $(trigger).html();
		$(select).data('toggle_text', 1);*/
		$(select).data('toggle_text', data);
	}
	var to_long = (data._text.length > length);
	
	var o = $(select).css({'height': 'auto', 'overflow': 'hidden'}).height()
	
	if (data._short) {
		$(select).html(data._text);
		$(trigger).html(data._toggle);
	} else {
		if(to_long) {
			$(select).html(data._text.trunc(length, true));
		}
		$(trigger).html(toggle);
	}
	
	var n = $(select).height();
	
	if (!data._short && to_long) {
		$(select).html(data._text);
	}
	
	$(select)
		.css({'height': o, 'overflow': 'hidden'})
		.animate({height: n}, duration, 'linear', function() {
			if (!data._short && to_long) {

				$(select).html(data._text.trunc(length, true));
			}
			
			data._short = !data._short;
			$(select).data('toggle_text', data);
		});
}

/**
 * function to toggel a div
 */
function toggle_div(select, time, trigger, toggle, auto) {
	var e = $(select);
	
	if (typeof e.data('toggle_div') == 'undefined') {
		e.data('toggle_div', {state: false, toggle: $(trigger).html()});
		
		if (typeof auto != 'undefined') {
			time = 0;
		}
	}
	
	var q = e.data('toggle_div');
	
	if (q['state']) {
		e.slideDown(time);
		$(trigger).html(q['toggle']);
	} else {
		e.slideUp(time);
		$(trigger).html(toggle);
	}
	
	q['state'] = !q['state'];
	
	e.data('toggle_div', q);
}

/**
 * function for toggled folding only selected parts (elements) of div
 */
function fold_div(elems, link, toggle, duration) {
	if(typeof duration === 'undefined') duration = 'fast';
	var data = $(link).data('FOLD');
	
	if(typeof data !== 'undefined') {
		if(data.folded) {
			if(data.toggleOff == '') {
				data.toggleOff = toggle;
			}
			toggle = data.toggleOff;
		}
		else {
			toggle = data.toggleOn;
		}
		data.folded = (data.folded) ? false : true;
		$(link).data('FOLD', data);
	}
	else {
		$(link).data('FOLD', {
			toggleOn : toggle,
			toggleOff : '',
			folded : true
		});
	}
	$(elems).slideToggle(duration);
	$(link).html(toggle);
}

/** function to select uploaded image URL into tinyMCE editor */
 function selectURL(url) {
	if (url == '') return false;
	//url = '../..' + url;
	field = window.top.opener.browserWin.document.forms[0].elements[window.top.opener.browserField];
	field.value = url;
	if (field.onchange != null) field.onchange();
	window.top.close();
	window.top.opener.browserWin.focus();
 }
