var flag = false;
var title = '';
var isLoading = false;

function intVal(value) {
	var intVal = parseInt(value);
	return isNaN(intVal) ? 0 : intVal;
}

function formatText(text, actions, charList) {
	if(actions.indexOf('strip') > -1) {
		text = text.replace(/(<[^<>]*>)/g, "");
	}
	if(actions.indexOf('trim') > -1) {
		text = text.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
	}
	var length = charList.length;
	for(var i = 0; i < length; i++) {
		var pos = text.indexOf(charList.charAt(i));
		if(pos > -1) {
			text = text.substring(0, pos) + text.substring(pos + 1, text.length);
		}
	}
	return text;
}

function validateEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(email);
}

function setCookie(name, value, expireDays, path) {
	var cookieValue = escape(value);
	if(typeof expireDays != 'undefined') {
		var expireDate = new Date();
		expireDate.setDate(expireDate.getDate() + expireDays);
		cookieValue += '; expires=' + expireDate.toUTCString();
	}
	if(typeof path != 'undefined') {
		cookieValue += "; path=/";
	}
	document.cookie = name + "=" + cookieValue;
}
		
function goTo(url) {
	document.location.href = url;
	return false;
}

function blink() {
	if(!flag) $('#promotion_field').addClass('selected');
	else $('#promotion_field').removeClass('selected');
	flag = !flag;
}

function initPromotion() {
	var promotion = $('#promotion_field');
	if(promotion.length > 0 && !promotion.hasClass('selected')) {
		setInterval('blink()', 1000);
	}
}

function toolTip(element, content) {
	if(content != '') {
		overlib(content);
		$(element).mouseout(function(e) {
			nd();
		});
	}
}
function imgTip(element, src, width, height) {
	var attributes = new Array();
	var style = '';
	if(typeof width != 'undefined') attributes.push('width:' + width + 'px'); 
	if(typeof height != 'undefined') attributes.push('height:' + height + 'px');
	if(attributes.length > 0) style = ' style="' + attributes.join(';') + '"';
	toolTip(element, '<img src="' + src + '"' + style + ' border="0">');
}
function colorTip(element, color) {
	toolTip(element, '<div style="background-color:' + color + ';width:20px;height:20px;"></div>');
}

function initTitles() {
	$('a').each(function() {
		if($(this).attr('title') && $(this).parent().attr('id') != 'gallery') {
			$(this).mouseover(function() {
				title = $(this).attr('title');
				$(this).attr('title', '');
			});
			$(this).mouseout(function() {
				$(this).attr('title', title);
			});
		}
	});
}

function ajaxLoad(source, destination, id) {
	if(isLoading) return false;
	var object = $(source).parent();
	var container = $('#' + (id ? destination + '_' + id : destination));
	var selectedContainer = $('#selected_' + (id ? destination + '_' + id : destination));
	if(container.html().length > 0) {
		var expand = object.hasClass('LIClosed');
		if(expand) {
			selectedContainer.hide();
			container.show();
			object.removeClass('LIClosed');
			object.addClass('LIOpen');
		}
		else {
			selectedContainer.show();
			container.hide();
			object.removeClass('LIOpen');
			object.addClass('LIClosed');
		}
	}
	else {
		object.removeClass('LIClosed');
		object.addClass('LILoading');
		isLoading = true;
		$.post('', {
			act: 'load_' + destination,
			t: 'ajax',
			aid: id
		}, function(response) {
			isLoading = false;
			selectedContainer.hide();
			container.html(response);
			container.show();
			object.removeClass('LILoading');
			object.addClass('LIOpen');
			initTitles();
		});
	}
	return false;
}

function initLanguages() {
	$('#lango a').each(function() {
		$(this).click(function () {
			$('#language').val($(this).attr('id'));
			$('#change_lang').submit();
			return false;
		});
	});
}
