/*
	
	Hand Crafted by Nevub -> [www.nevub.com || info@nevub.com]
	Copyright (c) 2007 Nevub.  All Rights Reserved.
	
*/

var pagify = {
	body : '',
	tLen : null,
	curr : 0,
	old : null,
	load : function() {
		if (!document.getElementById || !document.getElementsByTagName) return;				
		this.create();
	},
	create : function(width) {				
		var elm = document.getElementById('storybody');
		if (!elm) return;
		
		var ja  = elm.innerHTML.split(/<span class="?pagebreak"?><\/span>/i);
		this.tLen = ja.length;
		
		if (this.tLen == 1) return;
		
		for (var i = 0; i < this.tLen; i++) {
			this.body += '<div id="pge' + i + '" class="pge">';
			this.body += ja[i];
			this.body += '</div>';
		}
		
		this.body += '<div id="pge_pn"></div>';
		this.body += '<div id="pge_nav">';
		this.body  += '<a id="pge_prev" href="#" onclick="pagify.prev(); return false;">&laquo;</a> &nbsp;';
		for (var i = 0; i < this.tLen; i++) {
			this.body  += '<a id="ipge' + i + '" href="#" class="ipge" onclick="pagify.move(' + i + '); return false;">' + (i + 1) + '</a> &nbsp;';
		}
		this.body  += '<a id="pge_next" href="#" onclick="pagify.next(); return false;">&raquo;</a>';
		this.body  += '</div>';
		
		elm.innerHTML = this.body;
		this.set(0);
	},
	set : function(n) {
		this.old = this.curr;
		this.curr = n;
		
		var p1 = document.getElementById('pge' + this.old);
		if (!p1) return;
		p1.className = 'pge';
		
		var p2 = document.getElementById('pge' + n);
		if (!p2) return;
		p2.className = 'pge_on';
		
		var o1 = document.getElementById('ipge' + this.old);
		if (!o1) return;
		o1.className = 'ipge';
		
		var o2 = document.getElementById('ipge' + n);
		if (!o2) return;
		o2.className = 'ipge_on';
		
		this.set_np(n);
	},
	set_np : function(n) {
		var pn = document.getElementById('pge_pn');
		if (!pn) return;
		if (n == 0) {
			pn.innerHTML = '<a href="#" onclick="pagify.next(); return false;" class="pge_link">Next &raquo;</a>';
		} else if (n == (this.tLen - 1)) {
			pn.innerHTML = '<a href="#" onclick="pagify.prev(); return false;" class="pge_link">&laquo; Previous</a>';
		} else {
			pn.innerHTML = '<a href="#" onclick="pagify.prev(); return false;" class="pge_link">&laquo; Previous</a> &nbsp; <a href="#" onclick="pagify.next(); return false;" class="pge_link">Next &raquo;</a>';
		}
	},
	prev : function() {
		var n = this.curr - 1;
		if (n < 0) n = (this.tLen - 1);
		this.move(n);
	},
	next : function() {
		var n = this.curr + 1;
		if (n > (this.tLen - 1)) n = 0;
		this.move(n);
	},
	move : function(n) {
		this.set(n);
	}
}

var gallery = {
	num : 0,
	curr : 0,
	old : null,
	t1 : null,
	active : 1,
	load : function() {
		var pg = document.getElementById('photogallery');
		if (!pg) return;
		this.feed = pg.getElementsByTagName('div');
		this.img = [];
		for (var i = 0; i < this.feed.length; i++) {
			this.img[i] = this.feed[i].innerHTML;
		}
		this.tLen = this.img.length;
		pg.innerHTML = '<div id="galleryNav"></div><div id="gallery"></div>';
		pg.style.display = 'block';
		this.obj = document.getElementById('gallery');
		if (!this.obj) return;
		for (var i = 0; i < this.img.length; i++) {
			this.obj.innerHTML += '<span id="photo' + i + '" style="display:none;">' + this.img[i] + '</span>';
		}
		if (this.tLen > 1) this.makeNav();
		this.setData();
	},
	makeNav : function() {
		var html = '';
		html += '<span id="playpause"></span>';
		html += '<a id="gallery_prev" href="#" onclick="gallery.doPrev();">&laquo;</a>';
		for (var i = 0; i < this.tLen; i++) {
			html += '<a id="tn' + i +'" class="gallery" href="#" onclick="gallery.doNav(' + i + '); return false;">' + i + '</a>';
		}
		html += '<a id="gallery_next" href="#" onclick="gallery.doNext();">&raquo;</a>';
		html += '';
		var nav = document.getElementById('galleryNav');
		nav.innerHTML = html;
		this.pp = document.getElementById('playpause');
	},
	setData : function() {
		clearTimeout(this.t1);
		if (this.curr > (this.tLen - 1)) {
			this.curr = 0;
		}
		if (this.curr < 0) {
			this.curr = (this.tLen - 1);
		}
		var oto = document.getElementById('photo' + this.old);
		if (oto) oto.style.display = 'none';
		var to = document.getElementById('photo' + this.curr);
		if (to) to.style.display = 'block';
		//this.obj.innerHTML = this.img[this.curr - 1];
		var oldelm = document.getElementById('tn' + this.old);
		if (oldelm) oldelm.className = 'gallery';
		var currelm = document.getElementById('tn' + this.curr);
		if (currelm) currelm.className = 'gallery_on';
		if (this.tLen > 1) {
			this.fadeIn();
			if (this.active == 1) {
				this.pp.innerHTML = '<a href="#" onclick="gallery.doPause(); return false;">Pause</a>';
			} else {
				this.pp.innerHTML = '<a href="#" onclick="gallery.doPlay(); return false;">Play</a>';
			}
		}
	},
	doPlay : function() {
		this.active = 1;
		this.fadeIn();
		this.pp.innerHTML = '<a href="#" onclick="gallery.doPause(); return false;">Pause</a>';
	},
	doPause : function() {
		clearTimeout(this.t1);
		this.active = 0;
		this.pp.innerHTML = '<a href="#" onclick="gallery.doPlay(); return false;">Play</a>';
	},
	doNav : function(v) {
		if (this.num > 0 && this.num < 100) return;
		this.active = 0;
		this.old = this.curr;
		this.curr = parseInt(v);
		this.fadeOut();	
	},
	doNext : function() {
		this.old = this.curr;
		this.curr++;
		this.fadeOut();
	},
	doPrev : function() {
		this.old = this.curr;
		this.curr--;
		this.fadeOut();
	},
	fadeIn : function() {
		if (this.num < 100) {
			this.setFade(this.num);
			this.num += 5;
			setTimeout('gallery.fadeIn()', 10);
		} else {
			this.num = 100;
			this.setFade(100);
			if (this.active == 1) {
				this.t1 = setTimeout('gallery.doNext()', 3000);
			}
		}
	},
	fadeOut : function() {
		if (this.num > 0) {
			this.setFade(this.num);
			this.num -= 5;
			setTimeout('gallery.fadeOut()', 10);
		} else {
			this.num = 0;
			this.setFade(0);
			this.setData();
		}
	},
	setFade : function(opacity) {
		this.obj.style.opacity = (opacity / 100);
		this.obj.style.MozOpacity = (opacity / 100);
		this.obj.style.KhtmlOpacity = (opacity / 100);
		this.obj.style.filter = 'alpha(opacity=' + opacity + ')';
	}
}

function popWin(url, winName, w, h, scroll) {
	var sw = screen.width;
	var sh = screen.height;
	var cx = (.5 * sw) - (w * .5);
	var cy = (.5 * sh) - (h * .5);
	var values = 'toolbar=no,status=yes,menubar=no,scrollbars=' + scroll + ',resizable=yes,screenX=' + cx + ',screenY=' + cy + ',left=' + cx + ',top=' + cy + ',width=' + w + ',height=' + h + '';
	winChild = window.open(url, winName, values);
}

window.onerror = function() {
	//return true;
}

function globalInit() {
	
}

function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent('on' + evType, fn);
		return r;
	} else {
		return false;
	}
}
addEvent(window, 'load', globalInit);
