// JavaScript Document

function launchWindow(id) 
{
	
		//Get the screen height and width
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		jQuery('#mask').fadeIn(1000);	
		jQuery('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
              
		//Set the popup window to center
		jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
		jQuery(id).css('left', winW/2-jQuery(id).width()/2);
	
		//transition effect
		jQuery(id).fadeIn(2000); 
}

function hideAddThis() {
	var toolbox = jQuery('.addthis_toolbox');
	toolbox.hide();
	jQuery('#content').css('padding-top','28px');
}

function showAddThis() {
	var toolbox = jQuery('.addthis_toolbox');
	toolbox.show();
	jQuery('#content').css('padding-top','0');
}

jQuery(function () {
	// When a link where name=modal is clicked
	jQuery('a[name=modal]').click(function (e) {

		//Cancel the link behavior
		e.preventDefault();

		//Get the A tag
		var id = jQuery(this).attr('href');

		// Call the function to create the modal window
		launchWindow(id);

		// Hide the google+ icon since I can't make it stay behind the video players
		hideAddThis();
	});

	//if close button is clicked
	jQuery('.close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();

		// Show google+ now that the modal window is going away
		showAddThis();

		// Hide the modal window, returning page to normal.
		jQuery('#mask, .window').hide();

	});

	//if mask is clicked
	jQuery('#mask').click(function () {
		// Show google+ now that the modal window is going away
		showAddThis();

		// hide the mask and the window.
		jQuery(this).hide();
		jQuery('.window').hide();
	});

});


// based on http://www.queness.com/post/77/simple-jquery-modal-window-tutorial
// this method doesn't use #boxes because boxes is deep in the div soup and it screws up IE7's z-index rules.
function launchGatedForm(docid, ssd) 
{
	
	function readCookie(name)
	{
		// borrowed from http://www.headfirstlabs.com
		var searchName = name + "=";
		var cookies = document.cookie.split(';');
		for(var i=0;i < cookies.length;i++) 
		{
			var c = cookies[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(searchName) == 0) return c.substring(searchName.length,c.length);
		}
		return null;
	}
	// Check to see if cookie is true
	if (readCookie('gatedform') == "true") 
	{
		// if true, start download directly and bypass the rest of this function (return)
		window.location = "http://www.lifesize.com/~/media/" + docid + ".ashx";
		return;
	}
	
	
	// check for <div id="gatedform".
	if (document.getElementById("gatedform") != null) 
	{
		// if exists, delete (to be replaced with below)
		// .remove() deletes the element and attached events and data. use detach() instead to just delete the element if there are problems.
		jQuery("#gatedform").remove()
	}

	// this adds the gated form div to the bottom of the body element.
	// the gated form div contains an iframe to the short gated form, which includes the docid as a parameter.
	// The docid is supplied by the content editor when they call this function with OnClick. 
	// i.e. <a href="#" OnClick="launchGatedForm('database-id-less-dashes-and-brackets')">Link Text</a>
	// couldn't get the close link for this to work. see mkt-1598 worklog
	jQuery("body").append('<div id="gatedform" class="window"><iframe id="modaliframe" frameborder="0" width="500px" height="400px" scrolling="auto" src="/landingpages/gated-forms/basic.html?docid=' + docid + '&ssd=' + ssd +'"></iframe></div>');
	
	// This runs all the jquery stuff to pull up the mask and raise the div that is sent in the argument.
	launchWindow('#gatedform');
}
 

