

/*///////////////////////////////////////////////////////
Home Page 'Explore' Tab
///////////////////////////////////////////////////////*/
var delveImages = new Array();
var delveDelays = 1000;
var delveZindex = '20';

(function($){
	$.fn.shuffle = function() {
		return this.each(function() {
			var items = $(this).children();
			return (items.length) ? $(this).html($.shuffle(items)) : this ;
		});
	}

	$.shuffle = function(arr) {
		for (
			var j, x, i = arr.length; i;
			j = parseInt(Math.random() * i),
			x = arr[--i], arr[i] = arr[j], arr[j] = x
		);
		return arr;
	}
})(jQuery);


// De-Obfuscate Email Addresses
jQuery.fn.deobfuscate = function() {
	return this.each(function() {
		// Original Usage: <span class="email">user(put an @ here)example.com</span>
		// var email = String($(this).html()).replace(/\s*\(.+\)\s*/, "@");

		// Usage: <span class="email">user[at]example.com</span>
		var email = String($(this).html()).replace('[at]', '@');
		$(this).before('<a href="mailto:' + email + '">' + email + "</a>").remove();
	});
};



$(document).ready(function() {
	// De-Obfuscate Email Addresses
	$('.email').deobfuscate();

	$('#home #nav-delve > a').mouseenter(function () {
		$('#home #slider').data('nivo:vars').stop = true; // Pause the Slider

		$(this).stop().animate({ height: "100px" }, 250);

		$('#home #panel-delve').fadeIn('500');

		$('#home #panel-delve > div > img').delay(500).fadeIn(500);

		delveImages = [1,2,3,4];
		delveImages = $.shuffle(delveImages);

		for (i=0; i<=delveImages.length; i++) {
			// $('#delve-0'+delveImages[i]).delay(delveDelays).fadeIn('750');
			$('#delve-0'+delveImages[i]).delay(1000).fadeIn(150);
			$('#delve-0'+delveImages[i]).css('zIndex', delveZindex);

			delveDelays = delveDelays + 550;
			delveZindex = delveZindex + 5;
		}

	});


	$('#home #nav-delve > a').mouseleave(function () {

		$(this).stop().animate({ height: "80px" }, 250);

		$('#home #panel-delve').fadeOut('250');

		$('#home #panel-delve > div > img').fadeOut(0);

		for (i=0; i<=delveImages.length; i++) {
			$('#delve-0'+delveImages[i]).fadeOut(0);
		}

		$('#home #slider').data('nivo:vars').stop = false; // Start the Slider again
	});
});


/*///////////////////////////////////////////////////////
Home Page 'Understand' Tab
///////////////////////////////////////////////////////*/
$(document).ready(function() {

	$('#home #nav-learn > a').mouseenter(function () {
		$('#home #slider').data('nivo:vars').stop = true; // Pause the Slider

		$(this).stop().animate({ height: "100px" }, 250);
	});


	$('#home #nav-learn > a').mouseleave(function () {

		$(this).stop().animate({ height: "80px" }, 250);

		$('#home #slider').data('nivo:vars').stop = false; // Start the Slider again
	});
});


/*///////////////////////////////////////////////////////
Home Page 'Share' Tab
///////////////////////////////////////////////////////*/
$(document).ready(function() {

	$('#home #nav-share > a').mouseenter(function () {
		$('#home #slider').data('nivo:vars').stop = true; // Pause the Slider

		$(this).stop().animate({ height: "100px" }, 250);

		$('#home .nivo-caption').fadeOut(0);
		$('#home #panel-share').fadeIn('500');
	});


	$('#home #nav-share > a').mouseleave(function () {

		$(this).stop().animate({ height: "80px" }, 250);

		$('#home #panel-share').fadeOut('250');
		$('#home .nivo-caption').fadeIn(0);

		$('#home #slider').data('nivo:vars').stop = false; // Start the Slider again
	});
});


/*///////////////////////////////////////////////////////
Home Page Slider
///////////////////////////////////////////////////////*/
$(document).ready(function() {
	$('#slider').nivoSlider({
		effect:'fade', 				// Specify sets like: 'random,fold,fade,sliceDown'
		slices:1,
		animSpeed:200,
		pauseTime:10000,
		startSlide:0, 				// Set starting Slide (0 index)
		directionNav:true, 			// Next & Prev
		directionNavHide:true, 			// Only show on hover
		controlNav:true, 			// 1,2,3...
		controlNavThumbs:false, 		// Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, 		// Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', 	// Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', 	//...this in thumb Image src
		keyboardNav:true, 			// Use left & right arrows
		pauseOnHover:true, 			// Stop animation while hovering
		manualAdvance:false, 			// Force manual transitions
		captionOpacity:1.0, 			// Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} 		// Triggers after all slides have been shown
	});
});


/*///////////////////////////////////////////////////////
Home Page Tooltips
///////////////////////////////////////////////////////*/
/*
$(document).ready(function() {
	$("#home #welcome a[title]").tooltip({

	   // Tweak the position
	   offset: [10, 0],

	   // Use the "slide" effect
	   effect: 'slide'

	// Add dynamic plugin with optional configuration for bottom edge
	}).dynamic({ bottom: { direction: 'down', bounce: true } });
});
*/


/*///////////////////////////////////////////////////////
Onload Events
///////////////////////////////////////////////////////*/
$(document).ready(function() {

	// Current Page Selection
	$(function(){
		var path = location.pathname.substring(1);
		if ( path ) {
			$('#subnav a[href$="' + path + '"]').attr('class', 'selected');
		}

		var pageTitle = $('#content h3').text();
		if ( pageTitle ) {
			$('#subnav li li ul').hide();
			$('#subnav a:contains('+pageTitle+')').parent().attr('class', 'active');
			$('#subnav a:contains('+pageTitle+')').next().show();
		}

	});


	// Home Page Downloads
	//$('#home #related div a').colorbox();
	//$('#home #related div a').colorbox();


	// Home Page PopUp
	if ($('#home').length) {
		//$.fn.colorbox({width:"50%", height:"50%", inline:true, href:'#draft'});
	}


	// Colorboxes for various functions
	$("a.geo").colorbox({width:'700px', height:'550px', iframe:true});
	$("a[rel='gallery']").colorbox({preloading:true, current:''});
	$("a[rel='example3']").colorbox({transition:'none', width:'75%', height:'75%'});



	// AJAX Image Status
	$('a.status').css('display','block');
	$('a.status').click(function(e){
		// Stop normal link click
		e.preventDefault();

		var thisImage 	= $(this).attr('id');
		var ajaxHREF 	= $(this).attr('href');
		var newResult 	= ($(this).text()=='Make Private') ? 'Make Public' : 'Make Private';
		var newTitle 	= ($(this).text()=='Make Private') ? 'This image is currently private. Make this image public' : 'This image is currently public. Make this image private';
		var newHREF 	= ($(this).text()=='Make Private') ? 'share' : 'unshare';
		var newHREF 	= ajaxHREF.replace(/action=([a-z]+)&/,'action='+newHREF+'&');

		// Send request
		$.ajax({
			async: false,
			url: ajaxHREF,
			error: function(){
				alert('An error occurred trying to change the activation status.');
			},
			success: function(data) {
				thisImage = $('#'+thisImage);
				if (thisImage.text()=='Delete') {
					thisImage.parent().hide();
				} else {
					thisImage.text( newResult );
					thisImage.attr('href',newHREF);
					thisImage.attr('title',newTitle);
				}

				$('#message').text( data );
				$('#message').fadeIn(300).delay(1000).fadeOut(800);
			}
		});
	});
});

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

