var newsListFormSubmit = function () {
	new Ajax.Request('/ajax.php?function=subscribe', {parameters: $('newsListForm').serialize(),
        onComplete: function(transport){
            //console.log();
            show_lightbox(transport.responseText);
        }
    });
};

var show_lightbox = function (text) {
    Modalbox.show("<br><br><br><center><span style='font-size: 16px;font-weight: bold;'>" + text, {title: '<div style="float: right;height: 20px;color: white;"><a href="#" onclick="Modalbox.hide();">X</a></div><div>&nbsp;</div>', width: 300,height : 150});
    Modalbox.deactivate();
};


var show_lightboxtwo = function (text, myTitle) {
	if (!myTitle) {
		myTitle = '';
	}
    //Modalbox.show("<center><span style='font-size: 16px;font-weight: bold; color:black;'>" + text, {title: myTitle +  '<div style="float: right;height: 20px;color: white;"><a href="#" onclick="Modalbox.hide();"><strong>X</strong></a></div><div>&nbsp;</div>', width: 350,height : 250});
	Modalbox.show("<center><span style='font-size: 16px;font-weight: bold; color:black;'>" + text, {title: myTitle +  '<div style="float: right;height: 20px;color: white;"><a href="#" onclick="Modalbox.hide();"><strong>X</strong></a></div><div>&nbsp;</div>', width: 350});
    Modalbox.deactivate();
};

var forgotPassword = function (email) {
   	new Ajax.Request('/ajax.php?function=forgot_password&email='+email, {
        onComplete: function(transport){
            show_lightboxtwo(transport.responseText, '<div style="float: left; text-shadow: none;">Retrieve Your Password:</div>');
        }
    });
};

var restoryPassword = function (email) {
	new Ajax.Request('/ajax.php?function=restoryPass&email='+email, {
        onComplete: function(transport){
            show_lightboxtwo(transport.responseText);
        }
    });
};

/**
 * Показывает индикатор загрузки с указанным текстом.
 * @param text Текст для отображения в индикаторе загрузки.
 * @param nom Ключ для выбора текста из списка возможных вариантов.
 * @return
 */
var ajaxIndicatorShov = function(text, nom) {
	// Текст по умолчанию.
	if (text == '') { 
		text = 'Please wait.';
	}
	// Список возможных вариантов.
	var key = { 
		login:'',
		registration:'',
		billing:'',
		shipping:'',
		order_cartdata:'Please wait, checking your credit card.',
		order_cartdatap:'Stand By while we take you to PayPal.',
		newsletter:'',
		newsletter:'Order Confirmation Details.',
		customer_newsletter:'Submitting Your Details.'
	};
	try {
		if(key[nom] != '') {
			text = key[nom];
		}
	} catch (e) {
		// Ничего не делаем
	}

	$j('#ajax-indicator-text').text(text);
	$j('#ajax-indicator').show();
};
var ajaxIndicatorHide = function() {
	$j('#ajax-indicator').hide();
};


/*
 * Показать или Скрыть сообщения о ошибках.
 */

/**
 * ID активного элемента.
 */
focusId = '';
/**
 * Время через которое все сообшения об ошибках скроются.
 */
timeVisibleEroroMesages = 5000;

/**
 * Время за которое показывается сообшение об ошибке
 */
timeInEroroMesages = 1000;
/**
 * Время за которое скрываеться сообшение об ошибке
 */
timeOutEroroMesages = 1000;

$j(document).ready(function() {
    // Спрятать сообщения об ошибках. После загрузки страницы, через некоторе время.
    $j(document).everyTime(timeVisibleEroroMesages, function(i) {
        $j('.error_border div.error').each( function() {
            var inp = $j(this).next('input, select');
            inp.data('val', inp.val());
            if ( inp.attr('id') != focusId) {
                $j(this).fadeOut(timeOutEroroMesages);
            }
        });
    }, 1);
    
    // Стать на первую ошибку.
    var focus = $j('div.error_border input:first').focus();
    focus.focus();
    focusId = focus.attr('id');
    
    // При наведении или выборе показывать тест ошибки. Иначе скрывать.
    $j('div.error_border input, div.error_border select').hover(
        function() { // Показать
            if ( !$j(this).hasClass('error_border') ) {
                return false;
            }
            $j(this).prev('div.error').fadeIn(timeInEroroMesages);
            //focusId = $j(this).attr('id');
        }, 
        function() { // Скрыть есле не вабрано для редактирования.
            var erMes = $j(this).prev('div.error');
            var id = $j(this).attr('id');
            if (focusId != id) {
                erMes.fadeOut(timeOutEroroMesages);
            }
        }
    ).select(
        function(){ // Показать
            if ( !$j(this).hasClass('error_border') ) {
                return false;
            }
            $j(this).prev('div.error').fadeIn(timeInEroroMesages);
            focusId = $j(this).attr('id');
        }
    ).blur( 
        function(){ // Скрыть
            $j(this).prev('div.error').fadeOut(timeOutEroroMesages);
            focusId = '';
        }
    ).change(function () {
        // Если ввели данные, то скрываем сообщение об ошибке.
        var jthis = $j(this);
        if (jthis.val() != jthis.data('val')) {
            jthis.parent('.error_border').removeClass('error_border');
        }
    });
;
});