var eco_friendly_jq = 0
var eco_friendly_checkbox_jq = 0

$(document).ready(function() {
	removeSeoObjects();
 
    eco_friendly_jq = $('#eco_friendly_label')
    eco_friendly_checkbox_jq = $('#eco_friendly_checkbox')

    eco_friendly_jq.hover(function() {
        if (!eco_friendly_checkbox_jq.attr('checked')) {
            eco_friendly_jq.addClass('active')
        }
    }, function() {
        if (!eco_friendly_checkbox_jq.attr('checked')) {
            eco_friendly_jq.removeClass('active')
        }
    })

    eco_friendly_checkbox_jq.hover(function() {
        if (!eco_friendly_checkbox_jq.attr('checked')) {
            eco_friendly_jq.addClass('active')
        }
    }, function() {
        if (!eco_friendly_checkbox_jq.attr('checked')) {
            eco_friendly_jq.removeClass('active')
        }
    })

    eco_friendly_jq.click(handle_eco_friendly_click)
    eco_friendly_checkbox_jq.change(handle_eco_friendly_change)

	function removeSeoObjects() {
		$('body > h1:first').remove();	
	}
})

function handle_eco_friendly_click() {
    if (eco_friendly_checkbox_jq.attr('checked')) {
        $.get('/rpc_toggle_ecofriendly/0/', function() {
            eco_friendly_checkbox_jq.attr('checked', false)
            if ($('body').hasClass('productlisting')) {
                window.location.reload()
            }
        })
    } else {
        $.get('/rpc_toggle_ecofriendly/1/', function() {
            eco_friendly_checkbox_jq.attr('checked', true)
            if ($('body').hasClass('productlisting')) {
                window.location.reload()
            }
        })
    }
}

/* FIXME: This is stupid, the only difference between this and
          handle_eco_friendly_click is the if/else */
function handle_eco_friendly_change() {
    if (eco_friendly_checkbox_jq.attr('checked')) {
        $.get('/rpc_toggle_ecofriendly/1/', function() {
            eco_friendly_checkbox_jq.attr('checked', true)
            if ($('body').hasClass('productlisting')) {
                window.location.reload()
            }
        })
    } else {
        $.get('/rpc_toggle_ecofriendly/0/', function() {
            eco_friendly_checkbox_jq.attr('checked', false)
            if ($('body').hasClass('productlisting')) {
                window.location.reload()
            }
        })
    }
}

