function watchit(ing){
    /*alert($('#watching').html())*/
    var url = '/badthings/ingredients/watch/'+escape(ing)+'/';
    $("#watching").load( url, {}, function(){
                               $("#ajax-message-msg").html('You are now watching '+ing);
                               $("#ajax-message").show();
                               $("#ajax-message").fadeOut(5000);});
    return false;
}

function unwatchit(ing){
    /*alert($('#watching').html())*/
    var url = '/badthings/ingredients/unwatch/'+escape(ing)+'/';
    $("#watching").load( url, {}, function(){
                               $("#ajax-message-msg").html('You are no longer watching '+ing);
                               $("#ajax-message").show();
                               $("#ajax-message").fadeOut(5000);});
    return false;
}

function listedcheck(term,type){
    var url="/check/"+escape(type)+"/"+escape(term)+"/";
    $("#check-listed").load( url, {}, function(){ });
//    alert(term);
    return true;
}

function listedcheck_in_gen(term,gen){
    var url="/check/products/"+escape(term)+":"+escape(gen)+"/";
    $("#check-listed").load( url, {}, function(){ });
    return true;
}

function searchit(term,type){
    var url="/search/"+escape(type)+"/"+escape(term)+"/";
    $("#results").load( url, {}, function(){
                $("#search-term").val("");
                $("#search-options").show();
                $("#wait").hide();
                });
    $.scrollTo("#results"); 
    $("#search-options").hide();
    $("#wait").show();
    return false;
}

function searchclear(box){
    var text = $(box).val();
    if (text == "Search term"){
        $(box).val("");
        $(box).css("color","black");
    }
}


function checkEnter(e){ //e is event object passed from function invocation
    var characterCode //literal character code will be stored in this variable
    
    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which //character code is contained in NN4's which property
    }
    else{
        e = event
        characterCode = e.keyCode //character code is contained in IE's keyCode property
    }
    
    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        searchit($('#search-term').val(),$("input[@name='search_type']:checked").val());
        return false;
    }
    else{
        return true;
    }

}

function showprods(type){
    
    $(".prodhide").hide()
    $("#"+type).show()
    $(".prodcat").css("background-color","#dfc");
    $("#"+type+"i").css("background-color","#ad9");
    }
    
    
function percentof(value,arg) {
    if (arg == 0){
        return 100;
    }
    if (value < 0){
        return 0;
    }
    return (value/arg*100);
}

function vote(name, direction) {
    $.post('/badthings/ingredients/'+direction+'vote/'+escape(name)+'/', {HTTP_X_REQUESTED:'XMLHttpRequest'},
           function(data) {
               if (data.success == true) {
                   $('#score').text(percentof(data.score.score,data.score.num_votes));
                   $('#num_votes').text(data.score.num_votes);
                   if (direction == "up"){
                        $('#vdn').attr('src','/site_media/icons/votedn.png').attr("title","Vote: Its Not Bad!");
                        $('#vup').attr('src','/site_media/icons/votedup.png').attr("title","You Voted: Its Bad!");
                        $('#vcl').attr('src','/site_media/icons/clear.png').attr("title","Clear Vote");
                    }
                    if (direction == "down"){
                        $('#vup').attr('src','/site_media/icons/voteup.png').attr("title","Vote: Its Bad!")
                        $('#vdn').attr('src','/site_media/icons/voteddn.png').attr("title","You Voted: Its Not Bad!");
                        $('#vcl').attr('src','/site_media/icons/clear.png').attr("title","Clear Vote");
                    }
                    if (direction == "clear"){
                        $('#vup').attr('src','/site_media/icons/voteup.png').attr("title","Vote: Its Bad!");
                        $('#vdn').attr('src','/site_media/icons/votedn.png').attr("title","Vote: Its Not Bad!");
                        $('#vcl').attr('src','/site_media/icons/cleared.png').attr("title","You cleared your vote!");
                    }
               } else {
                   if (data.error_message == "Not authenticated."){
                        window.location="/signup/";
                    } else {
                       alert('ERROR: ' + data.error_message);
                    }
               }
           }, 'json'
          );
    return false;
}

function resvote(id, direction) {
    $.post('/badthings/research/'+direction+'vote/'+escape(id)+'/', {HTTP_X_REQUESTED:'XMLHttpRequest'},
           function(data) {
               if (data.success == true) {
                   $('#'+id+'score').text(percentof(data.score.score,data.score.num_votes));
                   $('#'+id+'num_votes').text(data.score.num_votes);
               } else {
                   if (data.error_message == "Not authenticated."){
                        window.location="/signup/";
                    } else {
                       alert('ERROR: ' + data.error_message);
                    }
               }
           }, 'json'
          );
    return false;
}

function switchtab(tab){
    if (tab == "news"){
        other = "watchlist";
    } else {
        other = "news";
    }
    $('#tab-'+tab).css("background-color","#cec");
    $('#tab-'+tab).css("color","#555");
    $('#tab-'+other).css("background-color","#555");
    $('#tab-'+other).css("color","#fff");
    $('#tabpage-'+tab).show()
    $('#tabpage-'+other).hide()
}