jQuery.noConflict();
jQuery(document) .ready(function($) {

// Gimme function to retreive url parameters
$.extend({
  getUrlVars: function(){
    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;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


	//--Setup--//

	// Retreive cookies and url parameters
	var firstLinkRequested = $.cookie("firstLinkRequested");
	var haveUserInfo = $.cookie("haveUserInfo");
	var downloadFirstLink = $.getUrlVar("downloadFirstLink");

	// Move form to be a direct child of body
	$("body > form").prepend("<div id=\"resourcesFormContainer\"></div>");
	$(".resource-center #mainContent-section .element > .form_container").prependTo( $("#resourcesFormContainer") );
	$(".resource-center > form > #resourcesFormContainer .form_container").prepend("<button id=\"closeForm\">Close</button>");
	$("#closeForm").click(function() {
		hideForm();
		return false;
	});

	// When form submit button is clicked, make sure we are scrolled to the top of the page
	$(".resource-center > form > #resourcesFormContainer .submitButton").click(function() {
		$("html").scrollTop(0);
	});


	// When a resource link is clicked...
	$(".resource-center .linksModule a").click(function() {
		// Make sure we've got the latest value of the cookie
		var haveUserInfo = $.cookie("haveUserInfo");
		// If we've already collected the user's information (boolean stored in cookie),
		// load the requested file
		if(haveUserInfo == "true") {
			window.location.href = $(this).attr("href");
		}
		// If we haven't collected the user's information yet, show the form and set 
		// a cookie for the first url they clicked (so we can load it automatically
		// after they submit the form)
		else {
			showForm();
			$.cookie("firstLinkRequested", $(this).parent("li").attr("id"));
		}
		// Return false so the link clicked on doesn't load
		return false;
	});


	// After the user has submitted the form successfully...
	// The form loads the page again with the downloadFirstLink paramater set to 1.
	// This also makes sure we have been passed a link id and that this is the first 
	// time we've been passed downloadFirstLink.
	if(downloadFirstLink == "true" && typeof firstLinkRequested == "string") {
		// Set cookie so user doesn't need to fill form anymore
		$.cookie("haveUserInfo", "true", { expires: 365, path: '/' });
		// Load the link the user wanted before they had to fill out the form
		var firstLinkRequestedHref = $("#"+firstLinkRequested).children("a").attr("href");
		location.href = firstLinkRequestedHref;
	}


	// Show the form
	function showForm() {
		$(".resource-center > form > #resourcesFormContainer").show();
		$("body").css("overflow", "hidden");
	}
	// Hide the form
	function hideForm() {
		$(".resource-center > form > #resourcesFormContainer").hide();
		$("body").css("overflow", "auto");
	}

});
