$(document).ready(function(){
    $('#cba').hover(
        function () {
            $(this).attr('src', '/images/smi_contact_button_on.png');
            $(this).css('cursor', 'pointer');
        },
        function () {
            $(this).attr('src', '/images/smi_contact_button_off.png');
        }
    ).click(function() {
        window.location='#email_anchor';
        $('#your_name').focus();
    });
    $('#cbp').hover(
        function () {
            $(this).attr('src', '/images/smi_contact_button_on_p.png');
            $(this).css('cursor', 'pointer');
        },
        function () {
            $(this).attr('src', '/images/smi_contact_button_off_p.png');
        }
    ).click(function() {
        window.location='#email_anchor';
        $('#your_name').focus();
    });
    $('#reply INPUT, #reply TEXTAREA').focus(function() {
        $(this).css({background: '#ff9'});
    }).blur(function() {
        $(this).css({background: '#fff'});
    });
    $('#submit_reply').hover(
        function () {
            $(this).attr('src', '/images/smi_email_button_on.png');
        },
        function () {
            $(this).attr('src', '/images/smi_email_button_off.png');
        }
    );
    $('form#reply').submit(function()
    {
        var values = {};
        $('form#reply input, form#reply textarea').each(function(index, element)
        {
            values[element.name] = $(element).val();
        });
        
        var is_valid = validate_user_inputs(values);
        
        // Don't submit the form if there are any warnings
        if( is_valid == false )
        {
            return false;
        }
        
        values['action'] = 'reply';
        $('form#reply #loading').show();
        $.post('/ajax_endpoint.php', values, function(data)
        {
            $('form#reply #loading').hide();
            $('form#reply #submit_reply').show();
            
            if(data['errors'].length == 0)
            {
                // Submission successful.
                $('form#reply #thankyou').show();
                $('form#reply #errors, form#reply #submit_reply').hide();
                
                // Track this event in Google Analytics.
                pageTracker._trackEvent("ad_reply", $('input[name=type]').val(), $('input[name=id]').val());

                // Track Reply Conversion
                $('#g_rsc').append('<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1069610512/?label=oLF4CN7xvgEQkOyD_gM&guid=ON&script=0" />');
            }
            else
            {
                // Oh noes, errors!
                $('#reply #errors').empty();
                
                $('#reply #errors').append(sprintf("<strong>W twojej wiadomości wystąpiły błędy:</strong>"));
                $('#reply #errors').append("<ol>");
                for(var index = 0; index < data['errors'].length; index++)
                    $('#reply #errors OL').append(sprintf("<li>%s</li>", data['errors'][index]));
                $('#reply #errors').append("</ol>");
                
                $('#reply #errors').slideDown();
                Recaptcha.reload();
            }
        }, 'json');
        
        // Never let the browser submit the form normally.
        return false;
    });
    
    // Send to a friend form
    $('#sf_form').submit(function()
    {
        var values = {};
        $('#sf_form INPUT').each(function(index, element)
        {
            values[element.name] = $(element).val();
        });
        
        var is_valid = validate_friend_inputs(values);
        
        // Don't submit the form if there are any warnings
        if( is_valid == false )
        {
            return false;
        }
        
        values['action'] = 'send_to_friend';
        $('#sf_loading').show();
        $.post('/ajax_endpoint.php', values, function(data)
        {
            $('#sf_loading').hide();
            if(data['errors'].length == 0)
            {
                $('#sf_thankyou').show();
                setTimeout('tb_remove();', 5000);
            }
            else
            {
                $('#sf_errors').empty();
                $('#sf_errors').append(sprintf("<strong>W twojej wiadomości wystąpiły błędy:</strong>"));
                $('#sf_errors').append("<ol>");
                for(var index = 0; index < data['errors'].length; index++)
                    $('#sf_errors OL').append(sprintf("<li>%s</li>", data['errors'][index]));
                $('#sf_errors').append("</ol>");
                $('#sf_errors').slideDown();
                Recaptcha.reload();
            }
        }, 'json');
        
        // Never let the browser submit the form normally.
        return false;
    });
    
    $("#carousel").jCarouselLite({
        btnPrev: "#previous",
        btnNext: "#next",
        scroll: 7,
        speed: 1000,
        visible: 7
    });
    $('#carousel LI IMG').hover(
        function () {
            $(this).css('border', '1px solid #999');
            $(this).css('cursor', 'pointer');
        },
        function () {
            $(this).css('border', '1px solid #d5d5d5');
        }
    );
    $('#previous').hover(
        function () {
            $(this).attr('src', '/images/smi_carousel_button_left_off.png');
            $(this).css('cursor', 'pointer');
        },
        function () {
            $(this).attr('src', '/images/smi_carousel_button_left_off.png');
        }
    );
    $('#next').hover(
        function () {
            $(this).attr('src', '/images/smi_carousel_button_right_off.png');
            $(this).css('cursor', 'pointer');
        },
        function () {
            $(this).attr('src', '/images/smi_carousel_button_right_off.png');
        }
    );
        
    type = 'map_view';
    $('#map_view').click(
        function () {
            toggle_map_view("R");
            type = 'map_view';
            $('#map_buttons LI').removeClass('active');
            $(this).addClass('active');
        }
    );
    $('#sat_view').click(
        function () {
            toggle_map_view("S");
            type = 'sat_view';
            $('#map_buttons LI').removeClass('active');
            $(this).addClass('active');
        }
    );
    $('#hyb_view').click(
        function () {
            toggle_map_view("H");
            type = 'hyb_view';
            $('#map_buttons LI').removeClass('active');
            $(this).addClass('active');
        }
    );
    $('#ter_view').click(
        function () {
            toggle_map_view("T");
            type = 'ter_view';
            $('#map_buttons LI').removeClass('active');
            $(this).addClass('active');
        }
    );
    $('#map_buttons LI').hover(
        function () {
            if ( $(this).attr('ID') != type )
            {
                $(this).css('background', 'url(/images/hp_accordion_tab_bg_on.png) repeat-x');
                $(this).children('DIV').css('background', 'url(/images/hp_accordion_tab_bg_right_on.png) no-repeat right');
                $(this).children('DIV').children('H3').css('background', 'url(/images/hp_accordion_tab_bg_left_on.png) no-repeat left');
            }
        },
        function () {
            if ( $(this).attr('ID') != type )
            {
                $(this).css('background', 'url(/images/hp_accordion_tab_bg_off.png) repeat-x');
                $(this).children('DIV').css('background', 'url(/images/hp_accordion_tab_bg_right_off.png) no-repeat right');
                $(this).children('DIV').children('H3').css('background', 'url(/images/hp_accordion_tab_bg_left_off.png) no-repeat left');
            }
        }
    ).click(
        // Un-highlight all other tabs
        function(){
            $('#map_buttons LI:not(#'+type+')').css('background', 'url(/images/hp_accordion_tab_bg_off.png) repeat-x');
            $('#map_buttons LI:not(#'+type+')').children('DIV').css('background', 'url(/images/hp_accordion_tab_bg_right_off.png) no-repeat right');
            $('#map_buttons LI:not(#'+type+')').children('DIV').children('H3').css('background', 'url(/images/hp_accordion_tab_bg_left_off.png) no-repeat left');
        }
    );
    
    $('#thumbnails IMG').hover(
        function () {
            $(this).css('border', '1px solid #7c7c7c');
        },
        function () {
            $(this).css('border', '1px solid #c5c6c8');
        }
    );
});

function validate_user_inputs(values)
{
    var error_count = 0;

    // Validate name
    if(values['name'].length == 0)
    {
        error_count++;
        $('#name_label').addClass('warning');
    }
    else
    {
        $('#name_label').removeClass('warning');
    }
    
    if(values['message'].length == 0)
    {
        error_count++;
        $('#msg_label').addClass('warning');
    }
    else
    {
        $('#msg_label').removeClass('warning');
    }

    // Email
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    if (values['email'].match(re) === null)
    {
        error_count++;
        $('#email_label').addClass('warning');
    }
    else
    {
        $('#email_label').removeClass('warning');
    }

    return (error_count == 0);
}

function validate_friend_inputs(values)
{
    var error_count = 0;
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    // Validate names
    if(values['sf_yn'].length == 0)
    {
        error_count++;
        $('#sf_yn_label').addClass('warning');
    }
    else
    {
        $('#sf_yn_label').removeClass('warning');
    }
    if(values['sf_fn'].length == 0)
    {
        error_count++;
        $('#sf_fn_label').addClass('warning');
    }
    else
    {
        $('#sf_fn_label').removeClass('warning');
    }

    // Emails
    if (values['sf_ye'].match(re) === null)
    {
        error_count++;
        $('#sf_ye_label').addClass('warning');
    }
    else
    {
        $('#sf_ye_label').removeClass('warning');
    }
    if (values['sf_fe'].match(re) === null)
    {
        error_count++;
        $('#sf_fe_label').addClass('warning');
    }
    else
    {
        $('#sf_fe_label').removeClass('warning');
    }

    return (error_count == 0);
}
