/*
JavaScript Bookmarking Script
=============================
Developed originally for easy reading of webcomics :D
Feel free to use this script in anyway you see fit,
as far as I am concerned and aware the below code
is copyright free.

Script Author: Ash Young
Email: ash@evoluted.net
Website: http://evoluted.net/

Default Graphics: Pontus Madsen
Website: www.little-gamers.com

Modified by Ben Chamberlain, smbhax.com
*/

/*
Config Settings
*/
var cl = 31;
var imgTagOn = 'http://smbhax.com/images/ct1.png';       //add tag image.
var imgTagOff = 'http://smbhax.com/images/ct1a.png';       //add tag image.
var imgClearOff = 'http://smbhax.com/images/ct3a.png'; //no comic tagged, clear not possible
var imgGotoOff = 'http://smbhax.com/images/ct2a.png';  //no comic tagged, goto not possible
var imgClearOn = 'http://smbhax.com/images/ct3.png';   //clear a tag, shows when comic previously tagged
var imgGotoOn = 'http://smbhax.com/images/ct2.png';    //shows when a comic is tagged
var imgInfo = 'http://smbhax.com/images/ct4.png';      //img that displays the help
var comicDir = '/';         //alter this if you run multiple comics in different directories on your site.

/*
Now write out the applicable links
*/
var loc = window.location.href;
if(document.getElementById && loc.indexOf('smbhax') !=-1) {
    var l = readCookie('bm');
    var gt = imgGotoOff;
    var ct = imgClearOff;
    var bt = imgTagOn;
    if(l) {
        gt = imgGotoOn;
        ct = imgClearOn;
    }
    if(l && (l == window.location || l == window.durl)) {
        bt = imgTagOff;
        gt = imgGotoOff;
    }
    document.write('<a href="#" onClick="bm();return false;"><img src="'+bt+'" alt="Bookmark this comic" id="btc" border="0" width="60" height="15"></a>');
    document.write('<a href="#" onClick="gto();return false;"><img src="'+gt+'" alt="Goto bookmark" border="0" id="gtc" width="35" height="15"></a>');
    document.write('<a href="#" onClick="bmc();return false;"><img src="'+ct+'" alt="Clear bookmark" border="0" id="rmc" width="39" height="15"></a>');
    document.write('<a href="#" onClick="document.getElementById(\'bmh\').style.visibility=\'visible\';return false;"><img src="'+imgInfo+'" alt="" border="0" width="16" height="15"></a>');
    document.write('<div id="bmh" onClick="document.getElementById(\'bmh\').style.visibility=\'hidden\';" style="padding:4px; font-size:small;margin-left:-150px;margin-top:0px;font-family:Trebuchet MS, Trebuchet, Arial, Helvetica, sans-serif;position:absolute;width:600px;color:#999999;background-color:#333333;border: 1px solid #555555;visibility:hidden;text-align:left;">The <b>bookmark</b> button saves your place. When you come back to the site, click <b>goto</b> from any page to return to your bookmark and continue reading where you left off.</div>');
}

/*
Below are our functions for this little script
*/
function bm() {
    if(document.getElementById) {
        document.getElementById('rmc').src = imgClearOn;
        document.getElementById('gtc').src = imgGotoOff;
        document.getElementById('btc').src = imgTagOff;
    }
    if(window.durl) {
        createCookie("bm", durl, cl);
    } else createCookie("bm", window.location, cl);
}

function bmc() {
    if(document.getElementById) {
        document.getElementById('gtc').src = imgGotoOff;
        document.getElementById('rmc').src = imgClearOff;
        document.getElementById('btc').src = imgTagOn;

    }
    createCookie("bm","",-1);
}

function gto() {
    var g = readCookie('bm');
    if(g && (g != window.location)) {
        window.location = g;
    }
}

/*
The follow functions have been borrowed from Peter-Paul Koch.
Please find them here: http://www.quirksmode.org
*/
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else var expires = "";
    document.cookie = name+"="+value+expires+"; path="+comicDir;
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
