/*
 * A Casco
 */

$(document).ready(function () {
    
    $('.email_no_js').hide();//info that js has to be enabled for viewing email
    $('tr td:even').css('background', '#ffff99');

    if($('.en').length){
        //initially hide the English translation
        $('.en').hide();
        $('.blue').show();

        $('.ph .blue').click(function(){
            $('.ph').hide();
            $('.en').show();
            //$('.blue').show();
        });
        $('.en .blue').click(function(){
            $('.en').hide();
            $('.ph').show();
            //$('.blue').show();
        });
    }

    
    if($('.link').length){
        $('.link').betterTooltip();//jquery.betterTooltip.js
        /*
        //change the title attribute so that in prettyPhoto it is more descriptive
        var title = '<b>Location map of the Church'
                    +'<br/>About 5 minutes walk '
                    +'from Golders Green Tube Station'
                    +'<br/>[Northen Line - Edgware Branch]</b>';

        $('.map').each(function(){
            $(this).click(function(){
                $(this).attr('title', title);
            });
        });*/
    }
    if($('.gallery_album').length){
        clickableDiv();
        galleryPagination();
        hoverImg();
    }
    /*
    //jquery.prettyPhoto.js
    if($("a[rel^='prettyPhoto']").length)
    {
        $("a[rel^='prettyPhoto']").prettyPhoto({
            //callback when prettyPhoto window closed;
            callback: function(){
                
                //revert to original title attributes
                $('.map').each(function(){
                    var title = 'Click for Location Map view';
                    $(this).attr('title', title);
                });
            }
        });
    }*/
    //end prettyPhoto
    
    //fancybox
    if($('.map').length){
        $("a.iframe").fancybox({
            centerOnScroll  :true,
            titleShow       :false,
            width           :'90%',
            height          :'90%',
            'transitionIn'  :'elastic',
            'transitionOut' :'elastic'
        });
    }
        
    if($("a[rel=group]").length){
        /* Apply fancybox to multiple items */
	applyFancyboxToGroup();
    }
    
});
function applyFancyboxToGroup(){
    $("a[rel=group]").fancybox({
            'transitionIn'  :   'elastic',
            'transitionOut'	:   'elastic',
            'speedIn'	:   600, 
            'speedOut'	:   200,
            'centerOnScroll':   true,
            'titlePosition' :   'inside',
            'titleFormat'   :   formatTitle
    });
}
function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="group-title">' + (title && title.length ? '<b>' + title + '</b><br/>' : '' ) + 'image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</div>';
}

function clickableDiv(){
    //clickable div
    $('.gallery_album').each(function(){
        var span = $(this).children('span');
        span.css({'display':'none'});
        var link = $(this).find('a');
        var title = $(this).find('a').attr('title');
        
        $(this).attr('title', title)
        .mouseover(function(){
            link.css({'text-decoration': 'underline'});
            span.css({'display':''});
            $(this).css({cursor: 'pointer',background:'#FFFF99'});
        })
        .mouseout(function(){
            link.css({'text-decoration': 'none'});
            span.css({'display':'none'});
            $(this).css({background:''});
        })
        .click(function(){
            window.location.href = link.attr('href');
        });
    });
}
function hoverImg(){
    /*
    $('div.pic-holder img').hoverpulse({
        size: 40,  // number of pixels to pulse element (in each direction)
        speed: 400 // speed of the animation 
    });*/
    $('div.pic-holder img').hoverpulse();
}
function galleryPagination(){
    $('#pagination > a').live('click', function(e){
        e.preventDefault();

        var link = $(this).attr('href');

        $.ajax({
            url: link,
            type: "GET",
            dataType: "html",
            beforeSend: function(){
                //apply blockUI.js
                $.blockUI(); 
                setTimeout($.unblockUI, 2000); 
            },	
            success: function(html) {
                //update
                window.setTimeout( function(){
                    $('#gallery-content').html(html);
                    //reapply prettyPhoto
                    //$("a[rel^='prettyPhoto']").prettyPhoto();
                    applyFancyboxToGroup();
                    hoverImg();
                }, 1000)
                
            }
        });
    });	

}
