// constructor
function RateMe (args) {
    // constants / pre-set vars
    this.post_uri    = args.post_uri    ? args.post_uri    : "rateme.pl";
    this.yes_on_src  = args.yes_on_src  ? args.yes_on_src  : "thumbs_up.png";
    this.yes_off_src = args.yes_off_src ? args.yes_off_src : "thumbs_up.png";
    this.no_on_src   = args.no_on_src   ? args.no_on_src   : "thumbs_down.png";
    this.no_off_src  = args.no_off_src  ? args.no_off_src  : "thumbs_down.png";
    
    this.timeout = args.timeout ? args.timeout : 10000;

    this.container_id  = args.container_id  ? args.container_id  : "rateme";
    this.thumbs_div_id = args.thumbs_div_id ? args.thumbs_div_id : "thumbs";
    this.thanks_div_id = args.thanks_div_id ? args.thanks_div_id : "thanks";
    
    this.thumbs_up_div_id   = args.thumbs_up_div_id   ? args.thumbs_up_div_id   : "thumbs_up";
    this.thumbs_down_div_id = args.thumbs_down_div_id ? args.thumbs_down_div_id : "thumbs_down";
    this.thanks_img_id      = args.thanks_img_id      ? args.thanks_img_id      : "thanks_img";
    this.img_class          = args.img_class          ? args.img_class          : "thumbs_img";

    // method calls
    this.init            = init;
    this.display_thumbs  = display_thumbs;
    this.display_thanks  = display_thanks;
    this.register_rating = register_rating;
    this.create_image    = create_image;
    
    this.img_yes_on  = this.create_image(this.yes_on_src,  this.img_class, "i like this", "i like this");
    this.img_yes_off = this.create_image(this.yes_off_src, this.img_class, "", "");
    this.img_no_on   = this.create_image(this.no_on_src,   this.img_class, "i do not like this", "i do not like this");
    this.img_no_off  = this.create_image(this.no_off_src,  this.img_class, "", "");
    
    var href = window.location.href;
    var uri  = href.substr(0, href.indexOf("?"));
    this.uri = href;

    var $rateme = document.getElementById(this.container_id);
    if ($rateme) {
        this.init($rateme);
    }
}

function create_image(src, elem_class, title, alt) {
    var img = document.createElement('img');
    if (elem_class != null) {
        img.className = elem_class;
    }
    img.src = src;
    if (title != null) {
        img.title = title;
    }
    if (alt != null) {
        img.alt = alt;
    }

    return img;
}

function init(container) {
    var $thumbs      = document.getElementById(this.thumbs_div_id);
    var $thanks      = document.getElementById(this.thanks_div_id);
    var $thumbs_up   = document.getElementById(this.thumbs_up_div_id);
    var $thumbs_down = document.getElementById(this.thumbs_down_div_id);
    
    this.containers = {};
    
    this.containers.container = container;
    this.containers.thumbs    = $thumbs;
    this.containers.thanks    = $thanks;
    
    $thumbs_up.innerHTML   = "";
    $thumbs_down.innerHTML = "";
    
    $thumbs_up.appendChild(this.img_yes_off);
    $thumbs_down.appendChild(this.img_no_off);
    
    var $rateme = this;
    $thumbs_up.onclick   = function() { $rateme.register_rating(true);  $rateme.display_thanks(true); };
    $thumbs_down.onclick = function() { $rateme.register_rating(false); $rateme.display_thanks(false); };
    
    this.display_thumbs();
    this.containers.container.style.display = "block";

    $rateme.img_yes_off.onmouseover = function() { $thumbs_up.innerHTML = ""; $thumbs_up.appendChild($rateme.img_yes_on) };
    $rateme.img_yes_on.onmouseout   = function() { $thumbs_up.innerHTML = ""; $thumbs_up.appendChild($rateme.img_yes_off) };

    $rateme.img_no_off.onmouseover = function() { $thumbs_down.innerHTML = ""; $thumbs_down.appendChild($rateme.img_no_on) };
    $rateme.img_no_on.onmouseout   = function() { $thumbs_down.innerHTML = ""; $thumbs_down.appendChild($rateme.img_no_off) };
}

function register_rating(vote) {
    var ajax   = new AJAX();
    var params = "vote=" + (vote ? 1 : 0) + "&uri=" + this.uri;
    
    ajax.action = this.post_uri;
    ajax.params = params;

    ajax.response_func = function() {
        this.destroy();
    };
    ajax.error_func = function() {
        this.destroy();
    };
    
    ajax.async_request();

}

function display_thumbs () {
    var $thumb_style = this.containers.thumbs.style;
    $thumb_style.display = "block";
    
    var $thanks_style = this.containers.thanks.style;
    $thanks_style.display = "none";
}

function display_thanks(vote) {
    var $thanks_img_div = document.getElementById(this.thanks_img_id);
    $thanks_img_div.innerHTML = "";
    $thanks_img_div.appendChild(vote ? this.img_yes_on.cloneNode(false) : this.img_no_on.cloneNode(false));

    var $thumb_style = this.containers.thumbs.style;
    $thumb_style.display = "none";
    
    var $thanks_style = this.containers.thanks.style;
    $thanks_style.display = "block";

    var $rateme = this;
    var reload_display = function() { $rateme.display_thumbs(); };

    setTimeout(reload_display, this.timeout);
}
