/*

mrmattspangler.com - homepage special posts


*/

/*
are we on the homepage?

*/
var thissite = window.location.href.split("/");
var homepage_flag = false;
var queryresult = thissite[thissite.length - 1].split("?"); //getting querystring
var getvals = new Array();

if(thissite.length <= 5){
	if(thissite.length <= 4){
		homepage_flag = true;
	} else if(thissite[thissite.length - 2] == "page"){
		homepage_flag = true;
	}
}

function get_querystring(queryresult){
	
	if(queryresult.length < 1){
		return false;
	}
	
	if(!queryresult[1]){
		return false;
	}
	
	var queryvals = new Array();
	
	var querystring = queryresult[1].split("&");
	
	if(querystring.length > 0){
		for(l = 0; l < querystring.length; l++){
			querytemp = querystring[l].split("=");
			queryvals[querytemp[0]] = querytemp[1];
		}
	} else {
		return false;
	}
	
	return queryvals;
}


var getvals = get_querystring(queryresult);


var hp_mainposts;

function mms_highjack(){
	
	hp_mainposts = document.getElementById("mainpostlist");
	
	if(hp_mainposts){
		//highjack the main post file
		hp_mainposts.className = "hidden";
	}
}

function mms_homepage(){
	
	var valid = new Array(); //so we know what's valid
	var hp_main_index;
	
	//What page are we on?
	var hp_main_paged; //what page in the archive we're at
	var urlpieces = window.location.href.split("/");
	
	if(urlpieces[urlpieces.length - 2] == "page"){
		//in case there are querystring
		var pagepieces = urlpieces[urlpieces.length - 1].split("?");
		var hp_main_paged = parseInt(pagepieces[0]);
	
	} else {
	
		hp_main_paged = 1;
	
	}
	//What's the total number of pages we can possibly have?
	var hp_main_maxpages = 9999; //the max number of pages that exist on the blog - TODO:how to get total number of pages available?
	
	
	// First, figure out what we got to work with
	// Hide all photos and quotes
	for(i = 0; i < hp_mainposts.childNodes.length; i++){
		
		if(hp_mainposts.childNodes[i].tagName == "LI"){ //only LIs
			
			
			if((hp_mainposts.childNodes[i].className == "postitem posttype-quote" || hp_mainposts.childNodes[i].className == "postitem posttype-photo") && hp_main_paged == 1){
				
				//that are not photos or quotes
				
				hp_mainposts.childNodes[i].className = "hidden";
				hp_mainposts.childNodes[i].innerHTML = "";
				
			} else if(hp_mainposts.childNodes[i].className == "postitem posttype-social" || hp_mainposts.childNodes[i].className == "hidden") {
				
				//igonre the last social-type post, or posts already hidden
				
			} else {
				valid[valid.length] = new Array(i, hp_mainposts.childNodes[i].className); //these are all the posts that count
			}
		}
	}
	
	var hp_main_total = valid.length;
	
	var main_nav = document.getElementById("main_nav");
	
	this.mms_prev_url = function(){
		if(hp_main_paged > 1){
			return "http://" + window.location.hostname + "/page/" + (hp_main_paged - 1) + "?postposition=last";
		} else {
			return "#";
		}
	}
	
	this.mms_next_url = function(){
		if(hp_main_paged < hp_main_maxpages){
			return "http://" + window.location.hostname + "/page/" + (hp_main_paged + 1);
		} else {
			return "#";
		}
	}
	
	if(main_nav){
		main_nav.innerHTML = '<a href="' + this.mms_prev_url() + '" class="nav_prev" id="main_nav_prev" onclick="return mms_hp.mms_hp_prev()"><span>prev</span></a><a href="' + this.mms_next_url() + '" class="nav_next" id="main_nav_next" onclick="return mms_hp.mms_hp_next()"><span>next</span></a>';
		
		var main_prev_arrow = document.getElementById('main_nav_prev');
		var main_next_arrow = document.getElementById('main_nav_next');
	}
	
	this.setup_nav = function(){
		
		if(hp_main_total < 1){
			
			main_nav.className = "hidden";
			
		} else if(hp_main_index == 0){
			if(hp_main_paged <= 1){
				main_prev_arrow.className = "hidden";
			} else {
				main_prev_arrow.className = "nav_prev";
			}
			main_next_arrow.className = "nav_next";
			
		} else if(hp_main_index == hp_main_total){
			
			main_prev_arrow.className = "nav_prev";
			
			if(hp_main_paged == hp_main_maxpages){
				main_next_arrow.className = "hidden";
			} else {
				main_next_arrow.className = "nav_next";
			}
			
		} else {
			main_prev_arrow.className = "nav_prev";
			main_next_arrow.className = "nav_next";
		}
	}
	
	this.mms_hp_prev = function(){
		hp_main_index-= 1;
		this.setup_nav();
		
		if(hp_main_index < 0){
			//we're out of posts
			/*hp_main_paged-= 1;
			window.location = "http://" + window.location.hostname + "/page/" + hp_main_paged + "?postposition=last";*/
			return true;
		} else {
			this.mms_hp_show();
			return false;
		}
		
		
	}
	
	
	
	this.mms_hp_next = function(){
		hp_main_index+= 1;
		this.setup_nav();
		
		if(hp_main_index >= hp_main_total){
			return true;
		} else {	
			this.mms_hp_show();
			return false;
		}
	}
	
	
	
	this.mms_hp_show = function(){
	
		for(d = 0; d < valid.length; d++){
			if(d == hp_main_index){
			   hp_mainposts.childNodes[valid[d][0]].className = valid[d][1];
			} else {
				hp_mainposts.childNodes[valid[d][0]].className = "hidden";
			}
		}
	
	}
	
	if(getvals["postposition"]){
		if(getvals["postposition"] == "last"){
			hp_main_index = valid.length - 1; //the current post
		} else {
			hp_main_index = 0;
		}
	} else {
		hp_main_index = 0;
	}
	
	this.setup_nav();
	this.mms_hp_show();
	hp_mainposts.className = "postlist";
}