
/* fire events when document loads */

$(document).ready(function() {
		
	// get rid of ugly link lines
	blurLinks();
	
	// anti-spam format for email
	EmailUnobsfuscate();
	
	FillBG_Image();
	
	
});








/* remove all focus lines from links when clicked 
******************************************************************/
function blurLinks() {
	var links = document.getElementsByTagName('a');
	for (i=0;i<links.length;i++) {
		links[i].onfocus = function() {
			this.blur();
		}
	}
}




/* anti-robot email formatting
******************************************************************/
function EmailUnobsfuscate() {
	
	// find all links in HTML
	var link = document.getElementsByTagName && document.getElementsByTagName("a");
	var email, e;
	
	// examine all links
	for (e = 0; link && e < link.length; e++) {
	
		// does the link have use a class named "email"
		if ((" "+link[e].className+" ").indexOf(" email ") >= 0) {
		
			// get the obfuscated email address
			email = link[e].firstChild.nodeValue.toLowerCase() || "";
			
			// transform into real email address
			email = email.replace(/dot/ig, ".");
			email = email.replace(/\(at\)/ig, "@");
			email = email.replace(/\s/g, "");
			
			// is email valid?
			if (/^[^@]+@[a-z0-9]+([_\.\-]{0,1}[a-z0-9]+)*([\.]{1}[a-z0-9]+)+$/.test(email)) {
				// change into a real mailto link
				link[e].href = "mailto:" + email;
				link[e].firstChild.nodeValue = email;
			}
			
		}
	}
}




/* preload images
******************************************************************/
function preloadImages(path, imgArray) {
	if (imgArray.length > 0) {
		
		 if(document.images) {
			
			var preload_image = new Array ();
			for(var i=0; i<imgArray.length; i++) {
				preload_image[i]= new Image();
				preload_image[i].src = path + imgArray[i];
			}
			
		 }
		
	}
}



/* tweet feed
******************************************************************/
function init_tweet_feed(numTweets) {
	$(".tweet").tweet({
			username: "Magpul_HQ",
			join_text: "auto",
			avatar_size: 26,
			count: numTweets,
			auto_join_text_default: "we said:",
			auto_join_text_ed: "we",
			auto_join_text_ing: "we were",
			auto_join_text_reply: "we replied to",
			auto_join_text_url: "we were checking out",
			loading_text: "loading tweets..."
	});
}






function FillBG_Image() {   
			
		var theWindow        = $(window),
			$bg              = $("#bg"),
			aspectRatio      = $bg.width() / $bg.height();
			
												
		function resizeBg() {
			
				var newHeight = $('#body-wrapper').height();
				$('#body-background').height(newHeight); 
				
				if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
					$bg
						.removeClass()
						.addClass('bgheight');
				} else {
					$bg
						.removeClass()
						.addClass('bgwidth');
				}
										
		}
												
		theWindow.resize(function() {
				resizeBg();
		}).trigger("resize");

}




$(document).ready(function() {
	$('#btn-calendar-toggle').click(function() {
		
		var cw = 383; // calendar width
		var as = 300; // an
		
		
		if ($(this).hasClass('closed')==true) {
			$('#calendar-content').animate( {marginLeft: 0, width: 'toggle'}, 200);
			$(this).removeClass('closed');
			$(this).addClass('open');
		} else {
			$('#calendar-content').animate( {marginLeft: cw, width: 'toggle'}, 200);
			$(this).removeClass('open');
			$(this).addClass('closed');
		}
		
		
	});		
});	



$(document).ready(function() {
	var bbh = $('#body-background').height();
	$('#body-wrapper').height(bbh);
});		
	


$(document).ready(function() {
	
	// if the label is clicked
	$('#class-selector span').click(function() {
	 
	 	if ($(this).hasClass('closed')==true) {
			$('#class-list').slideToggle(200);
			$(this).removeClass('closed');
			$(this).addClass('open');
		} else {
			$('#class-list').slideToggle(200);
			$(this).removeClass('open');
			$(this).addClass('closed');
		}
	 
	});
	
	$('#class-list li a').click(function() {
		
		var newValue = $(this).html();
		$('#class-selector span').html(newValue);
		
		$('#class-list').slideToggle(200);
		$('#class-selector span').removeClass('open');
		$('#class-selector span').addClass('closed');
			
	});
	
	
});	



