
$(document).ready(function(){
    $('#top_menu li a, #top_menu li span, #menu_contact,h1.heading, .footer-widget h3, h3.siderbar-title').hide();
    //Cufon.replace( '#logo p', {fontFamily: 'Futura Book'});
    Cufon.replace( '#top_menu li a, #top_menu li span, #menu_contact', {fontFamily: 'Futura Book', hover: true});
    Cufon.replace( 'h1.heading', {fontFamily: 'Futura Book'});
    Cufon.replace( '.footer-widget h3', {fontFamily: 'Futura Std'});
    Cufon.replace( 'h3.siderbar-title', {fontFamily: 'Futura Std'});    
    Cufon.now();
    $('#top_menu li a, #top_menu li span, #menu_contact,h1.heading, .footer-widget h3, h3.siderbar-title').show();
    //
    // set default item of slider
    $( '.slide-item' ).hide();
    $( '.slide-item:first' ).show();
    $( '.slide-nav-item:first' ).addClass('selected');
	$( '.slider-nav' ).css('width', $( '.slider-nav' ).find('li').length * 16 );
    
    // slider nav
    $('.slide-nav-item').click(function(){
        $(this).moveToSlide();
        return false;
    })
    
    // set slider auto scroll
    setInterval( function() {
        if ( $('.slide-nav-item.selected').parent().next().length != 0 )
            $('.slide-nav-item.selected').parent().next().find('a').moveToSlide();
        else
            $('.slide-nav-item:first').moveToSlide();
    }, $('meta[name=delay_time]').attr('content') * 1000 );
    
    $('.contact-form').initialValidate();
});

$.fn.moveToSlide = function()
{
    var current = $(this),
    href = current.attr( 'href' );
    
    if ( current.hasClass( 'slide-nav-item' ) )
    {
        $('.slide-nav-item').removeClass('selected');
        current.addClass('selected')
        
        $( '.slide-item' ).each(function(){
            if ( !$(this).is(':hidden') )
            {
                $(this).fadeOut('slow' , function(){
                    $( href ).fadeIn('slow');
                });
            }
        });
    }
}

$.fn.initialValidate = function()
{
    var button = $(this).find('input[type=submit]');
    
    $(this).submit(function(){
        var current = $(this),
            elements = current.find( 'input[type=text], select, input[type=password]' ),
            validateFail = false;
            
        elements.each(function(){
            var elem = $(this),
            value = elem.val(),
            hasError = false,
            container = elem.parent(),
            regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            
            if ( elem.hasClass('not_null') && value == '' )
            {
                hasError = true;
            }
            
            if ( elem.hasClass('is_email') && !regex.test( value ) )
            {
                hasError = true;
            }
            
            if ( hasError )
            {
                validateFail = true;
                container.addClass('error');
            }
            else
                container.removeClass('error');
                
        });
        
        if ( validateFail )
        {
            return false;
        }
        return true;
    });
};
