DRS = {
	root: "/",
	
	redirectComic: function(loc) {
		location.href=loc;
	}
}

DRS.KIT = {
	addBindings: function() {
		jQuery('#ftr_email').find('.eu_btn').bind('click',function() {
			DRS.KIT.checkForm('#ftr_email');
		});
	},
	
	removeBindings: function() {
		jQuery('#ftr_email').find('.eu_btn').unbind('click',function() {});
	},
	
	checkForm: function(theForm) {
		var oktosubmit = false;
		var fieldsNotEmpty = false;
		jQuery('.error').css('display','none');
		var fields = jQuery(theForm).serializeArray();
		jQuery.each(fields, function(i, field) {
			if (field.value == "") {
				jQuery('.error').text("Please fill out all fields.");
				jQuery('.error').css('display','block');
				jQuery('#name').focus();
				fieldsNotEmpty = false;
				return false;
			} else {
				fieldsNotEmpty = true;
			}
		});
		
		if (fieldsNotEmpty) {
			//Check Name
			var n = DRS.KIT.checkName(fields[0]);
			if (!n) { return false; }
			//Check Email Address
			var e = DRS.KIT.checkEmail(fields[1]);
			if (!e) { return false; }
			//Clean Message
			var cm = DRS.KIT.cleanMsg(fields[2]);
			if (!cm) { return false; }
			if (n && e && cm) {
				oktosubmit = true;
			} else {
				oktosubmit = false;
			}
		}
		if(oktosubmit) {
			DRS.KIT.submitForm('#ftr_email');
		}
	},
	
	checkName: function(n) {
		var person_name = n.value;           
		var name_regex = /[A-Za-z]/ig;           
		if(!name_regex.test(person_name)){
			DRS.KIT.displayMsg("Name must contain letters only.");
			jQuery('#name').focus();
			return false;
		} else {
			return true;
		}
	},
	
	checkEmail: function(e) {
		var email_address = e.value;           
		var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;           
		if(!email_regex.test(email_address)){
			DRS.KIT.displayMsg("Email address is invalid.");
			jQuery('#email').focus();
			return false;
		} else {
			return true;
		}
	},
	
	cleanMsg: function(m) {
		var msg = m.value;           
		var msg_regex = /[><]/;
		var msg_search = msg.search(msg_regex);
		if(msg_search != -1){
			DRS.KIT.displayMsg("Message must be plain text only.");
			jQuery('#msg').focus();
			return false;
		} else {
			return true;
		}
	},
	
	displayMsg: function(msg) {
		jQuery('.error').text(msg);
		jQuery('.error').css('display','block');
	},
	
	tyMsg: function(msg) {
		jQuery('.error').text(msg);
		jQuery('.error').css('display','block');
		jQuery('#name').val("");
		jQuery('#email').val("");
		jQuery('#msg').val("");
	},
	
	submitForm: function(theForm) {
		var formdata = jQuery(theForm).serialize();
		$.ajax({
			type: 'POST',
			url: DRS.root+'kit/kit.php',
			dataType : 'json',
			data: formdata,
			success: function(data) {
				DRS.KIT.tyMsg(data.message);
			},
			error: function(data) {
				DRS.KIT.displayMsg(data.message);
				alert(data.message);
			}
		});
	},
	
	grabToken: function() {
		jQuery.ajax({
		  url: DRS.root+"kit/token.php",
		  success: function(data) {
			jQuery("#ftr_email").append('<input type="hidden" name="ts" value="'+data+'" />');
		  }
		});
	},
	
	init: function() {
		jQuery('.warning').remove();
		jQuery('.error').css('display','none');
		DRS.KIT.grabToken();
		DRS.KIT.removeBindings();
		DRS.KIT.addBindings();		
	}
}

DRS.SWAPNAV = {
	//Comic listing
	curCLNav: "#comics_all_nav",
	prevCLNav: "",
	curCLDiv: "#comics_all",
	prevCLDiv: "",

	//Gallery listing
	curGLNav: "#all_nav",
	prevGLNav: "",
	curGLDiv: "#all",
	prevGLDiv: "",

	swapCLNav: function(elem) {
		var selDiv = "#" + elem;
		var selNav = selDiv + "_nav";
		if (selDiv != DRS.SWAPNAV.curCLDiv) {
			jQuery(selNav).addClass("selected");
			jQuery(DRS.SWAPNAV.curCLNav).removeClass("selected");
			jQuery(DRS.SWAPNAV.curCLDiv).removeClass("selectedCat");
			jQuery(selDiv).addClass("selectedCat");
			DRS.SWAPNAV.prevCLDiv = DRS.SWAPNAV.curCLDiv;
			DRS.SWAPNAV.prevCLNav = DRS.SWAPNAV.curCLNav;
			DRS.SWAPNAV.curCLDiv = selDiv;
			DRS.SWAPNAV.curCLNav = selNav;	
		}
	},

	//Gallery
	swapGLNav: function(elem) {
		var selDiv = "#" + elem;
		var selNav = selDiv + "_nav";
		if (selDiv != DRS.SWAPNAV.curGLDiv) {
			jQuery(selNav).addClass("selected");
			jQuery(DRS.SWAPNAV.curGLNav).removeClass("selected");	
			jQuery(DRS.SWAPNAV.curGLDiv).removeClass("selectedCat");
			jQuery(selDiv).addClass("selectedCat");
			DRS.SWAPNAV.prevGLDiv = DRS.SWAPNAV.curGLDiv;
			DRS.SWAPNAV.prevGLNav = DRS.SWAPNAV.curGLNav;
			DRS.SWAPNAV.curGLDiv = selDiv;
			DRS.SWAPNAV.curGLNav = selNav;	
		}
	}
}

DRS.AUTOLIGHTBOX = {
	// Automagically load Lightbox on Page Load - by Bramus! (http://www.bram.us/)
	// Code modded from http://www.huddletogether.com/forum/comments.php?DiscussionID=1269&page=1#Item_0
	autoFireLightbox: function() {
		//Check if location.hash matches a lightbox-anchor. If so, trigger popup of image.
		setTimeout(function() {
				var urlStr = jQuery("#"+document.location.hash.substr(1)).attr('rel');
				if(document.location.hash && urlStr.indexOf('lightbox')!=-1) {
					jQuery('#'+document.location.hash.substr(1)).trigger('click');
				}
			}, 250);
	}
}

DRS.BANNERCAROUSEL = {
	autoplay: true,
	speed: 5000,
	paused: false,
	reset: false,
	timer: 0,
	windowsize: 0,
	carouselcontainersize: 0,
	currentDiv: 1,
	totalDivs: 0,
	scrollamt: 0,
	WinPort: {
		newDivWidthSize: "",
		maxDivWidth: 0,
		maxDivWidthPixels: ""
	},
	init: function() {
		DRS.BANNERCAROUSEL.removeBinders();
		DRS.BANNERCAROUSEL.addBinders();
		DRS.BANNERCAROUSEL.update();
		if(DRS.BANNERCAROUSEL.autoplay) {
			DRS.BANNERCAROUSEL.play();
		}
	},
	removeBinders: function() {
		jQuery('#banner_carousel').unbind('resize',function(){
			DRS.BANNERCAROUSEL.watch();
		});
		if(DRS.BANNERCAROUSEL.autoplay) {
			jQuery(window).unbind('focus',DRS.BANNERCAROUSEL.play);
			jQuery(window).unbind('blur',DRS.BANNERCAROUSEL.pause);
			jQuery('#banner_carousel').unbind('mouseenter mouseleave');
		}
	},
	addBinders: function() {
		if(DRS.BANNERCAROUSEL.autoplay) {
			jQuery(window).bind('focus',DRS.BANNERCAROUSEL.play);
			jQuery(window).bind('blur',DRS.BANNERCAROUSEL.pause);
			jQuery('#banner_carousel').hover(DRS.BANNERCAROUSEL.pause,DRS.BANNERCAROUSEL.play);
		}
		jQuery('#banner_carousel').bind('resize',function() {
			DRS.BANNERCAROUSEL.watch()
		});
	},
	watch: function() {
		if(DRS.BANNERCAROUSEL.autoplay) {
			DRS.BANNERCAROUSEL.pause();
		}
		jQuery(document).bind('sizeupdated',function(e){DRS.BANNERCAROUSEL.continueslides()});
		DRS.BANNERCAROUSEL.sizeupdate();
	},
	continueslides: function() {
		jQuery(document).unbind('sizeupdated',function(e){DRS.BANNERCAROUSEL.continueslides()});
		if(DRS.BANNERCAROUSEL.autoplay) {
			DRS.BANNERCAROUSEL.play();
		}
	},
	sizeupdate: function() {		
		DRS.BANNERCAROUSEL.update();
		DRS.BANNERCAROUSEL.readjust();
	},
	readjust: function() {
		var curPos = (DRS.BANNERCAROUSEL.currentDiv != 1) ? '-'+(DRS.BANNERCAROUSEL.scrollamt * (DRS.BANNERCAROUSEL.currentDiv-1))+'px' : '0px';
		jQuery('#banner_carousel_content').css('left',curPos);		
		jQuery(document).trigger("sizeupdated");
	},
	update: function() {
		DRS.BANNERCAROUSEL.totalDivs = jQuery('#banner_carousel_content div').length;
		var divWidth = jQuery('#banner_carousel_content div').width();
		DRS.BANNERCAROUSEL.scrollamt = divWidth;
		DRS.BANNERCAROUSEL.WinPort.newDivWidthSize = jQuery('#banner_carousel_content div').width() + "px";
		DRS.BANNERCAROUSEL.WinPort.maxDivWidth = jQuery('#banner_carousel_content').innerWidth();
		DRS.BANNERCAROUSEL.WinPort.maxDivWidthPixels = "-"+DRS.BANNERCAROUSEL.WinPort.maxDivWidth+"px";
		DRS.BANNERCAROUSEL.carouselcontainersize = divWidth * DRS.BANNERCAROUSEL.totalDivs;
		jQuery('#banner_carousel_content').width(DRS.BANNERCAROUSEL.carouselcontainersize);
	},
	play: function() {
		DRS.BANNERCAROUSEL.paused = false;
		DRS.BANNERCAROUSEL.startTimer();
	},
	pause: function() {
		DRS.BANNERCAROUSEL.paused = true;		
		DRS.BANNERCAROUSEL.clearTimer();
	},	
	rewind: function() {
		DRS.BANNERCAROUSEL.pause();
		DRS.BANNERCAROUSEL.reset = true;		
		jQuery('#banner_carousel_content').delay(DRS.BANNERCAROUSEL.speed).animate( {
			left: '0'
		}, 500, function() { 
			DRS.BANNERCAROUSEL.currentDiv = 1;			
			jQuery("#slide4").removeClass("selected").addClass("notselected");
			jQuery("#slide1").removeClass("notselected").addClass("selected");
			DRS.BANNERCAROUSEL.pause();
			DRS.BANNERCAROUSEL.play();
		});
	},
	startTimer: function() {
		if(DRS.BANNERCAROUSEL.timer == 0) {
			DRS.BANNERCAROUSEL.timer = setTimeout(DRS.BANNERCAROUSEL.scrollpanel, DRS.BANNERCAROUSEL.speed);
		}
	},	
	clearTimer: function() {
		try {
			window.clearTimeout(DRS.BANNERCAROUSEL.timer);
			DRS.BANNERCAROUSEL.timer = 0;
		} catch(e) {}
	},
	scrollpanel: function() {
		try {
			DRS.BANNERCAROUSEL.clearTimer(); 
			if(!DRS.BANNERCAROUSEL.reset && !DRS.BANNERCAROUSEL.paused) {
				DRS.BANNERCAROUSEL.slidePort();
				DRS.BANNERCAROUSEL.startTimer();
			} else {
				if (DRS.BANNERCAROUSEL.timer == 0) {
					DRS.BANNERCAROUSEL.reset = false;
					DRS.BANNERCAROUSEL.scrollpanel();	
				}					
			}
		} catch(e) {}
	},
	slidePort: function() {
		//Do total instead of max container size...
		if (DRS.BANNERCAROUSEL.currentDiv != DRS.BANNERCAROUSEL.totalDivs) {
			var amtpx = '-='+DRS.BANNERCAROUSEL.scrollamt+'px';
			jQuery('#banner_carousel_content').animate( {
				left: amtpx
			}, 500, function() { 
				DRS.BANNERCAROUSEL.currentDiv++;
				var curSlide = "#slide"+DRS.BANNERCAROUSEL.currentDiv;
				var prevSlide = ((DRS.BANNERCAROUSEL.currentDiv - 1) < 1) ? "#slide1" : "#slide"+(DRS.BANNERCAROUSEL.currentDiv - 1);
				jQuery(prevSlide).removeClass("selected").addClass("notselected");
				jQuery(curSlide).removeClass("notselected").addClass("selected");
			});			
		} else {
			DRS.BANNERCAROUSEL.clearTimer();
			DRS.BANNERCAROUSEL.rewind();
		}
	},
}
