/*
 * Scripts
 *
 */
jQuery(function($) {
 
    var Engine = {
        utils : {
            links : function(){
                $('a[rel*=external]').click(function(e){
                    e.preventDefault();
                    window.open($(this).attr('href'));                          
                });
            },
            mails : function(){
                $('a[href^=mailto:]').each(function(){
                    var mail = $(this).attr('href').replace('mailto:','');
                    var replaced = mail.replace('/at/','@');
                    $(this).attr('href','mailto:'+replaced);
                    if($(this).text() == mail) {
                        $(this).text(replaced);
                    }
                });
            }
        },
        enhancements : {
            confirms : function(){
                $('#delete_group').click(function(){
                    return confirm('Are you sure you want to delete this exchange? There is no undo.'); 
                });
                
                $('#remove_participant').click(function(){
                    return confirm('Are you sure you want to remove this participant? There is no undo.'); 
                });
            },
            actions : function(){
                function activateThumb(thumbnail) {
                    $('#work_preview').find('.active').removeClass('active');
                    thumbnail.addClass('active');
                    $('#gallery_detail').load(thumbnail.attr("href"));
                };
                
                $('.thumb_link').click(function(){
                    activateThumb($(this));
                    return false;
                });
                
                $('#next_arrow').click(function(){
                    var active_thumb = $('.active', $('#work_preview'));
                    if (!active_thumb.hasClass('last')) {
                        
                        activateThumb(active_thumb.next('.thumb_link'));
                    }
                    return false;
                });

                $('#previous_arrow').click(function(){
                    var active_thumb = $('.active', $('#work_preview'));
                    if (!active_thumb.hasClass('first')) {
                        activateThumb(active_thumb.prev('.thumb_link'));
                    }
                    return false;
                });
                
                $('#show_detail_images').live("click", function(){
                    $('#detail_images').show();
                    var body_height = $('body').height();
                    $('#detail_images').css({'height':body_height + 54});
                    return false;
                }),

                $('.hide_detail_images').live("click", function(){
                    $('#detail_images').hide();
                    return false;
                }),
                
                $('#detail_images .next_arrow').live("click", function(){
                    var thisId = $(this).attr("href")
                    $('#detail_image_' + thisId).hide();
                    $('#detail_image_' + (parseInt(thisId) + 1)).show();
                    return false;
                });

                $('#detail_images .previous_arrow').live("click", function(){
                    var thisId = $(this).attr("href")
                    $('#detail_image_' + thisId).hide();
                    $('#detail_image_' + (parseInt(thisId) - 1)).show();
                    return false;
                });

            }
        }
    };

    Engine.utils.links();
    Engine.utils.mails();
    
    Engine.enhancements.actions();
    Engine.enhancements.confirms();
});

