/*
 * dress search form
 */

function updateOverlay(name,callback) {
	var str = jQuery("#dressForm").serialize();

	action = 'webnovias[action]=dress'+name;
	template = 'webnovias[template]='+name;
	jQuery('#'+name)
		.load(
			"?type="+ajaxPageType+"&"+action+"&"+str+"&"+template,
			{},
			callback
			);
}

function toggleDesigners(hide) {
	// no brand => no designer
  	if (hide) {
			jQuery('#hideDesigner').slideUp('fast');
			resetField('designer');
 	} else {
			jQuery('#hideDesigner').slideDown('fast');
	}
}

function resetField(name) {
		if (name=='type') {
			if (dressTypePreselect!='') {
				jQuery('#'+name).val(firstDressTypeUid);
			jQuery('#'+name+'Fake').val(firstDressTypeName);
		}
		} else {
			jQuery('#'+name).val(0);
			jQuery('#'+name+'Fake').val(textViewAll);
		}
}

function attachFieldClick(name) {
	jQuery('#'+name+'Fake').click(function(){
		resetField(name);
			dressFormChange(function(){
				jQuery('.layerSearch')
					.stop()
					.hide()
					.html(jQuery('#'+name+'s').html());

		jQuery('.searchLabel')
			.unbind()
			.click(overlayClick);

				jQuery('.searchClick')
					.unbind()
					.click(overlayClick)
					.css('opacity','0.5')
					.hover(function(){
						jQuery(this).css('opacity','1.0');
					},function(){
						jQuery(this).css('opacity','0.5');
					}
				);
			jQuery('.layerSearch').show();
		});
	});
}

function fakeField(name) {
	var fakeValue = textViewAll;

	/* Commented by Sergio for Complementos section */
	/* if (name=='type') {
		fakeValue = firstDressTypeName;
	}; */

	var value = jQuery('#'+name).val();

	jQuery('#'+name)
		.after('<input readonly="readonly" type="text" id="'+
			name+'Fake" value="'+fakeValue+'" />')
		.after('<input type="hidden" id="'+name+'" '+
			'name="webnovias['+name+']" value="'+value+'" />')
		.remove();
}

// serialize the form and update the dressCount element
function dressFormChange(callback) {
	var str = jQuery("#dressForm").serialize();
	var action = 'webnovias[action]=dresscount';

	jQuery("#dressCount").load(
		"?type="+ajaxPageType+"&"+action+"&"+str,{},
		function(text){
			if (text.match(/^0\b/)) {
				jQuery('#dressFormSubmit').attr('disabled','disabled');
			} else {
				jQuery('#dressFormSubmit').attr('disabled','');
			}
  		}
  	);

	// show some parts of the form just for the bride
  	if (jQuery('#type').val()!=1) {
  		jQuery('#cutAndNeckline').slideUp('fast');
  		resetField('cut');
  		resetField('neckline');
  	} else {
  		jQuery('#cutAndNeckline').slideDown('fast');
	}

	// hide brands and designers if type is not choosen
  	if (jQuery('#type').val()=='') {
  		jQuery('#hideFromUndecided').slideUp('fast');
  		resetField('brand');
  		resetField('designer');
  	} else {
  		jQuery('#hideFromUndecided').slideDown('fast');
	}

	if (jQuery('#brand').val()!=0) {
		updateOverlay('brands', function(){
			updateOverlay('designers', function(){
				var designerCount = jQuery('#designers div.multiColumnList ul')
					.children().length;

				toggleDesigners(designerCount<=0);

				callback();
			});
		});
	} else {
		updateOverlay('brands', callback);
	}

	jQuery('.searchLabel')
		.unbind()
		.click(overlayClick);
}

// attach the click elements to legend entries
function overlayClick() {
	var classNames=jQuery(this).attr('class');
	// split class names
	var classes = classNames.split(/[ \t\n]+/);
	var name=classes[1];
	// split "neckline_1234" format into type & number
	var param=name.split('_');
	// set the input field of the type to the number
	jQuery("#"+param[0]).val(param[1]);
	// fill the text of the fake input field
	var label = "."+name+":first";
	jQuery("#"+param[0]+"Fake").val(jQuery.trim(jQuery(this).text()));

	dressFormChange();

	jQuery('.layerSearch').hide();

	return false;
}

/**
 * Adding width of search overlay list images to each p element below.
 * This helps us to have correct positioned labels in Firefox.
 *
 * @author	Dennis Riedel <riedel@opus5.info>
 */

function updateWidthOfSearchLabels() {

	jQuery("div#webnoviasSearchForm ul li img").each(
		function () {
				var imageWidth = jQuery(this).attr("width");
			jQuery(this)
				.next(".searchLabel")
				.attr("style", "width:"+imageWidth+"px;");
		}
	);
}


jQuery().ready(function() {
	jQuery('select').change(dressFormChange);
	jQuery('#viewAll').change(dressFormChange);
	jQuery('#hideDesigner').hide();
	dressFormChange();

	jQuery(".container").append('<div class="layerSearch"></div>');
	jQuery(".layerSearch").hide();


	if (dressTypePreselect=='') {
		fakeField('type');
		attachFieldClick('type');

		if (openTypeOverlay=='1') {
			jQuery('.layerSearch')
				.html(jQuery('#types').html())
				.show();

			jQuery('#typeFake').click();
		}
	}
	fakeField('cut');
	attachFieldClick('cut');
	fakeField('neckline');
	attachFieldClick('neckline');
	fakeField('designer');
	attachFieldClick('designer');
	fakeField('brand');
	attachFieldClick('brand');

	if(jQuery.browser.mozilla) {
		updateWidthOfSearchLabels();
	}

	//Replace system checkbox with replacement checkbox
	jQuery("input.defaultCheckbox").replaceWith('<span class="checkboxReplace inactive"></span><input type="hidden" name="webnovias[viewall]" id="viewAll" />');

	//Handle onClick for the replacement checkbox
	jQuery("span.checkboxReplace").click( function() {
		if (jQuery(this).hasClass("inactive")) {
			jQuery(this).css("background", "#FFFFFF url(fileadmin/templates/img/checkbox-on.gif) no-repeat top left")
			jQuery(this).removeClass("inactive");
			jQuery("#viewAll").val('checked');
		} else {
			jQuery(this).css("background", "#FFFFFF url(fileadmin/templates/img/checkbox-off.gif) no-repeat top left")
			jQuery(this).addClass("inactive");
			jQuery("#viewAll").val('');
		}
		dressFormChange();
	});

});

