$(document).ready(function(){
    
    //    Top login form
    $('#calder_login_form .login_field input').focus(function(){
        $(this).css('color', '#333');
    });
    $('#calder_login_form .login_field input').blur(function(){
        $(this).css('color', '#999');
    });

    $("#form-login").validate({
        rules: {
            username: {
                minlength: 2
            },
            password: {
                minlength: 4
            }
        }
    });
    
    $('#search_site_form').validate({
        rules:{
            searchword:{
                minlength: 4
            }
        }
    })

    $('h3.search_title:first').css('margin-top', '0');

    $('#form-login').submit(function(){
        //Clear defaults
        if ($('#username').val()=='username'){
            $('#username').val('');
        }
        if ($('#password').val()=='password'){
            $('#password').val('');
        }
        if ($('#form-login').valid()){
            return true;
        }else{
            alert('Red fields are required');
        }
        return false;
    });


    //Block search if empty request
    $('#calder_search_site form').submit(function(){
        search_str = $(this).find('input:first');
        if (($(this).find('input:first').val()=='search') || ($(this).find('input:first').val()=='')){
            alert('Please, enter something to search');
            $(search_str).blur();
            return false;
        }
        error_str = $(this).find('label.error').text();
        if (error_str != ''){
            alert(error_str);
            return false;
        }
    });

    //    Call me request
    $("#call_me").validate({
        rules: {
            call_name: {
                required: function(element) {
                    return ($('#call_name').val() != 'name');
                },
                minlength: 4
            },
            call_phone: {
                required: function(element) {
                    return ($(element).val() != 'telephone');
                },
                minlength: 4
            }
        }
    });

    $('#call_me').submit(function(){
        //Clear defaults
        if ($('#call_name').val()=='name'){
            $('#call_name').val('');
        }
        if ($('#call_phone').val()=='telephone'){
            $('#call_phone').val('');
        }
        if ($('#call_email').val()=='e-mail'){
            $('#call_email').val('');
        }
        if (!$(this).valid()){
            alert('Red fields are required');
            if ($('#call_name').hasClass('error')) $('#call_name').val('name');
            if ($('#call_phone').hasClass('error')) $('#call_phone').val('telephone');
            if ($('#call_email').hasClass('error')) $('#call_email').val('e-mail');
            $('#callme_form input').blur();
        }else{
            web_path = $('#web_path').text();
            call_name = $('#call_name').val();
            call_phone = $('#call_phone').val();
            call_email = $('#call_email').val();
            ip = $('p#ip_adress').text();
            $.ajax({
                url: web_path+"ajax/callme.php",
                type: "POST",
                dataType: 'json',
                data: {
                    name:call_name,
                    phone:call_phone,
                    mail:call_email,
                    ip:ip
                },
                success: function(data){
                    jQuery('#call_name').val('name');
                    jQuery('#call_phone').val('telephone');
                    jQuery('#call_email').val('e-mail');
                    $('#callme_form input').blur();
                    alert(data.result);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    alert('Error occured. Try again.');
                }
            });
        }
        return false;
    });

    //    Subscriptions
    $("#subscipt_news").validate({
        rules: {
            s_name: {
                minlength: 4
            }
        }
    });

    $('#subscipt_news').submit(function(){
        //Clear defaults
        if ($('#s_name').val()=='Name'){
            $('#s_name').val('');
        }
        if ($('#s_email').val()=='E-mail'){
            $('#s_email').val('');
        }

        web_path = $('#web_path').text();
        s_name = $('#s_name').val();
        s_email = $('#s_email').val();
        
        if (!$('#subscipt_news').valid()){
            alert('Red fields are required');
            if ($('#s_name').hasClass('error')) $('#s_name').val('Name');
            if ($('#s_email').hasClass('error')) $('#s_email').val('E-mail');
            $('#subscipt_news input').blur();
        }else{
            //Show indicator
            $.ajax({
                url: web_path+"ajax/subscript.php",
                type: "POST",
                dataType: 'json',
                data: {
                    name:s_name,
                    mail:s_email
                },
                success: function(data){
                    jQuery('#s_name').val('Name');
                    jQuery('#s_email').val('E-mail');
                    $('#subscipt_news input').blur();
                    alert(data.result);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    alert('Erroe occured. Try again.');
                }
            });
        }
        
        return false;

        $.ajax({
            url: web_path+"send_form.php",
            type: "GET",
            dataType: 'json',
            data: {
                mail_sendto:mail_sendto,
                ip_adress:ip_adress,
                name:name,
                phone:phone,
                mail:mail
            },
            success: function(data){
                jQuery('#call_name').val('name');
                jQuery('#call_phone').val('telephone');
                jQuery('#call_email').val('e-mail');
                alert('Message send!');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert('Error occured. Try again.');
            }
        });


    });

});

function show_indicator (){
    web_path = $('#web_path').text();
    //Append div to prevent click
    $('body').append('<div id="operation_started"></div>');
    $('body').append('<div id="operation_indicator"><div id="operation_content"></div></div>');
    $('#operation_content').append('<p>Please wait...</p><img src="'+web_path+'images/ajax-loader.gif" alt=""/>');
    hh = $('#calder_float').height();
    $('#operation_started').css('height', hh+'px');
    st = $(window).scrollTop()+($(window).height()/2)-50;
    $('#operation_indicator').css('top',st+'px');
    $('#operation_started').fadeTo(0, 0.7);
    $('#operation_indicator').show();
}
function remove_indicator(){
    $('#operation_content').remove();
    $('#operation_indicator').remove();
}
