function displayImage(index, parent){
 	var images = document.getElementById(parent).getElementsByTagName("div");
	for(var i = 0; i < images.length; i++) {
	  var image = images[i];
	  if (image.className != 'pimage')  { continue }
	  if(i == index-1) {
	    image.style.display="block";
	  }
	  else {
	    image.style.display="none";
	  }		
	}	
}

(function(){
	var $ = jQuery
	
	$(document).ready(function(){
		productSwitch();
		imageSwitch();
		giftNote();
		inStock($('ul.variants li:first'))
	})
	
	$(window).load(function() { // These functions get called when everything has loaded
		$('#sidebar').height(($('#sidebar').height() < 600 ? 600 : ''))
		$('#main').height(($('#main').height() < $('#sidebar').height() ? $('#sidebar').height() : ''))
	})
	

function giftNote() {
        $('input#gift-message').click(function(){
            if($(this).hasClass("checked")) {
                $('p#gift_message_area').hide();
                $(this).removeClass('checked');
            } else {
                $('p#gift_message_area').show();
                $(this).addClass('checked');
            }
        });
}





	function productSwitch() {
		if(!$('div#product').size()) {
			return;
		}
		var prods = $('div#product div.item')
        prods.each(
            function(i) {
                var item = $(this)
                var radio_options = item.find('ul.variants li')
                var container = item.parent()
            	
                radio_options.each(
                        function(i) {
                            $(this).click(
                                    function(c) {
                                        c.preventDefault()
                                        prods.find('div.inside_item[current] div.product_images').fadeOut("slow")
                                        prods.find('div.inside_item[current]').removeAttr('current').hide()
                                        prods.find('div#product_' + i).attr({current: 'current'}).show()
                                        prods.find('div#product_' + i + ' div.product_images').fadeIn("slow")
                                        inStock($(this))
                                    }
                                )
                        }
                    )
            }
        )
        
        prods.find('div.inside_item div.product_images').hide()
        prods.find('div.inside_item').hide()
        if (! window.location.hash) {
            $(prods[0]).find('div.inside_item').attr({current: 'current'}).show()
            $(prods[0]).find('div.inside_item div.product_images').fadeIn("slow")
            
        }
        else {
            var prod = window.location.hash.substr(1)
            $(prods.find('.' + prod)).find('div.inside_item').attr({current: 'current'}).show()
            $(prods.find('.' + prod)).find('div.inside_item div.product_images').fadeIn("slow")
        }
        
        
	}
	
	function inStock(i_var) {
	    if(!$(i_var).size()) {
			return;
		}
	    
        if (!$(i_var).find('input').size()) {
            $('div.buttons input.primary').hide();
        }
        else {
            $('div.buttons input.primary').show();
        }
        
    }
    
	
	function imageSwitch() {
	    if(!$('div.product_images').size()) {
			return;
		}
		
        var image_groups = $('div.product_images')
        image_groups.each(
            function(i) {
				var group = $(this)
                var small_images = group.find('div.side_product_images div.pimage a')
                
                small_images.each(
                    function(i) {
                        $(this).click(
                            function(c) {
                                c.preventDefault()

				                var big_image = group.find('div.main_product_image img')
								var src = $(this).attr('href')

                                big_image.fadeOut('slow', function() {
									var img = new Image();
									$(img).css({ 'display': 'none' })
									big_image.replaceWith($(img))
									$(img).load(function(){
										$(img).fadeIn('slow')
									})
									img.src = src
								})                                
                            }
                            )
                    }
                    )
        	    
            }
            )
	}
	
})();