/* this script handles the menu/search behaviours. */
//jQuery.noConflict(); // no conflict is already done...
(function($){
	$(document).ready(function() {
		//hide children and create actions
		$('ul#products a').each(function() {
			$(this).attr('original_href',$(this).attr('href'));
		});
		$('ul#products ul ul')
			.hide()
			.each(($('body.home').length>0)?function(){return;}:function(){
				$this = $(this);
				tmp_parent = $($this.parent().get(0));
				if ( typeof tmp_parent == 'undefined' ) {
					return;
				} 
				parent_opener = $(tmp_parent).find('> a');
				if ( parent_opener.length == 0 ) {
					return;
				}
				$(parent_opener.get(0)).data('ul_target',$this);
				parent_opener.attr('href','#').click(function() {
					var initial_height = $(this).parents('.products,.gallery').height();
					$(this).data('ul_target').slideToggle('normal', function() {
						end_height = $(this).parents('.products,.gallery').height() - initial_height;
						$('div#body').animate({
							height: '+='+end_height
						},1);
					});
					return false;
				});
			});
		//toggle any current links
		/*** danh@20110325 - 15097 - modified for architectural lighting ***/
		if ( /cat=[0-9]+/.test(document.location.href) || /architectural-lighting/.test(document.location.href)) {
		/*** end danh@20110325 - 15097 - modified for architectural lighting ***/
			$('ul#products a').each(function() {
				link_href = $(this).attr('original_href')
					.replace(/#.*/g,'')
					.replace(/(\?cat=[0-9]+)&.*/,'$1')
					.replace(/^\//,'');
				doc_href = String(document.location.href)
					.replace(/^.*\//g,'');
				/*** danh@20110325 - 15097 - modified for architectural lighting ***/
				link_href = link_href.replace('products/lighting/arch', 'arch');
				/*** end danh@20110325 - 15097 - modified for architectural lighting ***/
				if ( link_href == doc_href ) {
					//in case this is a containing link show itself, and parents
					$(this).find('~ ul').show().end().parents('ul').show();
				}
				return;
			});
			//add some height in case a really long menu has arrived...
			if ( $('div#body').height()-$('ul#products').height() < 200 ) {
				$('div#body').height(
					$('div#body').height()+300
				);
			}
		}
		//search fields
		$('input[name=q]').focus(function() {
			$this = $(this);
			if ( $this.val() == 'Search...' ) {
				$this.val('');
			}
			$this.removeClass('search-text');
		}).blur(function() {
			$this = $(this);
			if ( $this.val() == '' || $this.val() == 'Search...' ) {
				$this.addClass('search-text').val('Search...');
			}
		}).blur();
	});
})(jQuery);
