﻿$(function () {

    $('#q1').click(function () {

        $('#box').slideToggle('slow', changeLinkText);
        return false;

    });

    $('#q1').mouseover(function () {

        $(this).css('color', '#00cf13');

        flipArrow($(this).siblings('img').get(0), 'green'); // = 'img/down_alt_green.png';

    });

    $('#q1').mouseout(function () {

        $(this).css('color', 'red');

        flipArrow($(this).siblings('img').get(0), 'red');

    });

    // form name
    $('input.i-name').focus(function () { 
		
		if($(this).val() == 'Ваше имя') {
			$(this).val('');
			$(this).css('color','white');
		} 
	});
    $('input.i-name').blur(function () { $(this).val() == '' ? $(this).val('Ваше имя') : null; });
	$('input.i-name').keypress(function () { $(this).css('color','white'); });
    // form mail
    $('input.i-mail').focus(function () {
        if ($(this).val() != 'E-mail') {

            checkMail($(this).val()) ? null : $(this).css('color', 'red');
        }
        else
            $(this).val('');
    });
    $('input.i-mail').blur(function () {

        var mailbox = $(this);
        mailbox.val() == '' ? mailbox.val('E-mail') : (checkMail(mailbox.val()) ? null : mailbox.css('color', 'red'));
    });
    $('input.i-mail').keypress(function () { $(this).css('color','white'); });

    //.subscribe-button
    $('input.subscribe-button').live('mouseover', function () { $(this).get(0).src = 'img/green.png'; });
    $('input.subscribe-button').live('mouseout', function () { $(this).get(0).src = 'img/red.png'; });
    $('input.subscribe-button').live('click', function () {

        var mailbox = $(this).closest('form').find('input#field_email');
		var namebox = $(this).closest('form').find('input#field_name_first');

        if (!checkMail(mailbox.val())) {
            mailbox.css('color', 'red');
            mailbox.focus();
            return false;
        }
		else if(namebox.val() == '' || namebox.val() == 'Ваше имя'){
            namebox.css('color', 'red');
            namebox.focus();
            return false;
        }
        else
            return true;

    });

    if (document.images) {

        var imagesPreload = new Object();

        imagesPreload["up_red"] = new Image(32, 32);
        imagesPreload["up_green"] = new Image(32, 32);
        imagesPreload["down_green"] = new Image(32, 32);
        imagesPreload["green_btn"] = new Image(344, 57);
        imagesPreload["up_red"].src = "img/up_alt_red.png";
        imagesPreload["up_green"].src = "img/up_alt_green.png";
        imagesPreload["down_green"].src = "img/down_alt_green.png";
        imagesPreload["green_btn"].src = "img/green.png";
    }
});



function flipArrow(elt, color, direction) {



    if (typeof elt == 'undefined' || typeof color == 'undefined')

        return false;



    var direction = (typeof direction == 'undefined') ? ($('#box').is(':visible') ? 'up' : 'down') : direction;

    elt.src = 'img/' + direction + '_alt_' + color + '.png';

    return true;

}

function changeLinkText(elt) {

    var aLink = $('#q1');
    var color = aLink.css('color').toLowerCase() == 'rgb(0, 207, 19)' ? 'green' : 'red';

    if ($('#box').is(':visible')) {
        aLink.text('Скрыть подробный список выпусков');
        flipArrow(aLink.siblings('img').get(0), color, 'up');
    }
    else {
        aLink.text('Показать подробный список выпусков');
        flipArrow(aLink.siblings('img').get(0), color, 'down');
    }

    return false;
}

function checkMail(email) {

    if (typeof email == 'undefined')
        return false;

    var emailTemplate = new RegExp('^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$', 'i');

    return emailTemplate.test(email) ? true : false;
}
