$(document).ready(function(){initializeProductDetailPage();})

function toggleAttributeImageBorder(current,newval){
	if(current != 0){
		$('#att_image_'+current).removeClass('selected');
		$('#att_link_'+current).removeClass('selected');
	}
	$('#att_image_'+newval).addClass('selected');
	$('#att_link_'+newval).addClass('selected');
}

function initializeProductDetailPage(){
	//setup radio buttons
	$(':radio[name^=radio_]').click(function(){
		var attribute = $('#'+$(this).attr('name').substring(6));
		attribute.val($(this).val());
	});
	
	//setup images
	$('.att-image').click(function(){
		var attribute = $(this).parents('div[id^=purchaseStep]').children(':hidden[name^=att_]');
		var current = attribute.val();
		var newval = $(this).attr('id').substring(10);
		toggleAttributeImageBorder(current,newval);
		attribute.val(newval);
	});
	
	//setup image links
	$('.att-link').click(function(){
		var attribute = $(this).parents('div[id^=purchaseStep]').children(':hidden[name^=att_]');
		var current = attribute.val();
		var newval = $(this).attr('id').substring(9);
		toggleAttributeImageBorder(current,newval);
		attribute.val(newval);
		return false;
	});
	
	$('.selectBTN').click(function(){
		var attribute = $('#'+$(this).attr('id').substring(7));
		var selected = parseInt(attribute.val());
		var container = $(this).parents('div[id^=purchaseStep]');
		var container_id = container.attr('id');
		var step_number = parseInt(container_id.substring(12));
		var next_step = step_number + 1;
		
		//make sure we have a valid selection
		if(selected === 0){
			//nothing selected - throw error
			alert($('#'+container_id+' .titlebar p').html()+ ' to continue.');
			return false;
		}
		
		//determine price and text for title
		price = eval('attributes.attribute_'+selected+'.price');
		if(price >= 0){
			price_text = '+$'+price.toFixed(2);
		}else{
			price_text = '-$'+Math.abs(price).toFixed(2);
		}
		var text = eval('attributes.attribute_'+selected+'.label');
		
		//toggle title bar
		container.children('.options').slideUp();
		container.toggleClass('closed').addClass('done').children('.titlebar').html('<a class="edit-link" href="#">edit</a><p class="total">'+price_text+'</p><span>'+step_number+'</span><p>'+text+'</p>').children('a').click(function(){
			reopenContainer(container_id);
			return false;
		});
		
		//check for/open next option
		if($('#purchaseStep'+next_step).length > 0){
			//next step found, open it
			$('#purchaseStep'+next_step).removeClass('closed');
		}else{
			//must be last step
			$('#purchaseTotal').removeClass('closed');
			$('#add-to-cart').attr('src','/images/add-to-cart-active.gif').css('cursor','pointer').click(function(){
				$(this).parents('form[name=rcicart]').submit();
				return false;
			});
		}
		
		//update total price
		updateProductDetailPrice();
	});
}
function reopenContainer(id){
	$('div[id^=purchaseStep]').addClass('closed').children('.options').slideUp();
	$('#'+id).removeClass('closed');
	$('#'+id).children('.options').slideDown();
}
function updateProductDetailPrice(){
	var att_ids = $(':input[name=att_id_list]').val().split(',');//get all attributes on page
	var total = product_price;//get product base price from javascript variable
	//get each attribute additional value
	for(x=0;x<att_ids.length;x++){
		var att_value = $('#att_'+att_ids[x]).val();
		
		if(att_value != 0){
			var att_price = eval('attributes.attribute_'+att_value+'.price');
			total += att_price;
		}
	}
	//display total price
	$('#total-price').html('$'+total.toFixed(2));
}