function showWidescreen(width) {
  var kookie = readCookie('style');
  var w = window.innerWidth || document.documentElement.offsetWidth;

  if (kookie != 'modern') {
    if (w > width) {
      $("#widescreen").show();
      $('body').addClass('wide');
    } else {
      $('#widescreen:visible').hide();
      $('body').removeClass('wide');
    }
  }
}

$(document).ready(function() {
  $('#twitter:contains(XML error)').remove();
  
  $('#widescreen').hide();
  showWidescreen(1020); 
  swap_letter();   
  // comment form validation
  $('#post').click( validateComment );
  //switch widescreen bonus content
  $('#blogroll-title a').click( bindFirst );     
  
  // prevent auto-spambot comments
  var commentAction = $('#comments_form').attr('action');
  if ((/nothing/).test(commentAction)) {
    $('#comments_form').attr({'action': commentAction.replace(/nothing\.php$/,'sub-to-com.cgi')});
  }
});

window.onresize = function() {
  showWidescreen(1020);  
};
function swap_letter() {
  var first_paragraph = $('#main-content p')[0];
  if (!first_paragraph) return false;
  var node = first_paragraph;
  while (node.childNodes.length) {
    node = node.firstChild;
  }
  var text = node.nodeValue;
  if ( text ) {
    var first_letter = text.substr(0,1);

    node.nodeValue = text.slice(1);
    $('<img />')
        .attr('src','http://www.englishrules.com/images/alphabet/' + first_letter.toLowerCase() + '.gif')
        .attr('alt',first_letter)
        .addClass('fancy-letter')
        .prependTo( first_paragraph ); 
  }
};
function validateComment() {
  var msg = "The following fields are required: \n<ul>";
  var authorlen = $("#author").val().length;
  var emaillen = $("#email").val().length;
  var commentlen = $("#text").val().length;
  var problem = "false";
  //$("p").removeClass("error");    
  if (authorlen < 1) {
    msg = msg + "<li>Name</li>\n";
    problem = "true";
    $("#author").parent().addClass("error");
   }
  if (emaillen < 1) {
    msg = msg + "<li>Email Address</li>\n";
    problem = "true";
    $("#email").parent().addClass("error");
   }
   if (commentlen < 1) {
     msg = msg + "<li>Comments</li>\n";
     problem = "true";
    $("#text").parent().addClass("error");       
    }      
 
  if (problem == "true") {
    msg = "<div class=\"error\">" + msg + "</ul></div>";
    $("div.error").remove();
    $("#text").parent().after(msg);
    // $(".error").highlightFade('red','crawl',null,'exponential');
    
    return false;      
   }
}

// widescreen bonus - content loading
function bindFirst() {
  $('div.widescreenbox').load('/includes/blogroll.php',function(){
    // $('div.widescreenbox').highlightFade();
  });
  $('#blogroll-title').html('Blogroll');
  $('#noteworthy-title')
    .html('<a href="/extras/noteworthy.php">+&nbsp;Noteworthy Articles</a>')
    .find("a").click( bindSecond );
  return false;
}

function bindSecond(){
  $('div.widescreenbox').load('/includes/noteworthy.php',function(){
    // $('div.widescreenbox').highlightFade();
  });
  $('#noteworthy-title').html('Noteworthy Articles');
  $('#blogroll-title')
    .html('<a href="/extras/blogroll.php">+ Blogroll</a>')
    .find("a").click( bindFirst );
  return false;
}

	
/* ----------------------
   show-hide advanced search 
   ---------------------- */

$(document).ready(function() {
  var advancedSearch = $('div.advanced-search').hide();
  $('#open-advanced-search').toggle(function() {
    advancedSearch.slideDown();
    $(this).html('-&nbsp;&nbsp;Advanced Search');    
    return false;
  }, function() {
    advancedSearch.slideUp();
    $(this).html('+&nbsp;Advanced Search');
    return false;
  });
});

/***************************************
 	* Styleswitch stylesheet switcher built on jQuery
 	* Under an Attribution, Share Alike License
 	* By Kelvin Luck ( http://www.kelvinluck.com/ )   
-------------------------------------- */

$(document).ready(function() {
  switchLink();
	$("#keyhole").find("a").click(function() {			
		switchStylestyle(this.getAttribute("rel"));
		switchLink();
		return false;
	});
	var c = readCookie('style');
	if (c) {
	  switchStylestyle(c);
	}
});

function switchLink() {
  var x = readCookie('style');
  if (x == "antique") {
    $('#keyhole > a[@rel="modern"]').show();      
    $('#keyhole > a[@rel="antique"]').hide();    
    $('body').removeClass('modern');  	
  } else {
    $('#keyhole > a[@rel="modern"]').hide();
    $('#keyhole > a[@rel="antique"]').show();
    $('body').removeClass('wide').addClass('modern');  
    $('#widescreen:visible').css('display','none');
  }
}

$(document).ready(function() {
  $('#comments_form').each(function() {
      var cEmail = readCookie('mtcmtmail'), cAuthor = readCookie('mtcmtauth'), cUrl = readCookie('mtcmthome');
      if (cEmail) $('#email').val(cEmail);
      if (cAuthor) $('#author').val(cAuthor);
      if (cUrl) $('#url').val(unescape(cUrl));      
  });
  
  $('blockquote[@cite]').each(function() {
    var citeUrl = $(this).attr('cite');
    $(this).append('<p class="text-right"><a href="' + citeUrl + '">source</a></p>');
  });
});

function switchStylestyle(styleName) {
	$('link[@rel*=stylesheet]').each(function(i) {      
    this.disabled = true;
		if (this.getAttribute('title') == styleName) {
		  this.disabled = false;
		}
	});   
	createCookie('style', styleName, 365);
}
	
// 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);
}
