$(document).ready(function(){
	$(".check-loi").click(function(){
		id = $(this).attr("id");
		if ($('input:checkbox[name=check-loi-'+id+']:checked').length) {
			$("#vote-down-loi-"+id).removeClass();
			$("#vote-up-loi-"+id).removeClass();
			$("#vote-down-loi-"+id).addClass('vote-down');
			$("#vote-up-loi-"+id).addClass('vote-up');
		} else {
				$("#vote-down-loi-"+id).removeClass();
				$("#vote-up-loi-"+id).removeClass();
				$("#vote-down-loi-"+id).addClass('vote-down-grey');
				$("#vote-up-loi-"+id).addClass('vote-up-grey');
		}
	});	
	
	$(".check-article").click(function(){
		id = $(this).attr("id");
		if ($('input:checkbox[name=check-article-'+id+']:checked').length) {
			$("#vote-down-article-"+id).removeClass();
			$("#vote-up-article-"+id).removeClass();
			$("#vote-down-article-"+id).addClass('vote-down');
			$("#vote-up-article-"+id).addClass('vote-up');
		} else {
				$("#vote-down-article-"+id).removeClass();
				$("#vote-up-article-"+id).removeClass();
				$("#vote-down-article-"+id).addClass('vote-down-grey');
				$("#vote-up-article-"+id).addClass('vote-up-grey');
		}
	});	
	
	$(".vote-up").live("click", function(){
		id = $(this).attr("id");
		if(id.search('loi')>-1) {
		//loi
			id = id.replace('vote-up-loi-','');
			$.ajax({
				type: 'GET', 			
				url: '/lois/vote_up/'+id, 
				success: function(retour){
					$("#vote-loi-"+id).hide();
					$("#vote-loi-"+id).html(retour);
					$("#vote-loi-"+id).fadeIn('slow');
				}
			});
		} else {
			//article
			id = id.replace('vote-up-article-','');
			id_loi = $(".id-loi").attr("id");
			id_loi = id_loi.replace('id-loi-','');
			$.ajax({
				type: 'GET', 			
				url: '/lois/vote_up/'+id_loi+'/'+id, 
				success: function(retour){
					$("#vote-article-"+id).hide();
					$("#vote-article-"+id).html(retour);
					$("#vote-article-"+id).fadeIn('slow');
				}
			});
		}
	});
	
	$(".vote-down").live("click", function(){
		id = $(this).attr("id");
		if(id.search('loi')>-1) {
		//loi
			id = id.replace('vote-down-loi-','');
			$.ajax({
				type: 'GET', 			
				url: '/lois/vote_down/'+id, 
				success: function(retour){
					$("#vote-loi-"+id).hide();
					$("#vote-loi-"+id).html(retour);
					$("#vote-loi-"+id).fadeIn('slow');
				}
			});
		} else {
			//article
			id = id.replace('vote-down-article-','');
			id_loi = $(".id-loi").attr("id");
			id_loi = id_loi.replace('id-loi-','');
			$.ajax({
				type: 'GET', 			
				url: '/lois/vote_down/'+id_loi+'/'+id, 
				success: function(retour){
					$("#vote-article-"+id).hide();
					$("#vote-article-"+id).html(retour);
					$("#vote-article-"+id).fadeIn('slow');
				}
			});
		}
	});
	
	var options = { 
        target:        '#proposer_lien_retour',   // target element(s) to be updated with server response 
       // beforeSubmit:  showRequest,  // pre-submit callback 
       // success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#proposer_lien').ajaxForm(options); 
	
	var options2 = { 
        target:        '#proposer_email_retour',   // target element(s) to be updated with server response 
       // beforeSubmit:  showRequest,  // pre-submit callback 
       // success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	  $('#proposer_email').ajaxForm(options2); 
});