jQuery(document).ready(function()
{
	MainMyOwnBossCA.init ();
	//MainMyOwnBossCA.rotateAds();
});

var MainMyOwnBossCA = {
	ad_timer : 7000,
	ad_count : 0,
	ad_timeout_id : -1,
	carousel_timer: 4000,

	init : function()
	{
		var input = jQuery("form#searchform input[name='s']");

		input.bind('focus', function(e)
		{
			if (!jQuery(this).attr('visited'))
				this.value = '';

			jQuery(this).attr('visited', true);
		});

		input.bind('blur', function(e)
		{
			if (this.value == '')
			{
				this.value = 'Search';
				jQuery(this).removeAttr('visited');
			}
		});
		
		$("#main-menu li.current-cat").each(function() 
		{
			$(this).find("a").addClass("current-cat");
		});
		
		if (jQuery(".scrollable").get(0))
		{
			var root = jQuery(".scrollable").scrollable({
				vertical: false,
				speed: 0,
					prev: '.prev',
					next: '.next',
				circular: true,
				items: '.items',
				initialIndex: 0,
				onSeek: function()
				{
	//				$("#featured-nav.unitPng").css("position", "relative");
					
					$("#featured-nav .count").html((this.getIndex()+1) + "/" + $("ul#section-navi li").length);
				}
			}).autoscroll({ autoplay: true, interval: MainMyOwnBossCA.carousel_timer }).navigator({ navi: 'ul#section-navi' });
			
			$($("ul#section-navi li").get(0)).addClass("active");
			
			var api = root.data("scrollable");
			
			jQuery('.navi, ul#section-navi li a').bind('click', function() {
	//			jQuery('.navi').bind('click', function() {
				api.stop();
			});
		}
		
		// find all the input elements with title attributes
		jQuery('input[title!=""]').hint();
		
                if(typeof window.ellipsis) {
                    $('body.home .ellipsis-container').ellipsis();
                }
		
		MainMyOwnBossCA.createColorboxDescription();
//		$('a[rel=photos]').colorbox({}}); 
	
		$("a[rel^='lightbox']").each(function(){
			$(this).colorbox({
				current:"{current} of {total}", 
				title:function(){ return $(this).attr('caption'); },
				onComplete:function(){ MainMyOwnBossCA.des.html($(this).attr('desc')); /* $("#cboxContent").width('550px'); */ }
				}); 
		});
		
	},
	createColorboxDescription : function()
	{
		if ($('#cboxDescription').get(0))
			return true;
			
		MainMyOwnBossCA.des = $('<div id="cboxDescription"/>').appendTo('#cboxContent');
		window.setTimeout("MainMyOwnBossCA.createColorboxDescription()", 1000);
	},
	
	rotateAds : function() 
	{
        if (jQuery('#rotating-big-ads').get(0))
		{
			MainMyOwnBossCA.ad_timeout_id = window.setTimeout("MainMyOwnBossCA.ad_rotate()", MainMyOwnBossCA.ad_timer);
		}
	},
	
	ad_rotate : function() 
	{
		if (++MainMyOwnBossCA.ad_count > 1)
			MainMyOwnBossCA.ad_count = 0;
			
		jQuery("#rotating-big-ads .big-box, #rotating-big-ads2 .big-box").hide();
		jQuery("#rotating-big-ads .big-box").eq(MainMyOwnBossCA.ad_count).show();
		jQuery("#rotating-big-ads2 .big-box").eq(MainMyOwnBossCA.ad_count).show();
		
		MainMyOwnBossCA.ad_timeout_id = setTimeout ("MainMyOwnBossCA.ad_rotate()", MainMyOwnBossCA.ad_timer);
	}
	
}

// On window load. This waits until images have loaded which is essential
$(window).load(function(){
	
	if ($("body.page-id-2").get(0) && 
		!($.browser.msie && ($.browser.version=="6.0" || parseInt($.browser.version) == 7)))
	{
		// Fade in images so there isn't a color "pop" document load and then on window load
		$(".blogger img").animate({opacity:1},500);
		
		// clone image
		$('.blogger 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 
		$('.blogger img').mouseover(function(){
			$(this).parent().find('img:first').stop().animate({opacity:1}, 100);
		})
		$('.img_grayscale').mouseout(function(){
			$(this).stop().animate({opacity:0}, 100);
		});
	}
});

// 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();
}

