var separator = '-'; 
var counter = 1;
var z_actual = 150;
var lastCheckbox = '';

jQuery.fn.jLook = function(options){
	var self = this;
	$(this).addClass('jLookForm');
	this.each(function(){
		var actualForm = this;
		$('label', this).addClass("jLookLabel");
		$('input:text', this).addClass("jLookInput");	

		var lastRadio = '';
		$('input:radio', this).each(function(){
			$(this).before('<a href="#" id="'+$(this).attr("name")+'-'+$(this).attr("value")+'" class="jLookRadio"></a>');
			$(this).remove();
			if(lastRadio != $(this).attr("name")){
				$(actualForm).prepend('<input type="hidden" id="'+$(this).attr("name")+'" name="'+$(this).attr("name")+'" value="null" />');
				lastRadio = $(this).attr("name");
			}
		});
		$('.jLookRadio').bind('click', radioClick)

		$('input:checkbox', this).each(function(){
			var className="jLookCheckbox";
			if(this.checked){
				className = className + " jLookCheckbox_checked";
			}
			var disabled_value="";
			if(this.disabled){
				disabled_value='disabled="true"';
			}
			$(this).before('<a ' + disabled_value + ' href="#" id="'+$(this).attr("name") + separator + $(this).attr("value")+'" class="' + className + '"></a>');
			$(this).remove();
			if(lastCheckbox != $(this).attr("name")){
				var hidvalue = "null";
				if(this.checked){
					hidvalue=this.value;
				}
				$(actualForm).prepend('<input type="hidden" id="'+$(this).attr("name")+'" name="'+$(this).attr("name")+'" value="' + hidvalue + '" />');
				lastCheckbox = $(this).attr("name");
			}
		});
		$('.jLookCheckbox').bind('click', checkboxClick);
		
		$('select', this).each(function(){
			var nam = $(this).attr('name')+'_wrapper';
			$(this).wrap('<div class="jLookSelectWrapper" id="' + nam + '"></div>');
			var name = $(this).attr('name');
			var indiceSeleccionado = this.selectedIndex;
			var selectContentHeader= '<input type="hidden" value="" name="' + name + '" id="' + name + '" /><div class="jLookSelectHeader"><div></div><a href="#" class="jLookSelectOpen"></a></div>'; // xhtml replace
			var selectContent = '<ul>';
			if(options.selectHeight) var selectContent = '<div class="optionsWrapper"><ul style=" height:' + options.selectHeight + 'px">';
			var index = 0;
			$('option', this).each(function(){
				selectContent += '<li><a href="#" class="jLookSelectOption" id="' +  name + separator + $(this).attr('value') + '">' + $(this).html() + ' </a></li>';
				if(indiceSeleccionado == index){
					selectContentHeader = '<input type="hidden" value="' + $(this).attr('value') + '" name="' + name + '" id="' + name + '" /><div class="jLookSelectHeader"><div>' + $(this).html() + '</div><a href="#" class="jLookSelectOpen"></a></div>'; // xhtml replace	
				}
				$(this).remove();
				index = index + 1;
			});
			selectContent += '</ul></div>';
			$('#' + nam).html(selectContentHeader + selectContent);
		});
		$('.jLookSelectOpen').bind('click', selectClick);
		$('.jLookSelectOption').bind('click', optionClick);
		$('.jLookSelectOption').bind('click', optionlocClick);
		
		$('textarea', this).each(function(){
			$(this).wrap("<div class='jLookTextareaWrapper'></div>");
		});
	});
}

checkboxClick = function(){
	valu = $(this).attr("id").split(separator);
	if($('#'+valu[0]).attr("value") == valu[1]){
		$(this).removeClass('jLookCheckbox_checked'); // uncheck
		$('#'+valu[0]).attr("value", 'null');
	}else{
		$(this).addClass("jLookCheckbox_checked");
		$('#'+valu[0]).attr("value", valu[1]); // selectedValue
	}
	return false;
}


radioClick = function(){
	valu = $(this).attr("id").split('-');
	actualValue = $('#' + valu[0]).attr("value");
	if( actualValue != valu[1]){
		$('#' + valu[0] + '-' + actualValue).removeClass('jLookRadio_checked');
		$('#' + valu[0]).attr("value", valu[1]);
		$(this).addClass('jLookRadio_checked');
	}
	return false;
}

function selectClick(){
	z_actual = z_actual + 10;
	var wrapperId = $(this).parent().parent().attr('id');
	if($('#' + wrapperId).is('.jLookSelectOpened')){
		$('#' + wrapperId).removeClass('jLookSelectOpened');
		$('#' + wrapperId).css('position', 'relative');
		$('#' + wrapperId).css('z-index', '80');
		$('.optionsWrapper', $('#' + wrapperId)).slideUp(0);
	}else{
		$('.jLookSelectWrapper').each(function(i){
			if($(this).attr('id') != wrapperId){
				if ($(this).hasClass('jLookSelectOpened')){
						$('.jLookSelectOpen').eq(i).trigger('click');
					}
			}
		});
		$('#' + wrapperId).css('position', 'relative');
		$('#' + wrapperId).css('z-index', '130');

		$('#' + wrapperId).addClass('jLookSelectOpened');
		$('.optionsWrapper', $('#' + wrapperId)).slideDown();
	}
	
	return false;
};

optionClick = function(){
    valu = $(this).attr('id').split(separator);
    var wrapperId = valu[0] + '_wrapper';
    $('#' + valu[0]).attr('value', valu[1]);
    $('div', $('#' + valu[0] + '_wrapper .jLookSelectHeader')).html($(this).html());
	$('#' + wrapperId).removeClass('jLookSelectOpened');
	$('.optionsWrapper', $('#' + wrapperId)).slideUp('slow', function(){
		$('.jLookSelectWrapper').each(function(){
			if($(this).attr('id') != wrapperId){
			}
		});	
	});
	return false;
};
optionlocClick = function(){	
	if($(this).parent().parent().parent().parent().parent().attr('id')=="location-select")
	{
		 valu = $(this).attr('id').split(separator);
		document.location.href=valu[1];
	}
};

setHidden = function(id, value){
}

	
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
$(document).ready(function() {
	 var atscccc =  jQuery.makeArray($('ul.compare-product:first li'));
	 for (var i = 0; i <atscccc.length; i ++)
	 {
	      var eKey = $(atscccc[i]).attr('class');
	      equalHeight($(".compare-list ."+eKey));
	  }
});
