$(function() {
	$('.toggler').bind('click', function() {		
		$('#accordion').data('openItem', $(this).parents('.item').attr('id'));
		var curOpenItem = $(this).parents('.item');
		curOpenItem = $(curOpenItem[0]);
		
		var dirPath = "/Careers/graduates/PublishingImages/";
		
		//if it has content and is not currently visible
		if ($(this).siblings('.acc_content').length && !$(this).siblings('.acc_content').is(':visible')) {
			//hide the currently open content
			if ($('.acc_content:visible').length) {
				//get the top item from the current open item - used below when clicking a toggler in another item (not nested)
				var parentArray = $('.acc_content:visible').parents('.item');
				parentItemToClose = parentArray[parentArray.length-1];
				parentItemToClose = $(parentItemToClose).attr('id');
				
				var parentsArray = $(this).parents('.item');
				curTopParent = parentsArray[parentsArray.length-1];
				curTopParent = $(curTopParent).attr('id');
				
				//if the top parent is different from the current top parent
				if (parentItemToClose != curTopParent) {
					//close the second item first
					$('#'+parentItemToClose).children('.item').children('.inner').children('.acc_content').children('.close').addClass("dontOpenTop");
					$('#'+parentItemToClose).children('.item').children('.inner').children('.acc_content').children('.close').click();
					//now close the top item
					$('#'+parentItemToClose).children('.inner').children('.acc_content').children('.close').click();
				}
				$('.acc_content:visible').slideUp();
			}
			
			//show the current content
			$(this).siblings('.acc_content').slideDown();
			
			//check if the current open item has any children
			if (curOpenItem.children('.item').length) {
				//check to see if we are opening a top level item, if so we need to close any third level items
				var openItemArr = $('#accordion').data('openItem').split("_");
				if (openItemArr.length == 2) { //something like acc_1
					$('#'+$('#accordion').data('openItem')).children('.item').children('.item').slideUp();
					//change my bottom image					
					var curSrc = $('#'+$('#accordion').data('openItem')).children('.item').children('.inner').siblings('.itemBottom').attr('src');
					if (curSrc.substr(0, dirPath.length+7) == dirPath + "parent-") {
						$('#'+$('#accordion').data('openItem')).children('.item').children('.inner').siblings('.itemBottom').attr('src', dirPath + curSrc.substr(dirPath.length+7));
					}
				}
				//change bottom image - not using curOpenItem as it has changed here
				var curSrc = $(this).parent().siblings('.itemBottom').attr('src');
				if (curSrc.substr(0, dirPath.length+7) != dirPath + "parent-") {
					$(this).parent().siblings('.itemBottom').attr('src', dirPath + 'parent-'+curSrc.substr(dirPath.length));
				}
				//show the child
				curOpenItem.children('.item').slideDown();
			}
			
		}
	});
	
	$('.close').bind('click', function() {
		//get the items to close
		//var openItemArr = $('#accordion').data('openItem').split("_");
		var clickParent = $(this).parent().parent().parent().attr("id");
		var openItemArr = clickParent.split("_");
		var dirPath = "/Careers/graduates/PublishingImages/";
		
		if (openItemArr.length == 2) { //something like acc_1
			//close any children items
			$('#'+clickParent).children('.item').slideUp();
			//close myself
			$('#'+clickParent).children('.inner').children('.acc_content').slideUp();
			//change my bottom image
			var curSrc = $(this).parents('.inner').siblings('.itemBottom').attr('src');
			if (curSrc.substr(0, dirPath.length+7) == dirPath + "parent-") {
				$(this).parents('.inner').siblings('.itemBottom').attr('src', dirPath + curSrc.substr(dirPath.length+7));
			}
		}
		else if (openItemArr.length == 3) { //something like acc_1_2
			//close any children items
			$('#'+clickParent).children('.item').slideUp();
			//close myself
			$('#'+clickParent).children('.inner').children('.acc_content').slideUp();
			
			//open up the parent item - if not told otherwise
			if (!$(this).is('.dontOpenTop')) {
				$('#acc_'+openItemArr[1]).children('.inner').children('.acc_content').slideDown();
			}
			else {
				$(this).removeClass('dontOpenTop');
			}
			//set the active open item
			$('#accordion').data('openItem', 'acc_'+openItemArr[1]);
			
			//change my bottom image
			var curSrc = $(this).parents('.inner').siblings('.itemBottom').attr('src');
			if (curSrc.substr(0, dirPath+7) == dirPath + "parent-") {
				$(this).parents('.inner').siblings('.itemBottom').attr('src', dirPath + curSrc.substr(dirPath.length+7));
			}
		}		
	});
});
