// This file contains all the Javascript functions used
// in Dropline GNOME's Wordpress theme.
// Written by Silvestre Herrera. Sorry for the bad Javascript!

$.ajaxSetup({cache:false}); // Disable Ajax Cache

// Check referrer to see if the user comes from a search engine
// If the user is trying to load a page, we want him to load the index first
// and then we'll load that page using AJAX
// ------------------------------------------------------------------------------

$(document).ready(function() {
	
	var referrer = document.referrer
	var referrerHostName = referrer.split('/')[2]
	
	if (referrer == '') {
		return false;												// If there is no referrer, do nothing
	} else if (referrerHostName == document.location.hostname) {	// else, load the referrer into div
		loadReferrer(referrer);
	} else {
		return false;
	}
	
});

// This function loads the requested URL using AJAX
function loadReferrer(slug) {
	
	var a = document.createElement('a');
	a.href = slug;
	var path = a.pathname + a.search;
	var path = path.replace('/', '')
	var path = path.replace('/', '')
	
	$('.featuresMenu').fadeOut('fast', function () {
		$('.features').slideUp('slow'); // Hide Features
		$('.download').slideUp('slow'); // Hide Download
	});
	
	$('#blog').fadeOut('fast', function() {
		$('#blogTop').hide();
		$('#blogCenter').hide();
		$('#blogBottom').hide();
		$('.blog').css('margin-top', '0px');
		$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
		$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
		$('.downloadHeader').removeClass('aboutHeader').addClass('singlePostTop');
		$('.singlePostTop').show()
		$('.downloadHeader').show();
		$('.singlePostCenter').show();
		$('.singlePostBottom').show();
		$('.singlePostCenter').load(slug, function () {
			$('#blog').fadeIn('fast');
		});
		
		$('#blog').show();

	});
	
	$('.menu div').css('background-position', 'center');
	
	document.title = 'dropline GNOME desktop';
	
	window.location.hash = path
}


// Make posts load into div with AJAX
// ------------------------------------------------------------------------------

// This should check the URL hash and select the correct button in the menu
// but for some reason it doesn't work.
$(document).ready(function() {

	if (location.hash == '#home') {
		loadHome(function() {
			$('.menu div').css('background-position', 'center');
			$('.mnuHome').css('background-position', 'bottom');
		});	
	} else if (location.hash == '#download') {
		loadDownloads(function() {
			$('.menu div').css('background-position', 'center');;
			$('.mnuDownload').css('background-position', 'bottom');
		});
	} else if (location.hash == '#involved') {
		loadInvolved(function() {
	   		$('.menu div').css('background-position', 'center');
			$('.mnuInvolved').css('background-position', 'bottom');
		});
	} else if (location.hash == '#about') {
		loadAbout(function() {
	   		$('.menu div').css('background-position', 'center');;
			$('.mnuAbout').css('background-position', 'bottom');
		});
	} else if (location.hash == '') {
		/*
		loadHome(function() {
	   		$('.menu div').css('background-position', 'center');
			$('.mnuHome').css('background-position', 'bottom');
		});
		*/
	}

});

// This changes the behavior of links in Post Titles
$(document).ready(function() {

	$(".blogPostTitle a").click(function() {
		var post_id = $(this).attr("href")
		var a = document.createElement('a');
		a.href = post_id;
		var path = a.pathname + a.search;
		var path = path.replace('/', '')
		var path = path.replace('/', '')
		
		$('.features').slideUp('slow'); 
		$('.download').slideUp('slow');
		$('.blog').css('margin-top', '0px');
		$('#blog').fadeOut('fast', function() {
			$('#blogTop').hide();
			$('#blogCenter').hide();
			$('#blogBottom').hide();
			$('.singlePostCenter').html('<p class="loading">Loading, please wait...</p>');
			$(".singlePostCenter").load("" + post_id );
			window.location.hash = path
			$('.downloadHeader').removeClass('downloadHeader').addClass('singlePostTop');
			$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
			$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
			$('.singlePostTop').show();
			$('.singlePostCenter').show();
			$('.singlePostBottom').show();
			$('#blog').fadeIn('fast');
			$('.menu div').css('background-position', 'center');
		});
		return false;
	});	
});

// This changes the behavior of the "read more" link
$(document).ready(function() {
	$(".more-link").click(function() {
		var post_id = $(this).attr("href")
		$('.features').slideUp('slow'); 
		$('.download').slideUp('slow');
		$('.blog').css('margin-top', '0px');
		$('#blog').fadeOut('fast', function() {
			$('#blogTop').hide();
			$('#blogCenter').hide();
			$('#blogBottom').hide();
			$('.singlePostCenter').html('<p class="loading">Loading, please wait...</p>');
			$(".singlePostCenter").load("" + post_id );
			window.location = "#post"
			$('.downloadHeader').removeClass('downloadHeader').addClass('singlePostTop');
			$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
			$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
			$('.singlePostTop').show();
			$('.singlePostCenter').show();
			$('.singlePostBottom').show();
			$('#blog').fadeIn('fast');
			$('.menu div').css('background-position', 'center');
		});
		return false;
	});	
});

// This changes the behavior in the archives titles
$(document).ready(function() {
	$(".singlePostTitle a").click(function() {
		var post_id = $(this).attr("href")
		var a = document.createElement('a');
		a.href = post_id;
		var path = a.pathname + a.search;
		var path = path.replace('/', '')
		var path = path.replace('/', '')
		
		$('.features').slideUp('slow'); 
		$('.download').slideUp('slow');
		$('.blog').css('margin-top', '0px');
		$('#blog').fadeOut('fast', function() {
			$('#blogTop').hide();
			$('#blogCenter').hide();
			$('#blogBottom').hide();
			$('.singlePostCenter').html('<p class="loading">Loading, please wait...</p>');
			$(".singlePostCenter").load("" + post_id );
			window.location.hash = path
			$('.downloadHeader').removeClass('downloadHeader').addClass('singlePostTop');
			$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
			$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
			$('.singlePostTop').show();
			$('.singlePostCenter').show();
			$('.singlePostBottom').show();
			$('#blog').fadeIn('fast');
			$('.menu div').css('background-position', 'center');
		});
		return false;
	});	
});

// This changes the behavior in the footer's archives links
$(document).ready(function() {
	$(".archives a").click(function() {
		var post_id = $(this).attr("href")
		var a = document.createElement('a');
		a.href = post_id;
		var path = a.pathname + a.search;
		var path = path.replace('/', '')
		var path = path.replace('/', '')
		
		$('.features').slideUp('slow'); 
		$('.download').slideUp('slow');
		$('.blog').css('margin-top', '0px');
		$('#blog').fadeOut('fast', function() {
			$('#blogTop').hide();
			$('#blogCenter').hide();
			$('#blogBottom').hide();
			$('.singlePostCenter').html('<p class="loading">Loading, please wait...</p>');
			$(".singlePostCenter").load("" + post_id );
			window.location.hash = path
			$('.downloadHeader').removeClass('downloadHeader').addClass('singlePostTop');
			$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
			$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
			$('.singlePostTop').show();
			$('.singlePostCenter').show();
			$('.singlePostBottom').show();
			$('#blog').fadeIn('fast');
			$('.menu div').css('background-position', 'center');
		});
		return false;
	});	
});

// This changes the behavior in the footer's pages links
$(document).ready(function() {
	$(".pages a").click(function() {
		var post_id = $(this).attr("href")
		var a = document.createElement('a');
		a.href = post_id;
		var path = a.pathname + a.search;
		var path = path.replace('/', '')
		var path = path.replace('/', '')
		
		window.location.hash = path
		/*
		$('.features').slideUp('slow'); 
		$('.download').slideUp('slow');
		$('.blog').css('margin-top', '0px');
		$('#blog').fadeOut('fast', function() {
			$('#blogTop').hide();
			$('#blogCenter').hide();
			$('#blogBottom').hide();
			$('.singlePostCenter').html('<p class="loading">Loading, please wait...</p>');
			$(".singlePostCenter").load("" + post_id );
			window.location.hash = path
			$('.downloadHeader').removeClass('downloadHeader').addClass('singlePostTop');
			$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
			$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
			$('.singlePostTop').show();
			$('.singlePostCenter').show();
			$('.singlePostBottom').show();
			$('#blog').fadeIn('fast');
			$('.menu div').css('background-position', 'center');
		});
		*/
		return false;
	});	
});

// Features Carousel
// Note to self: should simplify this to something like loadFeature(featureName)
// ------------------------------------------------------------------------------

$(document).ready(function() { // Loads the first feature
  $('.featureImg').fadeOut('fast', function() {
    $('.featureImg').html('<img alt="Desktop" src="wp-content/themes/dropline/images/features/newDesktop.png" />').fadeIn('fast');
  });
  $('.featuresMenu1').css('background-position', 'top');
  $('.featuresMenu2').css('background-position', 'bottom');
  $('.featuresMenu3').css('background-position', 'bottom');
  $('.mnuHome').css('background-position', 'bottom');
});

function loadFeature1() { // Loads the first feature
  $('.featureImg img').fadeOut('fast');
  $('.featureImg').fadeOut('fast', function() {
    $('.featureImg').html('<img alt="Desktop" src="wp-content/themes/dropline/images/features/newDesktop.png" />').fadeIn('fast');
  });
  $('.featuresMenu1').css('background-position', 'top');
  $('.featuresMenu2').css('background-position', 'bottom');
  $('.featuresMenu3').css('background-position', 'bottom');
}
function loadFeature2() { // Loads the second feature
  $('.featureImg img').fadeOut('fast');
  $('.featureImg').fadeOut('fast', function() {
    $('.featureImg').html('<img alt="Nautilus" src="wp-content/themes/dropline/images/features/Nautilus.png" />').fadeIn('fast');
  });
  $('.featuresMenu2').css('background-position', 'top');
  $('.featuresMenu1').css('background-position', 'bottom');
  $('.featuresMenu3').css('background-position', 'bottom');
}
function loadFeature3() { // Loads the third feature
  $('.featureImg img').fadeOut('fast');
  $('.featureImg').fadeOut('fast', function() {
    $('.featureImg').html('<img alt="Social" src="wp-content/themes/dropline/images/features/socialGnome.png" />').fadeIn('fast');
  });
  $('.featuresMenu3').css('background-position', 'top');
  $('.featuresMenu2').css('background-position', 'bottom');
  $('.featuresMenu1').css('background-position', 'bottom');
}


// Main menu functions
// ------------------------------------------------------------------------------

// This function loads the home page
function loadHome () {
	$('.features').slideDown('slow', function() { // Show Features
			$('.featuresMenu').fadeIn('fast');
		}); 
	
	$('#blog').fadeOut('fast', function() {
		$('.downloadHeader').hide();
		$('.singlePostTop').hide();
		$('.singlePostCenter').hide();
		$('.singlePostBottom').hide();
		$('#blogTop').show();
		$('#blogCenter').show();
		$('#blogBottom').show();
		$('.blog').css('margin-top', '12px');
		$('#blog').fadeIn('fast');
		$('.download').fadeIn('fast'); // Show Download
		$('.downloadHeader').hide();
		$('.involvedHeader').hide();
		$('.aboutHeader').hide();
	});
	
	$('.menu div').css('background-position', 'center');
	$('.mnuHome').css('background-position', 'bottom');
	
	window.location.hash = "home"
	document.title = 'dropline GNOME desktop';
}

// This function loads the downloads page
function loadDownloads () {
	$('.featuresMenu').fadeOut('fast', function () {
		$('.features').slideUp('slow'); // Hide Features
		$('.download').slideUp('slow'); // Hide Download
	});
	
	$('#blog').fadeOut('fast', function() {
		$('#blogTop').hide();
		$('#blogCenter').hide();
		$('#blogBottom').hide();
		$('.blog').css('margin-top', '0px');
		$('.singlePostTop').show()
		$('.downloadHeader').show();
		$('.singlePostCenter').show();
		$('.singlePostBottom').show();
		$('.singlePostCenter').html('<p class="loading">Loading, please wait...</p>');
		$('.singlePostCenter').load('index.php/download', function () { // This line here is the actual link.
			$('.singlePostTitle').hide();
		});
		$('.singlePostTop').removeClass('singlePostTop').addClass('downloadHeader');
		$('.involvedHeader').removeClass('involvedHeader').addClass('downloadHeader');
		$('.aboutHeader').removeClass('aboutHeader').addClass('downloadHeader');
		$('#blog').fadeIn('fast');
	});
	
	$('.menu div').css('background-position', 'center');
	$('.mnuDownload').css('background-position', 'bottom');
	
	document.title = 'dropline GNOME desktop > Download';
	
 	window.location.hash = "download"
}

// This function loads the get involved page
function loadInvolved () {
	$('.featuresMenu').fadeOut('fast', function () {
		$('.features').slideUp('slow'); // Hide Features
		$('.download').slideUp('slow'); // Hide Download
	});
	
	$('#blog').fadeOut('fast', function() {
		$('#blogTop').hide();
		$('#blogCenter').hide();
		$('#blogBottom').hide();
		$('.blog').css('margin-top', '0px');
		$('.downloadHeader').removeClass('downloadHeader').addClass('singlePostTop');
		$('.involvedHeader').show();
		$('.singlePostTop').show();
		$('.singlePostCenter').show();
		$('.singlePostBottom').show();
		$('.singlePostCenter').html('<p class="loading">Loading, please wait...</p>');
		$('.singlePostCenter').load('index.php/involved', function () { // This line here is the actual link.
			$('.singlePostTitle').hide();
		});
		$('.singlePostTop').removeClass('singlePostTop').addClass('involvedHeader');
		$('.downloadHeader').removeClass('downloadHeader').addClass('involvedHeader');
		$('.aboutHeader').removeClass('aboutHeader').addClass('downloadHeader');
		$('#blog').fadeIn('fast');
		$('.singlePostTitle').hide();
	});
	
	$('.menu div').css('background-position', 'center');;
	$('.mnuInvolved').css('background-position', 'bottom');
	
	document.title = 'dropline GNOME desktop > Get Involved';
	
	window.location.hash = "involved"
}

// This function loads the about page
function loadAbout () {
	$('.featuresMenu').fadeOut('fast', function () {
		$('.features').slideUp('slow'); // Hide Features
		$('.download').slideUp('slow'); // Hide Download
	});
	
	$('#blog').fadeOut('fast', function() {
		$('#blogTop').hide();
		$('#blogCenter').hide();
		$('#blogBottom').hide();
		$('.blog').css('margin-top', '0px');
		$('.downloadHeader').removeClass('downloadHeader').addClass('singlePostTop');
		$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
		$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
		$('.singlePostTop').show();
		$('.singlePostCenter').show();
		$('.singlePostBottom').show();
		$('.singlePostCenter').html('<p class="loading">Loading, please wait...</p>');
		$('.singlePostCenter').load('index.php/about', function () { // This line here is the actual link.
			$('.singlePostTitle').hide();
		});
		$('.involvedHeader').removeClass('involvedHeader').addClass('aboutHeader');
		$('.downloadHeader').removeClass('downloadHeader').addClass('aboutHeader');
		$('.singlePostTop').removeClass('singlePostTop').addClass('aboutHeader');
		$('.singlePostTitle').show();
		$('#blog').fadeIn('fast');
	});
	
	$('.menu div').css('background-position', 'center');
	$('.mnuAbout').css('background-position', 'bottom');
	
	document.title = 'dropline GNOME desktop > About';
	
	window.location.hash = "about"
}

// This function loads any page or post slug
// When deep linking, use href="javascript:loadPage('slug');"
// This happens automatically on posts titles and "read more" links

// Special sections like About, Download, Get Involved and Home have 
// have their own function to call them because they require
// special style.

// Need to create the conditionals in loadPage() to merge all
// other special functions into this one.

function loadPage(slug) {
	$('.featuresMenu').fadeOut('fast', function () {
		$('.features').slideUp('slow'); // Hide Features
		$('.download').slideUp('slow'); // Hide Download
	});
	
	$('#blog').fadeOut('fast', function() {
		$('#blogTop').hide();
		$('#blogCenter').hide();
		$('#blogBottom').hide();
		$('.blog').css('margin-top', '0px');
		$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
		$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
		$('.downloadHeader').removeClass('aboutHeader').addClass('singlePostTop');
		$('.singlePostTop').show()
		$('.downloadHeader').show();
		$('.singlePostCenter').show();
		$('.singlePostBottom').show();
		$('.singlePostCenter').load('index.php/' + slug, function () {
			$('#blog').fadeIn('fast');
		});

	});
	
	$('.menu div').css('background-position', 'center');
	
	document.title = 'dropline GNOME desktop > ' + slug;
	
	window.location.hash = slug
}

/*
$(document).ready(function () {
	
	$(".menu a").click(function () {
		
		var url = $(this).attr.href
		a = document.createElement('a');

		a.href = url;
		var path = a.pathname + a.search;
		
		$('.featuresMenu').fadeOut('fast', function () {
			$('.features').slideUp('slow'); // Hide Features
			$('.download').slideUp('slow'); // Hide Download
		});

		$('#blog').fadeOut('fast', function() {
			$('#blogTop').hide();
			$('#blogCenter').hide();
			$('#blogBottom').hide();
			$('.blog').css('margin-top', '0px');
			$('.involvedHeader').removeClass('involvedHeader').addClass('singlePostTop');
			$('.aboutHeader').removeClass('aboutHeader').addClass('singlePostTop');
			$('.downloadHeader').removeClass('aboutHeader').addClass('singlePostTop');
			$('.singlePostTop').show()
			$('.downloadHeader').show();
			$('.singlePostCenter').show();
			$('.singlePostBottom').show();
			$('.singlePostCenter').load(url, function () {
				$('#blog').fadeIn('fast');
			});

		});

		$('.menu div').css('background-position', 'center');

		document.title = 'dropline GNOME desktop > ' + url;

		window.location.hash = path
		
	});
});
*/

// Look for hash changes to enable the back button navigation
// This implementation is not perfect, but it works for now.

var hash = location.hash;

setInterval(function()
{
    if (location.hash != hash)
    {  
        if (location.hash == '#home') {
        	loadHome();
        } else if (location.hash == '#download') {
        	loadDownloads();
        	$('.menu div').css('background-position', 'center');
			$('.mnuDownload').css('background-position', 'bottom');
        } else if (location.hash == '#involved') {
        	loadInvolved();
        	$('.menu div').css('background-position', 'center');
			$('.mnuInvolved').css('background-position', 'bottom');
        } else if (location.hash == '#about') {
        	loadAbout();
        	$('.menu div').css('background-position', 'center');
			$('.mnuAbout').css('background-position', 'bottom');
        } else if (location.hash == '') {
        	loadHome();
        } else {
			var slug = location.hash.replace('#','');
			loadPage(slug);
		}

        hash = location.hash;

    }
}, 100);
