// 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 == '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");
			
			//Get the product ID
			if( href.match(/id=[0-9][0-9]/) ){
				var requested_id = href.match(/id=[0-9][0-9]/);
				//alert("product_id set to = " + requested_id);
			}
			
			if( type == 'option' ){ // option: just append the value to href
			
				if(href == '/us/customer-care/ghd-salon-locator'){
					href = '/shop-us/shopping_cart.php?action=add_product&products_';
					//var id = product_id;
					var id = cta.attr("id");
					var newURL = href + id + "&" + valueName + "=" + valueID;
					//alert ("im in this clause... id" + id + "value name = " + valueName + " valueID = " + valueID);
				}
				else{
					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
			//UPDATE - DAN - 11/8/09: Size 5 - Ceramic brush - is out of stock, if size 5 is
			//selected change button to "find a stockist" and change url to salon loactor
			if(newURL == '/shop-us/shopping_cart.php?action=add_product&products_id=45&id[2]=5'){
				cta.attr("class",'find_a_stockist');	
				cta.attr("href",'/us/customer-care/ghd-salon-locator');
				cta.attr("id",requested_id);
			}
			else{
				cta.attr("class",'buy_now');
				cta.attr("href",newURL);
			}
		}
		// close the dropdown
		dropdown.children('div.options').slideUp();
		getPrice(valueID);
		
		return false;
		
		
	});

	$('body').click(function() {
		$('div.options:not(:animated)').slideUp();
	});

	
});