// JQUERY CUSTOM COMMANDS
$(document).ready( function() {  // start javascript when document is loaded
	
	/* ADD CLASSES FOR LAYOUT STYLING */
	$('#columnRight .box:last').addClass('last');
	
	/* MENU HOVERS */
	$('#topMenu li, #mainMenu li, .myMenu li').hover(function() { // IE6 does not support :hover on elements other than <a>
	    $(this).addClass('hover');
	}, function() {
	    $(this).removeClass('hover');
	});
	$('#mainMenu li:has(ul) > a').addClass('hasSub'); // give class to parent li's.
	
	/* avoid double link-execution when hyperlink within onclick is clicked */
	$('.myMenu li a').click(function(){
			$(this).parents('li').removeAttr('onclick');
	});

	
	// INPUT FOCUS & BLUR
	$('input.textfield, textarea.textarea, select.select').focus(function() {
	    $(this).addClass("focus");
	});
	$('input.textfield, textarea.textarea, select.select').blur(function() {
	    $(this).removeClass("focus");
		$(this).removeClass("error");
		$(this).parents("li").find("div.error").empty();
	});
	
	// HIDE EMPTY ERROR FIELDS
	$('ul.formfields div.error:empty').hide();
	
	
	// CUSTOM SELECT
	// Show on click
	$(".customSelect a.selected").click(function() {
		$(this).parents(".customSelect").css({position:"relative"});
		$(this).parents(".customSelect").find('ul').show();
		return false;
	});
	// Hide on mouseout
	$(".customSelect").mouseleave(function() {
	    $(this).find('ul').hide();
		$(this).css({position:"static"});
	});
	// Set width from parent to child
	$(".customSelect").each(function(i) {
		var $customSelectWidth = $(this).width();
		$(this).find('ul').width($customSelectWidth);
	});
	
	/*
	// ACCOUNT INPUT LABELS as background
	// fill or empty email field onDefault
	if ($(".accountEmail").val() == "") {
		$(".accountEmail").css({ backgroundImage : "url(images/account_input_email.gif)" });
	} else {
		$(".accountEmail").css({ backgroundImage : "url(images/account_input_empty.gif)" });
	};
	// fill or empty password field onDefault
	if ($(".accountPassword").val() == "") {
		$(".accountPassword").css({ backgroundImage : "url(images/account_input_password.gif)" });
	} else {
		$(".accountPassword").css({ backgroundImage : "url(images/account_input_empty.gif)" });
	};
	// empty fiels onClick
	$(".accountEmail, .accountPassword").click(function() {
		$(this).css({ backgroundImage : "url(images/account_input_empty.gif)" });
	});
	// empty fiels onFocus
	$(".accountEmail, .accountPassword").focus(function() {
		$(this).css({ backgroundImage : "url(images/account_input_empty.gif)" });
	});
	// refill emailfield onBlur
	$(".accountEmail").blur(function() {
		if ($(".accountEmail").val() == "") {
			$(".accountEmail").css({ backgroundImage : "url(images/account_input_email.gif)" });
		} else {
			$(".accountEmail").css({ backgroundImage : "url(images/account_input_empty.gif)" });
		};
	});
	// refill passfield onBlur
	$(".accountPassword").blur(function() {
		if ($(".accountPassword").val() == "") {
			$(".accountPassword").css({ backgroundImage : "url(images/account_input_password.gif)" });
		} else {
			$(".accountPassword").css({ backgroundImage : "url(images/account_input_empty.gif)" });
		};
	});
	*/
	
	// ACCOUNT LINKS
	$(".boxAccount li.dropdown a").click(function() {
		$(this).parent('li').find('ul').show();
		return false;
	});
	
	
	// SEARCH FIELD
	// Show panel onclick
	$(".siteSearch .input .textfield").click(function() {
		$('.siteSearchOptions').show();
	});
	$(".siteSearch .input .textfield").focus(function() {
		$('.siteSearchOptions').show();
	});

	// Hide panel onmouseout
	$(".siteSearch").mouseleave(function() {
	   $('.siteSearchOptions').hide();
	});
	// Hide panel on close-link
	$(".siteSearch .close").click(function() {
		$('.siteSearchOptions').hide();
		return false;
	});
	
	
	// FILTERS
	// make filter active
	$('.customSelect:has(input)').removeClass('active');
	$('.customSelect:has(input:checked)').addClass('active');
	
	$(".customSelect input").click(function() {
		$('.customSelect:has(input)').removeClass('active');
		$('.customSelect:has(input:checked)').addClass('active');
	});
	// clear selection
	$(".clearSelection").click(function() {
		$('.filterBox .filters').find('input').attr('checked', '');
		$('.filterBox .filters .customSelect').removeClass('active');
		return false;
	});
	
	
	/* LISTS */
	$("tbody tr:odd").addClass('odd');
	$("tbody tr:even").addClass('even');
	$("tbody tr:first").addClass('first');
	$("tbody tr:last").addClass('last');
	$("tbody tr:last").prev('tr').find('.select').addClass('last_select'); // Select last select cell (because of rowspan)
	$('tbody tr').hover(function() {
	    $(this).addClass('hover');
	}, function() {
	    $(this).removeClass('hover');
	});
	// shoppingcart & productlist
	$(".shoppingcart tbody tr.rowA:odd, .productList tbody tr.rowA:odd").addClass('A_odd');
	$(".shoppingcart tbody tr.rowA:even, .productList tbody tr.rowA:even").addClass('A_even');
	$(".shoppingcart tbody tr.rowB:odd, .productList tbody tr.rowB:odd").addClass('B_odd');
	$(".shoppingcart tbody tr.rowB:even, .productList tbody tr.rowB:even").addClass('B_even');
	
	// Toggle checkboxes
	var tog_td_select = false; // or true if they are checked on load 
	var $listChecks = $('td.select input[type=checkbox]');
	$('th.select a').click(function(){
	  	if(!tog_td_select)
	   	{
	    	$listChecks.attr('checked','checked');
	   	}
	  	else
	   	{
	    	$listChecks.removeAttr('checked');
	   	}
	   	tog_td_select = !tog_td_select;
	})
	
	
	/* FAQ TOGGLE */
	$('.faqList .faqAnswer').hide();
	$('.faqList .faqQuestion a').click(function() {
		$(this).parents('.faqList').find(".faqAnswer").slideUp(200); // Hide all answers
		$(this).parents('.faqList').find('li').removeClass("active");
	    $(this).parents('li').find(".faqAnswer").slideDown(200); // Show current answer
		$(this).parents('li').addClass("active");
		return false;
	});
	
	
	// TOOLTIPS
	// documentation: http://docs.jquery.com/Plugins/Tooltip/tooltip
	$(".tooltip").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fade: 0
	});
	
	
	// ROLLOVER IMAGES (1/2)
	DNZ.rollover.init();
   
}); // end ready function



// ROLLOVER IMAGES (2/2)
DNZ = {};
DNZ.rollover = {
   init: function() {
      this.preload();
      $(".rollover").hover(
         function () { $(this).attr( 'src', DNZ.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', DNZ.rollover.oldimage($(this).attr('src')) ); }
      );
   },
   preload: function() {
      $(window).bind('load', function() {
         $('.rollover').each( function( key, elm ) { $('<img>').attr( 'src', DNZ.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   newimage: function( src ) {
      return src.substring( 0, src.search(/(\.[a-z]+)/) ) + '_on' + src.match(/(\.[a-z]+)/)[0];
   },
   oldimage: function( src ) {
      return src.replace(/_on/, '');
   }
};






