/*
	Custom "Resource Finder" code for Orange. Built by Jo ALbright with Hybr-id. http://hybr-id.com
*/

$.fn.resourceFinder = function(theWidth,thePage) {
	
	// console.log(thePage);
	
	return this.each(function(){
		
		var section = $(this);
		
		var $wrapper = $('> div', this),
			$sections = $wrapper.find('> div'),
			$firstSection = $sections.filter(':first'),
			$links = $sections.find('> h2'),
			$panels1 = $('#resource-section-1').find('#panel'),
			$panels2 = $('#resource-section-2').find('#panel'),
			$panels3 = $('#resource-section-3').find('#panel'),
			$firstPanel1 = $panels1.filter(':first'),
			$firstPanel2 = $panels2.filter(':first'),
			$firstPanel3 = $panels3.filter(':first'),
			$groupingNum = 1;
		
		$sections.addClass("hiddenSection");
		$firstSection.addClass("currentSection");
		$firstSection.removeClass("hiddenSection");
		
		if (thePage == '252basics')
		{
			$firstSection.removeClass("currentSection");
			$firstSection.addClass("hiddenSection");
			$('#resource-section-2').addClass("currentSection");
			$('#resource-section-2').removeClass("hiddenSection");
			$groupingNum = 2;
		} else if (thePage == 'xp3students')
		{
			$firstSection.removeClass("currentSection");
			$firstSection.addClass("hiddenSection");
			$('#resource-section-3').addClass("currentSection");
			$('#resource-section-3').removeClass("hiddenSection");
			$groupingNum = 3;
		} else if (thePage == 'xp3college')
		{
			$firstSection.removeClass("currentSection");
			$firstSection.addClass("hiddenSection");
			$('#resource-section-3').addClass("currentSection");
			$('#resource-section-3').removeClass("hiddenSection");
			$groupingNum = 3;
		}
		
		$('.hiddenSection').hide();
		
		if (thePage == '252basics')
		{
			$wrapper.before('<ul class="rfNav"><li><a id="preschool" class="rfNav_1" name="preschool">Preschool</a></li><li><a id="childrens" class="rfNav_2 selected" name="childrens">Children’s</a></li><li><a id="students" class="rfNav_3" name="students">Students</a></li></ul>');
		} else 	if (thePage == 'xp3students') {
			$wrapper.before('<ul class="rfNav"><li><a id="preschool" class="rfNav_1" name="preschool">Preschool</a></li><li><a id="childrens" class="rfNav_2" name="childrens">Children’s</a></li><li><a id="students" class="rfNav_3 selected" name="students">Students</a></li></ul>');
		} else 	if (thePage == 'xp3college') {
				$wrapper.before('<ul class="rfNav"><li><a id="preschool" class="rfNav_1" name="preschool">Preschool</a></li><li><a id="childrens" class="rfNav_2" name="childrens">Children’s</a></li><li><a id="students" class="rfNav_3 selected" name="students">Students</a></li></ul>');
		} else {
			$wrapper.before('<ul class="rfNav"><li><a id="preschool" class="rfNav_1 selected" name="preschool">Preschool</a></li><li><a id="childrens" class="rfNav_2" name="childrens">Children’s</a></li><li><a id="students" class="rfNav_3" name="students">Students</a></li></ul>');
		}
		
		$('.rfNav a').click(function () { 
		    var target = $(this).attr("id");
			var groupClass = $(this).attr("class");
			$groupingNum = groupClass.replace("rfNav_", "");
			
			updateLinks($groupingNum);
			
		    $(".rfNav a").removeClass("selected");
		    $(this).addClass("selected");

      		$(".currentSection").hide();
		    $(".currentSection").addClass("hiddenSection");
		    $(".currentSection").removeClass("currentSection");

	      	$('.'+target).removeClass("hiddenSection");
		   	$('.'+target).addClass("currentSection");
		    $('.'+target).fadeIn();

		    //console.log($groupingNum);
		});
		
		$sections.find('> div').before('<div class="listNav"><div class="left-arrow"><a class="arrow back">&lt;</a></div><div class="navigation"><div class="wrapper"><ul></ul></div></div><div class="right-arrow"><a class="arrow forward">&gt;</a></div></div>')
		
		$currentSelection = 1;
		$count = 0;
		$split = 0;
		$group = 0;
		
		
		for (var i = 0; i < 3; i++) {
			
			var $sectionNum = '#resource-section-'+(i+1)
			
			ul = $($sectionNum).find('.listNav .navigation .wrapper ul');
			$('#panel', $sectionNum).each(function(n) {
				$count++;
				//console.log(i+" "+ul+" "+n);
			});
		
			for (var N1 = 0; N1 < $count; N1++) {
				$panelName = $sectionNum+' .panel'+(N1+1);
				//console.log($panelName+" "+i)
				if ($split < 4) {
					if ($split == 0) { ul.append('<li class="group'+ ($group+1) +'"></li>'); };
					li = $($sectionNum+' .navigation').find('.group'+ ($group+1));
					if (N1 < 1) {
						li.append('<a id="panel'+ (N1+1) +'" class="selected">'+$($panelName).find('h2.title').text()+'</a>');
					} else {
						li.append('<a id="panel'+ (N1+1) +'">'+$($panelName).find('h2.title').text()+'</a>');
					}
					$split++;
				} else {
					li.append('<a id="panel'+ (N1+1) +'">'+$($panelName).find('h2.title').text()+'</a>');
					$split = 0;
					$group++;
				}
			}

				var	$slider = $wrapper.find($sectionNum+' .navigation .wrapper ul'),
			        $items = $slider.find('> li'),
			        $single = $items.filter(':first'),

			        singleWidth = theWidth, 
			        visible = 1,
			        currentGroup = 1;

				function repeat(str, num) {
				    return new Array( num + 1 ).join( str );
				}

				if (($items.length % visible) != 0) {
					$slider.append(repeat('<li class="empty"></li>', visible - ($items.length % visible)));
					$items = $slider.find('> li');
				}

				$items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
				$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
				$items = $slider.find('> li');

				//$slider.scrollLeft(singleWidth);
			
			$split = 0;
			$group = 0;
			$count = 0;
			
		}
				
		//console.log($firstPanel1.attr("id"))
		
		$panels1.addClass("hiddenPanel");
		$panels2.addClass("hiddenPanel");
		$panels3.addClass("hiddenPanel");
		$firstPanel1.addClass("currentPanel");
		$firstPanel2.addClass("currentPanel");
		$firstPanel3.addClass("currentPanel");
		$firstPanel1.removeClass("hiddenPanel");
		$firstPanel2.removeClass("hiddenPanel");
		$firstPanel3.removeClass("hiddenPanel");
		$('.hiddenPanel').hide();
		
		updateLinks($groupingNum);
		
		function updateLinks(groupingNum) {
			
			var $linkSection = '#resource-section-'+(groupingNum),
				$allLinks = $linkSection+' .listNav .navigation a',
				$currPanel = $linkSection+' .currentPanel';
				currentGroup = 1;
			
			$($linkSection+' .listNav .navigation li a').click(function () { 
				var targetPanel = $(this).attr("id"); 
				//console.log($linkSection+" "+groupingNum)
		
		   		$($allLinks).removeClass("selected");
		   		//$(this).addClass("selected");
				$($linkSection+' .listNav .navigation #'+targetPanel).addClass("selected");
			
	    		$($currPanel).hide().addClass("hiddenPanel").removeClass("currentPanel");
		
		   		$($linkSection+' .'+targetPanel).removeClass("hiddenPanel").addClass("currentPanel").fadeIn();
			});
	
			$slider = $wrapper.find($linkSection+' .navigation .wrapper');
			$items = $slider.find('ul li');
	        $single = $items.filter(':first');
			groups = Math.ceil($items.length / visible);
			$slider.filter(':not(:animated)').animate({scrollLeft : '+=' + singleWidth});
			
	        $($linkSection+' .listNav a.back').click(function () {
	            gotoGroup(currentGroup - 1, $linkSection);
	
	        });

	        $($linkSection+' .listNav a.forward').click(function () {
	            gotoGroup(currentGroup + 1, $linkSection);
	        });
	
		}
		
		function gotoGroup(group, linkURL) {
			$slider = $wrapper.find(linkURL+' .navigation .wrapper');
			$items = $slider.find('ul li');
	        $single = $items.filter(':first');
			groups = Math.ceil($items.length / visible);
			
	      	var dir = group < currentGroup ? -1 : 1,
	          	n = Math.abs(currentGroup - group),
	            left = singleWidth * dir * n;

	        $slider.filter(':not(:animated)').animate({
	           	scrollLeft : '+=' + left
	        }, 500, function () {
	            if (group == 0) {
	               	$slider.scrollLeft(singleWidth * (groups-2));
	                group = groups-2;
	           	} else if (group >= groups-1) {
	            	$slider.scrollLeft(singleWidth);
	               	// reset back to start position
	               	group = 1;
	           	};
	          	currentGroup = group;
	      	});              

      		return false;
	    }
		
		$wrapper.removeClass('hiddenMain');
	});
};

$.fn.moreStuff = function() {

	return this.each(function(){
		
		var $wrapper = $('> div', this),
			$pages = $wrapper.find('> div'),
			$firstPage = $pages.filter(':first');
		
		$pages.addClass("hiddenPage");
		$firstPage.addClass("currentPage");
		$firstPage.removeClass("hiddenPage");
		$('.hiddenPage').hide();
		
		$wrapper.before('<ul class="msNav"><li><a id="resourceStuff" class="msNav_1 selected" name="resourceStuff">Resources</a></li><li><a id="trainingStuff" class="msNav_2" name="trainingStuff">Training</a></li><li><a id="eventStuff" class="msNav_3" name="eventStuff">Events</a></li></ul>');
		
		$('.msNav a').click(function () { 
		    var target = $(this).attr("id");

		    $(".msNav a").removeClass("selected");
		    $(this).addClass("selected");

		    $(".currentPage").hide();
		    $(".currentPage").addClass("hiddenPage");
		    $(".currentPage").removeClass("currentPage");

		    $('.'+target).removeClass("hiddenPage");
		    $('.'+target).addClass("currentPage");
		    $('.'+target).fadeIn();

		    //console.log(target5);
		});
				
		$wrapper.removeClass('hiddenMain');
	});
}

$.fn.orangeGuy = function(qcount) {

	return this.each(function(){
		
		var $wrapper = $(this),
			$questions = $wrapper.find('> div'),
			$firstQ = $questions.filter(':first'),
			$count = 1;
		
		$questions.addClass("hiddenQ");
		$firstQ.addClass("currentQ");
		$firstQ.removeClass("hiddenQ");
		$('.qNav').removeClass('hiddenQ').show();
		$('.hiddenQ').hide();
		
		$('.qNav a').click(function () { 
		    var target = $(this).attr("class");
			
			if (target == "back") {
				if ($count > 1)
				{
					$count--;
				} else {
					$count = qcount;
				}
			} else {
				if ($count < qcount)
				{
					$count++;
				} else {
					$count = 1;
				}
			}
			// console.log(qcount);
			// console.log('.q'+$count);

		    $(".currentQ").hide();
		    $(".currentQ").addClass("hiddenQ");
		    $(".currentQ").removeClass("currentQ");

		    $('.q'+$count).removeClass("hiddenQ");
		    $('.q'+$count).addClass("currentQ");
		    $('.q'+$count).fadeIn();

		    //console.log(target5);
		});
				
		$wrapper.removeClass('hidden');
	});
}

$.fn.teamMembers = function() {

	return this.each(function(){
		
		var $wrapper = $(this);
			$teamOpen = $wrapper.find('.bio_group'),
			$teamMembers = $wrapper.find('.bios'),
			$teamClose = $wrapper.find('.closeBtn');
		
		$teamMembers.hide();
		
		$teamOpen.click(function () { 
		    var target = $(this).attr("id");
			$teamMembers.hide();
		    $('.'+target).show(1, 
			function() { 
		   		var theContent = $('#content-area');
		   		var contentHeight = theContent.outerHeight();
				$('#gradientBG').height(contentHeight);
				changeHeight();
				document.body.style.height = contentHeight+390 + "px";
				
			});
		    //console.log(target);
		});

		$teamClose.click(function () { 
		    var target = $(this).attr("id");
		    $('.'+target).hide(1, 
			function() { 
		   		var theContent = $('#content-area');
		   		var contentHeight = theContent.outerHeight();
				$('#gradientBG').height(contentHeight);
				document.body.style.height = contentHeight+390 + "px";
				changeHeight();
			});
		});
				
		$teamMembers.removeClass('hidden');
		
		function changeHeight() {
		    if (typeof(window.innerHeight) == 'number') {
		        //Non-IE
		        windowHeight = window.innerHeight;
		        windowWidth = window.innerWidth;
		        scrollBar = 15;
		    } else if (document.documentElement && document.documentElement.clientHeight) {
		        //IE 6+ in 'standards compliant mode'
		        windowHeight = document.documentElement.clientHeight;
		        windowWidth = document.documentElement.clientWidth;
		    } else if (document.body && document.body.clientHeight) {
		        //IE 4 compatible
		        windowHeight = document.body.clientHeight;
		        windowHeight = document.body.clientWidth;
		    }

		    
		    htmlHeight = document.body.offsetHeight
		    scrollWidth = document.body.scrollWidth;
		    scrollPosition = document.body.scrollLeft;
		    if (htmlHeight < windowHeight)
		    {
		        if (windowWidth < scrollWidth) {
		            document.body.style.height = (windowHeight - scrollBar) + "px";
		        } else {
		            document.body.style.height = windowHeight + "px";
		        }
				// console.log("error");
		    }
			
		    // console.log(scrollWidth+" : "+scrollPosition+" : "+windowWidth)
		    // console.log(htmlHeight, scrollWidth, scrollPosition, windowHeight)
		}
		
	});
}


$.fn.faqs = function(catID) {

	return this.each(function(){
		
		var $wrapper = $(this);
			$catID = $wrapper.find('.'+catID),
			$faqQ = $wrapper.find('.faq-question'),
			$faqA = $wrapper.find('.faq-answer'),
			$faqClose = $wrapper.find('.closeBtn'),
			$thisLink = "";
		
		$faqA.hide();
		
		$faqQ.click(function () { 
		    var target = $(this).attr("id");
			if ($thisLink != $(this).attr("id")) {
				$thisLink = $(this).attr("id");
				$faqA.hide();
		    	$('.'+target).show(1, 
				function() { 
		   			var theContent = $('#content-area');
		   			var contentHeight = theContent.outerHeight();
					$('#gradientBG').height(contentHeight);
				});
			}
		    //console.log(target);
		});

		$faqClose.click(function () { 
		    var target = $(this).attr("id");
			$thisLink = "";
		    $('.'+target).hide(1, 
			function() { 
		   		var theContent = $('#content-area');
		   		var contentHeight = theContent.outerHeight();
				$('#gradientBG').height(contentHeight);
			});
		});
				
		$catID.removeClass('hidden');
		$faqA.removeClass('hidden');
	});
}


$.fn.showMe = function() {

	return this.each(function(){
		
		var $wrapper = $(this),
			$link = $wrapper.find('.showme'),
			$hidden = $wrapper.find('.hidden'),
			$close = $wrapper.find('.closeBtn'),
			$thisLink = "";
		
		$hidden.hide();
		
		$link.click(function () { 
		    var target = $(this).attr("id");
			if ($thisLink != $(this).attr("id")) {
				$thisLink = $(this).attr("id");
				$hidden.hide();
		    	$('.'+target).show();
			} else { 
				$thisLink = "";
				$('.'+target).hide();
			}
			
		});

		$close.click(function () { 
		    var target = $(this).attr("id");
			$thisLink = "";
		    $('.'+target).hide();
		});
		
		$hidden.removeClass('hidden');
	});
}
