/**
 * This document only contains scripts that could/should be applied to any site
 */

if (typeof Panagora != 'object' || typeof Panagora == 'undefined')
	var Panagora = {};

var $j = jQuery.noConflict();

$j(document).ready(function() {
	
	/**
	 * Get rid of target="_blank"
	 * Do like this instead.. <a href="somewhere.asp" rel="external">Somewhere</a>
	 */
	
	$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);
				}
			}
		}
	}	
});
	
Panagora.SwitchImageAndStep = function(options) {
	var currentImageId = 0;
	
	function switchImg(e) {
		e.stopPropagation();
		e.cancelBubble = true;
		e.preventDefault();
		var back = false;
		
		var nextImageId = 0;
		var nextImage = 0;
		
		if (this.id == 'product-image-switch-previous')
			back = true;

		for (var i = 0; i < productImages.ids.length; i++) {
			if ($j('#product-main-image-link').attr('href') == productImages.baseUrl + "/" + productImages[productImages.ids[i]].huge)
				currentImageId = i;
		}
		
		if (!back && ++currentImageId > productImages.ids.length-1 ) {
			 currentImageId = 0;
		} else if (back && --currentImageId < 0) {
			currentImageId = productImages.ids.length-1;
		}
		
		$j('#product-main-image-link')
			.attr('href', productImages.baseUrl + "/" + productImages[productImages.ids[currentImageId]].huge);
		$j('#product-main-image')
			.attr('src', productImages.baseUrl + "/" + productImages[productImages.ids[currentImageId]].large);
			
		$j('.product-alternative-image')
			.attr('src', '/lib/css/gfx/bg-alternative-image.gif');
		
		$j('#alt-' + productImages.ids[currentImageId])
			.attr('src', '/lib/css/gfx/bg-alternative-image-selected.gif');

		return false;
	}

	var switchElement;
	
	options = options || {};

	// declare img variable for later use
	var img;
	
	$j(document).ready(function () {
		//$j('#product-main-image-link').click(switchImg);
		$j('.product-image-switch').click(switchImg);
	});
}
	