/*===========================================================================
	global.js

	This JavaScript executes dynamic behaviors:
	- Patch missing JavaScript features for certain browser versions
	- Define commonly used functions and variables
	- Preload images
	- Flash Replacements (i.e. sIFR, UFO, SWFObject, etc.)
	- Decorate the DOM with presentational elements
	- Collapse/expand/animate sections
	- Validate forms
	- Pop-up windows
===========================================================================*/

/*===========================================================================
	document.ready
	
	Initialize while the page is loading
===========================================================================*/
$(document).ready(function() {
	log("document rendering");
	
	// Fix Safari bottom margin bug
	//$("div.main *:last").parent().andSelf().addClass("eof");
	//$("div.block").find("*:last").parent().andSelf().addClass("eof");
});

/*===========================================================================
	window.load
	
	Initialize after the page has loaded
===========================================================================*/
$(window).load(function(){
	log("document rendered");
});


/*===========================================================================
	Page-specific Initialization
===========================================================================*/
var conspiracy =
{
	interior :
	{
		init : function()
		{
			// colorbox iFrame
			$("a.cboxelement.iframe").colorbox({width:"90%",height:"80%",iframe:true,title:"Watermark Conspiracy"});
			
			// Tabs
			if ($(".mod_articleList").length) {
				var $panels = $("div[id^=tabs]");
				var $tabs = $(".mod_articleList a");
				
				$panels.hide();
				$panels.eq(0).show();
				$tabs.eq(0).addClass("active");
				
				$tabs.click(function() {
					var index = $tabs.index(this);
					$panels.hide().eq(index).show();

					$tabs.removeClass("active");
					$(this).addClass("active");
					return false;
				});
			}
			
			// Subscription form
			$("#subscription-form").submit(function() {
				$("span.error").remove();

				var $input = $("input[name='Email Address']");
				var adr = $input.val();
				var apos = adr.indexOf("@");
				var dpos = adr.lastIndexOf(".");
				var errorStr = '';

				if (!adr) {
					errorStr += "Please enter your email address";
				} else if (apos<1 || dpos-apos<2) {
					errorStr += "Please enter a valid email address";
				}

				if (errorStr) {
					$input.prev("label").addClass("error").after('<p class="error"> ' + errorStr + '</p>');
					return false;
				}
			});
		}
	}
}

/*===========================================================================
	Utilities
===========================================================================*/
// Enable console logging
function log(msg) {
	if (window.console && window.console.log)
		console.log(msg);
}

// Prevent IE6 background flicker
try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}

// Allow application of CSS rules to IE6's unknown elements
document.createElement("abbr");