function cururl(ajax) {
	var url = window.location.href;
	url = url.replace(/#/, '');
	
	if(ajax === true)
	{
		url += url.match('/\?/') ? '&ajax=true' : '?ajax=true';
	}
	
	return url;
}

function currUrl(add)
{
	return currentUrl + (currentUrl.match('/\?/') ? '&' : '?') + add;
}

function cleancururl()
{
	var url = currentUrl.replace(/addcosts=true/, '');
	url = url.replace(/addpicture=true/, '');
	url = url.replace(/addinvalid=true/, '');
	url = url.replace(/ajax=true/, '');
	url = url.replace(/sendtofriend=true/, '');
	url = url.replace(/sendmessage=true/, '');
	url = url.replace(/\?(\&)?$/, '')

	return url;
}

var currentUrl 		= cururl(false);
var currentUrlAjax 	= cururl(true);
var currentUrlClean = cleancururl();
var web_url			= window.location.protocol+'//'+window.location.hostname+'/';
var totalAjaxRequests = 0;

function ajaxLoadStart()
{
	window.addEvent('domready', function () {
		var ajaxElement = 'ajax-loading-icon';
		if(!$defined($(ajaxElement))) {
			var container = new Element('div', {
				'id' : ajaxElement,
				'styles' : {
					'position': 'fixed',
					'top' : 150,
					'right' : 0,
					'width' : 100,
					'height' : 50,
					'z-index' : 1000,
					'display' : 'block',
					'overflow' : 'hidden',
					'background' : "url('/img/ajax-loading-background.gif') no-repeat"
				}
			});
			
			if(Browser.Engine.trident) {
				var scroll = document.documentElement.scrollTop;
				container.setStyles({
					'position' : 'absolute',
					'top' : 100 + scroll
				});
				window.addEvent('scroll', function () {
					var scroll = document.documentElement.scrollTop;
					container.setStyle('top', 100 + scroll);
				});
			}
	
			new Element('img', {
				'src' : '/img/ajax-loader.gif',
				'width' : '24',
				'height' : '24',
				'alt' : 'ajax loader',
				'styles' : {
					'margin-left' : 25,
					'margin-top' : 7
				}
			}).inject(container);
			container.inject($(document).getElement('body'));
		}
		totalAjaxRequests++;
		$(ajaxElement).setStyle('display', 'block');
		$(ajaxElement).get('tween', {duration: 'short', transition : Fx.Transitions.Back.easeOut}).start('width', 0, 80);
		$(ajaxElement).addClass('ajax-load-open');
	});
}

function ajaxLoadStop() {
	window.addEvent('domready', function () {
		totalAjaxRequests--;
		if(totalAjaxRequests == 0)
		{
			var ajaxElement = 'ajax-loading-icon';
			$(ajaxElement).get('tween', {duration: 'short', transition : Fx.Transitions.Back.easeIn}).start('width', 80, 0);
			$(ajaxElement).removeClass('ajax-load-open');
		}
	});
}

function ajaxLoadToggle() {
	var ajaxElement = 'ajax-loading-icon';
	if(!$defined($(ajaxElement))) {
		ajaxLoadStart();
		return;
	}
	if($(ajaxElement).hasClass('ajax-load-open')) {
		ajaxLoadStop();
	}
	else
	{
		ajaxLoadStart();
	}
}

/*
var popupText = new Class({
	title : null,
	content : null,
	
	overlay : null,
	container : null,
	
	options : {
		width: 600,
		height : 'auto'
	},
	
	Implements: Options,
		
	initialize : function (element, title, content, options) {
		this.setOptions(options);
		this.title = title;
		this.content = content;
		
		element.setProperty('onclick', '');
		
		element.addEvent('click', function(event) {
			event.stop();
			
			this.show();
		}.bind(this));
		
		this.show();
	},
	
	show : function() {
		if(this.overlay === null && this.container === null) {
			this.render();
		}
		
		this.overlay.removeClass('d-none');
		this.container.removeClass('d-none');
	},
	
	close : function () {
		this.overlay.addClass('d-none');
		this.container.addClass('d-none');
	},
	
	render : function() {
		var coord = window.getCoordinates();
		
		this.overlay = new Element('div', {
			'styles' : {
				'position' : 'fixed',
				'top' : 0,
				'left' : 0,
				'right' : 0,
				'bottom' : 0,
				'background' : 'white',
				'overflow' : 'hidden',
				'z-index' : '140',
				'opacity' : 0.6
			},
			'class' : 'd-none',
			'events': {
				'click' : function (event) {
					this.close();
				}.bind(this)
			}
		});
		
		this.container = new Element('div', {
			'styles' : {
				'position' : 'fixed',
				'width' : this.options.width,
				'top' : 100,
				'left' : (coord.width - 600) / 2,
				'overflow' : 'hidden',
				'z-index' : '150',
				'height' : this.options.height
			},
			'class' : 'd-none'
		});
		
		new Element('div', {
			'class' : 'title',
			'html' : this.title
		}).inject(this.container);
		
		new Element('a', {
			'href' : '#',
			'events' : {
				'click' : function (event) {
					event.stop();
					this.close();
				}.bind(this)
			},
			'html' : 'x'
		}).inject(
			new Element('div', {
				'class' : 'close'
			}).inject(this.container)
		);
		
		new Element('div', {
			'class' : 'content',
			'html' : this.content
		}).inject(this.container);
		
		this.overlay.inject(document.getElement('body'), 'after');
		this.container.inject(document.getElement('body'), 'after');
	}
});
*/

function pagerLinksAjax(cl, cont) {
	$(document).getElements('.'+cl+' a').each(function (el) {
		el.addEvent('click', function(event) {
			event.stop();
			
			var url = el.getProperty('href');
			url += url.contains('?') ? '&ajax=true' : '?ajax=true';
			
			ajaxLoadStart();
			
			new Request.HTML({
				url : url,
				update : $(cont),
				onSuccess : function() {
					ajaxLoadStop();
				}
			}).send();
		});
	});
}

/*
var UserData = new Class({
	dataKey : null,
	url : null,
	cookie : null,
	response : null,
	
	initialize : function(key) {
		this.dataKey = key;
		this.url = window.location.protocol + '//' + window.location.hostname + '/a/?ajax=true';
		this.cookie = new Cookie(this.dataKey, {
			'path' : '/'
		});
	}
});

UserData.get = function(key) {
	var cookie = new Cookie(key, {
		'path' : '/'
	});
	
	var value = cookie.read();
	
	if(value)
	{
		jsonValue = JSON.decode(value);
		if(!isNaN(jsonValue)) {
			return jsonValue;
		} else {
			return value;
		}
	}
	
	var url = window.location.protocol + '//' + window.location.hostname + '/a/?ajax=true';
	
	var data = 'key='+key;
	this.response = false;
	ajaxLoadStart();
	new Request({
		url : url+'&c=getuserdata',
		method : 'post',
		onSuccess : function(resp) {
			ajaxLoadStop();
			this.response = resp;
		}.bind(this)
	}).send(data);
	
	return null;
}

var sentState = {};

UserData.set = function (key, value) {
	var url = window.location.protocol + '//' + window.location.hostname + '/a/?ajax=true';
	var cookie = new Cookie(key, {
		'path' : '/'
	});
	
	if(!$defined(sentState[key]))
	{
		sentState[key] = false;
	}
	
	var link = sentState[key] ? 'cancel' : 'chain';
	
	sentState[key] = true;
	
	var data = 'key='+key+'&value='+value;
	cookie.write(JSON.encode(value));
	ajaxLoadStart();
	new Request({
		url : url + '&c=saveuserdata',
		method : 'post',
		link : link,
		onSuccess : function () {
			sentState[key] = false;
			ajaxLoadStop();
		}
	}).send(data);
}
*/

function centerElement(id) {
	var element = $(id);
	if(!element) {
		return false;
	}
	
	var hadDNone = false;
	
	element.addClass('v-hidden');
	
	if(element.hasClass('d-none'))
	{
		hadDNone = true;
		element.removeClass('d-none');
	}
	
	//var coord = window.getCoordinates();
	//var elementSize = element.getCoordinates();
	
	var left = (($(window).width() - $(element).width()) / 2);
	var top = (($(window).height() - $(element).height()) / 2) * 0.7;
	
	if(jQuery.browser.msie && jQuery.browser.version  < 7)
	{
		var newtop = Math.round(top + $(window).scrollTop());
		element.css('top', newtop);
		element.css('position', 'absolute');
		
		$(window).scroll(function() {
			var newtop = top + $(window).scrollTop();
			
			element.css('top', newtop);
		});
	} else {
		element.css('position', 'fixed');
		element.css('top', top);
	}
	
	element.css('left', left);
	
	if(hadDNone)
	{
		element.addClass('d-none');
	}
	
	element.removeClass('v-hidden');
	
	$(window).resize(function() {

		var left = (($(window).width() - $(element).width()) / 2);
		var top = (($(window).height() - $(element).height()) / 2) * 0.7;
		
		if(jQuery.browser.msie && jQuery.browser.version  < 7)
		{
			top += $(window).scrollTop();
		}
		
		element.css('left', left);
		element.css('top', top);
		
	});
}

var overlayClone = false;

function fixOverlay()
{
	if(jQuery.browser.msie && jQuery.browser.version  < 7)
	{
		var overlay = $('#ajax-popup-overlay');

		if(overlay.length > 0)
		{
			overlay.css('width', window.scrollY);
			overlay.css('height', window.scrollX);
		}
	}
}

if(jQuery.browser.msie && jQuery.browser.version  < 7)
{
	$(document).ready(fixOverlay);
}

function toggleOverlay(id) {
	var overlay = $('#ajax-popup-overlay');

	if(overlayClone == false)
	{
		overlayClone = overlay.clone();
		
		if(!overlayClone.hasClass('d-none'))
		{
			overlayClone.addClass('d-none');
		}
	}
	
	if(overlay.length == 0)
	{
		overlay = overlayClone.clone();
		overlay.attr('id', 'ajax-popup-overlay');
		overlay.appendTo($('body'));
	}
	
	var element = $(id);
	
	if(element.length == 0)
	{
		return true;
	}

	element.css('z-index', overlay.css('z-index') + 1);

	if(element.hasClass('d-none')) {
		centerElement(element);
		
		if(jQuery.browser.msie && jQuery.browser.version  < 7)
		{
			fixOverlay();

			$$('select').each(function (el) {
				el.addClass('v-hidden');
			});
		}	
		element.removeClass('d-none');
		overlay.removeClass('d-none');
		overlay.click(function () {
			toggleOverlay(id);
			return false;
		});
	} else {
		if(jQuery.browser.msie && jQuery.browser.version  < 7)
		{
			$$('select').each(function (el) {
				el.removeClass('v-hidden');
			});
		}
		element.remove();
		overlay.remove();
	}
	
	return false;
}

function closeAjaxOverlay(el) {
	var parent;
	var current = $(el);
	var found = false;
	
	while(parent = current.parent())
	{
		if(!parent) {
			break;
		}
		if(parent.hasClass('popup'))
		{
			found = true;
			break;
		}
		
		current = parent;
	}
	
	if(found)
	{
		var url = el.href.replace(/#/, '');
		if(url == currentUrl)
		{
			var id = parent.attr('id');
			toggleOverlay(parent);
		}
		else
		{
			window.location = url;
		}
		
		return false;
	}
	
	return true;
}

function confirmPopup(el, key, title, valTrue, valFalse, moretext, options)
{
	var texts = moretext || false;
	
	var options = options || false;
	
	var popup = new Element('div', {'class' : 'popup pop-confirm-c d-none', 'id' : 'overlay-confirm-form' });
	new Element('div', { 'class' : 'title', 'html' : title }).inject(popup);
	new Element('div', { 'class' : 'c' }).inject(popup);
	
	var content = new Element('div', { 'class' : 'content' }).inject(popup);
	if(texts)
	{
		new Element('p', { 'html' : texts }).inject(content);
	}
	
	if(el)
	{
		url = el.getProperty('href')
	}
	else
	{
		url = currentUrl;
	}
	
	var form = new Element('form', {
		'method' : 'post',
		'action' : url
	}).inject(content);
	
	if(options && $defined(options.onSubmit))
	{
		form.addEvent('submit', options.onSubmit);
	}
	
	var buttons = new Element('div', { 'class' : 'buttons' }).inject(form);
	
	new Element('input', { 'type' : 'hidden', 'name' : 'confirm_key', 'value' : key }).inject(buttons);
	
	var yes = new Element('div', { 'class' : 'button-green' }).inject(buttons);
	new Element('div', { 'class' : 'left' }).inject(yes);
	new Element('input', {
		'type' : 'submit',
		'name' : 'yes',
		'value' : valTrue,
		'class' : 'middle'
	}).inject(yes);
	new Element('div', { 'class' : 'right' }).inject(yes);
	
	var no = new Element('div', { 'class' : 'button-gray' }).inject(buttons);
	new Element('div', { 'class' : 'left' }).inject(no);
	new Element('input', {
		'type' : 'submit',
		'name' : 'no', 
		'value' : valFalse,
		'class' : 'middle',
		'events' : {
			'click' : function (event) {
				event.stop();
				toggleOverlay($('overlay-confirm-form'));
				popup.dispose();
			}
		}
	}).inject(no);
	new Element('div', { 'class' : 'right' }).inject(no);
	
	popup.inject(document.getElement('body'));
	
	toggleOverlay($('overlay-confirm-form'));
	
	centerElement($('overlay-confirm-form'));
	
	return false;
}

var symbolCounter = function(el, limit)
{
	window.addEvent('domready', function () {
		if(!$(el))
		{
			return false;
		}

		var coord = $(el).getCoordinates();
		var counter_c = new Element('div', {
			'class' : 'sybmol-counter', 
			'styles' : {
				'width' : coord.width,
				'height' : 'auto',
				'text-align' : 'right'
			},
			'html' : limit - $(el).get('value').length
		});

		counter_c.inject($(el), 'after');

		$(el).addEvent('keyup', function (event) {
			var chars = $(el).get('value').length;
			if(chars > limit)
			{
				$(el).set('value', $(el).get('value').substr(0, limit));
			}
			chars = $(el).get('value').length;
			counter_c.set('html', limit - chars);
		});
	});
}