/* The following is the code for the Subscribe to Cectic popup.
Thank you to ComicPress for the subscribe button template:
http://mindfaucet.com/comicpress/2007/05/22/subscribe-buttons/ */

var subscribeTimeout = null;

function getLeftOffset(objid){
	var offset = 0;
	var obj = document.getElementById(objid);
	if(obj.offsetParent){
		offset = obj.offsetLeft;
		while(obj = obj.offsetParent){
			offset += obj.offsetLeft;
		}
	}
	return offset;
}

function loadSubscribe(){
	d = new Date();
	rval = d.getYear() + '-' + d.getMonth() + '-' + d.getDate();
	try {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	} catch(e) {
		try {
			xmlDoc = document.implementation.createDocument("","",null);
		} catch(e){
			return;
		}
	}
	try {
		xmlDoc.async = false;
		xmlDoc.load('subscribe.dat?'+rval);
	} catch(e){
		subscribehttp = new XMLHttpRequest();
		if(subscribehttp){
			subscribehttp.onreadystatechange = function(){
				if(subscribehttp.readyState == 4){
					xmlDoc = subscribehttp.responseXML;
				}
			};
			subscribehttp.open("GET",'http://cectic.com/subscribe.dat?'+rval,false);
			subscribehttp.overrideMimeType('text/xml');
			subscribehttp.send(null);
		}
	}
	document.getElementById('subscribe').innerHTML = xmlDoc.getElementsByTagName('data')[0].childNodes[0].nodeValue;
}

function subscribe(){
	if(document.getElementById){
		if(document.getElementById('subscribe').innerHTML == ''){
			loadSubscribe();
		}
		clearTimeout(subscribeTimeout);
		document.getElementById('subscribe').style.left = (getLeftOffset('feedlink') - 18) + 'px';
		document.getElementById('subscribe').style.display = 'block';
		document.getElementById('subscribe').style.visibility = 'visible';
	} else {
		return;
	}
}

function subscribeClose(){
	if(document.getElementById){
		subscribeTimeout = setTimeout("hideSubscribe()", 500);
	} else {
		return;
	}
}

function hideSubscribe(){
	document.getElementById('subscribe').style.display = 'none';
	document.getElementById('subscribe').style.visibility = 'hidden';
}

/* The following is the code for the comic archive scroller.
Sorry for not commenting. That's just how I roll, sucka. */

function dragger() {
	this.target = null;
	this.targetWidth = 0;
	this.mouseX = 0;
	this.offsetX = 0;
	this.mouseDown = function(e) {
		if(e == null) e = window.event;
		this.target = e.target != null ? e.target : e.srcElement;
		if(this.target.id == 'nav_content' && ((window.event != null && e.button == 1) || e.button == 0)){
			this.mouseX = e.clientX;
			this.offsetX = this.target.offsetLeft;
			this.targetWidth = this.target.scrollWidth;
			document.onmousemove = function(e) {
				if(e == null) e = window.event;
				var newleft = this.offsetX + e.clientX - this.mouseX;
				if(newleft < 740 - this.targetWidth) newleft = 740 - this.targetWidth;
				if(newleft > 0) newleft = 0;
				this.target.style.left = newleft + 'px';
			}
			document.body.focus();
			document.onselectstart = function(){ return false; };
			return false;
		}
	}
	this.mouseUp = function(e){
		document.onmousemove = null;
		document.onselectstart = null;
		archiveStrip.loadThumbs();
	}
	this.scrollStart = function(){
		document.getElementById('nav_content').style.left = '0px';
		this.loadThumbs();
	}
	this.scrollEnd = function(){
		var newleft = 740 - document.getElementById('nav_content').scrollWidth;
		document.getElementById('nav_content').style.left = newleft + 'px';
		this.loadThumbs();
	}
	this.scrollCurrent = function(uri){
		uri = unescape(uri);
		var file = uri.substring(uri.lastIndexOf('/') + 1);
		var comic = file.substring(0, file.indexOf('.'));
		document.getElementById('nav_comic_'+comic).className = 'nav_comic_current';
		var newleft = parseInt(comic, 10) * -118 + 418;
		if(newleft < 740 - document.getElementById('nav_content').scrollWidth) newleft = 740 - document.getElementById('nav_content').scrollWidth;
		if(newleft > 0) newleft = 0;
		document.getElementById('nav_content').style.left = newleft + 'px';
		this.loadThumbs();
	}
	this.loadArchive = function(navid){
		try { //internet explorer
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		} catch(e) { //firefox, opera
			xmlDoc = document.implementation.createDocument("","",null);
		}
		try { //internet explorer, firefox, opera
			xmlDoc.async = false;
			xmlDoc.load('scroller.dat');
		} catch(e) { //safari
			scrollerhttp = new XMLHttpRequest();
			if(scrollerhttp){
				scrollerhttp.onreadystatechange = function(){
					if(scrollerhttp.readyState == 4){
						xmlDoc = scrollerhttp.responseXML;
					}
				};
				scrollerhttp.open("GET",'http://cectic.com/scroller.dat',false);
				scrollerhttp.overrideMimeType('text/xml');
				scrollerhttp.send(null);
			}
		}
		document.getElementById(navid).innerHTML = xmlDoc.getElementsByTagName('data')[0].childNodes[0].nodeValue;
	}
	this.loadThumbs = function(){
		var curleft = parseInt(document.getElementById('nav_content').style.left);
		var startcomic = Math.ceil((curleft - 1) / -118) - 7;
		for(i = startcomic; i < startcomic + 21; i++){
			var tag = i;
			if(i < 10){
				tag = '00'+tag;
			} else if(i < 100){
				tag = '0'+tag;
			}
			var navc = document.getElementById('nav_comic_'+tag);
			if(navc && navc.innerHTML == '') navc.innerHTML = '<a href="'+tag+'.html"><img alt="'+tag+'" src="thumbs/'+tag+'.png" /></a>';
		}
	}
}

var archiveStrip = null;
// old init function that forced archive mode, no longer used
/*function initArchiveScroll() {
	try {
		document.getElementById('nav_static').style.display = 'none';
		document.getElementById('nav_static_links').style.display = 'none';
		document.getElementById('nav_archive').style.display = 'block';
		document.getElementById('nav_links').style.display = 'block';
		if(document.getElementById('navmode_modeselect'))
			document.getElementById('navmode_modeselect').style.display = 'inline';
		archiveStrip = new dragger();
		archiveStrip.loadArchive('nav_archive');
		archiveStrip.scrollCurrent(addthis_url);
		document.onmousedown = archiveStrip.mouseDown;
		document.onmouseup = archiveStrip.mouseUp;
	} catch(e) {
		document.getElementById('nav_archive').style.display = 'none';
		document.getElementById('nav_links').style.display = 'none';
		document.getElementById('nav_static').style.display = 'block';
		document.getElementById('nav_static_links').style.display = 'block';
	}
}*/

function initNavbar(){
	try {
		document.getElementById('navmode_modeselect').style.display = 'inline';
		archiveStrip = new dragger();
		archiveStrip.loadArchive('nav_archive');
		document.onmousedown = archiveStrip.mouseDown;
		document.onmouseup = archiveStrip.mouseUp;
		if(getCookie('navmode') == 'fancy')
			setMode(1);
	} catch(e) {
		document.getElementById('nav_archive').style.display = 'none';
		document.getElementById('nav_links').style.display = 'none';
		document.getElementById('navmode_modeselect').style.display = 'none';
		document.getElementById('nav_static').style.display = 'block';
		document.getElementById('nav_static_links').style.display = 'block';
	}
}

function setMode(which){
	if(which == 0){
		document.getElementById('nav_archive').style.display = 'none';
		document.getElementById('nav_links').style.display = 'none';
		document.getElementById('nav_static').style.display = 'block';
		document.getElementById('nav_static_links').style.display = 'block';
		setCookie('navmode','classic',9999);
	} else if(which == 1){
		document.getElementById('nav_static').style.display = 'none';
		document.getElementById('nav_static_links').style.display = 'none';
		document.getElementById('nav_archive').style.display = 'block';
		document.getElementById('nav_links').style.display = 'block';
		archiveStrip.scrollCurrent(addthis_url);
		setCookie('navmode','fancy',9999);
	}
}

// cookie code borrowed from http://www.w3schools.com/JS/js_cookies.asp
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}
