$(document).ready(function() {


	var options = {
	        insert : '#productImage',
	        history : false,
	        clickNext : false,
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

			image.click(function(){
				window.location = image.attr("src").replace("311x154","0x0");
			});

				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);

				// fetch the thumbnail container
				var _li = thumb.parents('li');

				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);

				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');

			},
			onThumb : function(thumb) { // thumbnail effects goes here

				// fetch the thumbnail container
				var _li = thumb.parents('li');

				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';

				// fade in the thumbnail when finnished loading
				thumb.css({opacity:_fadeTo});

				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}

	};

	$('ul.galleria').galleria(options);
	$('ul.galleria li:first').attr("class","active");
	$.galleria.activate( $('ul.galleria li.active img').attr("src") );

	if( $('ul.galleria li').size() == 1 ){
		$('ul.galleria').css('display','none');
	}

	if(document.getElementById('productForm') && document.getElementById('totalPrice')){
		updateProductPrice();
		$('#productForm select').bind("change", updateProductPrice);
	}

});


function updateProductPrice(event){

		if(event){
			var selectIndex = $(event.target).attr('selectedIndex');
			var img = $("#"+event.target.id+' option:eq('+selectIndex+')').attr('img')
			if(img){
				$('ul.galleria li:first').attr("class","");
				$('ul.galleria img').css({opacity:0.3});

				$.galleria.activate(img);
			}
		}

		var dropdowns = $('#productForm select');
		var price = parseFloat($('#initialPrice').html().replace(",",""));

		for(i = 0; i < dropdowns.length; i++){
			var selectIndex = $('#productForm select:eq('+i+')').attr('selectedIndex');
			var optionPrice = $('#productForm select:eq('+i+') option:eq('+selectIndex+')').attr('price');
			price = price + parseFloat(optionPrice);
		}

		price = new String(price);
		if(price.indexOf('.') < 0) { price += '.00'; }
		if(price.indexOf('.') == (price.length - 2)) { price += '0'; }

		document.getElementById('totalPrice').innerHTML = price;

}
