/**************************************************************************
* Custom Functions
* 
**************************************************************************/

// Grayscale W canvas method.
function grayscale(src){
	var canvas = document.createElement('canvas');
	var ctx = canvas.getContext('2d');
	var imgObj = new Image();
	imgObj.src = src;
	canvas.width = imgObj.width;
	canvas.height = imgObj.height; 
	ctx.drawImage(imgObj, 0, 0); 
	var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
	for(var y = 0; y < imgPixels.height; y++){
		for(var x = 0; x < imgPixels.width; x++){
			var i = (y * 4) * imgPixels.width + x * 4;
			var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
			imgPixels.data[i] = avg; 
			imgPixels.data[i + 1] = avg; 
			imgPixels.data[i + 2] = avg;
		}
	}
	ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
	return canvas.toDataURL();
}




$(document).ready(function(){

	// Initialize hover over dropdown navigation menus
	$("ul.nav").superfish({
	  delay: 200,
	  animation: {
		opacity: 'show',
		height: 'show'
		},
	  speed: 'fast',
	  autoArrows: false
	});
	
	
	// Initialize javascript tabs for toggling
	$("ul.tabs").tabs("div.pane", {
		effect: 'fade'
	});
	
	//Comments/Trackbacks switch
	var comment_list_now = 'comments';
	$('.comment-controls a').click(function() {
		var type = this.href.split('#')[1];
		if(comment_list_now != type) {
			comment_list_now = type;
			$(this).parent().find('a').toggleClass('button');
			jQuery('.commentlist').toggle();
		}
		return false;
	});
	
	
	// Add Classes
	$("#main-nav ul.nav li:last-child").addClass("last");
	$("#featured .post:nth-child(3)").addClass("last");
	//$("#main div.one_third:nth-child(3n+0)").addClass("last");
	$("#main .posts .half:nth-child(2n+0)").addClass("even");
	$("body.single-post #main .post-section:last-child").addClass("last");
	$("#comments ol.commentlist li:last-child").addClass("last");
	$("#related-posts .posts .post:odd").addClass("even");
	$("#sidebar .widget:last-child").addClass("last");
	$("#sidebar .widget-video h3").addClass("fl");
	$("#subfooter .widget-video h3").addClass("fl");
	$("#subfooter .widget:nth-child(3n+0)").addClass("last");
	//$(".flickr_badge_image:nth-child(3n+4)").addClass("last");
	$("#pagination div > a").addClass("button");
	jQuery('#sidebar .widget-ads .square .ad-image:odd').addClass('odd');
	
	// Insert Clearing Div
	$("#subfooter .widget:nth-child(4n+0)").before("<div class=\"divider_padding\">");

	
	//Featured Slider
	var bmg_slider_length = Math.ceil($('#featured .post').length / 3), bmg_slider_current = 0;

	$('#featured-controls a').click(function() {
		var rel = $(this).attr('rel');
		if(rel == 'prev') {
			bmg_slider_current--;
		} else if(rel == 'next') {
			bmg_slider_current++;
		} else {
			bmg_slider_current = parseInt(rel) - 1;
		}

		if(bmg_slider_current < 0) {
			bmg_slider_current = bmg_slider_length - 1;
		} else if(bmg_slider_current >= bmg_slider_length) {
			bmg_slider_current = 0;
		}

		$(this).siblings('a').removeClass('active');
		$(this).parent().find('a.featured-dot').eq(bmg_slider_current).addClass('active');

		$('#featured-inner').animate({
			left: -945 * bmg_slider_current
		});

		return false;
	});
	
	/**************************************************************************
	* Sponsor Zone
	* Use grayscale() function
	**************************************************************************/
		
	// On window load. This waits until images have loaded which is essential
	$(window).load(function(){
		
		// Fade in images so there isn't a color "pop" document load and then on window load
		$(".sponsor_bar img").fadeIn(500);
		
		// clone image
		$('.sponsor_bar img').each(function(){
			var el = $(this);
			el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
				var el = $(this);
				el.parent().css({"width":this.width,"height":this.height});
				el.dequeue();
			});
			this.src = grayscale(this.src);
		});
		
		// Fade image 
		$('.sponsor_bar img').mouseover(function(){
			$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
		})
		$('.img_grayscale').mouseout(function(){
			$(this).stop().animate({opacity:0}, 1000);
		});		
	});
	
	    

	// This is a hack to hide home slider and after it's been loaded, show it again.	
	$('#gallery_container').removeClass('hidden');

});

