$(document).ready(function() {
    $('#blocks_home .block').hover(BlockHomeRollover, BlockHomeRollout);
    $('#prod_list .box').hover(GridRollover, GridRollout);
    
    $('#grid .box').each(function(i) {
        $(this).css('opacity', 0).delay(i * 200).animate({opacity:1});
    });
    
    if($('input#cerca').length > 0) {
        $('input#cerca').result(function(event, data, formatted) { GoToProductDetail(data); });
    } else if($('input#cercap').length > 0) {
        $('input#cercap').result(function(event, data, formatted) { GoToPeopleDetail(data); });
    }
    
    $('input#cerca').keyup(function(e) {
        if($('input#cerca').val() != '') $('#search_del').show();
        else $('#search_del').hide();
    });
    
    $('input#cercap').keyup(function(e) {
        if($('input#cercap').val() != '') $('#search_del').show();
        else $('#search_del').hide();
    });
    
    //$('#detail_image').height($('#detail_image img').height());
    $('#detail_image').height($('#foto0').height());
    $('#detail_image img').load(function() {
		//$('#detail_image').height($('#detail_image img').height());
	});
});

function GridRollover() { var id = $(this).attr('id'); $('#'+id+' .rollover').show(); }
function GridRollout() { var id = $(this).attr('id'); $('#'+id+' .rollover').fadeOut(250); }

function BlockHomeRollover() { var id = $(this).attr('id'); $('#'+id+' .rollover').show(); }
function BlockHomeRollout() { var id = $(this).attr('id'); $('#'+id+' .rollover').fadeOut(250); }

function GoToProductDetail(prodName) { location.href = 'detail.php?type=c&search='+encodeURIComponent(prodName); }
function GoToPeopleDetail(peopleName) { location.href = 'people_detail.php?type=o&search='+encodeURIComponent(peopleName); }

function PrintMailLink(nome, dominio, label){
    var email = nome+'@'+dominio;
    if(!label) label = email;
    document.write('<a href="mailto:'+email+'">'+label+'</a>');
}

function SwitchImage(id) {
    if(id != curId) {
        curId = id;
        $('#dida_it').html(gallery[id].it);
        $('#dida_en').html(gallery[id].en);
        $('#detail_grid a').removeClass('selected');
        $('#detail_grid a#link'+id).addClass('selected');
        $('#detail_image :not(img#foto'+id+')').css('z-index', '-1');
        $('#detail_image img#foto'+id).css('z-index', '1').fadeIn(250, function(){
            $('#detail_image :not(img#foto'+id+')').hide();
        });
        $('#leftCol').height($('#foto'+id).height() + 102);
    }
}

function OnKeyEnter(elem, section, e) {
    if(e.keyCode == 13) {
        PerformProductSearch($(elem).val(), section);
    }
}

function OnKeyEnterPeople(elem, e) {
    if(e.keyCode == 13) {
        PerformPeopleSearch($(elem).val());
    }
}
