$(document).ready(function(){
	$("div.link_list span:even").css("background-color", "#BDC4CA");
	$("div.link_list_right span:odd").css("background-color", "#BDC4CA");
	$("div.reg_list span:even").css("background-color", "#BDC4CA");
	
	$("body.tax-product_brand div.wide_dealer:even div.post:even").addClass("even");
	$("body.tax-product_brand div.wide_dealer:even div.post:odd").addClass("odd");
	$("body.tax-product_brand div.wide_dealer:odd div.post:odd").addClass("even");
	$("body.tax-product_brand div.wide_dealer:odd div.post:even").addClass("odd");
	
	
	$("body.tax-type_products div.wide_dealer:even div.post:even").addClass("even");
	$("body.tax-type_products div.wide_dealer:even div.post:odd").addClass("odd");
	$("body.tax-type_products div.wide_dealer:odd div.post:odd").addClass("even");
	$("body.tax-type_products div.wide_dealer:odd div.post:even").addClass("odd");
	
	
	$("body.tax-province div.wide_dealer:odd div.item:odd").addClass("even");
	$("body.tax-province div.wide_dealer:odd div.item:even").addClass("odd");
	$("body.tax-province div.wide_dealer:even div.item:odd").addClass("odd");
	$("body.tax-province div.wide_dealer:even div.item:even").addClass("even");
	initHover();
	initFadeIn();
	initFancybox();
	initActive();
});

function initActive(){
	$('.remko_wide span a.active').css('background',hover_color);
	$("a.hover-color").hover(
		function (){
			if(!$(this).hasClass('active')) $(this).css('background',hover_color);
		},
		function (){
			if(!$(this).hasClass('active')) $(this).css('background','none');
		}
	);
};

$(window).load(function(){
	initTaxonomyLogos();
});

function initTaxonomyLogos(){
	$('.taxonomyLogos').each(function(){
		var holder = $(this);
		var links = $('a', holder);
		var holderW = holder.width();
		var holderH = holder.height();
		var listCount = links.length;
		var delay = 100;
		var animSpeed = 180;
		var linksW = 0;
		var leftIndent = 0;
		
		links.each(function(){
			var _this = $(this);
			
			if($(this).find('img').width() == 0){
				_this.remove();
			}
			else{
				var _thisH = _this.height();
				if(_thisH < holderH) _this.css({'marginTop': ~~((holderH - _thisH)/2)});
				linksW = linksW + _this.width();
			}
		});
		
		links = $('a', holder);
		
		leftIndent = ~~((holderW - linksW)/links.length/1.5);
		if(linksW < holderW) links.css({'marginLeft': leftIndent});
		
		links.each(function(){
			var _this = $(this);
			var image = _this.find('img');
			
			var gray = image.clone().appendTo(_this).addClass('over').grayscale();
			var t;
			
			image.addClass('fly').hide();
			
			_this.hover(function(){
				if(t) clearTimeout(t);
				t = setTimeout(function(){
					image.fadeIn(animSpeed);
				}, delay);
			},function(){
				if(t) clearTimeout(t);
				t = setTimeout(function(){
					image.fadeOut(animSpeed);
				}, delay);
			});
		});
	});
}

function initFadeIn(){
	var list = $("div.product-cat a");
	list.eq(0).animate({opacity: 1}, {queue:true, duration:1000});
	setTimeout(function(){
		list.eq(1).animate({opacity: 1}, {queue:true, duration:1000});
	}, 600);
	
	var home_list = $("div.home-product-cat a");
	home_list.eq(0).animate({opacity: 1}, {queue:true, duration:1000});
	setTimeout(function(){
		home_list.eq(1).animate({opacity: 1}, {queue:true, duration:1000});
	}, 600);
	setTimeout(function(){
		home_list.eq(2).animate({opacity: 1}, {queue:true, duration:1000});
	}, 1200);
	setTimeout(function(){
		home_list.eq(3).animate({opacity: 1}, {queue:true, duration:1000});
	}, 1800);
}

function initFancybox(){
	$("span#lightup a").fancybox();
}

function initHover(){
	$("div.product-cat a").hover(
		function () {
		  $(this).addClass('hover');
		}, 
		function () {
		  $(this).removeClass('hover');
		}
	);
	$("div.home-product-cat a").hover(
		function () {
		  $(this).addClass('hover');
		}, 
		function () {
		  $(this).removeClass('hover');
		}
	);
}

// grayscale plugin
(function($){
	function grayscale(image, bPlaceImage) {
		var myCanvas=document.createElement("canvas");
		var myCanvasContext=myCanvas.getContext("2d");
		var imgWidth=image.width;
		var imgHeight=image.height;
		myCanvas.width= imgWidth;
		myCanvas.height=imgHeight;
		
		myCanvasContext.drawImage(image,0,0);
		var imageData=myCanvasContext.getImageData(0,0, imgWidth, imgHeight);
		
		for (i=0; i<imageData.height; i++) {
			for (j=0; j<imageData.width; j++) {
				var index=(i*4)*imageData.width+(j*4);
				var red=imageData.data[index];
				var green=imageData.data[index+1];
				var blue=imageData.data[index+2];
				var alpha=imageData.data[index+3];
				var average=(red+green+blue)/3;
				imageData.data[index]=average;
				imageData.data[index+1]=average;
				imageData.data[index+2]=average;
				imageData.data[index+3]=alpha;
			}
		}
		myCanvasContext.putImageData(imageData,0,0,0,0, imageData.width, imageData.height);

		if (bPlaceImage) {
			var myDiv=document.createElement("div");
			myDiv.appendChild(myCanvas);
			image.parentNode.appendChild(myCanvas);
		}
		return myCanvas.toDataURL();
	}

	jQuery.fn.grayscale = function(_options){
		var _options = jQuery.extend({
			temp:1
		},_options);
		return this.each(function(){
			// options
			var image = this;
			var _temp = _options.temp;
			if($.browser.msie && $.browser.version < 9) {
				image.style.filter = 'gray'
			} else {
				image.src = grayscale(image);
			}
		});
	}
})(jQuery);
