
var TBK = {};


$(document).ready(function() { 
	
	$.fn.valign = function() {
		return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = (ph - ah) / 2;
		$(this).css('margin-top', mh);
		$(this).css('visibility', "visible");
		});
	};
	
	TBK.init();
});


TBK.init = function() {

	var _rolls = [ "/images/nav/home-on.gif", "/images/nav/products-on.gif", "/images/nav/services-on.gif", 
				   "/images/nav/showroom-on.gif","/images/nav/whyrecycle-on.gif","/images/nav/aboutus-on.gif","/images/nav/contactus-on.gif",
				   "/images/arrow-green.gif","/images/arrow-gray2.gif" ];
	for( var i = 0 ; i < _rolls.length ; i++)
	{
		var _img = $('<img />').attr('src', _rolls[i]);
	}
	// Code from firebug lite to provide a console object to browsers without one
	if (! ("console" in window) || !("firebug" in console)) {
	    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	    window.console = {};
	    for (var i = 0; i <names.length; ++i) window.console[names[i]] = function() {};
	}

	// Add a replaceWith function to the top level jquery object
	$.fn.replaceWith = function(html) { return this.after(html).remove(); };
	
	// VERTICALLY ALIGN FUNCTION
	window.setTimeout( function() { $('.valigned').valign() }, 200);
	
	$('ul.sf-menu').superfish({ 
 		delay:       200,                            // one second delay on mouseout 
   		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
    	speed:         50,           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
    	autoArrows:  false,                           // disable generation of arrow mark-up 
   		dropShadows: false                            // disable drop shadows 
 	}); 
	
	$(".active").each( function() {
		$(this).click( function(e) {
			if(!$(this).hasClass("sf-with-ul")) TBK.killEvent(e);
		});
	});
	
	$(".sf-menu li").mouseover( function() {
		$(this).find("a").addClass("navover");
	});
	$(".sf-menu li").mouseout( function() {
		$(this).find("a").removeClass("navover");
		
	});
	// find the number of the feature to show
	$(".featureBody").each( function() {
		TBK.featureCount++;
	});
	
	TBK.featureSlideShowInterval = window.setInterval(function() {
		TBK.featureCurrentSlide++;
		if(TBK.featureCurrentSlide > TBK.featureCount)
		{
			TBK.featureCurrentSlide = 1;
		}
		TBK.featureAction("#feature" + TBK.featureCurrentSlide);
		}, 6000);
	
	$(".featureLink").click( function(e) {
		TBK.featureSlideShow = false;
		window.clearInterval(TBK.featureSlideShowInterval);
		var _id = "#" + $(this).attr("id").split("_")[0];
		TBK.featureAction(_id, e);
		return TBK.killEvent(e);
	});
	
	
	// Activate any jcarousel
	if($(".productSlider").length > 0)
	{
		$('.productSlider').jcarousel({
         	vertical: true
		});
		
		$($('.productSlider li')[0]).addClass("active");
		
		$('.productSlider li').mouseover( function(e) {
			if(!$(this).hasClass("active"))
			{
	         	$(this).addClass("rollactive");
			}
   	 	});
		
		$('.productSlider li').mouseout( function(e) {
         	if(!$(this).hasClass("active"))
			{
	         	$(this).removeClass("rollactive")
			}
   	 	});
		
		$('.productSlider li').click( function(e) {
         	if(!$(this).hasClass("active"))
			{
	         	var _id = $(this).attr("id").split("_")[1];
				TBK.debug("Item ID: " + _id);
				$(".productItemHero").hide();
				$("#productItemMain" + _id).show();
				$('.productSlider li.active').removeClass("active");
				$('.productSlider li.rollactive').removeClass("rollactive");
				$(this).addClass("active");
			}
   	 	});

	}
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
	    phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please specify a valid phone number");
	jQuery.validator.addMethod("zip", function(value, element) {
  		return this.optional(element) || value.match(/^((\d{5}-\d{4})|(\d{5})|([a-z]\d[a-z]\s?\d[a-z]\d))$/i);  }, "US or Canadian postal code only"); 
	$("#contactForm").validate({
		ignoreTitle: true,
		rules : {
		 yourname: "required",
     	 company: "required",
     	 email: {
	       required: true,
	       email: true
	     },
		 zip: { zip: true },
		 phone: { phoneUS: true }
		},
		errorElement: "div",
		errorClass: "error"
	});
	$("#contactForm #submitContact").click(function(e) { $("#contactForm").submit(); return TBK.killEvent(e); });
	$(".shadowed").each( function() {
		$(this).wrap("<div class='shadowBottom'><div class='shadowRight'></div></div>");
	});
}
TBK.featureCount = 0;
TBK.featureSlideShow = true;
TBK.featureSlideShowInterval = null;
TBK.featureCurrentSlide = 1;
TBK.featureAction = function( id, e ) {
	TBK.debug(id);
	var _back = id == "#back";
	var _next = id == "#next";
	var _active_id;
	$(".featureBody").each( function() {
		if(!$(this).hasClass("hide"))
		{
			_active_id = $(this).attr("id").replace("feature", "").replace("Body", "");
			_active_id = parseInt(_active_id);
		}
	});
	if(_back || _next)
	{
		TBK.debug(_back);
		TBK.debug(_next);
		TBK.debug(_active_id);
		if( (_back && _active_id == 1) || (_next && _active_id == TBK.featureCount) )
		{
			return TBK.killEvent(e);
		}
		_active_id = _back ? _active_id - 1 : _active_id + 1;
		id = "#feature" + (_active_id);
		TBK.debug(_active_id);
		TBK.debug(id);
		
	}
	if(id != "#feature1") $("#back_link").removeClass("inactiveArrow");
	else $("#back_link").addClass("inactiveArrow");
	if(id != "#feature" + TBK.featureCount) $("#next_link").removeClass("inactiveArrow");
	else $("#next_link").addClass("inactiveArrow");
	$(".featureLink").removeClass("active");
	if(TBK.featureSlideShow)
	{
		$(".featureImg").fadeOut("slow");
		//$(".featureImg").addClass("hide");
		//$(id + "Img").removeClass("hide");
		$(id + "Img").fadeIn("slow");
		$(".featureBody").addClass("hide");
		$(id + "Body").removeClass("hide");
		$(id + "_link").addClass("active");
	}
	else
	{
		$(".featureImg").fadeOut("slow");
		//$(".featureImg").css("display","none");
		//$(id + "Img").css("display","block");
		$(id + "Img").fadeIn("slow");
		$(".featureBody").addClass("hide");
		$(id + "Body").removeClass("hide");
		$(id + "_link").addClass("active");
	}
}


/* Common function to prevent event propagation */
TBK.killEvent = function( e ) {
	e.stopPropagation();
    e.preventDefault();
    return false;
}

TBK.debug = function(msg) { 
	//DEBUGGING_ON
	if(console && console.debug)
	{
		console.debug(msg);
	}
}
	
