$(document).ready(function() {
	
	$('#site_container').hide();
	$('#site_logo a').click(function() {
		$('#site_logo').remove();
		$('#site_container').show();
	});
	
	/* LOGO */
	$('.logo').click(function() {
		$('#navigation a, #media_links a').each(function() {
			$(this).removeClass('selected');
		});
		$('#port_description').html('');
		$('.port_top').html('');
		$('#port_item').html('');
	});
	
	/* NAVIGATION */
	//hide sub link containers
	$('#media_links').hide(); 
	
	//Basically show meida link
	var lastClicked = null;
	$('.media, .previouswork, .about, .contact').click(function() {
			$('#navigation a, #media_links a').each(function() {
				$(this).removeClass('selected');
			});
			
			var className = $(this).attr('class');
			id = '#'+ className +"_links";
			
			$(this).addClass('selected');
			
			
			if (lastClicked != null && lastClicked != id) {
				$(lastClicked).hide();
			} 
						
			lastClicked = id;
			if ($(id).is(":hidden")) {
	        	$(id).slideDown("fast", function() {
					//loads a random media item...
					var length = Number($('#media_links a').length)-1;
					var rand = randomXToY(0,length,0);
					$('#media_links a:eq('+rand+')').trigger('click');
				});
	      	} else {
	        	$(id).hide();
	      	}	
	     
			//hide content when nav section clicked
			$('#port_description').html('');
			$('.port_top').html('');
			$('#port_item').html('');
	});
	
	//function to get random number upto m
	function randomXToY(minVal,maxVal,floatVal)
	{
		var randVal = minVal+(Math.random()*(maxVal-minVal));
	  	return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
	}
	
	//about page
	$('.about').click(function() {
		$('#port_description').html('');
		$('.port_top').html('');		
		$(lastClicked).hide();
		$.get(
			'pages/about.html',
			{ },
			function (html) {
				$('#port_item').html('<div class="page">'+ html +'</div>');
			}
		);
	});
	
	//previouswork page
	$('.previouswork').click(function() {
		$('#port_description').html('');
		$('.port_top').html('');		
		$(lastClicked).hide();
		$.get(
			'pages/previouswork.html',
			{ },
			function (html) {
				$('#port_item').html('<div class="page previouswork-container">'+ html +'</div>');	
				$('.previouswork-container').jScrollPane();
			}
		);
	});
	
	//contact page
	$('.contact').click(function() {
		$('#port_description').html('');
		$('.port_top').html('');
		$(lastClicked).hide();
		$.get(
			'pages/contact.html',
			{ },
			function (html) {
				$('#port_item').html('<div class="page">'+ html +'</div>');	
			}
		);
	});
		

	
	$('#media_links > a').click(function() {
		$('#media_links > a').each(function() {
			$(this).removeClass('selected');
		});
		
		var parent = $(this).parent('div'); //section
		$(this).addClass('selected');
		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();
			}
		);

	}
	
	//video links
	function initializeVideoLinks() {
		$('#video').hide();
		$('#playVideo').click(function() {
			$(this).hide();
			$('#video').show();
		});
	}

});
