/*
    Copyright (c) 2008-2010 JEON
    All Rights Reserved
    http://jeon.com.au
    
    Author: James Eunson <james@jeon.com.au>
*/

AppsBuzz = {
    
    init: function()
    {
        AppsBuzz.initNavbar();
        AppsBuzz.initSidebar();        
        
        // Initialise site modules
        AppsBuzz.SidebarApps.init();
        AppsBuzz.FeaturedScroll.init();
        
        if($('#app_info').is('*'))
            AppsBuzz.AppInfo.init();
        
        // Fancybox integration
        $('a.image').fancybox();
        
        // Fancybox for images in wordpress galleries
        if($('dt.gallery-icon a img').is('*')) {
            $('dt.gallery-icon a').fancybox();
        }

		// Scale images in post body
		var maxWidth = 630;
		$('#posts .post img').each(function(i){
			if(parseInt($(this).attr('width')) > maxWidth) {
				var width = parseInt($(this).attr('width'));
				var reduceFactor = maxWidth/width;
				var reduceY = parseInt($(this).attr('height')) * reduceFactor;
				
				$(this).attr({
					width: maxWidth,
					height: reduceY
				});
			}
		});
    },
    
    /**
     * Initialise all navbar related functions
     */
    initNavbar: function()
    {
        // Bind site-wide search box
        $('.search_wrapper input')
            .data('default', $('.search_wrapper input').attr('value'))
            .click(function(e){
                if($(this).attr('value') == $(this).data('default'))
                    $(this).attr('value','').removeClass('blur');
            })
            .blur(function(e){
                if($(this).attr('value') == "")
                    $(this).attr('value', $(this).data('default')).addClass('blur');
            });
            
        // Bind navigation fade-ins
        $('#nav li a').hover(function(){
            $(this).siblings('a.over')
                   .fadeIn(100);
        });
        
        $('#nav li a.over').mouseout(function(){
            if(!$(this).parent().hasClass('selected'))
                $(this).fadeOut(100);
        });
    },
    
    initSidebar: function()
    {
        // Sidebar recent comments/post tab system
        // Some redundancy for the sake of extreme simplicity of integration
        $('ul#recent_nav li a').eq(0).addClass('selected');
        
        // Bind recent posts tab
        $('ul#recent_nav li.posts a').click(function(e){
            e.preventDefault();
            $(this).addClass('selected');
            $('ul#recent_nav li.comments a').removeClass('selected');
            $('#recent_comments_posts #recententries').show();
            $('#recent_comments_posts #recentcomments').hide();
        });
        
        // Bind recent comments tab
        $('ul#recent_nav li.comments a').click(function(e){
            e.preventDefault();
            $(this).addClass('selected');
            $('ul#recent_nav li.posts a').removeClass('selected');
            $('#recent_comments_posts #recententries').hide();
            $('#recent_comments_posts #recentcomments').show();
        });
    }
};

/**
 * Application view related functions
 * - Image select for screenshots
 */
 
AppsBuzz.AppInfo = {
    
    init: function()
    {
        if($('ul#thumbs').is('*'))
            this.initImageSwitch();
    },
    
    initImageSwitch: function()
    {
        var screens = $('ul#screens li'),
            thumbs  = $('ul#thumbs li');
        
        thumbs.eq(0).addClass('current');
        
        // Position screens in descending z-index from current
        screens.each(function(i){
            if(i == 0) {
                $(this).addClass('current');
            } else {
                $(this).hide();
            }
                
            $(this).css('z-index', (screens.length - (i+1)));
        });
        
        // Bind change event to links
        thumbs.find('a.show').each(function(i){
           $(this).click(function(e){
               e.preventDefault();
               
               // Swap current thumb
               $(this).parent().addClass('current').siblings().removeClass('current');
               
               // Swap screens
               var topZ = parseInt($('ul#screens li.current').css('z-index'));
               
               $('ul#screens li.current')
                    .hide()
                    .css('z-index', parseInt(screens.eq(i).css('z-index')))
                    .removeClass('current');
                    
               screens.eq(i).show().addClass('current').css('z-index', topZ);
           });
        });
    }
}

/**
 *  Scrollbar on the front page for the featured content box
 */
AppsBuzz.FeaturedScroll = {
    
    init: function()
    {   
		// Image hover
		$('#featured li, #sidebar #recententries li').hover(function(){
			$(this).find('img').animate({ opacity: 0.6 }, 200);			
		}, function(){
			$(this).find('img').animate({ opacity: 1 }, 200);
		})
		
         //scrollpane parts
        var handle = $('#featured #handle'),
            scrollPane = $('#featured_inner'),
            scrollContent = $('#featured_inner ul');
        
        handle.css('width', 100 * (scrollContent.width() / handle.parent().width()));
        
        // Fix the width of the scroll content box so that it is
        // only as large as it need be
        var scrollContentWidth = 0;
        scrollContent.find('li').each(function(){
            scrollContentWidth += parseInt($(this).outerWidth()) + parseInt($(this).css('margin-right'));
        });
        scrollContent.css('width', scrollContentWidth);
        
        handle.draggable({axis : 'x', containment : 'parent', 
            drag : function(e, ui){
                var position = parseInt(handle.css('left')) / handle.parent().width();
                var scrollPosition = scrollContent.width() * position;
                scrollContent.css('left', -(scrollPosition));
            }
        });
        
    }
}

AppsBuzz.SidebarApps = {
    init: function() {
        
        var overlay = $('#app_hover');
        var self    = this;
 
        $('ol.paid li').each(function(i){
            $(this).hover(function(){
                clearTimeout(self.timeout);
                self.openHover($('ul.paid li').eq(i), $('ol.paid li').eq(i));
            }, function(){
                clearTimeout(self.timeout); 
                self.timeout = setTimeout(function(){
                        self.closeHover();
                    }, 500);
            });
        });
        
        $('ol.free li').each(function(i){
            $(this).hover(function(){
                clearTimeout(self.timeout);
                self.openHover($('ul.free li').eq(i), $('ol.free li').eq(i));
            }, function(){
                clearTimeout(self.timeout); 
                self.timeout = setTimeout(function(){
                        self.closeHover();
                    }, 500);
            });
        });
        
        $('#app_hover').mouseenter(function(){
            clearTimeout(self.timeout); 
        }).mouseleave(function(){
            clearTimeout(self.timeout); 
             self.timeout = setTimeout(function(){
                     self.closeHover();
                 }, 500);
        });
        
    },
    
    openHover: function(item, visible_item)
	{   	
	    this.closeHover();
	    
	    var html    = item.html();
	    var overlay = $('#app_hover');
	    
        overlay.css('display', 'none');
	    
	    // Exceeding screen edge fix
        var window_width  = $(window).width();
        var overlay_right = visible_item.offset().left + 230;
        
        if(overlay_right > window_width) { // x position is x pos - 230
            overlay.css({
	            top  : visible_item.offset().top + 70,
	            left : visible_item.offset().left - 150
	        });
        } else { // else x pos is normal x pos
            overlay.css({
	            top  : visible_item.offset().top + 70,
	            left : visible_item.offset().left + 0
	        });
        }
        
	    overlay
	        .empty()
	        .append(html);
	        
	    overlay.fadeIn(100);
	},
	
	closeHover: function()
	{
	    clearTimeout(self.timeout);
	    $('#app_hover').fadeOut(100);
	},
}

$(document).ready(function(){
  AppsBuzz.init();  
});
