/* Author: 

R. Clayton Miller 

*/

$(document).ready(function() {

    //######## Global Vars and Functions ########
    
    var slidespeed = 140;
    var pagetitle = "";
    
    // convert page anchor to real path
    function deanchor(dstring){
        var rstring;
        rstring = dstring.substring(1)+"/";
        return rstring;
    }
    
    //######## Page Layout ########
    
    function doLayout() {
        
        // check height
        if($(window).height()<$('div#main').height()){
            // top-glued version if the window is too short
            $('div#main').css({
                'top' : '0px',
                'margin' : '0'              
            });
            $('div#presteignbar').css({
                'top' : '360px',            
            });
        } else {
            // vertically-centered version if the window is tall enough
            $('div#main').css({
                'top' : '50%',
                'margin' : '-360px 0 0'             
            });
            $('div#presteignbar').css({
                'top' : '50%',          
            });
        }
    }
    
    $(window).resize(function(){
        doLayout();
    });
    
    //######## Page Loading ########
    
    // start loading a page in ajax before it's displayed
    // things in this fucntion happen immediately on click
    function loadContent(contentpath, pageanchor) {
    	// get content path from page anchor (once we switch to single-input for this function and update all calls)
    	//var contentpath = deanchor(pageanchor);

        $.get(contentpath, function(data) { 
            applyContent(data);
        });     
        
        // de-highlight all links
        $('div.halfquad a').removeClass('on');

		// highlight our new link and slide down a subnav if there is one        
        $('div#nav a[href$="'+pageanchor+'"]').addClass('on');
        $('div#nav a[href$="'+pageanchor+'"]').siblings().slideDown(slidespeed);
        
        //if ($('div#nav ul a[href="'+pageanchor+'+]').hasClass('top')){
		
		// does the new page have children? the dom will tell us because a link will have a sibling in its li
		if ($('div#nav a[href$="'+pageanchor+'"]').siblings().length>0){
        	// it has siblings, so don't slide anything up
        } else {
        	// no siblings, but it might be second-level, so check if there's a slash in the hash
        	if (pageanchor.search("/")==-1){
        		// nope, no slash, so clean up the subnavs
            	$('div#nav ul ul').slideUp(slidespeed);
        	}
        }
        
        // save a page title for the gas jets
        pagetitle = $('div#nav a[href$="'+pageanchor+'"]').text();
        
        // let's animate those panels
        
        var out1 = setTimeout("$('#q1 div.panel').css({'margin-left':360,'opacity':0})", 5); 
        var out2 = setTimeout("$('#q2 div.panel').css({'margin-top':360,'opacity':0})", 50);  
        var out3 = setTimeout("$('#q3 div.panel').css({'margin-top':-360,'opacity':0})", 100);  
        var out4 = setTimeout("$('#q4 div.panel').css({'margin-left':-360,'opacity':0})", 150);  
    }
    
    // display the data in the DOM on success of loadContent()
    // latency depends on load time
    function applyContent(contentdata) {	    	
        var contentarray = contentdata.split("<hr>"); // a real-life JS ninja wrote this part for me in like 5 seconds
        $("#q1 div.panel").html(contentarray[0]); 
        $("#q2 div.panel").html(contentarray[1]); 
        $("#q3 div.panel").html(contentarray[2]); 
        $("#q4 div.panel").html(contentarray[3]);               

        // highlight the a that corresponds to the location hash
        $('div#nav a[href$="'+window.location.hash+'"]').addClass('on');
        // expand subnav if it's there
        $('div#nav a[href$="'+window.location.hash+'"]').parent().children().slideDown(slidespeed);
        
        // animate panels back into place
        var in1 = setTimeout("$('#q1 div.panel').css({'margin-left':0,'opacity':100})", 5); 
        var in2 = setTimeout("$('#q2 div.panel').css({'margin-top':0,'opacity':100})", 50); 
        var in3 = setTimeout("$('#q3 div.panel').css({'margin-top':0,'opacity':100})", 100); 
        var in4 = setTimeout("$('#q4 div.panel').css({'margin-left':0,'opacity':100})", 150);
        
        // change the title
		if (pagetitle != "Presteign"){
	        document.title="PRESTEIGN / "+pagetitle;
		} else {
			document.title="PRESTEIGN";
		}
    	
    }
    
    //######## Navigation triggers ########
    
    // hashchange is our master navigation function now
    // everything is triggered by the hashchange
    $(window).hashchange( function(){
    	loadContent(deanchor(window.location.hash), window.location.hash);
    });
       
    // load something when you first arrive
    if (window.location.hash=="" || window.location.hash=="#"){
        // if nothing else, load the presteign page (i know, i shouldn't be hardcoding this)
        loadContent("presteign/", "#presteign");
        // and collapse all subnavs immediately
        $('div#nav ul ul').slideUp(0);
        // highlight the home a
        $('div#nav a[href$="#presteign"]').addClass('on');
    } else {
        // load what's in the hash (and also send the hash itself)
        loadContent(deanchor(window.location.hash), window.location.hash);
    }
    
      /*
    // when you click a link, run loadContent on the hidden actual url (the a just navigates to a #)
    $('div.halfquad a').click(function(){
        // send the uri for loading and the page anchor for immediate stuff
        // OH WAIT DON'T because it's a haschange event now
        //loadContent($(this).attr('data-sourceuri'), $(this).attr('href'));
        
        // immediately highlight and expand subnav (if there is one) to increase sense of responsiveness
        // now in loadcontent
        //$(this).addClass('on');
        //$(this).siblings().slideDown(slidespeed);
    });
    */
    
    //######## Run once ########
    
    doLayout();  
    
    //######## Other misc ########
    
    //+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/rot13 [v1.0]

String.prototype.rot13 = function(){ //v1.0
	return this.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
};  

});

