$.fn.id = function(attribute) {
	if (!attribute) attribute = 'id';
	var id = this.eq(0).attr(attribute);
	return id ? parseInt(id.match(/(\d+)$/)[1], 10) : null;
};

jQuery.fn.otherwise = function(func) {
	if (!this.length) {
		func.apply(this);
	}
	return this;
};

/* Menu Rollover Code */
$.fn.rolloverMenu = function(){
	return this.each(function(){
		$('LI', this).hover(
			function(){
				$(this).addClass('over');
			},
			function(){
				$(this).removeClass('over');
			}
		);
	});
};

$.fn.dropdownMenu = function(){
	return this.each(function(){
		var active = $('LI.active', this);
		$('LI', this).hover(
			function(){
				active.removeClass('active');
				$(this).addClass('over');
			},
			function(){
				$(this).removeClass('over');
				active.addClass('active');
			}
		);
	});
};

jQuery.fn.inputFieldText = function(string, hintClass) {
	this.each(function() {
      $(this).filter(function(){
      	return ($(this).val() == '' || $(this).val() == string);
      }).addClass(hintClass).val(string);
      $(this).focus(function(){
        if ($(this).val() == string){
          $(this).removeClass(hintClass).val('');
        }
      });
      $(this).blur(function(){
        if ($(this).val() == ''){
          $(this).addClass(hintClass).val(string);
        }
      });
      var $this = $(this);
      $this.parents('FORM').submit(function(){
      	if ($this.val() == string) {
      		$this.val('');
      	}
      	return true;
      });
  });
  
  return this;
};

$(function(){
	$('#nav').rolloverMenu();
});

/*$(function(){
	$('.base TABLE').each(function(){
		var i = 0;
		$('TR', this).each(function(){
			if ($('TH',this).length == 0) {
				if (++i % 2 == 0) {
					$(this).addClass('even');
				}
			}
		});
	});
});*/

$(function(){
	$('a[rel^=fancybox]').fancybox({
		zoomSpeedIn:400,
		zoomSpeedOut:400,
		overlayShow: true,
		overlayColor: '#000',
		overlayOpacity: 0.8,
		hideOnContentClick:false
	});
});

$(function(){
	$('A, AREA').filter(function(){
		var href = $(this).attr('href');
		return href && !this.target && ((href.indexOf(window.location.hostname) == -1 && href.match(/^https?/i)) || href.match(/\.pdf$/i));
	}).attr('target', '_blank');
});

$(function(){
	$('IMG[align=left]').addClass('alignLeft');
	$('IMG[align=right]').addClass('alignRight');
	
	$('HR').replaceWith('<div class="hr"></div>');
});

(function($){
	
	$(function(){
		preload();
		$(".ro").hover(
			function(){ $(this).attr( 'src', newimage($(this).attr('src')) ); },
			function(){ $(this).attr( 'src', oldimage($(this).attr('src')) ); }
    );
	});
	
	function preload() {
		$(window).bind('load', function() {
    	$('.ro').each( function( key, elm ) { $('<img>').attr( 'src', newimage( $(this).attr('src') ) ); });
		});
	}
   
	function newimage(src) {
		return src.substring(0, src.search(/(\.[a-z]+)$/) ) + '_over' + src.match(/(\.[a-z]+)$/)[0];
	}
	
	function oldimage(src) {
		return src.replace(/_over\./, '.');
	}
	
})(jQuery);

$.fn.columnize = function(options) {
	if (!options) options = {};
	return this.each(function(){
		var maxHeight = 0;
		$('.resized', this).removeClass('resized').css({height:'auto'});
		$(this).children().each(function(){
			maxHeight = Math.max(maxHeight, $(this).height());
		}).each(function(){
			var diff = maxHeight - $(this).height();
			if (diff > 0) {
				$el = $('.expand', this);
				if (!$el.length) {
					$el = $(this).children(':last');
				}
				$el.addClass('resized');
				$el.height($el.height()+diff);
			}
		});
		if (options.shite == undefined) {
			$(this).bind('column.resize', function(){
				$(this).columnize({shite:true});
			});
			
		}
	});
}

$(window).load(function(){
	$('.cols').columnize();
});

$(function(){
	$('.productList IMG').each(function(){
		$span = $('<div class="productTitle"><div class="bg"></div><div class="title">'+$(this).attr('alt')+'</div></div>');
		$(this).after($span);
	});
	$('.productList A').hover(function(){
		$('.bg', this).css({opacity:1});
	}, function(){
		$('.bg', this).css({opacity:0.4});
	}).find('.bg').css({opacity:0.4});
	$('.productList A:last-child').addClass('last-child');
});

$(function(){
	$('#featured-products').carousel({effect:'fade', loop:true, callback:updateTitle, autoSlide:false, height:162});
	
	$('#featured-products-carousel-controls IMG').click(function(){
		if ($(this).hasClass('prev')) {
			$('#featured-products .carousel-previous').click();
		} else {
			$('#featured-products .carousel-next').click();
		}
	});
	
	function updateTitle(index) {
		var title = $('#featured-products LI').eq(index).find('IMG').attr('title');
		title += ' ['+(index+1)+' of '+$('#featured-products LI').length+']';
		$('#featured-products-title').text(title);
	}
	
	updateTitle(0);
	
});	

DD_roundies.addRule('.roundedCorners', '4px', true);
if (!$.browser.msie) {
	DD_roundies.addRule('#nav UL', '0px 0px 4px 4px', true);
}

