// this is a bunch of shit liz found on the internet somewhere in 1995
window.onload = rolloverInit;

function rolloverInit() {
	for (var i=0; i<document.images.length; i++) {
		if (document.images[i].parentNode.tagName == "A") {

			setupRollover(document.images[i]);
		}
	}
}

function setupRollover(thisImage) {
	thisImage.outImage = new Image();
	thisImage.outImage.src = thisImage.src;
	thisImage.onmouseout = rollOut;


	thisImage.overImage = new Image();
	thisImage.overImage.src = "images/" + thisImage.id + "_on.jpg";
	thisImage.onmouseover = rollOver;	
}

function rollOut() {
	this.src = this.outImage.src;

}

function rollOver() {
	this.src = this.overImage.src;
}


function showPic (whichpic) {
	if (document.getElementById) {
	document.getElementById ('placeholder') .src = whichpic.href;

	return false;
	} 	
	else {
	return true;
	}
}
// end 1995


// now the real magic starts
init = function(){
    go = function(targetURL, targetTitle){
        if(targetURL == undefined && targetURL != '#/') {
            $('#content').load('404.php');
            window.history.pushState(data, '404! ' + rootTitle, fragmentURL[0] + rootURL + '/#/404040404');
            document.title = '404! ' + rootTitle;
            _gaq.push(['_trackEvent', 'Pages', 'Click', 'Error 404']);
        } else {
            $('#content').load(targetURL);
            window.history.pushState(data, targetTitle + ' | ' +rootTitle , fragmentURL[0] + rootURL + '/#/' + targetURL);
            document.title = targetTitle + ' | ' +rootTitle;
            _gaq.push(['_trackEvent', 'Pages', 'Click', targetTitle]);
        };
        
        return false;
        
    };
    
    switchLink = function(targetAnchor){
        $('#sidebar ul li a').removeClass('active');
        $('#sidebar .navigation').removeClass('navigation-active');
        $(targetAnchor).addClass('active');
        $(targetAnchor).parents().find('navigation').addClass('navigation-active');
        targetURL = targetAnchor.attr('href');
        targetTitle = targetAnchor.text();
        go(targetURL, targetTitle);
    };
    
    data = '';
    urlInspect = function(){
        rootURL = 'liztan.com';
        rootTitle = 'L I Z T A N: Portfolio and Projects';
        currentURL = window.location.href;
        fragmentURL = currentURL.split(rootURL);
        
        // if there's a page being looked for
        if(fragmentURL.length > 1) {
            // and it doesn't already have a hash
            if(currentURL.indexOf('#/') == -1) {
                window.history.pushState(data, "Home", fragmentURL[0] + rootURL + "/#/");
                _gaq.push(['_trackEvent', 'Pages', 'Click', 'Home']);
            } else {
                destinationURL = currentURL.split('#/');
                destinationURL = destinationURL[1];
                targetAnchor = $('#sidebar .navigation li a[href="'+destinationURL+'"]');
                
                if(destinationURL != '') {
                    switchLink(targetAnchor);
                };
                
            }
        } else {
        // there's nothing after the rootURL, so let's set up the hash structure
            
            window.location.href = fragmentURL[0] + rootURL + '#';
        }
        
    }
    
    $('#sidebar .navigation li a').live('click', function(){
        targetAnchor = $(this);
        switchLink(targetAnchor);
        return false;
        
    });
    
    keyNav = function(direction){
        if(!$('#sidebar ul li a.active').length) {
            $('#sidebar ul:first li:first a').addClass('active');
            targetAnchor = $('#sidebar ul li .active');
            switchLink(targetAnchor);
        } else {
            activeAnchor = $('#sidebar ul li .active');
            if(direction == 'right') {
                // go down the list
                targetAnchor = $('#sidebar ul li .active').parent().next().find('a');
                nextList = activeAnchor.parent().parent().next('ul');
                if(activeAnchor.parent().is(':last-child') && nextList.length) {
                    targetAnchor = nextList.find('li:first-child a');
                };
                
                if(activeAnchor.parent().is(':last-child') && nextList.length == 0) {
                    targetAnchor = activeAnchor;
                }
                
                switchLink(targetAnchor);
                
                
            } else {
                // go up the list
                targetAnchor = $('#sidebar ul li .active').parent().prev().find('a');
                nextList = activeAnchor.parent().parent().prev('ul');
                if(activeAnchor.parent().is(':first-child') && nextList.length) {
                    targetAnchor = nextList.find('li:last-child a');
                };
                
                if(activeAnchor.parent().is(':first-child') && nextList.length == 0) {
                    targetAnchor = activeAnchor;
                }
                
                switchLink(targetAnchor);
            }
        }
    };
    
    
    $(document).keyup(function(e) {
        switch(e.which) {
            case 39:    keyNav('right');
            break;
            case 37:    keyNav('left');
        }
    });
    
    urlInspect();
};


// HURROH!

