$(document).ready(function() {

    // attaching hoverbox and attaching first link as click event
    $('.hover_box')
    .hover(function() {
        $(this).css({background:'#d8d8d8'});
    }, function() {
        $(this).css({background:'transparent'});
    })
    .click(function() {
        location.href= $('a:last',this).attr('href');
    });



    // setting timeout delay for safari
    setTimeout(function() {
        // setting up cycles for case slideshows 
        $('.case_slideshow').each(function(index) {

            var next = "#" + $('.case_slideshow_next', this).attr('id')
            var prev = "#" + $('.case_slideshow_previous', this).attr('id')

            // removing nav if only one picture
            if ($('.case_slideshow_pictures img', this).size() == 1 ) {
                $('.case_slideshow_next, .case_slideshow_previous', this).remove()
            };


            //setting hight to biggest picture        
            var maxHeight = 0
            $('.case_slideshow_pictures img',this).each(function(index) {            
                if ($(this).height() > maxHeight) {maxHeight = $(this).height()};            
            });
            $('.case_slideshow_pictures', this).height(maxHeight);

            $('.case_slideshow_pictures',this).cycle({ 
                fx:      'fade', 
                speed:    200, 
                timeout:  0,
                random: 0,
                fit: 1,
                pause: 1,
                next: next,
                prev: prev
            });


            $('a[@rel*=lightbox]', this).lightBox({
             	overlayBgColor: '#000',
             	overlayOpacity: 0.9,
             	imageLoading: '/media/layout/lightbox-ico-loading.gif',
             	imageBtnClose: '/media/layout/lightbox-btn-close.gif',
             	imageBtnPrev: '/media/layout/lightbox-btn-prev.gif',
             	imageBtnNext: '/media/layout/lightbox-btn-next.gif',
             	containerResizeSpeed: 400,
             	txtImage: '',
             	txtOf: 'af'
            });


            $('.case_picture_show', this).click(function() {
                $(this).parents('.case_slideshow').find('.case_slideshow_pictures a:visible').click();
                return false;
            });


        });        
    }, 1000);


    // hooking up newsletter form inline labels
    $('input.inline_label').each(function(index) {
        $me = $(this);

        // first time run
        if ($me.val() == '' || $me.val() == $me.attr('title')) {
            $me.val($me.attr('title'));
            $me.siblings('[type=submit]').attr('disabled', 'disabled').addClass('disabled');
        }        
        
        $me.focus(function() {
            if ($me.val() == $me.attr('title')) {
                $me.val('');
            }
        });
        
        $me.blur(function() {
            if ($me.val() == '') {
                $me.val($me.attr('title'));
            }            
        });
        
        $me.keyup(function() {
            if ($me.val() == '' || $me.val() == $me.attr('title')) {
                $me.siblings('[type=submit]').attr('disabled', 'disabled').addClass('disabled');
            } else {
                $me.siblings('[type=submit]').attr('disabled', '').removeClass('disabled');
            }

            
        })
        
    });
    
    // handling and displaying page errors
    var url_params = getUrlVars();
        
    if (url_params.errors) {
        var error = decodeURIComponent(url_params.errors).replace(/\+/g, ' ');
        $('#header').after('<div id="message">'+ error +'</div>');
    };

    if (url_params.success) {
        var success = url_params.success.replace(/\+/g, ' ').replace(/\%2C/g, '').replace(/\%E6/g, 'æ');
        $('#header').after('<div id="message">'+ success +'</div>');
    };

    if (url_params.success2) {
        var success2 = decodeURIComponent(url_params.success2).replace(/\+/g, ' ');
        $('#header').after('<div id="message">'+ success2 +'</div>');
    };

        
    // replacing top on frontpage with flash
    $('#frontpage_top').flash({
        src: '/media/content/flash/Loft37_front.swf',
        width: 880,
        height: 324
    }).after('<hr />');
        
    
});


// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
