// ---------------------------
// ACCORDION
// ---------------------------

function hookArchiveLinks() {
	$("ul.archive a").click(function() {
		loadArticle(this);
		return false;
	});
}

function loadArticle(targetLink) {
	removeScrollers();
	$("#pagecontent> div").html('<div id="wait"><img src="themes/default/images/wait.gif" alt="Loading... please wait..." /></div>');
	var url = $(targetLink).attr("href").split("/");
	var targetid = url[url.length-1];
	var targetURL = "";
	for (var i = 1; i <= url.length - 3; i++){
		targetURL+= "/" + url[i];
	};
	$("#pagecontent> div").load(targetURL, { ajax: "true", id: targetid }, function() {
		hookArchiveLinks();
		createScrollers();
		var ieSaver = 10;
	});
}

function hookLinks() {
	// preloading wait.gif
	$("#pagecontent> div").append('<div id="wait"><img src="themes/default/images/wait.gif" alt="Loading... please wait..." /></div>');
	$("#wait").hide();
	
	$("dl.ajax.accordion a").click(function() {
		loadArticle(this);
		return false;
	});
}

function createAccordions() {
	$("dl.accordion dt").each(function() {
		$(this).addClass("toggler");
	});
	
	$("dl.accordion> dd").hide();
	$("dl.accordion> dt").click(function() {
		$(this).next('dd').slideToggle('slow')
		.siblings('dd:visible').slideUp('slow');
	});
	
	$("dl.accordion dd:first").slideToggle('slow');
	
	hookLinks();
}



// ---------------------------
// SCROLLERS
// ---------------------------

var currentTop = 0;
var currentLeft = 0;

function hookScrollers() {
	$(".scrolldown").click(function () {
		if(currentTop>($(".scrollcontainer").height()-$(".scrollcontent").height())) {
			currentTop = currentTop - 290;
			$(".scrollcontent").animate({ top:currentTop }, {
				duration: 1300,
				easing: "easeInOutQuart"
			});
		}
		return false;
    });

	$(".scrollup").click(function () {
		if(currentTop<0) {
			currentTop = currentTop + 290;
			$(".scrollcontent").animate({ top:currentTop }, {
				duration: 1300,
				easing: "easeInOutQuart"
			});
		}
		return false;
    });
}

function hookHorizontalScrollers() {
	$(".scrollright").click(function () {
		if(currentLeft>($("#thumbcontainer").width()-$("ul.thumbs").width())) {
			currentLeft = currentLeft - 240;
			$("ul.thumbs").animate({ left:currentLeft }, {
				duration: 1300,
				easing: "easeInOutQuart"
			});
		}
		return false;
    });

	$(".scrollleft").click(function () {
		if(currentLeft<0) {
			currentLeft = currentLeft + 240;
			$("ul.thumbs").animate({ left:currentLeft }, {
				duration: 1300,
				easing: "easeInOutQuart"
			});
		}
		return false;
    });
}


function removeScrollers() {
	$(".scrollable").each(function() {
		$(this).removeClass("scrollcontent");
		$(this).parent().removeClass("scrollcontainer");
		$(".scrollers").remove();
	});
}


function createScrollers() { 
	// $(".scrollable").each(function() {
	// 	$(this).addClass("scrollcontent");
	// 	$(this).parent().addClass("scrollcontainer");
	// 	if($(".scrollcontent").height()>$(".scrollcontainer").height()) {
	// 		$(this).before('<ul class="scrollers"><li class="scrollup"><a href="#">Up</a></li><li class="scrolldown"><a href="#">Down</a></li></ul>');
	// 		hookScrollers();
	// 	}
	// });
	
	// Gallery scroller
	$("ul.thumbs").each(function() {
		var thumbs = $(this).children();
		var thumbcount = thumbs.length;
		if(thumbcount>5){
			$(this).width((thumbcount+1)*55);								// need to set width for scrolling
			$("#thumbcontainer").css("position","relative");				// reference point
			$("#thumbcontainer").width(300);								// width must be set
			$("#thumbcontainer").height(55);								// height must be set
			$("#thumbcontainer").css("overflow","hidden");					// hide extra
			$("#thumbcontainer").css("float","left");
			$(this).css("position","absolute");								// position
			$(this).css("left","auto");
			$(this).css("top","auto");
			// INSERT ARROWS
			$("#scrollcontainer").append('<ul class="hscrollers"><li class="scrollleft"><a href="#">Left</a></li><li class="scrollright"><a href="#">Right</a></li></ul>');
			hookHorizontalScrollers();
		}
	});
}




// ---------------------------
// GALLERY
// ---------------------------

function createGallery() {
	$('.lightboxlink').lightBox(); // hook the first image
	$("ul.midsize li a").click(function() {
		$("#image-medium").html('<div id="wait"><img src="themes/default/images/wait-alt.gif" alt="Loading... please wait..." /></div>');
		var theimageid = this.id.replace(new RegExp("image"), "");
		var theimagegroup = $(this).attr("class").replace(new RegExp("group"), "");
		$("#image-medium").load("loadimage.cfm", { imageid: theimageid, imagegroup: theimagegroup }, function() {
				$('.lightboxlink').lightBox();
		});
		
		return false;
	});
}




// ---------------------------
// CONTACT
// ---------------------------

function hookContactForm() {
	$("#contactform").submit(function() {
		if (typeof document.body.style.maxHeight != "undefined") { // check to see if we're dealing with IE6
			$("#sidebar").fadeTo("slow", 0.6, function() {
				$("#sidebar").slideUp({
					duration: 2000,
					easing: "easeInOutQuart",
					complete: function() {
						var params = {}; 
						$(this)
							.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea") 
							.filter(":enabled")
						.each(function() { 
							params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; 
						});

						$.post("contact-logic.cfm", params, function(xml) {
							$("#sidebar").html(xml);
							// $("#sidebar").fadeTo("slow", 1);
							$("#sidebar").slideDown({
								duration: 1000,
								easing: "easeInOutQuart",
								complete: function() {
									$("#sidebar").fadeTo("slow", 1, function() {
										$("span.error").each(function() {
											$(this).animate({ paddingLeft:25 }, {
												duration: 300,
												easing: "easeInOutQuad",
												complete: function() {
													$(this).animate({ paddingLeft:0 }, {
														duration: 1000,
														easing: "easeOutBounce" });
												}
											});
										});
										hookContactForm();
									});
								}
							});
						});
					}
				}); 
			});
		} else {  // BROWSER IS IE6
			var params = {}; 
			$(this)
				.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea") 
				.filter(":enabled")
			.each(function() { 
				params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; 
			});
			$.post("contact-logic.cfm", params, function(xml) {
				$("#sidebar").html(xml);
				hookContactForm();
			});
		}
		return false;
	});
}


function hookEmailSignup() {
	if('#email') {
		$('#email').attr("value","Enter your email for free energy tips!");
		$('#email').click(function() {
			$('#email').attr("value","");
		})
	}
}




// ---------------------------
// INIT
// ---------------------------

$(document).ready(function(){
	createScrollers();
	createAccordions();
	createGallery();
	hookContactForm();
	hookEmailSignup();
});