/*================================================================================

	ThemeSwapper is a dynamic themeing system designed to easily swap themes for 
	websites or blogs...an example can be found at:
	
	http://www.bodesigns.com
	
	Usage:
	
	Create a new theme object by:
	var MyNewTheme = new Theme('MyNewTheme', { themeing options: 'see below'});
	
	Add your new theme to the cookie script (around line 120)
	This makes sure the site remembers your theme choice...if you don't do this the
	site will default back to the default theme when you refresh:
	
	//Set initial theme to match cookie
	if (Cookie.get("theme") == 'Base') {
		Base.setTheme();
	} else if (Cookie.get("theme") == 'Trippy') {
		Trippy.setTheme();
	} else if (Cookie.get("theme") == 'MyNewTheme') {
		MyNewTheme.setTheme();
	} else {
		Base.setTheme();
	}
	
	Toggle to new theme by creating a button or link that then fires off:
	MyNewTheme.setTheme();
	
	By: Brent Otterlei
	on: December 22 08
	
=================================================================================*/	

/*================================================================================

	Creates the super theme class
	
	Current Themeing options and their defaults if none are set:
	
	backgroundId: 'backgroundHolder', 	//Background ID
	backgroundImg: '',					//Background Image
	backgroundCol: '#EEE',				//Background color
	backgroundPos: 'top left',			//Background position
	backgroundRep: 'no-repeat',			//Background Repeat
	backgroundAtt: 'scroll',			//Background Scroll
	textCol: '#000',					//Text color on page
	linkCol: '#0000FF',					//Color of links on page
	contentId: 'contentHolder',			//Content ID
	contentBgColor: '#FFF',				//Content Background color
	contentBgImg: '',					//Content background image
	contentBgImgPos: 'top left',		//content background position
	contentBgRepeat: 'no-repeat',		//content background repeat
	contentOpac: '100%'					//content container opacity
	
=================================================================================*/

var Theme = Class.create({
	initialize: function(name) {
		this.name = name;
		this.options = Object.extend({
			backgroundId: 'backgroundHolder', 	//Background ID
			backgroundImg: '', 					//Background Image
			backgroundCol: '',					//Background color
			backgroundPos: 'top left',			//Background position
			backgroundRep: 'no-repeat',			//Background Repeat
			backgroundAtt: 'scroll',			//Background Scroll
			textCol: '#000',					//Text color on page
			linkCol: '#0000FF',					//Color of links on page
			contentId: 'main',			        //Content ID
			contentBgColor: '',			    	//Content Background color
			contentBgImg: '',					//Content background image
			contentBgImgPos: 'top left',		//content background position
			contentBgRepeat: 'no-repeat',		//content background repeat
			contentBorder: '1px solid #555',	//content border
			contentBorderTop: '1px solid #999', //Option to make top border different
			contentOpac: '1'					//content container opacity
		}, arguments[1] || {});
	},
	setTheme: function() {
		//Set background attributes
		$(this.options.backgroundId).setStyle({
			backgroundImage: 'url(' + this.options.backgroundImg + ')',
			backgroundColor: this.options.backgroundCol,
			backgroundPosition: this.options.backgroundPos,
			backgroundRepeat: this.options.backgroundRep,
			backgroundAttachment: this.options.backgroundAtt
		});
		//Set content attributes
		$(this.options.contentId).setStyle({
			backgroundImage: this.options.contentBgImg,
			backgroundColor: this.options.contentBgColor,
			backgroundPosition: this.options.contentBgImgPos,
			backgroundRepeat: this.options.contentBgRepeat,
			border: this.options.contentBorder,
			borderTop: this.options.contentBorderTop,
			opacity: this.options.contentOpac
		});
		//Set p element color
		var pElements = document.getElementsByTagName('p');
		for (var i = 0; i < pElements.length; i++){
		   pElements[i].style.color = this.options.textCol;
		}
		//Set link color
		var aElements = document.getElementsByTagName('a');
		for (var i = 0; i < aElements.length; i++){
		   aElements[i].style.color = this.options.linkCol;
		}
		
		/*set the cookie to current theme */
		Cookie.set('theme',this.name,3600*24*30);
		
	}
});

/*================================================================================

	check for the theme cookie...if user does not have one set, create it else
	set the remembered theme
	
=================================================================================*/	
	
function filterCookieCheck() {
    if (!Cookie.get("theme")) {
			Cookie.set('theme','Wood',3600*24*30);
    }
}

/*================================================================================

	On load setup cookies, create new themes, check and display initial theme
	
=================================================================================*/

Event.observe(window, 'load', function() {
	
	//Check to see if you have a theme cookie set if not add default
	filterCookieCheck();
	
	//Add theme objects here
	Base = new Theme('Base', { backgroundImg: 'images/bluWave.gif', backgroundCol: '#EEE', backgroundPos: 'top left', backgroundRep: 'repeat', contentBgColor: '#FFF', linkCol: '#005794' });
	Brown = new Theme('Brown', {  backgroundImg: 'images/brnBg.jpg', backgroundCol: '#EEE', backgroundPos: 'top left', backgroundRep: 'repeat', contentBgColor: '#FFF', linkCol: '#9e9187'  });
	Wood = new Theme('Wood', {  backgroundImg: 'images/wood.jpg', backgroundCol: '#EEE', backgroundPos: 'top left', backgroundRep: 'repeat', contentBgColor: '#FFF', linkCol: '#8a4733'  });
	Paisley = new Theme('Paisley', {  backgroundImg: 'images/paisley.jpg', backgroundCol: '#EEE', backgroundPos: 'top left', backgroundRep: 'repeat', contentBgColor: '#FFF', linkCol: '#CCC' });
	Clouds = new Theme('Clouds', {  backgroundImg: 'images/clouds.jpg', backgroundCol: '#FFF', backgroundPos: 'top left', backgroundRep: 'no-repeat', backgroundAtt: 'fixed', contentBgColor: '#FFF', linkCol: '#999' });
	Bluebubble = new Theme('Bluebubble', {  backgroundImg: 'images/blueBubble.jpg', backgroundCol: '#FFF', backgroundPos: 'top left', backgroundRep: 'no-repeat', backgroundAtt: 'fixed', contentBgColor: '#FFF', contentOpac: '0.7', linkCol: '#539DBC' });
	Blackboard = new Theme('Blackboard', {  backgroundImg: 'images/blackboard.jpg', backgroundCol: '#000', backgroundPos: 'top left', backgroundRep: 'no-repeat', backgroundAtt: 'fixed', contentBgColor: '#FFF', textCol: '#000', linkCol: '#AAA', contentBorder: '0', contentBorderTop: '0' });
	Redxmas = new Theme('Redxmas', {  backgroundImg: 'images/redXmas.jpg', backgroundCol: '#5D0504', backgroundPos: 'top left', backgroundRep: 'repeat-x', backgroundAtt: 'fixed', contentBgColor: '#FFF', textCol: '#000', linkCol: '#5D0504', contentBorder: '0', contentBorderTop: '0' });
	
	
	//Set initial theme to match cookie
	if (Cookie.get("theme") == 'Base') {
		Base.setTheme();
	} else if (Cookie.get("theme") == 'Brown') {
		Brown.setTheme();
	} else if (Cookie.get("theme") == 'Wood') {
		Wood.setTheme();
	} else if (Cookie.get("theme") == 'Paisley') {
		Paisley.setTheme();	
	} else if (Cookie.get("theme") == 'Clouds') {
		Clouds.setTheme();
	} else if (Cookie.get("theme") == 'Bluebubble') {
		Bluebubble.setTheme();	
	} else if (Cookie.get("theme") == 'Blackboard') {
		Blackboard.setTheme();	
	} else if (Cookie.get("theme") == 'Redxmas') {
		Redxmas.setTheme();		
	} else {
		Wood.setTheme();
	}
});


/*================================================================================

	To make working with cookies easier I borrowed this nice little cookie class
	from Ruby on Rails...
	
=================================================================================*/

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + "; path=/");
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};