;(function($){

    var cursorPosition = {};
    function getCursorOffset($element, mouseX, mouseY)
    {
          var offset = $element.offset();
          var x = mouseX - offset.left;
          var y = mouseY - offset.top;

          return {'x': x, 'y': y};
    }

    $(document).ready(function(){

        Cufon.replace('nav#main,h2,#logo h1,a#close,.contact-table tr,.contact-table td',{
            hover: true
        });

        // IE fixes
        $('nav ul li:has(ul)').hover(function(){
            $(this).addClass('hover');
        }, function(){
            $(this).removeClass('hover');
        });
        $('p.cf-sb input').val('');

        $(document).mousemove(function(e){
            cursorPosition = { 'x': e.pageX, 'y': e.pageY };
        });

        $(window).resize(function(){
            $('#container').css({
                paddingTop: Math.max(0, (($(window).height()-600)/2)+30)
            });
        }).trigger('resize');


        // gallery
        $('#prev')
            .click($.fn.colorbox.prev)
            .parent('li')
                .hide();

        $('#next')
            .click($.fn.colorbox.next)
            .parent('li')
                .hide();

        $('#close')
            .click($.fn.colorbox.cloxe);

        $('.item:not(.iframe)').colorbox({
            onOpen: function(){
                $('#prev').parent('li').show();
                $('#next').parent('li').show();
            },
            onClosed: function(){
                $('#prev').parent('li').hide();
                $('#next').parent('li').hide();
            }
        });
        
        $('.item.iframe').colorbox({
        	iframe: true,
        	width: 600,
        	height: 400,
            onOpen: function(){
                $('#prev').parent('li').show();
                $('#next').parent('li').show();
            },
            onClosed: function(){
                $('#prev').parent('li').hide();
                $('#next').parent('li').hide();
            }
        });
        
        var hoverInterval;

        /*** Slider ***/
        $('.items-container')
            .css('overflow-x', 'hidden')
            .css({width: $(window).width()})
            .live('mouseenter', function(e){

                var $itemContainer = $(this);
                var itemContainerWidth = $itemContainer.width();

                hoverInterval = window.setInterval(function(){
                    var cursorOffset = getCursorOffset($itemContainer, cursorPosition.x, cursorPosition.y);
                    var speed = (cursorOffset.x - (itemContainerWidth / 2)) / 20;
                    if ( speed < -2 || speed > 2 ) {
                        $itemContainer.scrollLeft($itemContainer.scrollLeft() + speed);
                    }

                }, 50);
            }).live('mouseleave', function(e){
                clearInterval(hoverInterval);
            });

        $(document).trigger('newContent');

    });

    $(document).bind('newContent', function(){

        var totalWidth = 0;
        $('.item:last').css('margin-left', 0);
        $('.item img').each(function(){
            totalWidth += ($(this).width() + 10);
        });
        $('.items').css('width', totalWidth - 10);
    });


})(jQuery);
