
$(document).ready(function() {
	
	$('#primaryMenu>ul>li').hover(
		function() {
			if(this.getElementsByTagName('ul').length > 0) {
				$(this).addClass('hover');
				var position = $(this).position();
				if(position.left > 715) {
					document.getElementById('primaryMenu').className = "reverse";
				}
			}
			else {
				$(this).addClass('highlight');
			}
		},
		function() {
			$(this).removeClass('hover').removeClass('highlight');
			document.getElementById('primaryMenu').className = "";
		}
	);
	
	$('.toggle .more a').click(function(e) {
		e.preventDefault();
		this.parentNode.parentNode.className = "toggleFull";
	});
	if(document.getElementById('faqs')) {
		faqs.init();
	}
	if(document.getElementById('homeBanner')) {
		slideshow.init();
	}
	if(document.getElementById('CommentPrayer')) {
	    createCommentPrayerLimit();
	}
	if(document.getElementById('Comment')) {
	    createCommentLimit();
	}
	
});

var faqs = {
	init : function() {
		$('#faqs .short a.more').click(function(e) {
			e.preventDefault();
			$(this).parents("li").addClass('open');
		});
	}
};

// Input Focus and Blur:
// Place the inputFocus function on the onfocus event of an input element and the inputBlur on the onblur event.
// Use 'this' for the object parameter. 'css' and 'defaultValue' parameters are optional.

function inputFocus(object, css, defaultValue) {
    var val;
    if(defaultValue) val = defaultValue;
    else val = object.defaultValue;
    if(val == object.value) {
        object.value = "";
    }
    if(css) renderCSS(object, css);
}

function inputBlur(object, css, defaultValue) {
    if($.trim(object.value) == "") {
        var val;
        if(defaultValue) val = defaultValue;
        else val = object.defaultValue;
        object.value = val;
        if(css) renderCSS(object, css);
    }
}

function renderCSS(object, css) {
    var attributes = css.split(';');
    for(var x=0;x<attributes.length;x++) {
        var elem = attributes[x].split(':');
        $(object).css(elem[0],elem[1]);
    }
}


// Submit on Enter Key
// onkeypress="return enterKey(event,'objectID')"
function enterKey(event,id) {
	if(document.getElementById(id)) {
		if(!event) {
			event = window.event;
		}
		if ((event.which || event.keyCode) && ((event.which == 13) || (event.keyCode == 13))) {
			//eval(code);
			var object = document.getElementById(id);
			if(object.tagName == 'A') {
			    eval(unescape(object.href.substr(11)));
				return false;
			}
			else if(object.tagName == 'INPUT') {
			    object.click();
				return false;
			}
			else {
				return true;
			}
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function customValidationStyle(oElem, isValid) {
    if ("INPUT" == oElem.tagName || "SELECT" == oElem.tagName || "TEXTAREA" == oElem.tagName) {
        if (isValid) {
            oElem.style.background = "none";
        }
        else {
            oElem.style.background = "#EACCBA";
        }
    }
    else {
        design_validationStyle(oElem, isValid);
    }
}


var slideshow = {
	slideSpeed : 8000,
	fadeSpeed : 25,
	fadeChange : 3,
	shows : new Array(),
	init : function() {
		var self = this;
		var frame = $('.slideshow');
		for(var y=0;y<frame.length;y++) {
			this.shows[y] = {id:'slideshow',status:0,rotate:null,fade:null,number:0,previous:0,count:0,opacity:0};
			
			var currentSlide = frame[y];
			currentSlide.style.position = "relative";
			if(currentSlide.id.length < 1) {
				currentSlide.id = "slideshow"+y;
			}
			this.shows[y].id = currentSlide.id;
			
			var slides = $(currentSlide).children('.slide');
			this.shows[y].count = slides.length;
			if(slides.length > 1) {
				for(var x=0;x<slides.length;x++) {
					slides[x].style.position = "absolute";
					slides[x].style.top = "0px";
					slides[x].style.left = "0px";
					if(x > 0) {
						slides[x].style.zIndex = "100";
						slides[x].style.opacity = "0";
						slides[x].style.filter = "alpha(Opacity=0)";
					}
					else {
						slides[0].style.zIndex = "200";
					}
				}
				this.callInterval(y);
			}
		}

		$('.slideNext').click(function() {
			if(this.rel.length > 0) {
				var slideId = this.rel;
				var slideNum = self.getFromId(slideId);
				self.rotate(slideNum,'++');
			}
			else {
				for(var j=0;j<self.shows.length;j++) {
					self.rotate(j,'++');
				}
			}
			return false;
		});
		$('.slidePrevious').click(function() {
			if(this.rel.length > 0) {
				var slideId = this.rel;
				var slideNum = self.getFromId(slideId);
				self.rotate(slideNum,'--');
			}
			else {
				for(var j=0;j<self.shows.length;j++) {
					self.rotate(j,'--');
				}
			}
			return false;
		});
		$('.slidePause').click(function() {
			if(this.rel.length > 0) {
				var slideId = this.rel;
				var slideNum = self.getFromId(slideId);
				if(self.shows[slideNum].status == 0) {
					clearTimeout(self.shows[slideNum].rotate);
					self.shows[slideNum].status = 1;
					$(this).removeClass('slidePause');
				}
				else {
					self.callInterval(slideNum);
					self.shows[slideNum].status = 0;
					$(this).addClass('slidePause');
				}
			}
			else {
				for(var j=0;j<self.shows.length;j++) {
					if(self.shows[j].status == 0) {
						clearTimeout(self.shows[j].rotate);
						self.shows[j].status = 1;
					}
					else {
						self.callInterval(j);
						self.shows[j].status = 0;
					}
				}
			}
			return false;
		});

	},
	
	getFromId : function(id) {
		var num = -1;
		for(var i=0;i<this.shows.length;i++) {
			if(id == this.shows[i].id) {
				num = i;
			}
		}
		return num;
	},
	
	callInterval : function(num) {
		var self = this;
		this.shows[num].rotate = setTimeout(function() { self.rotate(num); }, this.slideSpeed);
	},
	
	state : function(y,num,state) {
		var node = $('#'+this.shows[y].id).children('.slide')[num];
		switch(state) {
			case 1:
				node.style.zIndex = "100";
				node.style.opacity = "0";
				node.style.filter = "alpha(Opacity=0)";
				break;
			case 2:
				node.style.zIndex = "150";
				node.style.opacity = "1";
				node.style.filter = "alpha(Opacity=100)";
				break;
			case 3:
				node.style.zIndex = "200";
				node.style.opacity = (this.shows[y].opacity / 100);
				node.style.filter = "alpha(Opacity="+this.shows[y].opacity+")";
				break;
		}
	},
	
	bounds : function(y,num) {
		if(num >= this.shows[y].count) {
			num = 0;
		}
		if(num < 0) {
			num = this.shows[y].count - 1;
		}
		return num;
	},

	rotate : function(y,operator) {
		var self = this;
		clearInterval(this.shows[y].fade);
		clearTimeout(this.shows[y].rotate);
		
		if(this.shows[y].previous != this.shows[y].number) {
			// hide the previous slide
			this.state(y,this.shows[y].previous,1);
			// set the previous slide to the current
			this.shows[y].previous = this.shows[y].number;
			this.shows[y].opacity = 50;
		}

		// increment the current slide
		if(operator == '++') {
			this.shows[y].number = this.shows[y].number + 1;
		}
		else if(operator == '--') {
			this.shows[y].number = this.shows[y].number - 1;
		}
		else {
			this.shows[y].number = this.shows[y].number + 1;
		}
		this.shows[y].number = this.bounds(y,this.shows[y].number);
		
		// set the slides to the correct states
		this.state(y,this.shows[y].previous,2);
		this.state(y,this.shows[y].number,3);
		
		this.shows[y].fade = setInterval(function() {self.fade(y); }, this.fadeSpeed);
	},

	fade : function(y) {
		var self = this;
		var nextNode = $('#'+this.shows[y].id).children('.slide')[this.shows[y].number];
		this.shows[y].opacity = this.shows[y].opacity + this.fadeChange;
		if(this.shows[y].opacity >= 100) {
			nextNode.style.opacity = "1";
			nextNode.style.filter = "alpha(Opacity=100)";
			// hide the previous slide
			this.state(y,this.shows[y].previous,1);
			
			this.shows[y].opacity = 0;
			this.shows[y].previous = this.shows[y].number;
			clearInterval(this.shows[y].fade);
			if(this.shows[y].status == 0) {
				this.shows[y].rotate = setTimeout(function() { self.rotate(y); }, this.slideSpeed);
			}
		}
		else {
			nextNode.style.opacity = (this.shows[y].opacity / 100);
			nextNode.style.filter = "alpha(Opacity="+this.shows[y].opacity+")";
		}
	}
};

function createCommentPrayerLimit() {
    var cplimit = new textLimit();
    cplimit.input = document.getElementById('CommentPrayer');
    cplimit.limit = 500;
    if($('.charRemaining').length) {
        cplimit.remaining = $('.charRemaining')[0];
        var newLimit = parseInt(cplimit.remaining.innerHTML,10);
        if(newLimit > 0) {
            cplimit.limit = newLimit;
        }
    }
    cplimit.init();
}
function createCommentLimit() {
    var commentlimit = new textLimit();
    commentlimit.input = document.getElementById('Comment');
    commentlimit.limit = 500;
    if($('.charRemaining').length) {
        commentlimit.remaining = $('.charRemaining')[0];
        var newLimit = parseInt(commentlimit.remaining.innerHTML,10);
        if(newLimit > 0) {
            commentlimit.limit = newLimit;
        }
    }
    commentlimit.init();
}

// Text Limiter:
// requires jQuery for 'adjustStyle'
//		var textarea = new textLimit();
//		textarea.input = document.getElementById('textarea');
//		textarea.limit = 500;
//		textarea.init();

function textLimit() {
	this.limit = 200;
	this.regex = /<[^ ]\/?[^>]+>/gi;
	this.allowOver = false;
	this.input = null;
	this.count = null;
	this.remaining = null;
	this.init = function() {
		var self = this;
		this.input.onkeydown = function(event) {
			if(this.value.length >= self.limit) {
				if(document.selection && document.selection.createRange().text.length > 0) {
					return true;
				}
				else if(self.input.selectionEnd - self.input.selectionStart > 0) {
					return true;
				}
				var key = c2.getKey(event);
				if(key > 46 || key == 13 || key == 32) {
					return false;
				}
			}
		}
		this.input.onkeyup = this.input.onchange = function() {
			var val = self.clean(this.value);
			if(!self.allowOver && val.length > self.limit) {
				val = val.substr(0, self.limit);
			}
			if(this.value != val) {
				this.value = val;
				self.input.scrollTop = self.input.scrollHeight;
			}
			self.setCount(val.length);
			self.setRemaining(val.length);
		};
		setTimeout(function() {
				self.setCount(self.input.value.length);
				self.setRemaining(self.input.value.length);
			}, 10);
	}
	this.setCount = function(length) {
		if(this.count) {
			this.count.innerHTML = length;
			this.adjustStyle(this.count, length);
		}
	}
	this.setRemaining = function(length) {
		if(this.remaining) {
			this.remaining.innerHTML = this.limit - length;
			this.adjustStyle(this.remaining, length);
		}
	}
	this.adjustStyle = function(object, length) {
		if(length > this.limit) {
			$(object).addClass('error');
		}
		else {
			$(object).removeClass('error');
		}
	};
	this.clean = function(value) {
		return value.replace(this.regex, '');
	}
}

var c2 = {
	findElementPos: function(elemFind) {
		var elemX = 0;
		var elemY = 0;
		do {
			elemX += elemFind.offsetLeft;
			elemY += elemFind.offsetTop;
		} while ( elemFind = elemFind.offsetParent )
		return Array(elemX, elemY);
	},
	randomInt: function(min, max) {
		return Math.floor(Math.random()*(max+1-min)) + min;
	},
	getQuery: function(param) {
		var p = escape(unescape(param));
		var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
		var match = regex.exec(window.location.search);
		var value = null;
		if( match != null ){
			value = match[1];
		}
		return value;
	},
	getKey: function(e) {
		if(!e) e = window.event;
		var key = e.which;
		if(!key) key = e.keyCode;
		return key;
	}
};

function swap(listIdPrefix) {
    collapsedList = document.getElementById(listIdPrefix + "_collapsed");
    expandedList = document.getElementById(listIdPrefix + "_expanded");
    if (collapsedList.style.display == "block") {
        collapsedList.style.display = "none";
        expandedList.style.display = "block";
    } else {
        collapsedList.style.display = "block";
        expandedList.style.display = "none";
    }
} 



