$(document).ready(function() {

	$("body").removeClass("no-js").addClass("has-js");

	var $cycle = $("#main-content").find(".carousel").first();
	var pedalCyclist;
	function stopCycle() {
		clearInterval(pedalCyclist);
	};
	function startCycle() {
		pedalCyclist = setInterval(cyclist, 6500);
	};
	function cyclist() {
		if ( $cycle.find(".slide-list").first().data("hover") == false ) {
			spinRound($cycle,"forward");
		}
	};

	$cycle.data("activeLI", 0).delegate(".back", "click", function(){
		spinRound($(this),"back");
		return false;
	}).delegate(".forward", "click", function(){
		spinRound($(this),"forward");
		return false;
	}).find(".slide").each(function(i){
		var $t = $(this);
		//make jQ display as block when .show()
		$t.not(":first-child").css("display","block").hide();
		var $c = $(this).closest(".carousel");
		var i2 = i + 1;
		$c.find(".control:last-child").clone().removeClass("back").addClass("item-control").html("<a href='#ITEMlink#' title='News item "+ i2 +"'>"+ i2 +"</a>")
			.insertAfter(".forward").bind("click",function(){
				spinRound($(this),"direct");
				return false;
			});
	}).end().find(".item-control a").last().addClass("active").end().end().find(".slide-list").first().hover(function() {
			$.data(this, 'hover', true);
		}, function() {
			$.data(this, 'hover', false);
		}
	).data('hover', false);

	function spinRound($this,direction) {
		if ( !direction ) {
			var direction = "forward";
		}
		var $c = $this.closest(".carousel");
		if ( $this.is($cycle) ) {
			$c = $this;
		}
		var len = $c.find(".slide").length;
		var act = $c.data("activeLI");
		var newAct;
		var newCon;

		if ( direction == "forward") {
			newAct = act + 1;

			if ( newAct == len ) {
				newAct = 0;
			}
			newCon = -(newAct+1);
		} else if ( direction == "back" ) {
			newAct = act - 1;

			if ( newAct == (len * -1) - 1 ) {
				newAct = -1;
			}
			newCon = -(newAct+1);
		} else if ( direction == "direct" ) {
			var base = $c.find(".item-control").index($this);
			newCon = (base - len);
			newAct = -1 * (newCon + 1);
		}

		$c.data("activeLI", newAct)
		.find(".slide").stop(true).css("opacity",1).fadeOut(800, function(){
			$c.find(".item-control a").removeClass("active").eq(newCon).addClass("active");
		}).eq(newAct).fadeIn(820, function(){
			stopCycle();
			startCycle();
		});
	};

	if ( $cycle.length > 0 ) {
		startCycle();
	}


	var plus = '+';
	var minus = '\u2013';
	var moreInfo = "read more...";
	var lessInfo = "read less...";
	var plusMore = "<strong>"+plus+"</strong> "+moreInfo;
	var minusLess = "<strong>"+minus+"</strong> "+lessInfo;


	$(".vid-share").css("width",function(){
		//have to use attr width instead of css width b/c IE
		return $(this).prev().attr("width");
	}).find(".vid-share-box").hide().find(".fb-share a").click(function(){
		var url = $(this).attr("href");
		window.open(url, "fbWindow", "resizable=0,width=620,height=430");
		return false;
	}).end().end().find("h2").css("cursor","pointer").prepend("<strong>"+plus+"</strong> ").click(function(){
		$(this).closest(".vid-share").find(".vid-share-box").slideToggle(200).end()
		.find("strong").text(function(i,txt){
			if ( txt == plus ) {
				return minus;
			} else if ( txt == minus ) {
				return plus;
			}
		});
	});


	$(".callout").find(".moreinfo").hide().prepend("<a class='showinfo exit active' href='#LESSinfo#'>close | x</a>").after("<a class='showinfo' href='#TOGGLEinfo#'>"+plusMore+"</a>").end()
	.delegate(".showinfo:not('.exit')", "click", function(){
		$(this).prev(".moreinfo").slideToggle().end()
		.html(function(i,oldhtml){
			if ( oldhtml == plusMore ) {
				return minusLess;
			} else if ( oldhtml == minusLess ) {
				return plusMore;
			}
		}).toggleClass("active");
		return false;
	})
	.delegate(".showinfo.exit", "click", function(){
		$(this).closest(".moreinfo").next(".showinfo").click();
		return false;
	});

	$(".downloads #main-content").find("table.download-table").find("td a").attr("title", "Click to download/view PDF..").end().end().find("ul,p").find("a[href$='.pdf']").attr("title", "Click to download/view PDF..");

	$("form.weblead-form").submit(function(){
		return ValidateForm($(this).attr("id"));
	});

});

function ValidateForm(formID) {

	if ((returnObjById('company').value).length == 0) {
		alert('You must enter your Name of Parish, School, or Diocese');
		return false;
	}
	if ((returnObjById('type_of_org').value).length == 0) {
		alert('You must enter your Type');
		return false;
	}
	if ((returnObjById('first_name').value).length == 0) {
		alert('You must enter your First Name');
		return false;
	}
	if ((returnObjById('last_name').value).length == 0) {
		alert('You must enter your Last Name');
		return false;
	}

	if ( formID == "review-pack-standard-form" || "review-pack-4-form" ) {
		if ((returnObjById('title').value).length == 0) {
			alert('You must enter your Title');
			return false ;
		}
		if ((returnObjById('phone').value).length == 0) {
			alert('You must enter your Phone');
			return false ;
		}
		if ((returnObjById('email').value).length == 0) {
			alert('You must enter your Email');
			return false ;
		}
		if ((returnObjById('street').value).length == 0) {
			alert('You must enter your Street');
			return false ;
		}
		if ((returnObjById('city').value).length == 0) {
			alert('You must enter your City');
			return false ;
		}
		if ((returnObjById('state').value).length == 0) {
			alert('You must enter your State');
			return false ;
		}
		if ((returnObjById('zip').value).length == 0) {
			alert('You must enter your Zip');
			return false ;
		}
		if ((returnObjById('phone').value).length == 0 && (returnObjById('email').value).length == 0) {
			alert('You must enter your Phone or Email.');
			return false ;
		}
	}

	return true;
}

function returnObjById(id) {
	if (document.getElementById)
	var returnVar = document.getElementById(id);
	else if (document.all)
	var returnVar = document.all[id];
	else if (document.layers)
	var returnVar = document.layers[id];
	return returnVar;
}
