
var SiteFunctions = function() {

	this.AlertDiv = 'AlertWindow';

	this.createCookie = function(name, value, days) {

		if(days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = '; expires=' + date.toGMTString();
		}
		else {
			var expires = '';
		}
		document.cookie = name + '=' + value + expires + '; path=/';
	};

	this.readCookie = function(name) {

		var nameEQ = name + '=';
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while(c.charAt(0) == ' ') {
				c = c.substring(1, c.length);
			}
			if(c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length, c.length);
			}
		}

		return null;
	};

	this.eraseCookie = function(name) {

		this.createCookie(name, '', -1);
	};

	this.openSearch = function() {

		if($j('.searchmenu_wrap').css('display') == 'none') {
			$j('.searchmenu_wrap').fadeIn('fast');
			this.eraseCookie('NoSearch');
		} else {
			$j('.searchmenu_wrap').fadeOut('fast');
			this.createCookie('NoSearch', true, 31);
		}

		return false;
	};

	this.globalSearch = function() {

		if(!$j('#globalSearchQuery').val()) {
			$j('#globalSearchQuery').focus();
			return false;
		} else {
			document.location = '/search/?Query=' + $j('#globalSearchQuery').val();
		}

		return false;
	};

	this.globalSearchKey = function(e) {

		var keycode = window.event ? window.event.keyCode : e.which;
		if(keycode == 13) {
			this.globalSearch();
		}
	};

	this.animateLogo = function() {

		$j('#header-mainnew #logoempty').animate({ top:-21 }, 300);
		$j('#header-mainnew #strela').delay(380).animate({ top:-21 }, 220);
		$j('#header-mainnew #strela').animate({ top:-37 }, 130);
		$j('#header-mainnew #strela').animate({ top:-21 }, 120);
		$j('#header-mainnew #strela').animate({ top:-30 }, 100);
		$j('#header-mainnew #strela').animate({ top:-21 }, 90);
		$j('#header-mainnew #strela').animate({ top:-24 }, 70);
		$j('#header-mainnew #strela').animate({ top:-21 }, 60);
	};

	this.alertShow = function(Message, isFaded) {

		$j('#' + this.AlertDiv).find('p').html(Message);

		this.alertCenter();
		$j('#' + this.AlertDiv).show();

		if(isFaded) {
			$j('#' + this.AlertDiv).delay(1000).fadeOut('fast');
		}
	};

	this.alertCenter = function() {

		this.centerDiv(this.AlertDiv);
	};

	this.alertClose = function() {

		$j('#' + this.AlertDiv).hide();

		return false;
	};

	this.centerDiv = function(DivID) {

		var dimensions = { width:0, height:0 };
		dimensions.width = $j('#' + DivID).width();
		dimensions.height = $j('#' + DivID).height();

		var positions = { x:0, y:0 };
		positions.x = $j(window).width() / 2 - dimensions.width / 2;
		positions.y = $j(document).scrollTop() + $j(window).height() / 2 - dimensions.height / 2;

		$j('#' + DivID).css('top', positions.y);
		$j('#' + DivID).css('left', positions.x);
	};

	this.BuyPremium = function(Type) {

		var data = 'Action=Buy&Type=' + Type;

		var self = this;
		$j.ajax({
			type: 'POST',
			url: '/engine/premium.php',
			data: data,
			success: function(response) {
				eval('var response = ' + response + ';');

				if(response.Error) {
					self.alertShow(response.ErrorText, true);
				} else {
					window.location.reload();
				}
			}
		});

		return false;
	};

	this.PaySMS = function() {

		if(!$j('#SMS_Amount').val()) {
			$j('#SMS_Amount').focus();
		} else if(!parseInt($j('#SMS_Amount').val())) {
			$j('#SMS_Amount').select().focus();
		} else {
			$j.ajax({
				type: 'POST',
				url: '/engine/pay.php',
				data: 'Method=SMS&Amount=' + $j('#SMS_Amount').val(),
				success: function(response) {
					eval('var response = ' + response + ';');

					$j('#Payform_SMS_s_purse').val(response.s_purse);
					$j('#Payform_SMS_s_order_id').val(response.s_order_id);
					$j('#Payform_SMS_s_amount').val(response.s_amount);
					$j('#Payform_SMS_s_clear_amount').val(response.s_clear_amount);
					$j('#Payform_SMS_s_description').val(response.s_description);
					$j('#Payform_SMS_s_sign').val(response.s_sign);
					$j('#Payform_SMS').submit();
				}
			});
		}

		return false;
	};
};

var Site = new SiteFunctions();

