// validation for dropdown
function getPrice(valueID) {
	$.post("/ajax/get_property.php", {product_id: valueID, property: 'price'}, function(data){
		if (data != "") $('#product_price').html(data);									
	});
}
function getAttributes(element) {
	
	var dropDown = $(element).prev('div').hasClass('rec-product-links-inner') ? $(element).prev('div').children('div.select_box') : $(element).siblings('div.select_box:last');
	var value = dropDown.children('input:hidden').val();
	
	if( value != '' && value != "group_product"  ) {
		return true;
	}
	var selectText = dropDown.children("span.default").attr("title");
	if( selectText == '' ) selectText = 'Please select';
	dropDown.children("span.default").html('<span style="color:red;">'+selectText+'</span>');
	
	return false;
};


$(document).ready(function() {

	// country selector
	$('a.country').click(function() {
		$('#country_list').slideDown('slow');
		return false;
	});
	$('body').click(function() {
		$('#country_list:not(:animated)').slideUp();
	});
	
	// dropdowns
	$('div.select_box span.default').click(function(event){
		// if not visible drop down
		event.stopPropagation();
		$(this).next('div.options:not(:visible)').slideDown();
	});
	
	// set values of dropdowns
	$('div.options > a').click(function(event) {
		// set dropdown
		var dropdown = $(this).parent('div.options').parent('div.select_box');
		var valueField = $(this).parent('div.options').next('input:hidden');
		
		// get value of clicked element
		var selectedValue = $(this).attr("href");
		
		// sort out the new value
		if( selectedValue == 'replenish-shampoo-conditioner' ) {
			valueID = 42;
		}
		else if( selectedValue == 'guardian-shampoo-conditioner' ) {
			valueID = 43;
		}
		else if( selectedValue == 'elevation-shampoo-conditioner' ) {
			valueID = 44;
		}
		else if( selectedValue == 'nurture-shampoo-conditioner' ) {
			valueID = 45;
		}
		else if( selectedValue == 'ghd-iv-styler' ) {
			valueID = 173;
		} else if( selectedValue == 'ghd-iv-mini-styler' ) {
			valueID = 174;
		} else if( selectedValue == 'ghd-iv-salon-styler' ) {
			valueID = 175;
		} else if( selectedValue == 'normal-fine' ) {
			valueID = 32;
		} else if( selectedValue == 'dry-coarse' ) {
			valueID = 33;
		} else if( selectedValue == 'weak-damaged' ) {
			valueID = 34;
		} else {
			valueID = selectedValue;
		}
				
		// get content of clicked element
		var selectedContent = $(this).html();
		// shorten content if too long
		//if( $(this).children().hasClass('orange') ) {
		//	selectedContent = selectedContent.replace(/<span(.*)span>/,"");
		//}
		if( $(this).children().length == 0 && selectedContent.length >= 20 ){
			selectedContent = selectedContent.substring(0,17);
			selectedContent = selectedContent + '...';
		}
		
		// insert content into drop down main area
		dropdown.children('span.default').html(selectedContent);
		
		// update value of hidden field
		valueField.val(valueID);
		
		// check if the dropdown selects an option or a product
		var valueName = valueField.attr("name");
		if( valueName.match(/id\[/) != null ){
			var type = 'option';
		} else {
			var type = '';
		}
		
		// update value of "buy now" href or form action if needed (!!!!)
		if( dropdown.parent().hasClass("rec-product-links-inner") ){
			var cta = dropdown.parent('div').next();
		} else {
			var cta = dropdown.next();
		}
		if( cta.get(0).tagName == "INPUT" && type != 'option' ){ 
			// input and product ID: we need to update the action of the form
			var action = cta.parent('form').attr("action");
			var newAction = action.replace(/(products_id=)([0-9]*)/,"products_id="+valueID);
			// change form action
			cta.parent('form').attr("action",newAction);
		} else if( cta.get(0).tagName == "A" ) { // a
			var href = cta.attr("href");
			if( type == 'option' ){ // option: just append the value to href
				href = href.replace(/&id\[([0-9]*)\]=([0-9]*)/g,"");
				var newURL = href + "&" + valueName + "=" + valueID;
			} else { // product id: replace product id
				var newURL = href.replace(/(products_id=)([0-9]*)/,"products_id="+valueID);
			}
			// change value of href
			cta.attr("href",newURL);
		}
		// close the dropdown
		dropdown.children('div.options').slideUp();
		getPrice(valueID);
		
		return false;
		
		
	});

	$('body').click(function() {
		$('div.options:not(:animated)').slideUp();
	});

	
});