// slide elements into place
// ################################################################################

if(typeof console == 'undefined') {
	var console = {}
	console.log = function() {
    
		}
}

// show/hide imagetool
slidingPaths = function(h) {
	var $_paths = jQuery("#paths");
	if (h > 0) {
		jQuery('#paths object, #paths embed').show();
		jQuery('#paths object').attr('height', h);
	} else {
		jQuery('#paths object, #paths embed').hide();
	}
	$_paths.animate({
		height: h + "px"
	}, {
		duration: 250,
		easing: "easeOutQuad",
		queue: false
	});
};

// sticky nav
slidingNav = function(winH) {
	var nav = jQuery("nav");
	
	if(jQuery("a#toggler_path").hasClass("active")) {
		if(jQuery(this).scrollTop() > winH) {
			jQuery("nav").css({
				"position":"fixed",
				"top":"0px"
			});
		} else {
			jQuery("nav").css({
				"position":"relative",
				"top":"0"
			});
		}
	} else {
		jQuery("nav").css({
			"position":"fixed"
		});
	}
}

togglerToggle = function(link) {
	link = jQuery(link);
	if(link.hasClass('close')) {
		link.removeClass('close').removeClass('open');
		jQuery(link.attr('data-selector')).removeClass('disabled').removeClass('enabled');
		
		link.addClass('open');
		jQuery(link.attr('data-selector')).addClass('enabled').slideDown();
	} else {
		link.removeClass('close').removeClass('open');
		jQuery(link.attr('data-selector')).removeClass('disabled').removeClass('enabled');
		
		link.addClass('close');
		jQuery(link.attr('data-selector')).addClass('disabled').slideUp();
	}
}

function getParameterByName(name)
{
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if(results == null)
		return "";
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}


// REGULATORS! MOUNT UP! LET'S RIDE!
// ################################################################################
jQuery(function($) {
	});

(function($) {
	Drupal.behaviors.mainNav = {};
	Drupal.behaviors.mainNav.attach = function(context) {
		var winH = $(window).height() - 101;
		$(window).resize(function() {
			winH = $(window).height() - 101;
			if($("a#toggler_path").hasClass("active")) {
				slidingPaths(winH);
			}
		});
		$(window).scroll(function() {
			slidingNav(winH);
		});
		Drupal.settings.itool = $("#paths div:first").detach(); // remove from dom to reduce load when not in use
		$("a#toggler_path", context).click(function(e) {
			if(!$(this).hasClass('system')) {
				$("a.toggler[href*='/user'].open").addClass('system').click();
			}
			$(this).removeClass('system');

			e.preventDefault();			
			setTimeout(function () {
				$("body, html").animate({
					"scrollTop": "0px"
				});				
			}, 300);
			if($(this).hasClass("active")) {
				$(this).removeClass("active");
				var swf = $('#paths object, #paths embed');
				if (swf.length) {
//					swf[0].pausePath();
				}

				$(this).find("span").text("N").css({
					"text-transform":"uppercase"
				});
				slidingPaths(0);
				$("nav header .logo").fadeIn();
			} else {
				if (Drupal.settings.itool !== false) {
					Drupal.settings.itool.appendTo("#paths"); // add it back, once
					Drupal.settings.itool = false;
				}  
				$(this).addClass("active");
				$(this).find("span").text("r").css({
					"text-transform":"lowercase"
				});
				slidingPaths(winH);
				slidingNav(winH);
				$("nav header .logo").fadeOut();
			}
		});
    
		if(getParameterByName('play')) {
			$("a#toggler_path", context).click();
		}
		$("a.path_play").click(function(e) {
			e.preventDefault();
			$("a#toggler_path", context).click();
		});

		$("form.unsubmitted *", context).removeClass('error');
    
		$('.toggler', context).each(function() {
			var link = $(this);
			if($(link).hasClass('open')) {
				$(link.attr('data-selector')).addClass('enabled').show();
			}
			else {
				$(link).addClass('close');
				$(link.attr('data-selector')).addClass('disabled').hide();
			}
		});
		$('.toggler', context).click(function(e) {
			e.preventDefault();
			if(!$(this).hasClass('system')) {
				$("a#toggler_path.active").addClass('system').click();
				
				if ($(this).attr('data-selector') == '#nav-login') {
					if ($(this).hasClass('close')) {
						$("#flashObj").hide();
					} else {
						$("#flashObj").show();
					}
					
				}
			}
			$(this).removeClass('system');
			togglerToggle(this);
		});

		$('.links li', context).hover(function(e) {
			if($("a#toggler_path.active").length == 0) {
				$('.sub-links', $(this)).addClass('enabled');
			}
		}, function(e) {
			$('.sub-links', $(this)).removeClass('enabled');
		});

		$("a.randomized").each(function(i, e) {
			var href = $(this).attr('href');
			var d = new Date();
			href += href.indexOf('?') ? '?' : '&';
			href += '_r='+Math.floor(Math.random()*999999)+'-'+d.getTime();
			$(this).attr('href', href);
		});
		$(".noflash-overlay", context).overlay({
			mask: {
				// you might also consider a "transparent" color for the mask
				color: '#fff',
				// load mask a little faster
				loadSpeed: 200,
				// very transparent
				opacity: 0.5
			},
			load: true
		});
	}


})(jQuery);;

