$(document).ready(function() {
    $('header nav a').click(function() {
        var href = $(this).attr('href');
        $.scrollTo(href, 500);
    });
    
    /*
    $('#portfolio .screens a').hover(function() {
        $('span', this).hide().slideDown();
    },
    function() {
        $('span', this).slideUp();
    });
    */
    
    var loading = new Image();
    loading.src = '/wp/wp-content/themes/wordpressfreelancer/img/loading.png';
    
    $('#portfolio .screens a').click(function() {
        if (!$('#popup').get(0)) {
            $('body').append('<div id="popup"></div>');
        }
        
        var anchor = $(this);

        anchor.append('<img src="' + loading.src + '" alt="" />');

        var deg = 0;
        var rotate = function() {
            window.setTimeout(rotate, 40);
            deg += 10;
            if (deg > 360) deg = 0;
            $('img', anchor).css({
                '-webkit-transform' : 'rotate(' + deg + 'deg)',
                '-moz-transform' : 'rotate(' + deg + 'deg)',
                '-o-transform' : 'rotate(' + deg + 'deg)'
            });
        };
        rotate();
        
        var pos = anchor.offset();
        pos.top = anchor.get(0).offsetTop;
        pos.right = $('body').width() - (pos.left + anchor.width());
        
        var img = new Image();
        
        $(img).load(function() {

            $('#popup')
                .hide()
                .html('')
                .append('\
                    <img src="' + anchor.attr('href') + '" alt="" />\
                    <div class="close" style="display: none;"></div>\
                    <div class="details" style="display: none;">\
                        <p>' + $('span', anchor).html() + '</p>\
                    </div>\
                ')
                .css('top', (pos.top - 4) + 'px');

            $('#popup .details').append(anchor.siblings('.visit').clone());

            $('#popup').show();
            
            var width = $('#popup img').width();
            var height = $('#popup img').height();
            
            var new_width = width;
            if (width > $('body').width()) {
                new_width = $('body').width() - 60;
                height = height / width * new_width;
            }

            var left = '';
            var right = '';
            var dest_left = '';
            if (pos.left > $('body').width() / 2) {
                left = 'auto';
                right = (pos.right - 4) + 'px';
            }
            else {
                left = dest_left = (pos.left - 4) + 'px';                    
                right = 'auto';

                if (width > $('body').width() - pos.left) {
                    dest_left = ($('body').width() - new_width) / 2 - 16 + 'px';
                }
            }

            $('#popup').css({
                'right' : right,
                'left' : left
            });
            
            width = new_width;
            
            if (left != 'auto') {
                $('#popup').animate({'left' : dest_left}, 500);
            }
            
            $('#popup img')
                .css('width', '400px')
                .css('height', '242px')
                .animate({'width' : width + 'px', 'height': height + 'px'}, 500, function() {
                    $('#popup .details').slideDown();
                    
                    $('#popup .close').click(function() {
                        $('#popup').fadeOut();
                    }).show();
                    
                    // IE 7 bug
                    $('#popup .details').width(width - 12 + 'px');
                });
            
            $('img', anchor).remove();
        });

        img.src = anchor.attr('href');
        
        return false;
    });
    
    $('#contact form').submit(function() {
        var form = $(this);
        var post_data = form.serialize();
        $.post(form.attr('action') + '?ajax=1', post_data, function(data) {
            form.html(data);
            $('.error-msg, .error', form).hide().fadeIn();
        });
        return false;
    });
});
