/**
 * This document only contains scripts that could/should be applied to any site
 */

var $j = jQuery.noConflict();

$j(document).ready(function() {
	
	$j.fn.setInputFieldValueFromLabel = function(labelContent, inputFieldId){
		$j(inputFieldId).val(labelContent).css('color', '#000');
		
		$j(inputFieldId).blur(function(){
			if($j(inputFieldId).val() == ''){
				$j(inputFieldId).val(labelContent).css('color', '#000');
			}
		});
		
		$j(inputFieldId).focus(function(){
			if($j(inputFieldId).val() == labelContent){
				$j(inputFieldId).val('');
			}
		});
	}

	
	
	/**
	 * Accordion menu
	 */
	$j('#category p.menu-head').click(function()
	{
		var bgImg = 'url(/lib/css/gfx/btn-minus.gif)';
		var wasOpened = false;
		
		try {
			if ($j(this).next('div.menu-body')[0].style.display != 'block') {
				wasOpened = true;
			} else {
				bgImg = 'url(/lib/css/gfx/btn-plus.gif)';
			}
		} catch(ex) {
		}
		
		var skip = false;
		
		// ie fix -- freaks out on slideToggle and slideUp
		/*@cc_on
			skip = true;

			$j(this)
				.css({backgroundImage:bgImg})
				.next('div.menu-body')
				.toggle()
				.siblings('div.menu-body')
				.hide();
		@*/

		if (!skip)
			$j(this)
				.css({'background-image':bgImg})
				.next('div.menu-body')
				.slideToggle(300)
				.siblings('div.menu-body')
				.slideUp('slow');

		$j(this)
			.siblings('p')
			.css({'background-image':'url(/lib/css/gfx/btn-plus.gif)', 'background-repeat': 'no-repeat'});
	});


	
	/**
	* Function to get rid of target="_blank"
	* Write like this instead.. <a href="somewhere.asp" rel="external">Somewhere</a>
	*/
	$j('a[@rel="external"]').click(function() {
		return !window.open($j(this).attr('href'));
	});

	
	
	$j('#product-custom-message').focus(function(e){
		if (this.value == this.defaultValue)
			this.value = ''; 
	});
	$j('#product-custom-message').blur(function(e){
		if (this.value == '') 
			this.value = this.defaultValue;
	});

	$j('#product-custom-message').keyup(function(e){
		var str = this.value;
		str = str.replace(/[^[a-zA-Z0-9]*/g, '');
		this.value = str;
	});

	/**
	 * Fix to be able to use css attribute selector for <td headers="something"> in Intercrap Explorer 6
	 * 
	 * Example:
	 * Write like this in your main stylesheet: td[headers="something"]{some-attribute: some-value;}
	 * And then do a normal class in your ie6 stylesheet: td.something{some-attribute: some-value;}
	 */
	if($j.browser.msie && $j.browser.version.substr(0,3) == '6.0'){
		if(document.body.getElementsByTagName('td').length > 0){
			for(i = 0; i < document.body.getElementsByTagName('td').length; i++){
				var header = new String(document.body.getElementsByTagName('td')[i].getAttribute('headers'));
				if(header != ''){
					document.body.getElementsByTagName('td')[i].setAttribute('className', header);
				}
			}
		}
	}	
});
