// HA Design User interface
var HADESIGN = {

    /**
     * Init functions
     */         
    init: function () {
        HADESIGN.emulateHover($('#nav > li'));
        HADESIGN.openInNewWindow($('.work-detail .desc a'));
        if ($('#work-carousel').length) {
            HADESIGN.workCarousel();
            HADESIGN.workSwitching();
        }
        HADESIGN.thumbsFading($('#home-work img'), 0.45);
        HADESIGN.thumbsFading($('#work-carousel .thumb img'), 0.5);        
    },
    
    /**
     * Thumbs fade in and fade out
     */
    thumbsFading: function (items, opacity) {
        $(items).not('.current').css('opacity', opacity);
        $(items).hover(
            function() { $(this).fadeTo('fast', 1); },
            function() { $(this).not('.current').fadeTo('fast', opacity); }
        )
    }, 
    
    /**
     * Work switching
     */         
    workSwitching: function () {
        
        // Switching from URL
        var hash = location.hash.replace(/#/, '');
        
        if (hash) {
            // Select first from the curent set
            switch(hash) {
                case 'web': 
                    switchWork($('#work-carousel > li:eq(0) .work:first'));
                    break;
                case 'logos': 
                    switchWork($('#work-carousel > li:eq(1) .work:first'));
                    break;
                case 'print': 
                    switchWork($('#work-carousel > li:eq(2) .work:first'));
                    break;
                default:
                    if ($('#' + hash).length) {    
                        switchWork($('#' + hash)); 
                    }       
            }
        } else {
            switchWork($('#work-carousel > li:eq(0) .work:first'));
        }
        
        // Switching on click
        $('#work-carousel .thumb').bind('click', function () {
            switchWork($(this).parent(), true);
            return false;
        });

        // Switch work details
        function switchWork(item, fading) {

            // Image
            var thumb = $(item).find('.thumb img');
            
            // Preload image
            var big_image = new Image();
            big_image.src = thumb.attr('src').replace(/-thumb.jpg/, '-big.jpg')
            
            var link = $(item).find('.desc a');
            
            // Select current in thumbs
            var thumb = $(item).find('img');
            var thumbs = $('#work-carousel .thumb img');
            thumbs.removeClass('current');
            thumbs.not(thumb).fadeTo('def', 0.5);
            thumb.addClass('current');
            
            // Description
            var desc = $(item).find('.desc').html();
            $('.work-detail .desc').html(desc);
            HADESIGN.openInNewWindow($('.work-detail .desc a'));
            
            if (fading) {
                $('.work-detail .img img').fadeTo(1000, 0, function () {
                    $(this).attr('src', thumb.attr('src').replace(/-thumb.jpg/, '-big.jpg'));
                    $(this).fadeTo(1000, 1.0);
                });
            } else {
                $('.work-detail .img img').attr('src', thumb.attr('src').replace(/-thumb.jpg/, '-big.jpg'));
            }
            
            // Link
            if (link.length) {
                $('.work-detail .img img').wrap("<a></a>");
                $('.work-detail .img a').attr('href', link.attr('href'));
                HADESIGN.openInNewWindow($('.work-detail .img a'));
            } else {
                var only_img = $('.work-detail .img img'); 
                $('.work-detail .img a').remove();
                only_img.appendTo('.work-detail .img');
            }
            
        }
    }, 
    
    /**
     * Work carousel
     */         
    workCarousel: function () {

        // Find out start position from links like work.html#logos etc.
        function findStartPosition() {
            var start_position = 1;
            var parts = location.hash.split('-');
            if (parts[0]) {
                var hash = parts[0].replace(/#/, '');
                if (hash) {
                    switch(hash) {
                        case 'logos': 
                            start_position = 2;
                            break;
                        case 'print': 
                            start_position = 3;
                            break;        
                    }
                }
            }
            return start_position;
        }
        
        // Set current state in work section of navigation
        function workcarousel_itemVisibleInCallback(carousel, item, idx, state) {
            $('#nav ul a').removeClass('current');
            switch(idx) {
                case 1: 
                    $('#nav ul li:eq(0) a').addClass('current');
                    break;
                case 2: 
                    $('#nav ul li:eq(1) a').addClass('current');
                    break;
                case 3: 
                    $('#nav ul li:eq(2) a').addClass('current');
                    break;
            }  
        };
        
        function workcarousel_initCallback(carousel) {
            jQuery('#nav li li a').bind('click', function() {
                var position = 1;
                var parts = $(this).attr('href').split('#');
                if (parts[1]) {
                    switch(parts[1]) {
                        case 'web': 
                            position = 1;
                            break;
                        case 'logos': 
                            position = 2;
                            break;
                        case 'print': 
                            position = 3;
                            break;        
                    }
                    carousel.scroll(position);
                }
            });
        
            jQuery('#work-carousel .nav .next').bind('click', function() {
                carousel.next();
                return false;
            });
        
            jQuery('#work-carousel .nav .prev').bind('click', function() {
                carousel.prev();
                return false;
            });
        };
        
        $('#work-carousel').jcarousel({
                scroll: 1,
                start: findStartPosition(),
                initCallback: workcarousel_initCallback,
                itemVisibleInCallback: workcarousel_itemVisibleInCallback,
                buttonNextHTML: null,
                buttonPrevHTML: null
        });
    },
    
    /**
     * Emulate hover
     */
    emulateHover: function (items) {
        items.hover(
            function () {
                $(this).addClass('hover');
            },
            function () {
                $(this).removeClass('hover');
            }
        )
    },
    
    /**
     * Open in new windows
     */         
    openInNewWindow: function (items) {
        items.bind('click', function() {
            window.open($(this).attr('href'), 'name');
            return false;
        });    
    },    
    

    /**
     * Preload images
     * @param {Array} images array with names of images   
     */               
    preloadImages: function (images) {
        for (var i = 0; i < images.length; i++) {
            var image = new Image();
            image.src = images[i];
            console.log(images[i]);
        }
    }         
}

$(document).ready(function () {
    HADESIGN.init();
    
    if ($('#sidebar .tags').length > 0){
    	$('#sidebar .tags').find('li').each(function(){
    		$(this).addClass('size'+parseInt(10 * Math.random()));
    	})
    	
    	
    }
});

