/**************************************************
    Chrismacpherson.com JavaScript    
    File    : Interface.js
    Purpose : Provide moving box functionality
    Author  : Chris MacPherson
    Date    : July 2008
    Licence : CC http://creativecommons.org/licenses/by/1.0/
    
    @TODO Bug in IE 6/7 where left side bar intermittently does not reappear on slide
    
***************************************************/

// Set up some global vars (with kom_ prefix)

var kom_scrH            = 0;                   	// Available screen height
var kom_scrW            = 0;                   	// Available screen width
var kom_navName         = "#navigation";		// Id of our main navigation element
var kom_navBtm          = 0;                   	// Bottom of navigation element
var kom_navL			= 0;					// Left of main navigation
var kom_onScreen        = "#content_container"; // Id of our onscreen content container element 
var kom_offScreen       = "#offscreen_content_container";  // Id of our offscreen content container element
var kom_infoBar1		= "#info_bar_1";	    // Id of the first information bar element
var kom_infoBar2        = "#info_bar_2";        // Id of the second information bar element
var kom_contentH        = 0;                   	// Content container height
var kom_contentW        = 0;                   	// Content container width
var kom_contentL        = 0;                   	// The left coordinate of the content page
var kom_contentMargin   = 20;                  	// Margins
var kom_animTime        = 500;                 	// Milliseconds that animation should run for

var ajaxUrl             = "http://" + document.domain + "/ajax";

$(document).ready(function()
{
    initData();
    
    // Set all containers to position:absolute
    $(kom_onScreen + ',' + kom_offScreen + ',' + kom_infoBar1 + ',' + kom_infoBar2).css({position:"absolute"});
    
    var cookie_w = $.cookie('exp_cjm_w');
    var cookie_a = $.cookie('exp_cjm_a');

	initContentContainers();

	initAnchors();
    
	// Setup window resize event listener
	$(window).resize(function(){
		resizeInterface();
	});
});

 
function initData()
{
	// Need to calculate the height of page using the page footer, adding a margin.
    var offset = $(kom_onScreen + " .content_footer").offset();
    kom_scrH = offset.top + $(kom_onScreen + " .content_footer").height() + kom_contentMargin;
    kom_scrW = $(window).width();

    // Set clipping window so we dont get window horizontal scroll bars
    $("#clip_window").css({height: kom_scrH + "px", width:(kom_scrW - 4) + "px", overflow:"hidden"});
    
    // Work out the bottom coordinate of the primary navigation
    offset = $(kom_navName).offset({ scroll: false });
    kom_navBtm = offset.top + $(kom_navName).height();
	kom_navL = offset.left;
	
    // Get content page height/width
    kom_contentH = $(kom_onScreen).height();
    kom_contentW = $(kom_onScreen).width();

    offset = $(kom_onScreen).offset({ scroll: false });
    kom_contentL = offset.left;
}

function resizeInterface()
{
	initData();
	initContentContainers();
}

function initContentContainers()
{   
    $(kom_onScreen, kom_offScreen, kom_infoBar1, kom_infoBar2).css({top: (kom_navBtm + kom_contentMargin) + "px"});
    $(kom_onScreen).css({left: ((kom_scrW - kom_contentW) / 2) + "px"});
    $(kom_offScreen).css({left: (-2 - kom_contentW) + "px", display:"block"});
    
    // Only show info bars if screen wide enough 
    //(must use default secondary width as element doesnt exist)
    if ((kom_contentL + kom_contentW + kom_contentMargin + $(kom_infoBar1).width() + kom_contentMargin) < $(window).width())
    {
    	$(kom_infoBar1).css('visibility', 'visible');    	
    }
    else
    {
    	$(kom_infoBar1).css('visibility', 'hidden');
    }
    
    // Get left edge of left sidebar
    var offset = $(kom_infoBar2).offset();
    if (offset.left > kom_contentMargin)
    {
        $(kom_infoBar2).css('visibility', 'visible');     
    }
    else
    {
        $(kom_infoBar2).css('visibility', 'hidden');
    }
    
    $(kom_infoBar1).css({left: (kom_contentL + kom_contentW + kom_contentMargin) + "px"});
    $(kom_infoBar2).css({left: (kom_contentL - $(kom_infoBar2).width() - kom_contentMargin) + "px"});
    
    
    
    // Work out which container is the tallest and set clipping region so it is taller.
    var onHeight = (kom_contentMargin + $(kom_onScreen + " .content_title").height() + $(kom_onScreen).height() + $(kom_onScreen + " .content_footer").height() + kom_contentMargin);
    var offHeight = (kom_contentMargin + $(kom_offScreen + " .content_title").height() + $(kom_offScreen).height() + $(kom_offScreen + " .content_footer").height() + kom_contentMargin); 
    var h = (onHeight > offHeight ? onHeight : offHeight);
    $("#clip_window").css({height: kom_scrH + "px"});
}


function initAnchors()
{

	// Get all anchors that have an id starting with 'weblog_' 
    // Then link them to use Ajax functions
    $('a[@id^="weblog-"]').each(function(){
        
        // Get weblog name from id
        var weblog = this.id.substr(7);
        
        this.href = "javascript:getArticleList('" + weblog + "', '')";
    });
    
    // Get all anchors that have an id starting with 'category_' 
    // Then link them to use Ajax functions
    $('a[@id^="category-"]').each(function(){
        
        // Get weblog and category from id
        var a = this.id.split('-');
        
        var weblog = a[1];
        var category = a[2];
        
        this.href = "javascript:getArticleList('" + weblog + "', '" + category + "')";
    });
    
    // Get all anchors that have an id starting with 'article_' 
    // Then link them to use Ajax functions
    $('a[@id^="article-"]').each(function(){
        
        // Get weblog and category from id
        var a = this.id.split('-');
        
        var weblog = a[1];
        var article = a[2];
        
        this.href = "javascript:getArticle('" + weblog.toLowerCase() + "', '" + article + "')";
    });    
}


function slideContainers(direction)
{
    initAnchors();
    
    // Hide the content in way of slide
    $(kom_infoBar1 + ',' + kom_infoBar2).hide();
    
    if (direction == 'right')
    {
	    // Slide onScreen off the right hand side of the screen
	    $(kom_onScreen).animate({ 
	        left : (kom_scrW + 50) + "px"
	      }, 1500 );
	      
	    $(kom_offScreen).animate({ 
	        left : kom_contentL + "px"
	      }, 1500 );
	    
	    $(kom_onScreen).queue(function(){
	        // Move offscreen back to left hand side of screen 
	        $(kom_onScreen).css({left: (-2 - kom_contentW) + "px", display:"block"}); 
	        
	        // Now switch the id of the onScreen and offScreen vars
	        var temp = kom_onScreen;
	        kom_onScreen = kom_offScreen;
	        kom_offScreen = temp;
	        
	        // Show the hidden, in the way, content
            $(kom_infoBar1 + ',' + kom_infoBar2).fadeIn("slow");
            
	        $(this).dequeue();
	    });
    }
    else if (direction == 'left')
    {
    	// Set offScreen container to right hand side of screen
    	$(kom_offScreen).css({left: (kom_scrW + 10) + "px", display:"block"});
    	
    	// Slide onScreen off the left hand side of the screen
	    $(kom_onScreen).animate({ 
	        left : (-10 - kom_scrW) + "px"
	      }, 1500 );
	      
	    $(kom_offScreen).animate({ 
	        left : kom_contentL + "px"
	      }, 1500 );
	    
	    $(kom_onScreen).queue(function(){
	        // Move offscreen back to left hand side of screen 
	        $(kom_onScreen).css({left: (-2 - kom_contentW) + "px", display:"block"}); 
	        
	        // Now switch the id of the onScreen and offScreen vars
	        var temp = kom_onScreen;
	        kom_onScreen = kom_offScreen;
	        kom_offScreen = temp;
	        
	        // Show the hidden, in the way, content
            $(kom_infoBar1 + ',' + kom_infoBar2).fadeIn("slow");
            
	        $(this).dequeue();
	    });
    }
}



function getArticleList(weblog, category)
{
	if (weblog != '')
	{
		// Set up category is present
	    var cat = (category == '' ? '' : '/' + category);
	    var title = '';
	    
	    $.get(ajaxUrl + "/get_article_list/" + weblog + cat, function(data){
	        $(kom_offScreen).html(data);
	        
	        // Make sure clip window is large enough for new content
	        setWindowHeight(kom_offScreen);
	        
	        // Now change window title to match content
        	document.title = $(kom_offScreen + " .content_title h1").text();
        	
	        slideContainers('right');
	    });
	}
}


function getArticle(weblog, article)
{
    if (weblog != '' && article != '')
    {
        $.cookie('exp_cjm_w', weblog, {expires: 1});
        $.cookie('exp_cjm_a', article, {expires: 1});
		
        $.get(ajaxUrl + "/get_article/" + weblog + "/" + article, function(data){
            $(kom_offScreen).html(data);

            // Make sure clip window is large enough for new content
            setWindowHeight(kom_offScreen);
            
            // Now change window title to match content
            document.title = $(kom_offScreen + " .content_title h1").text();

            // Make sure SourceHighlighter is called again
            SyntaxHighlighter.highlight();

            slideContainers('left');
        });
    }
}

function setWindowHeight(contentDiv)
{
    var offset = $(contentDiv + " .content_footer").offset();
    var height = offset.top + $(contentDiv + " .content_footer").height() + kom_contentMargin;
    $("#clip_window").css({"height": height  + "px", "min-height":height + "px"});

}
