$(document).ready(function(){

    $('span.associated-text').each(function(){

        $(this).css({ display : 'block', zoom : 1 });

        var moded = false;
        while( $(this).height() > 22 ) {
            moded = true;
            $(this).text( $(this).text().substring(0, $(this).text().length - 1));
        }
        
        if( moded ) {
            $(this).text( $(this).text().substring(0, $(this).text().length - 4));
            $(this).text( $(this).text() + '...');
        }
    });

    $('input[type="text"][rel]').each(function(){
        if( jQuery.trim($(this).val()) == '')
            $(this).val($(this).attr('rel'));
        
        $(this).bind({
            'focus' : function(){
                if( $(this).val() == $(this).attr('rel') )
                    $(this).val('');
            },
            'blur' : function(){
                if( jQuery.trim($(this).val()) == '' )
                  $(this).val($(this).attr('rel'));  
            }
        });
    });

    $('a.border-list').hover(
        function(){
            var new_height = parseInt($(this).attr('original_height')) + 18;
            $(this).find('.text, .text-bg').animate({ height : new_height + 'px'}, 250);
        },
        function() {
            $(this).find('.text, .text-bg').animate({ height : $(this).attr('original_height') + 'px'}, 250);
        }
    ).each(function() {
        $(this).attr({ original_height : $(this).find('.text-bg').height() });
    });

    $('.gallery-thumb').click(function(){
        // $('#gallery-image').css({ height : $('#gallery-image').height() + 'px' });

        $('.gallery-thumb .overlay.selected').removeClass('selected');
        $(this).find('.overlay').addClass('selected');
        $('.gallery-thumb .overlay:not(.selected)').fadeOut();
        $(this).find('.overlay.selected').fadeTo('slow', 0.8);

        var new_src = jQuery.trim($(this).find('.image-src').css('background-image').replace('url("', '').replace('")', '').replace('url(', '').replace(')', '').replace('_tn', '') + '?date=' + Math.floor(Math.random()*110));
        $('#gallery-image').css({ backgroundImage : ''})

        $('#gallery-image img').fadeOut(500, function(){
            $(this).attr({ src : new_src });
        });
    });

    $('.gallery-thumb .overlay').eq(0).fadeTo('slow', 0.8);

    $('#gallery-image img').load(function(){
        $(this).fadeIn(800, function(){
            $(this).parent().css({ backgroundImage : 'none'});
        });
    });

    $('.our-email').text('info' + '@p' + 'ossum.' + 'co' + '.u' + 'k').attr({ href : 'mai' + 'lto:' + 'info' + '@p' + 'ossum.' + 'co' + '.u' + 'k' });
    $('.marketing-email').text('marketing' + '@p' + 'ossum.' + 'co' + '.u' + 'k').attr({ href : 'mai' + 'lto:' + 'marketing' + '@p' + 'ossum.' + 'co' + '.u' + 'k' });
    $('.building-email').text('building1' + '@p' + 'ossum.' + 'co' + '.u' + 'k').attr({ href : 'mai' + 'lto:' + 'building1' + '@p' + 'ossum.' + 'co' + '.u' + 'k' });
    
    $('.contact-form form, .newsletter-form').submit(function(){
        var valid_form = true;

        $(this).find('input.text, textarea').each(function(){
            if( jQuery.trim($(this).val()) == '' ) {
                $(this).parent().addClass('error');
                valid_form = false;
            } else if( $(this).attr('name') == 'email' ) {
                var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                var address = $(this).val();
                if(reg.test(address) == false) {
                    $(this).parent().addClass('error');
                    valid_form = false;
                } else
                    $(this).parent().removeClass('error');

            } else
                $(this).parent().removeClass('error');
        });

        return valid_form;
    });
});

swfobject.embedSWF("/static/swf/home.swf", 'home-flash', 844, 280, "8");
swfobject.embedSWF("/static/swf/possum_room.swf", 'eat-flash', 844, 280, "8");