/*********************************
*
*	Global JavaScript Functions
*
*********************************/

var j$ = jQuery;

/*--------------------------------------
	Stylesheet switcher using jQuery
---------------------------------------*/

(function (j$) {
	// Local vars for toggle
	var availableStylesheets = [];
	var activeStylesheetIndex = 0;

	// To loop through available stylesheets
	j$.stylesheetToggle = function () {
		activeStylesheetIndex++;
		activeStylesheetIndex %= availableStylesheets.length;
		j$.stylesheetSwitch(availableStylesheets[activeStylesheetIndex]);
	};

	// To switch to a specific named stylesheet
	j$.stylesheetSwitch = function (styleName) {
		j$('link[@rel*=style][title]').each(

		function (i) {
			this.disabled = true;
			if (this.getAttribute('title') == styleName) {
				this.disabled = false;
				activeStylesheetIndex = i;
			}
		});
		createCookie('style', styleName, 365);
	};

	// To initialise the stylesheet with it's 
	j$.stylesheetInit = function () {
		j$('link[rel*=style][title]').each(

		function (i) {
			availableStylesheets.push(this.getAttribute('title'));
		});
		var c = readCookie('style');
		if (c) {
			j$.stylesheetSwitch(c);
		}
	};
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}
// /cookie functions

j$(function () {
	// Call stylesheet init so that all stylesheet changing functions 
	// will work.
	j$.stylesheetInit();

	// This code loops through the stylesheets when you click the link with 
	// an ID of "toggler" below.
	j$('em#toggler').bind('click', function (e) {
		j$.stylesheetToggle();
		return false;
	});

	// When one of the styleswitch links is clicked then switch the stylesheet to
	// the one matching the value of that links rel attribute.
	j$('.styleswitch').bind('click', function (e) {
		j$.stylesheetSwitch(this.getAttribute('rel'));
		return false;
	});
});

j$(document).ready(function(){

/*--------------------------------------
	Smooth Scroll
---------------------------------------*/

	j$('a[href^=#]').filter(function(){
		return !j$(this).parents().hasClass('tab');
	}).click(function() {
		var href= this.hash;
		var j$target = j$(href == '#top' ? 'body' : href);
		if(j$target.size()) {
			j$.scrollTo(j$target, 800, {easing:'easeInOutQuart', duration: 1000});
		}
		return false;
	});

/*--------------------------------------
	IE
---------------------------------------*/

	if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Trident/4.0")!=-1) { // IE 8
//		j$("div#materials li:last-child, body.paljet dl.lineup2 ul li:last-child").addClass("last-child");
	} else 	if (navigator.userAgent.indexOf("MSIE")!=-1) { // IE 7  and older
		j$("ol.breadCrumbs li:not(:first-child)").prepend(" &gt; ");
	}

});
