$(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;
    });
    
    $('#carousel-controls a').click(function(){
        
        if( $('#carousel-controls a.selected').index($(this)) < 0 ) {
            $('#carousel-controls a.selected img').animate({ left : '15px' }, 300, function(){
                $(this).parent().removeClass('selected');
            });
        }

        $(this).addClass('selected');

        $(this).find('img').animate({ left : '0px' }, 300);

        var img_to_show = $(this).attr('href').replace('#', '');
        
        $('#carousel .caro-item:visible').fadeOut(400,function(){
            $('#carousel .caro-item#' + img_to_show).fadeIn(400);
        }).removeClass('selected');

        return false;
    }).mousedown(function(){
        clearInterval(carousel_timer);
    });
    
    if( $('#carousel-controls').length ) {
        carousel_timer = setInterval(function(){
            var links   = $('#carousel-controls a');
            var current = $('#carousel-controls a.selected');
            var future  = links.index(current) + 1;
            
            if( future >= links.length ) future = 0;
            
            links.eq(future).click();
        }, 10000);
    }
    
    $('#carousel-controls a.selected').click();
    
    if( jQuery.browser.msie && jQuery.browser.version < 7 && $('#carousel-controls a img').length ) {
        $('#carousel-controls a img').attr({ 'src' : $('#carousel-controls a img').attr('src').replace('caro-selected.png', 'caro-selected-ie.png') });
    }
    
    $('.featured-gallery').each(function(){
        var img = $(this).find('img').length;
        
        var pagi = '<a href="#next" class="feat-gal-pag prev-next"><span></span></a></div>';
        
        while ( img-- ) {
            pagi = '<a href="#' + img + '" class="feat-gal-pag">' + (img+1) + '</a>' + pagi;
        }
        
        pagi = '<div class="feat-pag"><a href="#prev" class="feat-gal-pag next-prev"><span></span></a>' + pagi;
        
        $(this).append(pagi);
    });

    $('.featured-gallery .feat-pag a').click(function(){
        var page_to_show = 0;
        
        if( $('.featured-gallery .feat-pag a.selected').length ) {
            page_to_show = $('.featured-gallery .feat-pag a.selected').text() - 1;
        }

        if( $(this).hasClass('prev-next') ) {
            page_to_show++;
        } else if( $(this).hasClass('next-prev') ) {
            page_to_show--;
        } else {
            page_to_show = parseInt( $(this).text() ) - 1;
        }
        
        if( page_to_show < 0 ) page_to_show = 0;        
        if( page_to_show > $('.featured-gallery>img').length - 1 ) page_to_show = $('.featured-gallery>img').length - 1;
        
        $('.featured-gallery .feat-pag a').removeClass('selected');
        $('.featured-gallery .feat-pag a').eq(page_to_show+1).addClass('selected');
        
        $('.featured-gallery>img:visible').fadeOut(200,function(){
            $('.featured-gallery>img').eq(page_to_show).fadeIn(200);
        }).removeClass('selected');
        
        
        return false;        
    });
    
    if( $('.featured-gallery .feat-pag a').length )
        $('.featured-gallery .feat-pag a').eq(1).click();
});

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