$(document).ready(function() {
	
	/* NAVIGATION */
	//hide sub link containers
	$('#design_links, #moving_links, #still_links').hide(); 
	
	//show sub link containers
	var lastClicked = null;
	$('.design, .moving, .still').click(function() {
			var className = $(this).attr('class');
			id = '#'+ className +"_links";
			
			if (lastClicked != null && lastClicked != id) {
				$(lastClicked).hide();
			} 
						
			lastClicked = id;
			if ($(id).is(":hidden")) {
	        	$(id).slideDown("fast");
	      	} else {
	        	$(id).hide();
	      	}	
	     
			//hide content when nav section clicked
			$('#port_description').html('');
			$('.port_top').html('');
			$('#port_item').html('');

	});
	
	//about page
	$('.about').click(function() {
		$('#port_description').html('');
		$('.port_top').html('');
		$('#port_item').html('<div class="about">&nbsp;</div>');
		$(lastClicked).hide();
	});
	
	//sublinks clicked
	$('#design_links > a, #moving_links > a, #still_links > a').click(function() {
		$('#design_links > a, #moving_links > a, #still_links > a').each(function() {
			$(this).removeClass('selected');
		});
		
		var parent = $(this).parent('div'); //section
		$(this).addClass('selected');
		if (lastClicked != "still_link") {
			$('.port_top').html('');
		}
		loadContent($(this).html(), parent);

	});
	
	/* CONTENT */
	//ajax to get content	
	var current = 1;
	function loadContent(title, parent) {
		var parentId = parent.attr('id');
		$('#port_description').fadeTo(1,0);
		//get & load the description
		$.get(
			"site.php",
			{ type: 'description', title: title, parent: parentId, content: true },
			function(data) {
				$('#port_description').html(data).fadeTo(300,1);
			}
		);
		//get & load the content
		$.get(
			"site.php",
			{ type: 'content', title: title, parent: parentId, content: true },
			function(data) {
				$('#port_item').html(data);
				initializeVideoLinks();
			}
		);
		//get links for multiple images
		if (parentId == "still_links" || parentId == "design_links") {
			$.get(
				"site.php",
				{ type: 'content', parent: parentId, title: title, images: true },
				function(data) {
					$('.port_top').html(data);
					initializeStillLinks();
				}
			);			
		}

	}
	//prev & next links for still images
	function initializeStillLinks() {
		current = 1;
		var totalCount = $('#total').html();
		$('.next_image').click(function() {
			if (current == totalCount) {
				current = 0;				
			} 
				current++;
				$('#current').html(current);
				tempId = 'still_img'+current;
				tempDesc = '.still_desc'+current;
				var i = 1;
				$('.still_item').each(function() {
					loopDesc = '.still_desc'+i;
					if ($(this).hasClass(tempId)) {
						$(this).css({'display':'block','opacity':'0'});
						$(this).fadeTo(800,1);						
						$(tempDesc).css({'display':'block','opacity':'0'});
						$(tempDesc).fadeTo(200,1);		
					} else {
						$(this).css({'display':'none','opacity':'0'});
						$(loopDesc).css({'display':'none','opacity':'0'});
					}
					i++
				});		
		});
		$('.next_link').click(function() {
			if (current == totalCount) {
				current = 0;				
			} 
				current++;
				$('#current').html(current);
				tempId = 'still_img'+current;
				tempDesc = '.still_desc'+current;
				var i = 1;
				$('.still_item').each(function() {
					loopDesc = '.still_desc'+i;
					if ($(this).hasClass(tempId)) {
						$(this).css({'display':'block','opacity':'0'});
						$(this).fadeTo(800,1);						
						$(tempDesc).css({'display':'block','opacity':'0'});
						$(tempDesc).fadeTo(200,1);		
					} else {
						$(this).css({'display':'none','opacity':'0'});
						$(loopDesc).css({'display':'none','opacity':'0'});
					}
					i++
				});		
		});
		$('.prev_link').click(function() {
			if (current == 1) {
				current = totalCount;
				current++;				
			} 
				current--;
				$('#current').html(current);
				tempId = 'still_img'+current;
				tempDesc = '.still_desc'+current;
				var i = 1;
				$('.still_item').each(function() {
					loopDesc = '.still_desc'+i;
					if ($(this).hasClass(tempId)) {
						$(this).css({'display':'block','opacity':'0'});
						$(this).fadeTo(800,1);						
						$(tempDesc).css({'display':'block','opacity':'0'});
						$(tempDesc).fadeTo(200,1);		
					} else {
						$(this).css({'display':'none','opacity':'0'});
						$(loopDesc).css({'display':'none','opacity':'0'});
					}
					i++
				});
		});
	}
	
	//video links
	function initializeVideoLinks() {
		$('#video').hide();
		$('#playVideo').click(function() {
			$(this).hide();
			$('#video').show();
		});
	}

});