﻿/*
    Author: JD Medina
    Date: 5/5/2009
    Description: Functions required in the website. First part deals with the faqs and the second with the side scroller.
*/


// ------------ FAQ TOGGLE FUNCTIONS by JD MEDINA for Solvay CREON

function checkExpanded() {
	var total = $(".faqContainer a[class]").size();
	var expanded = $(".faqContainer a.collapsed").size();
	var collapsed = $(".faqContainer a.expanded").size();
	
	if(expanded >= total) {
		blurExpand();
	}
	else if(collapsed >= total) {
		blurCollapse();
	}
}

function blurExpand() {
	$(".expandAll").animate({
		opacity:	0.3
	});
	$(".collapseAll").animate({
		opacity:	1
	});
	$(".expandAll").blur();
}

function blurCollapse() {
	$(".collapseAll").animate({
		opacity:	0.3
	});
	$(".expandAll").animate({
		opacity:	1
	});
	$(".collapseAll").blur();
}

function expandAll() {
	
	$(".faqContainer div.faqWrapper").show("normal");
	
	$(".faqContainer a[class]").each(function(i,val){
		if($(val).hasClass("expanded")){
			$(val).toggleClass("expanded");
			$(val).toggleClass("collapsed");
		}
	});
	
	$(".expandAll").animate({
		opacity:	0.3
	});
	$(".collapseAll").animate({
		opacity:	1
	});
	$(".expandAll").blur();
}

function collapseAll() {
	
	$(".faqContainer div.faqWrapper").hide("normal");
	
	$(".faqContainer a[class]").each(function(i,val){
		if($(val).hasClass("collapsed")){
			$(val).toggleClass("expanded");
			$(val).toggleClass("collapsed");
		}
	});
		
	$(".collapseAll").animate({
		opacity:	0.3
	});
	$(".expandAll").animate({
		opacity:	1
	});
	$(".collapseAll").blur();
}

// ------------ THE END ------------ //	 

$(function(){
		   
	if($(".col3Sub1").children().size() == 0){
		$(".col3Sub1").hide();
		$(".col3Sub2").width(725);
		$("h1.default").css({
			"marginLeft" : 		"10px"
		});
	}
	else {
		$(".col3Sub2").width(485);
	}
	
	$("#scroll").css({
			opacity:	0
	});		   
	
	// ------------ FAQ TOGGLE by JD MEDINA for Solvay CREON
	
	// RESET ALL p, ul, ol and classes to collapsed
	
	$(".faqContainer div.faqWrapper").hide();
	
	$(".collapseAll").animate({
			opacity:	0.3
	});
	
	if ($(".faqContainer a[class]").hasClass("expanded")) {
		$(this).removeClass("expanded");
		$(this).addClass("collapsed");
	}
	
	// TOGGLE EACH faq
	
	$(".faqContainer a[class]").click(function() {
		$(this).blur();
		$(this).parent().find("div.faqWrapper").toggle("normal");
		$(this).toggleClass("expanded");
		$(this).toggleClass("collapsed");
		checkExpanded();
	});
	
	$(".faqContainer h5").click(function() {
		$(this).blur();
		$(this).parent().find("div.faqWrapper").toggle("normal");
		$(this).parent().find("a[class]").toggleClass("expanded");
		$(this).parent().find("a[class]").toggleClass("collapsed");
		checkExpanded();
	});
	
	// EXPAND ALL
	
	$(".expandAll").click(expandAll);
	
	// COLLAPSE ALL
	
	$(".collapseAll").click(collapseAll);
	
	// ------------ THE END ------------ //	 
	
	
	// ------------ MORE INFO SCROLLER by JD MEDINA for Solvay CREON
		   
	var myPageHeight = Page.getWindowSize().pageHeight;
	var myWindowHeight = Page.getWindowSize().windowHeight;
	var scrollStop = myPageHeight - myWindowHeight;
	var timeOutScroll = 0;
	var currentScroll = Page.getPageScroll().y;
	var footerHeight = 440;
	var boxHeight = 55;
	var headerHeight = 220;
	var currentTop = 0;
	
	if ((myPageHeight - (currentScroll+myWindowHeight) - footerHeight) > 0){
		currentTop = (myWindowHeight + currentScroll) - (boxHeight + headerHeight);
		$("#scroll").animate({"top" : currentTop + "px"},"slow");
	}
	else {
		currentTop = myPageHeight - (footerHeight + boxHeight + headerHeight);
		$("#scroll").animate({"top" : currentTop + "px"},"slow");
	}
	
	$("#scroll").animate({
			opacity:	1
	},1000);
	
	$(window).scroll(function(){

		currentScroll = Page.getPageScroll().y;
		clearTimeout(timeOutScroll);
		
		timeOutScroll = setTimeout(function(){
	
			if((myPageHeight - (currentScroll+myWindowHeight) - footerHeight) > 0) {
				currentTop = (myWindowHeight + currentScroll) - (boxHeight + headerHeight);
				$("#scroll").animate({"top" : currentTop + "px"},"slow");
			}
			else {
				currentTop = myPageHeight - (footerHeight + boxHeight + headerHeight);
				$("#scroll").animate({"top" : currentTop + "px"},"slow");
			}
		}, 100);
		
	});
	
	// ------------ THE END ------------ //
	
	
	// ------------ Landing Page (Patient Site) Link Hunter and duplicator for body text by JD MEDINA for Solvay CREON
	
	$("#col3Box div.pullDiv").each(function(i, val) {
		$(val).parent().find("a.boxlink").attr("href", $(val).find("a").attr("href"));
	});
	
	// ------------ THE END ------------ //

});