﻿jQuery.fn.vchecks = function() {
	
	object = jQuery(this);
	object.addClass('geogoer_vchecks');
	var sopra = false;

	//removing checkboxes
	object.find("input[type=checkbox]").each(function(){
		$(this).hide();
	});
	//adding images true false
	var i = 1;
	object.find("li").each(function(){
		var current_class = $(this).attr("id")
		
		if($(this).find("input[type=checkbox]").attr('checked') == true){
			$(this).addClass(current_class + '_checked');
		}
		else{
			$(this).addClass(current_class + '_unchecked');
		}
		$(this).addClass('type' + i);
		i = i +1
	});

	//binding onClick function
	object.find("li").find('span').click(function(e){
		e.preventDefault();
		check_li = $(this).parent('li');
		
		var current_class = check_li.attr("id")
		checkbox = $(this).parent('li').find("input[type=checkbox]");

		if(checkbox.attr('checked') == true){
			checkbox.attr('checked',false);
			if(sopra){
						check_li.removeClass(current_class + '_checked_h');
						check_li.addClass(current_class + '_unchecked_h');
			}
			else{
						check_li.removeClass(current_class + '_checked');
						check_li.addClass(current_class + '_unchecked');
			}
			
		}
		else{
			checkbox.attr('checked',true);
			if(sopra){
						check_li.removeClass(current_class + '_unchecked_h');
						check_li.addClass(current_class + '_checked_h');
				}
			else{
						check_li.removeClass(current_class + '_checked');
						check_li.addClass(current_class + '_unchecked');
			}

		}
	});
	
	//mouse over / out
	//simple
	object.find("li").find('span').bind('mouseover', function(e){
		sopra			= true;
		var checkbox_h	= $(this).parent('li').find("input[type=checkbox]");
		var index		= $(this).parent('li').attr("id");
		
		if(checkbox_h.attr('checked') == true){
			
				$(this).parent('li').removeClass(index + '_checked');
				$(this).parent('li').addClass(index + '_checked_h');
				
		}else
		{
				$(this).parent('li').removeClass(index + '_unchecked');
				$(this).parent('li').addClass(index + '_unchecked_h');
		}
	});

	object.find("li").find('span').bind('mouseout', function(e){
		sopra			= false;
		var checkbox_h	= $(this).parent('li').find("input[type=checkbox]");
		var index		= $(this).parent('li').attr("id");
		
		if(checkbox_h.attr('checked') == true){
				$(this).parent('li').removeClass(index + '_checked_h');
				$(this).parent('li').addClass(index + '_checked');
		}else
		{
				$(this).parent('li').removeClass(index + '_unchecked_h');
				$(this).parent('li').addClass(index + '_unchecked');
		}

	});

	// *** INVIA FORM PRINCIPALE ***
	object.find("li").find('span').click(function(e){
		e.preventDefault();
		check_li = $(this).parent('li');
		checkbox = $(this).parent('li').find("input[type=checkbox]");
		if(checkbox.attr('checked') == true){
			$("form[name='asdfx']").submit();
		}
		else{
			$("form[name='asdfx']").submit();
		}
	
	});

}

// RADIO
jQuery.fn.vradios = function() {
	
	object = jQuery(this);
	object.addClass('geogoer_vradios');
	
	sopra_due = false
	
	//removing RADIO
	object.find("input[type=radio]").each(function(){
		$(this).hide();
	});

//adding images true false
	var i = 0;
	object.find("li").each(function(){

		if($(this).find("input[type=radio]").attr('checked') == true){
				$(this).addClass('cat_checked');
		}
		else{
		}
				$(this).addClass('cat_unchecked');
		i = i +1
	});


	//binding onClick function
	object.find("li").find('span').click(function(e){
		e.preventDefault();

		check_li	= $(this).parent('li');
		radio		= $(this).parent('li').find("input[type=radio]");

		if(radio.attr('checked') == true){
											//radio.attr('checked',false);
											//check_li.removeClass('cat_checked');
											//check_li.addClass('cat_unchecked');
		}
		else{
											object.find("li input[type=radio]").attr('checked',false);
											object.find("li").removeClass('cat_checked');
											object.find("li").addClass('cat_unchecked');
											
											radio.attr('checked',true);
											check_li.removeClass('cat_unchecked');
											check_li.addClass('cat_checked');
		}
	});
	
	
	//mouse over / out
	//simple
	/*
	object.find("li").find('span').bind('mouseover', function(e){
		var index = $(this).parent('li').attr("id");
		$(this).parent('li').removeClass('cat_unchecked');
		$(this).parent('li').addClass('cat_checked');

	});

	object.find("li").find('span').bind('mouseout', function(e){
		var index = $(this).parent('li').attr("id");
		$(this).parent('li').removeClass('cat_checked');
		$(this).parent('li').addClass('cat_unchecked');
	});
	*/

	// *** INVIA FORM SECONDARIO
	object.find("li").find('span').click(function(e){
		e.preventDefault();
		
		check_li	= $(this).parent('li');
		radio		= $(this).parent('li').find("input[type=radio]");

		if(radio.attr('checked') == true){

			var valore = radio.val();
			$("input[name='mycat']").val(radio.val());
			$("form[name='asdfx']").submit();
		}
		else{
			$("form[name='asdfx']").submit();
		}
	});

}
