var floaters = {'td_nag': '<div class="tdrive_window" style="left:265px; top:155px;z-index:26;" id="td_nag">\n\t<div class="tdrive_titlebar">\n\t\t<h1>Please Sign In for Full Access</h1> \n\t\t<div class="closeIcon">\n\t\t\t<a href="javascript:void(0);" id="hidden_closewin3" onclick="TestDriveManager.hide_current_window();return false;"><img src="/images/x.gif" width="10" height="10" alt="" border="0" style="border:0;margin:0;padding:0;" /></a> \n\t\t</div>\n\t</div>\n\t<div class="tdrive_content">\n\t\t<div id="nag_top">\n\t\t\t<div id="td_nag_folders" class="nag_section">\n\t\t\t\t<h2>About My Folders:</h2>\n\t\t\t\t<p>Use folders to save and organize your favorite Teachers\' Domain resources, sort your collections, and write notes specific to each resource. Folders are useful for making presentations and creating lesson plans.</p>\n\t\t\t</div>\n\t\t\t<div id="td_nag_download" class="nag_section">\n\t\t\t\t<h2>About Downloading Resources:</h2>\n\t\t\t\t<p>Many Teachers\' Domain resources are available for downloading, sharing, and remixing. This feature allows you the flexibility to embed video or audio into presentations, give compilations of resources to your students, or to create derivative works and mashups.</p>\n\t\t\t</div>\n\t\t\t<div id="td_nag_groups" class="nag_section">\n\t\t\t\t<h2>About My Groups:</h2>\n\t\t\t\t<p>Share access to resources you\'ve saved and notes you\'ve added.</p>\n\t\t\t\t<p>Groups can be useful for providing resources to students, collaborating with colleagues, sharing ideas, or creating portfolios.</p>\n\t\t\t</div>\n\t\t\t<div id="td_nag_profile" class="nag_section">\n\t\t\t\t<h2>About My Profile:</h2>\n\t\t\t\t<p>Customize your Teachers\' Domain experience by specifying the grade range you teach, the state standards you use, and the accessibility features you need. You can also change your user name and password and sign up for email alerts.</p>\n\t\t\t</div>\n\t\t\t<div id="td_nag_complete" class="nag_section">\n\t\t\t\t<h2 style="color:#cc0000;">Your "Test Drive" is now complete.</h2>\n\t\t\t\t<p>You have viewed all seven resources permitted in this limited trial period. You may continue to browse the site, but to view, download, share, and save resources, you must register now.</p>\n\t\t\t</div>\n\t\t</div>\n\t\t<div id="nag_bottom">\n\t\t\t<div id="nb_feature" class="nag_section">\n\t\t\t\t<p>This Teachers\' Domain feature requires registration, which is simple, safe, and free. <a href="javascript:void(0);" onclick="TestDriveManager.pop_learn_more(\'standard\');">Learn more</a></p>\n\t\t\t</div>\n\t\t\t<div id="nb_about" class="nag_section">\n\t\t\t\t<h2>About Registration:</h2>\n\t\t\t\t<p>Registering with Teachers\' Domain is free and allows you to:</p>\n\t\t\t\t<ul>\n\t\t\t\t\t<li>&#8226; View as many resources as you like</li>\n\t\t\t\t\t<li>&#8226; Save, sort, and share resources using My Folders and My Groups</li>\n\t\t\t\t\t<li>&#8226; Download resources to your desktop</li>\n\t\t\t\t\t<li>&#8226; See standards correlations for your state</li>\n\t\t\t\t</ul>\n\t\t\t</div>\n\t\t\t<div id="nb_reg" class="nag_section">\n\t\t\t\t<form action="javascript:void(0);">\n\t\t\t\t\t<input type="submit" class="button" value="Register Now" onclick="redirect_to(\'/register/\');return false;" /> \n\t\t\t\t</form>\n\t\t\t</div>\n\t\t\t<div id="nb_complete" class="nag_section">\n\t\t\t\t<p>Registration is simple, safe, and free!<br /><a href="javascript:void(0);" onclick="TestDriveManager.pop_learn_more(\'over\');">Learn more</a></p>\n\t\t\t</div>\n\t\t</div>\n\t</div>\n</div>\n'};
/**
TestDrive:

For this version of TestDrive, we want to allow the user to do as 
much as possible on the site without logging in. So, they should be able 
to do everything but view media, and use the top 'My *' functionality. 
Standards should return national and MCREL standards with a caveat that 
if they were logged in they could get their state standards as well. Search 
should function as well. 

TestDrive doesn't start until they actually try to view some media. They'll get 
nags when they try to access the 'My *' functionallity, but it won't start TestDrive.

Weve recently added an "enhanced" test drive for SSO partner users who haven't yet 
tied their td accounts to their sso accounts. They get the TestDrive interface, but don't have a 
limit on the amount of video they can watch.
*/
function TestDriveManager() {
	/**
	Class to manage the test drive process. Everything is done via 
	class methods and properties.
	*/
}
TestDriveManager.test_drive_complete = false;
TestDriveManager.current_nag_div_id = null;
TestDriveManager.max_views = 7;
TestDriveManager.cookie_name = 'tdr';
TestDriveManager.tdrive_display_name = 'Test Driver';


TestDriveManager.set_count_cookie = function(count) {
	/**
	Need to track 'View' clicks across http requests, so
	we'll be tracking them using a cookie. Cookie is made of 
	click_count + cookiesep + session_id
	
	*/
	var session_id = get_cookie_val(seu);
	if (!session_id) {
		session_id = '';
	}
	var cookie_value = count + cookiesep + session_id;
	document.cookie = TestDriveManager.cookie_name + '=' + cookie_value + ';path=/;';
};


TestDriveManager.process_view_click = function() {
	//see if weve started the testdrive process yet:
	if (AuthManager.status != AuthManager.AUTH_TESTDRIVE) {
		TestDriveManager.start_testdrive();
	}
	if (!SSOManager.is_sso_user()) {
	    var count = TestDriveManager.get_view_count();

    	TestDriveManager.update_view_count(count + 1);
    	TestDriveManager.set_view_count_string(count + 1);

    	//see if we need to end testdrive:
    	if (count >= TestDriveManager.max_views) {
    		throw new Error('tdrive_done');
    	}
	}
};

TestDriveManager.update_view_count = function(count) {
	TestDriveManager.set_count_cookie(count);
};

TestDriveManager.get_view_count = function() {
	var count = 0;
	var cookie_val = get_cookie_val(TestDriveManager.cookie_name);
	if (cookie_val) {
		var cookie_bits = cookie_val.split(cookiesep);
		var cookie_count = parseInt(cookie_bits[0]);
		if (!isNaN(cookie_count)) {
			count = cookie_count;
		}
	} 
	return count;
};

TestDriveManager.set_view_count_string = function(count) {
	var remaining = TestDriveManager.max_views - count;
	if (remaining < 0) {
		remaining = 0;
	}
	//set the display string:
	var num_node = document.getElementById('ltd-left-string');
	num_node.innerHTML = remaining;
};

TestDriveManager.start_testdrive = function() {
	AuthManager.status = AuthManager.AUTH_TESTDRIVE;
	TestDriveManager.init_testdrive_content();
};

TestDriveManager.init_testdrive_content = function() {
	/*
	Set up the page for a TestDriver.
	*/
	if (!SSOManager.is_sso_user()) {
	    if (TestDriveManager.get_view_count() >= TestDriveManager.max_views) {
    		TestDriveManager.test_drive_complete = true;
    	}
    	TestDriveManager.set_view_count_string(TestDriveManager.get_view_count());
	}
	TestDriveManager.set_tdrive_login_widget();
	TestDriveManager.set_banner_text();
	
};
TestDriveManager.set_banner_text = function() {
	/*
	Set the User: text in the standard banner.
	*/
	if (SSOManager.is_sso_user()) {
	    TestDriveManager.tdrive_display_name = SSOManager.get_sso_partner_name() + ' ' + TestDriveManager.tdrive_display_name;
    	set_banner_user_name(TestDriveManager.tdrive_display_name);
	} else {
    	set_banner_user_name(TestDriveManager.tdrive_display_name);
	}
};
TestDriveManager.set_tdrive_login_widget = function() {
	/* 
	Change the stuff in the right column of the 
	login widget, and set the remaining views parameter. 
	*/
	
	if (SSOManager.is_sso_user()) {
	    hideElement('ltd-rc-content-sso-pre');
	    var e = document.getElementById('sso-tdr-name');
	    e.innerHTML = SSOManager.get_sso_partner_name();
	    showElement('ltd-rc-content-sso');
	} else {
	    hideElement('ltd-rc-content-1');
	    if (TestDriveManager.test_drive_complete == true) {
    		//it's possible this was visible:
    		hideElement('ltd-rc-content-2');
    		showElement('ltd-rc-content-3p1');
    		showElement('ltd-rc-content-3p2');
    	} else {
    		showElement('ltd-rc-content-2');
    	}
	}
	
	
	changeBorderColor('ltd-right-col','#cc0000');
	changeBackgroundColor('ltd-right-col','#fff');
	//this may have been opened by the ltd-rc-content-1 content:
	hideElement('ltd-rc-learn-more');
};

TestDriveManager.show_nag = function(nag_name) {
	//pop-in the html:
	var insert_div = TestDriveManager.get_insertion_div();
	insert_div.innerHTML = floaters['td_nag'];
	
	
	//hide a previous nag window:
	if (TestDriveManager.current_nag_div_id) {
		hideElement(TestDriveManager.current_nag_div_id);
	}
	TestDriveManager.set_bottom_nag();
	
	//nag id is built:
	TestDriveManager.current_nag_div_id = 'td_nag_' + nag_name;
	//TO DO: talk to Chris W. about this. It seems better to continue 
	//to explain what the functionality is, rather then just saying tdrive over:
	/* 
	if (TestDriveManager.test_drive_complete) {
		TestDriveManager.current_nag_div_id = 'td_nag_complete';
	}
	*/
	showElement(TestDriveManager.current_nag_div_id);
	window.scrollTo(0,0);
	return false;
	
};


TestDriveManager.hide_current_window = function() {
	/*
	Getting the insertion div, automatically removes 
	any previous incantation of the insertion div.
	*/
	var div = TestDriveManager.get_insertion_div();
	TestDriveManager.set_bottom_nag();
};

TestDriveManager.get_insertion_sibling = function() {
	var main_content = document.getElementById('main_content');
	return main_content;
};

TestDriveManager.get_insertion_div = function() {
	/*
	Dynamically insert Test Drive 'windows' into the page. The 
	insertion div will be put into the page right before the main_content 
	div, and will have it's innertHTML property set by various functions.
	*/
	var tdrive_div = document.getElementById('test_drive_container');
	if (tdrive_div) {
		document.body.removeChild(tdrive_div);
	} 
	
	var insert_sibling = TestDriveManager.get_insertion_sibling();
	tdrive_div = document.createElement('div');
	tdrive_div.id = 'test_drive_container';
	document.body.insertBefore(tdrive_div,insert_sibling);
	return tdrive_div;
	
};

TestDriveManager.pop_learn_more = function(source) {
	/*
	Do the 'learn more' link in the nag pop-ups.
	*/
	if (source == 'over') {
		hideElement('nb_complete');
	} else if (source == 'standard') {
		hideElement('nb_feature');
	}
	showElement('nb_reg');
	showElement('nb_about');
};

TestDriveManager.set_bottom_nag = function() {
	/* Reset the 'learn more' stuff: */
	if (TestDriveManager.test_drive_complete == true) {
		showElement('nb_complete');
	} else {
		showElement('nb_feature');
	}
	hideElement('nb_about');
	hideElement('nb_reg');
};


TestDriveManager.lock_down = function() {
	/**
	Re-write links and form submissions actions as necessary to support 
	TestDrive functionality:
	
	
	*/
	for (var i = 0;i < document.links.length;i++) {
		var link = document.links[i];
		//ie6 pathname attribute bug:
		var ie666_pathname = '/' + link.pathname;
		if (should_ignore(link)) {
			continue;
		//My Folders button:
		} else if (link.id == 'pulldown_link') {
			link.onmousedown = function() {void(0);};
			link.onclick = function() {TestDriveManager.show_nag('folders');};
		//My Groups button:
		} else if (link.pathname == my_groups || ie666_pathname == my_groups) {
			link.href = "javascript:void(0);";
			link.onclick = function() {TestDriveManager.show_nag('groups');};
		//My Profile button:
		} else if (link.pathname == my_profile || ie666_pathname == my_profile) {
			link.href = "javascript:void(0);";
			link.onclick = function() {TestDriveManager.show_nag('profile');};
		//most text links to assets will look like this:
		} else if (link.pathname.search(/^\/assets\//) == 0 || ie666_pathname.search(/^\/assets\//) == 0) {
			link.href = "javascript:void(0);";
			link.onclick = view_click_handler;
		//Save lesson plan link and Save to a folder link:
		} else if (link.id == 'save_lp_action' || link.id == 'save_to_action') {
			link.onmousedown = function() {void(0);};
			link.onclick = function() {TestDriveManager.show_nag('folders');};
		} 
	}
	
	for (i = 0;i < document.forms.length;i++) {
		var f = document.forms[i];
		if (should_ignore(f)) {
			continue;
		}
	
		//need to check onclick handlers for View buttons:
		for (var j = 0;j < f.elements.length;j++) {
			var e = f.elements[j];
			if (e.type == 'submit' && e.value == 'View' && !should_ignore(e) && e.onclick) {
				//save the original onclick action:
				e.original_onclick = e.onclick;
				e.onclick = view_click_handler;
			} else if (e.type == 'submit' && e.value == 'Download' && !should_ignore(e) && e.onclick) {
				e.onclick = function() {TestDriveManager.show_nag('download');};
			}
		}
	}
	
	function view_click_handler() {
		try {
			TestDriveManager.process_view_click();
			//run the original onclick action:
			this.original_onclick();
		} catch(ex) {
			TestDriveManager.test_drive_complete = true;
			TestDriveManager.set_tdrive_login_widget();
			TestDriveManager.show_nag('complete');
		}
		
	}
	
	
	function should_ignore(element) {
		/* 
		Ignore any element that has a class name of 'tdr-ignore'.
		*/
		var cnames = get_class_names_by_element(element);
		var rval = false;
		for (var array_index in cnames) {
			if (cnames[array_index] == 'tdr-ignore') {
				rval = true;
				break;
			}
		}
		return rval;
	}
};


