/**
 * Shears of Elegance Main JavaScript File v1.0b
 * http://shearsofelegance.com
 *
 * Copyright 2011, Christopher Woodall
 * http://happrobotlabs.com
 *
 * Requires both jquery.js and jquery.url.js
 * both released under the MIT, BSD liscense
 *
 * Date: Thu Jun 2, 2011
 */
 
$(document).ready(function() {	

	// set initial states
	var bg_img = 2;
	
	// Setup an object containing all of pages and their titles
	var pages = {
		'innovation': 'Innovative Design',
		'cut': 'Cut & Styles',
		'color': 'Color & Texture',
		'treatments': 'Treatments',
		'appointments': 'Appointments',
		'special': 'Special Occasions',
		'products': 'Products',
		'boutique': 'Boutique Items',
		'specials': 'Specials',
		'error': 'Error'
	};
	
	var background = {
		'innovation': 2,
		'cut': 3,
		'color': 4,
		'treatments': 5,
		'appointments': 2,
		'special': 3,
		'products': 4,
		'boutique': 5,
		'specials': 2,
		'error': 1
	};
	
	// Get the current URL and set the page appropriately
	var url = $.url();
	var fragment = url.fsegment(1);
	 
	if (fragment == '') {
		hideAll();
		$('#nav_display_bg').fadeIn('slow');
		$('#nav_display').fadeIn('slow');
		$('#content').fadeIn('slow');
		$('#content').load('content/home.html');
	} else {	
		var viewStatus = changeView(fragment, pages);
		document.title = "Shears of Elegance - " + pages[fragment];
		$('#container').removeClass('bg' + background[fragment]).hide();
		$('#container').addClass('bg' + background[fragment]).show();
		if (viewStatus === false) {
			changeView('error', pages);
			$('#container').removeClass().hide();
			$('#container').addClass('bg' + background['error']).show();
			document.title = "Shears of Elegance - Error";
		}
	}		

	$('.nav_button').click(function () {
		// Change URL to the idea before performing the actions
		window.location.replace($(this).attr('href'));
		// Update the URL to the current content fragment
		url = $.url();
		fragment = (url.fsegment(1));

		var viewStatus = changeView(fragment, pages);
		if (viewStatus === false) {
			changeView('error', pages);
			$('#container').removeClass().hide();
			$('#container').addClass('bg' + background['error']).show();
		} else if (viewStatus === true) {
			$('#container').removeClass().hide();
			$('#container').addClass('bg' + background[fragment]).show();
		}
	});
});

function hideAll(speed) {
	// Hide all content at some speed.
	$('#content_bg').hide(speed);
	$('#content').hide(speed);
	$('#nav_display_bg').hide(speed);
	$('#nav_display').hide(speed);
}

function showAll(speed) {
	// Show all content at some speed.
	$('#content_bg').show(speed);
	$('#content').show(speed);
	$('#nav_display').show(speed);
	$('#nav_display_bg').show(speed);
}
function slideUpAll(speed) {
		clearAll();

	// Hide all content at some speed.
	$('#content_bg').fadeOut(speed);
	$('#content').fadeOut(speed);
	$('#nav_display_bg').fadeOut(speed);
	$('#nav_display').fadeOut(speed);
}

function slideDownAll(speed) {
	// Show all content at some speed.
	$('#content_bg').fadeIn(speed);
	$('#content').fadeIn(speed);
	$('#nav_display').fadeIn(speed);
	$('#nav_display_bg').fadeIn(speed);
}

function clearAll() {
	$('#content').text('');
	$('#nav_display h1').text('');
}

/**
 * changeView(fragment, pages): looks for the fragment in an object of pages
 * if a match is found, the script will carry out change and show the new content. 
 * If no view is found changeView will return false. A false return should be handled
 * in your code.
 *
 * All content is by default stored in content/page_property.html (content being in the
 * directory of your index.html page or whatever page is calling the script).
 *
 * Example:
 * 		var fragment = 'turtle';
 *		var pages = { 'meow': 'Cats and Stuff', 'dogs': 'Dogs and Stuff', 'error': 'Error Page'};
 *		viewStatus = changeView(fragment, pages);
 *		if (viewStatus === false) {
 *			changeView('error', pages);
 *		}
 *
 * @return match
 */
function changeView(fragment, pages) {
	hideAll();
	// Check to make sure fragment is an acceptable page
	var match = false;
	for (var page in pages) {
		if (fragment === page) {
			$('#content').load('content/'+fragment+'.html');
			$('#nav_display h1').text(pages[fragment]);
			document.title = "Shears of Elegance - " + pages[fragment];
			match = true;
		}
	}
	
	if (match === true) {
		slideDownAll('slow');
	}
	return match;
};
