var interval;
var arreglo = new Array();
var frases = new Array();
var intervalon = true;
var autoload1 = 0;
var slider = function (){
    $.ajax({ 
        url: "/index.php/ajax/frases",
        cache: false,
        success: function(data){
            frases = data;
        },
        dataType: 'json'
    })
    $( '.click' ).animate({
        opacity : 0.3
    } , 300 , function(){
        //animar img-footer
        $ ( '#img-footer').hide('fade', {
            width : 0
        } , 500 , '');
        //Cambiar imagenesa
        var i;
        var posicion;
        var name = $( '.click' ).attr( 'src');
        for( i = 0 ; i < arreglo.length ; i++ ){
            if( arreglo[i] == name )
                posicion = i;
        }
        posicion++;
        if( posicion  == arreglo.length )
            posicion = 0;
        $( '.click' ).attr( 'src' , arreglo[posicion] );
        //animar ahora la nueva imagen
        $( '.click' ).animate( {
            opacity : 1
        } , 500 , function(){
            var img_name = arreglo[posicion].split('/')
            $( '.img-title').html( frases[ img_name[ img_name.length - 1 ] ] );
            
            /*
            $.posiciont('/index.php/ajax/lang/' + img_name[ img_name.length - 1 ] + '_pie' ,
                function(data){
                    //if( data != '' )
                    $( '#foot_image').html( data )
                });
                */
                
            $ ( '#img-footer').show( 'drop', '' , 750 , '');
        })
    })
}
$( document ).ready(function(){
    $.getJSON('/index.php/ajax/img_'+ $( '.click' ).attr( 'id' ), function( data ){
        arreglo = data;
    })
    $.ajax({ 
        url: "/index.php/ajax/frases",
        cache: false,
        success: function(data){
            frases = data;
        },
        dataType: 'json'
    })
    
    interval = setInterval( slider  , 7500 );
    
    $('.baner img, #footer img').delay(10).fadeIn('slow');
    $( 'input').delay(100).fadeIn('slow');
    
    //$( '.submenu').hide();
    //$( '#menu-hover').hide();
    $( '.submenus' ).hover(function(){
        $( '#menu-hover').show();
    },
    function(){
        $( '#menu-hover').fadeOut()
    })
    
    /*
    $( '.efect' ).hover(function(){
        $( '.efect' ).show();
    },
    function(){
        $( '#menu-hover').fadeOut()
    })
    
    /*
    $( '#menu-hover' ).hover(
        function(){
            $( '#menu-hover' ).show();    
        },
        function(){
            $( '#menu-hover').fadeOut()
        });
        */
    
    $( '#nolink').click( function(){
        event.preventDefault();
    });
    $( '#nolink').removeAttr('href');
    $( '#username , #email, #mail, #phone, #country-code , #area' ).focus(function(){
        if( $( this ).val()== $( this ).attr('title') )
            $( this ).val('');
        $( this ).attr( 'class' , 'contacto-normal' );
    })
    $( '#username , #email, #mail, #phone ,#country-code , #area' ).blur(function(){
        if( $( this ).val()== '' )
            $( this ).val( $( this ).attr('title') );
    })
    $('#new1').closest('table').addClass('items');
    $('#new1').closest('tr').addClass('add');
    $( '.add' ).hide();
    $( ' .items th' ).hide();
    
    var which_table;
    $('.agregar').button();
    $('.agregar').click(function(){
        which_table = $( this ).attr('rel');
        $( '#add').dialog('open');
    });
    $( '#add').dialog({
        autoOpen: false,
        width: 350,
        modal: true,
        buttons: {
            "Agregar":function(){
                var str = '<div class="contenedor"><div class="fila">'+$('#item').val()+'</div><div class="fila"><input type="text" name="'+$('#quantity').val()+'" value="'+$('#quantity').val()+'"></div></div>';
                $( '#'+which_table ).append( str );
                $( this ).dialog('close')
                $('#item').val( '' )
                $('#quantity').val('')
            },
            "Cancelar":function(){
                $( this ).dialog('close')
            }
        }
    });
    //Company
    $( 'input[name="Nombre de la empresa: "] , input[name="Company Name: "]' ).closest('tr').addClass('company');
    $( 'input[name="Email de la empresa: "] , input[name="Company Email: "]' ).closest('tr').addClass('company');
    $( 'input[name="Contacto con la empresa"] , input[name="Company Contact: "]' ).closest('tr').addClass('company');
    $( '.company').hide();
    $( 'input[ id="company1"] , input[ id="company2"]').change(function(){
        if( $( this ).val()=='YES' )
            $( '.company').fadeIn('slow');
        else
            $( '.company').fadeOut('fast');
    })
    
    $( '#addres').hide();
    $( 'input[name=collection]' ).change(function(){
        if( $( this ).val() == 'YES')
            $( '#addres').fadeIn('slow');
        else
            $( '#addres').fadeOut('fast');
    })
    
    //Elevator
    $( 'input[name="Cargo elevator access: "] , input[name="Acceso a elevador de carga: "]' ).closest( 'tr').addClass( 'elevator');
    $( 'input[name="Todos los articulos caben en el elevador: "] , input[name="All items will fit into the elevator: "]' ).closest( 'tr').addClass( 'elevator');
    $( '.elevator').hide();
    $( 'input[name="Elevator: "] , input[name="Elevador: "]' ).change(function(){
        if( $( this ).val() == 'YES')
            $( '.elevator').fadeIn('slow');
        else
            $( '.elevator').fadeOut('fast');
    })
    //
    $( 'input[name="Floor that is to pick up merchandise: "] , input[name="Piso al que hay que recoger mercancia: "]' ).closest( 'tr' ).addClass( 'flors')
    $( 'input[name="Todo cabe por las escaleras: "] , input[name="Everything fits the stairs: "]' ).closest( 'tr' ).addClass( 'flors')
    $( '.flors' ).hide();
    $( 'input[name="Floors: "] , input[name="Pisos: "]' ).change(function(){
        if( $( this ).val() == 'YES')
            $( '.flors').fadeIn('slow');
        else
            $( '.flors').fadeOut('fast');
    })
    
    $( 'input[type=submit]').button();
    $( 'input[name=yellow-pages]').closest('span').buttonset();
    $( 'button').button();
    $( '.btn' ).button();
    $( 'input[type=radio]').closest('tr').buttonset()
    $( 'input[name=auto]').closest('tbody').hide()
    $( 'input[name=auto]' ).change(function(){
        if( $( this ).val() == 'YES')
            $( 'input[name=auto]').closest('tbody').fadeIn('slow');
        else
            $( 'input[name=auto]').closest('tbody').fadeOut('fast');
    })
    //yellow-pages
    $( 'input[id=directory]').closest('tr').hide()
    $( 'input[name="Seccion amarilla"],input[name="Yellow pages"]' ).change(function(){
        if( $( this ).val() == 'YES')
            $( 'input[id=directory]').closest('tr').hide().fadeIn('slow');
        else
            $( 'input[id=directory]').closest('tr').hide().fadeOut('fast');
    })
    //recomendacion
    $( 'input[name="¿Quien?"], input[name="Who?"]').closest('tr').hide()
    $( 'input[name="Recommendation: "] , input[name="Recomendacion: "]' ).change(function(){
        if( $( this ).val() == 'YES')
            $( 'input[name="¿Quien?"], input[name="Who?"]').closest('tr').hide().fadeIn('slow');
        else
            $( 'input[name="¿Quien?"], input[name="Who?"]').closest('tr').hide().fadeOut('fast');
    })
    //alinear en ariticulos
    $( '.form').closest('tr').addClass( 'align-right' );
    $( '.hide').hide();
    $( '#0').fadeIn();
    $( '.btn-item' ).click(function(){
        $( this ).fadeOut('fast', function(){
            $( '#' + ( parseInt( $( this ).attr( 'id' ) )+ parseInt(1) ) ).fadeIn('slow')
        });
    })
    
    $( '#autoload').hide();
    
    //if( autoload1 == 0 ){
        $.getJSON('/index.php/ajax/img_all', function( data ){
            $.each( data , function( key , val ){
                $( '#autoload').append('<img src="/images/'+val+'" alt="" >')
            })
        })
        $.getJSON('/index.php/ajax/img_index' , function(data ){
            $.each( data , function( key , val ){
                $( '#autoload').append('<img src="'+val+'" alt="" >')
            })
        })
        $.getJSON('/index.php/ajax/img_international' , function(data ){
            $.each( data , function( key , val ){
                $( '#autoload').append('<img src="'+val+'" alt="" >')
            })
        })
        autoload1 = 1;
    //}
    $('select[name=service]').change(function(){
        switch( $( this ).val() ){
            case 'storage':
                window.location = "/index.php/storage/estimate"
                break;
            case 'domestic':
                window.location = "/index.php/moving/estimate/domestic"
                break;
            case 'national':
                window.location = "/index.php/moving/estimate/national"
                break;
            case 'international':
                window.location = "/index.php/moving/estimate/international"
                break;
            case 'officeservice':
                window.location = "/index.php/moving/estimate/domestic/officeservice"
                break;
                
        }
    })
    $( "#tabs" ).tabs();
    phones();
    
    if ( $( '#tab-Recamara , #tab-Bedroom').length > 0 ){
        $.posiciont('/index.php/ajax/lang/recamara_mensaje', function( data ){
            $( '#tab-Recamara , #tab-Bedroom').prepend( data );
        })
        $.posiciont('/index.php/ajax/lang/bano_mensaje', function( data ){
            $( '#tab-Baño, #tab-Bathroom').prepend( data );
        })
    }
    
    var inputsDate = new Array();
    
    inputsDate[0] = 'Fecha de su mudanza: ';
    inputsDate[1] = 'Fecha de recepcion su mudanza: ';
    inputsDate[2] = 'Approximate moving date: ';
    inputsDate[3] = 'Preferred delivery date: ';
    
    inputsDate[4] = 'Posible fecha de comienzo de Almacenaje: ';
    inputsDate[5] = 'Posible fecha en la que se termine el Almacenaje: ';
    inputsDate[6] = 'Tentative date of commencement of storage: ';
    inputsDate[7] = 'Tentative date of completion of storage: ';
    
    for( i = 0 ; i < 8 ; i ++){
        $( 'input[name="'+inputsDate[i]+'"]' ).datepicker({
            changeMonth: true,
            changeYear: true 
        });   
    }
})
$( document ).blur(function(){
    clearInterval( interval );
    intervalon = false;
})
$( document ).focus(function(){
    if( !intervalon){
        interval = setInterval( slider  , 7500 )
        intervalon = true;
    }
    
})
$( window ).blur(function(){
    clearInterval( interval );
    intervalon = false;
})
$( window ).focus(function(){
    if( !intervalon){
        interval = setInterval( slider  , 7500 )
        intervalon = true;
    } 
})
function phones(){
    var width = $('#telefono').width()
    $('#phones input').width( width/3 )
}
