var current_slide = 1;
var slide_interval_id = null;

function home_slide(next) {

    if( $('#player:visible,#login:visible').length ) {
        return false;
    }

    if( typeof next != 'undefined' ) {
        clearInterval(slide_interval_id);
    }

    $('#slide' + current_slide).fadeOut(700);

    current_slide = typeof next == 'undefined' ? current_slide + 1 : next;
    
    if( current_slide > $('#slides > div').length ) {
        current_slide = 1;
    }

    $('#slide' + current_slide).fadeIn(700);
    $('#circles > div').removeClass('selected');
    $('#circles > div:eq(' + (current_slide-1) + ')').addClass('selected');
}

function show_features(name, show_slide) {

    show_slide = typeof show_slide == 'undefined' ? true : show_slide;

    clearInterval(slide_interval_id);
    $('#main_body').fadeOut(600);

    $('.scopri').hide();
    $('#video').hide();
    $('#circles').hide();

    if( show_slide ) {
        home_slide($('#slides > div._' + name).index() + 1);
    }

    $.get('/features/' + name, function(html) {
       $('#main_body').html(html);
       $('#main_body').fadeIn(600);
    });
}

function home_slide_clicked(index, feature_name) {
    home_slide(index);
}

function show_player() {

    show_overlay();

    $('#video_player > *').show();
    $('#vid_frame').attr('src', 'http://player.vimeo.com/video/16412588?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1');    
}

function close_video() {
    $('#vid_frame').attr('src', '');
    $('#video_player > div').hide();
    hide_overlay();
}

function show_overlay() {    
    $('#overlay').fadeTo(600, 0.75);
}

function hide_overlay() {
    $('#overlay').fadeOut(600);
}

$(function() {

    jQuery.fn.hinted = function() {

        this.each(function() {
            var default_value = this.value;
            $(this).focus(function() {
                if(this.value == default_value) {
                    this.value = '';
                }
            });
            $(this).blur(function() {
                if(this.value == '') {
                    this.value = default_value;
                    $(this).removeClass('changed');
                } else {
                    $(this).addClass('changed');
                }
            });
        });

    }

    jQuery.fn.passwordHinted = function() {
        this.each(function() {

            $('<input type="text" value="Password" onfocus="$(this).hide(); $(this).prev().show().focus();">').insertAfter(this);

            $(this).blur(function() {
                if( this.value == '') {
                    $(this).hide();
                    $(this).next().show();
                }
            });

        });
    }


    jQuery.fn.myGallery = function() {

        this.each(function() {
            $(this).children().click(function() {

                myGallery_handler = this;

                show_overlay();
                $('#large_image').show();

                myGallery_load_photo();

            })
        });

    }


});

var myGallery_handler = null;

function myGallery_onload() {
    var left = $('#large_image img.image').width()/2;
    var top = $('#large_image img.image').height()/2;

    $('#large_image').css('marginLeft', '-' + left + 'px' );
    $('#large_image').css('marginTop', '-' + top + 'px' );
    $('#large_image > img.image').show();
}

function myGallery_load_photo() {

    var src = $('img', myGallery_handler).attr('src').replace('.png', '_big.png');
    $('#large_image > img.image').hide();
    $('#large_image > img.image').attr('src', src);
    $('#large_image_title').html( $('.title', myGallery_handler).html() );

}

function myGallery_close() {
    $('#large_image').fadeOut(600);
    hide_overlay();
}

function myGallery_prev() {
    if( $(myGallery_handler).prev().length ) {
        myGallery_handler = $(myGallery_handler).prev();
        myGallery_load_photo();
    }
}

function myGallery_next() {
    if( $(myGallery_handler).next().length ) {
        myGallery_handler = $(myGallery_handler).next();
        myGallery_load_photo();
    }
}

function show_login() {
    show_overlay();
    $('#login').fadeIn(600);
}

function hide_login() {
    hide_overlay();
    $('#login').fadeOut(600);
}


var map_collapsed_height = 0;

function map_extend() {

    var offset_top = $(window).height() + $(window).scrollTop() - 52;

    if( offset_top > $('#footer').offset().top-48 ) {
        offset_top = $('#footer').offset().top-48;
    }
      
    map_collapsed_height = $('.map').height();
    
    $('#map_canvas').height(offset_top);
    $('.map').animate({height: offset_top }, 1000);

    setTimeout(function() {
        $('.map .expand').hide();
        $('.map .collapse').show();
        map.checkResize();
        map.setCenter(point, 15, G_NORMAL_MAP);
    }, 1000);
}

function map_collapse() {

    $('.map').animate({height: map_collapsed_height }, 1000);

    setTimeout(function() {
        $('.map .expand').show();
        $('.map .collapse').hide();
        map.setCenter(point, 15, G_NORMAL_MAP);
    }, 1000);
}

function preload(images) {

    for(var index in images) {
        var heavyImage = new Image();
        heavyImage.src = images[ index ];
    }
    
}

function send_mail() {

    if( $('#invia_text').hasClass('changed') ) {

        $.ajax({
           'url' : '/contact',
           'type' : 'POST',
           'dataType' : 'json',
           'data': { 'data[message]' : $('#invia_text').val() },
           'success' : function(data) {
                if( data.success ) {
                    $('#btn_invia').hide();
                    $('#invia_text').replaceWith('<span class="grazie">Grazie, ti contatteremo al piu\' presto</span>');
                }
           }
        });

    }

}

function demonstration_send_message() {

   var data = {};
   var error = false;
   
   $('#demonstration_form').find('input[type="text"],input[type="checkbox"]:checked,textarea').each(function() {

       if( $(this).parent().find('.required').length && $(this).val() == '' ) {
           $(this).parent().addClass('error');
           error = true;
       } else {
           $(this).parent().removeClass('error');
       }

       data[ $(this).attr('name') ] = $(this).val();
   });
   

   if( !error ) {

        $.ajax({
           'url' : '/home/demo_request',
           'type' : 'POST',
           'data': data,
           'success' : function(data) {
               $('#thankyou_name').html( document.getElementById('demo_name').value );
               $('#thankyou').show();
           }
        });
   }

}
