function styleswitch(color) {
  if(color == "light") {
     document.getElementById('style').href = "light.css";
  }
  if(color == "dark") {
     document.getElementById('style').href = "dark.css";
  }
  var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
  document.cookie = 'style'+"="+color+expires+"; path=/";
}

function getUrlVars(href) {
	    var vars = {};
	    var parts = href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
	        vars[key] = value;
	    });
	    return vars;
	}
	
function ucwords (str) {
    return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
        return $1.toUpperCase();
    });
}

$(document).ready(function(){
	$('#styleswitcher a').click(function(event){
		event.preventDefault();
		$('#loadimage').css("visibility", "visible").fadeIn('slow');
		var color = $(this).attr("href").substr(7);
		if(color == "light"){
			$(this).attr("href", "?style=dark");
			$(this).html("dark");	
		}else{
			$(this).attr("href", "?style=light");
			$(this).html("light");
		}
		$('#container').fadeOut('slow', function(){
			$('#style').attr("href", color+".css");
			$('#loadimage').fadeOut('slow');
			$('#container').fadeIn('slow');
			$.cookie("style", color, { path: '/' }, { expires: 365 });
		});
	});
	
	$('a.page_link').live('click', function(event){ //works?
		event.preventDefault();
		$('#loadimage').css("visibility", "visible").fadeIn('slow');
		var page = $(this).attr("href").substr(3);
		var title = getUrlVars($(this).attr("href"))["p"];
		title = title.replace("_", " ");
		title = ucwords(title);
		$("#main").fadeOut('slow',function(){
    		$(this).load("site/"+page+".php",function(response, status, xhr){
    			if (status == "error") {
    				$(window.location).attr('href', 'http://chritzel.net/404.php?p='+page);
    			}
    			// Set new handlers - do I need to?
    			$('#loadimage').fadeOut('slow');
    			$(this).fadeIn('slow');
    			document.title= title + " - Chritzel";
    			window.history.pushState("", "", "?p="+page);
    		});
  		});
	});
	
	$('a.sliderbutton').live('click', function(event){
		event.preventDefault();
		var slide = $(this).attr('href');
		$(this).siblings('.s_pressed').removeClass('s_pressed');
		$(this).addClass('s_pressed');
		$(slide).siblings('.s_active').fadeOut('fast', function(){
			$(slide).fadeIn('fast', function(){
				$(slide).siblings('.s_active').removeClass('s_active');
				$(slide).addClass('s_active');
			});
		});
	});

});
