/* functions for adding html after a tag */
function insertAfter(new_node, existing_node) {
	if (existing_node.nextSibling) {    
		existing_node.parentNode.insertBefore(new_node, existing_node.nextSibling);
	} else {   
		existing_node.parentNode.appendChild(new_node);
	}
}

function createTagNode(tagName, value) {
	var newNode = document.createElement(tagName);
	newNode.innerHTML = value;
	return newNode;
}



/* REGISTRAION PAGE */
function fixRegistrationPage(){
	if (document.getElementById('ka_mainContainer').className.indexOf('ka_registerUser') >= 0){
//		$('ka_joinUser').getElementsByTagName('label')[0].innerHTML = "Full Name:";
		$('ka_joinUser').getElementsByTagName('div')[0].innerHTML = "This will be the name you use to log into the site. We encourage you to use your real name, which can include spaces.<br> No symbols. Max 24 characters. Example: <em>JohnSmith</em> or <em>John Smith.</em> ";	
	}
	
	//text saying you must be 18 yrs old...		
	if (document.getElementById('ka_joinDOB')) {
		var elm = document.getElementById('ka_joinDOB');
		for (index = 0; index < elm.childNodes.length; index++) {
			if (elm.childNodes[index].tagName == 'LABEL') {
				elm.childNodes[index].innerHTML = elm.childNodes[index].innerHTML + "<div style=\"font-size:11px;font-weight:normal;color:#000;\">You must be 18 or older to contribute to <em>Get My Vote</em>.</div>";
				break;
			}
		}
	}	
		
}



/* UPLOAD PAGE */
function fixUploadPages(){

	// Fix the Upload Thumbnail section
	if (document.getElementById('ka_singleScreen')){
//		 var thumbnail_element = $('ka_singleScreen');
//		 $('ka_catWrap').insert({after: thumbnail_element});
//		 $('ka_singleScreen').parentNode.removeChild($('ka_singleScreen'));
//		 $('ka_emailFriendsWrap').parentNode.insertBefore(thumbnail_element, $('ka_emailFriendsWrap'));
		 $('ka_singleScreen').className = "hideme";
	}
	
	/* video page */
	if (document.getElementById('ka_mainContainer').className.indexOf('ka_newVideoUpload') >= 0){
			
		// changed "video recording" text
		$('ka_chooseFormat').getElementsByTagName('li')[1].innerHTML = $('ka_chooseFormat').getElementsByTagName('li')[1].innerHTML.replace("Video Recording", "Record Now Using WebCam");
		
		// adding intro text		
		if (document.getElementById('ka_manageContent')) {
			var h3Tag = $('ka_manageContent').getElementsByTagName('h3')[0];
			var newNode = createTagNode('p', '<strong>Commentary Tips:</strong> 1) Be personal; talk about your own experiences. 2) Tell us a <em>story.</em> 3) Stay on topic and under two minutes. 4) No endorsements or recruiting.  <a href="http://www.npr.org/getmyvote/sharestory.html">More Tips &raquo;</a>');
			insertAfter(newNode, h3Tag);
		}			
		
		// What are tags link
		if (document.getElementById('ka_tagsWrap')) {
			var elm = document.getElementById('ka_tagsWrap');
			for (index = 0; index < elm.childNodes.length; index++) {
				if (elm.childNodes[index].tagName == 'LABEL') {
					elm.childNodes[index].innerHTML = elm.childNodes[index].innerHTML + "<div style=\"padding:2px 10px;background-color:#f5f5f5;font-weight:normal;color:#000;font-size:11px;\">Use quote marks for phrases. Example: <em>Iraq \"national security\" troops</em> &nbsp;&nbsp;<a href=\"javascript:void(0);\" onclick=\"document.getElementById('tagpop2').style.display='block';\" class=\"whataretags2\">What are tags?</a></div><div class=\"tagpopwrap\"><div id=\"tagpop2\"><a href=\"javascript:void(0);\" onclick=\"document.getElementById('tagpop2').style.display='none';\" class=\"closetagpop\">close</a><h4>What Are Tags?</h4><p>Tags are words or phrases that describe the subject of a commentary. Adding tags to your commentary will help others to find it. Add as many relevant tags as you like.</p></div><div class=\"spacer\">&nbsp;</div></div>";
					break;
				}
			}
		}

	}
	

	/* audio page */
	if (document.getElementById('ka_mainContainer').className.indexOf('ka_newAudioUpload') >= 0){
	
		// adding intro text		
		if (document.getElementById('ka_manageContent')) {
			var h3Tag = $('ka_manageContent').getElementsByTagName('h3')[0];
			var newNode = createTagNode('p', '<strong>Commentary Tips:</strong> 1) Be personal; talk about your own experiences. 2) Tell us a <em>story.</em> 3) Stay on topic and under two minutes. 4) No endorsements or recruiting.  <a href="http://www.npr.org/getmyvote/sharestory.html">More Tips &raquo;</a>');
			insertAfter(newNode, h3Tag);
		}	
	
		// What are tags link
		if (document.getElementById('ka_fieldAudUpOptional')) {
			var elm = document.getElementById('ka_fieldAudUpOptional');
			for (index = 0; index < elm.childNodes.length; index++) {
				if (elm.childNodes[index].tagName == 'LABEL') {
					var htmlStr = elm.childNodes[index].innerHTML;
					if (htmlStr.indexOf('Tags') > -1) {
						elm.childNodes[index].innerHTML = elm.childNodes[index].innerHTML + "&nbsp;&nbsp;<div style=\"display:inline\"><a href=\"javascript:void(0);\" onclick=\"document.getElementById('tagpop2').style.display='block';\" class=\"whataretags2\">What are tags?</a></div><div class=\"tagpopwrap\"><div id=\"tagpop2\"><a href=\"javascript:void(0);\" onclick=\"document.getElementById('tagpop2').style.display='none';\" class=\"closetagpop\">close</a><h4>What Are Tags?</h4><p>Tags are words or phrases that describe the subject of a commentary. Adding tags to your commentary will help others to find it. Add as many relevant tags as you like.</p></div><div class=\"spacer\">&nbsp;</div></div>";
						break;
					}
				}
			}
		}	
	
	}	
	
	
	/* blog page */
	if (document.getElementById('ka_mainContainer').className.indexOf('ka_addBlog') >= 0){		
		
		// adding intro text		
		if (document.getElementById('ka_manageContent')) {
			var h3Tag = $('ka_manageContent').getElementsByTagName('h3')[0];
			var newNode = createTagNode('p', '<strong>Commentary Tips:</strong> 1) Be personal; talk about your own experiences. 2) Tell us a <em>story.</em> 3) Stay on topic and under 400 words. 4) No endorsements or recruiting.  <a href="http://www.npr.org/getmyvote/sharestory.html">More Tips &raquo;</a>');
			insertAfter(newNode, h3Tag);
		}		

		// What are tags link
		if (document.getElementById('ka_fieldBlogOpt')) {
			var elm = document.getElementById('ka_fieldBlogOpt');
			for (index = 0; index < elm.childNodes.length; index++) {
				//console.debug(elm.childNodes[index].tagName);
				if (elm.childNodes[index].tagName == 'LABEL') {
					var htmlStr = elm.childNodes[index].innerHTML;
					if (htmlStr.indexOf('Tags') > -1) {
						elm.childNodes[index].innerHTML = elm.childNodes[index].innerHTML + "&nbsp;&nbsp;<div style=\"display:inline\"><a href=\"javascript:void(0);\" onclick=\"document.getElementById('tagpop2').style.display='block';\" class=\"whataretags2\">What are tags?</a></div><div class=\"tagpopwrap\"><div id=\"tagpop2\"><a href=\"javascript:void(0);\" onclick=\"document.getElementById('tagpop2').style.display='none';\" class=\"closetagpop\">close</a><h4>What Are Tags?</h4><p>Tags are words or phrases that describe the subject of a commentary. Adding tags to your commentary will help others to find it. Add as many relevant tags as you like.</p></div><div class=\"spacer\">&nbsp;</div></div>";
						break;
					}
				}
			}
		}	
	
	}		
	

	
	// Enforce max title & description links	
	if (document.getElementById('mediaName')){
		$('mediaName').maxLength = 35;
	}
	if (document.getElementById('mediaDescription')){
		$('mediaDescription').setAttribute('maxLength', 100);
	}
	
	// Disable community tagging
	if (document.getElementById('isAllowPublicTagging')){
		 var community_tagging_option = $('isAllowPublicTagging');
		 community_tagging_option.checked = false;
	}
	if (document.getElementById('ka_allowTag')){
		 var community_tagging_div = $('ka_allowTag');
		 community_tagging_div.className = "hideme";
	}	
	
	// Disable community tagging for the blog page
	if (document.getElementById('isPublicTaggingAllowed')){
		var blog_community_tagging_option = $('isPublicTaggingAllowed');
		blog_community_tagging_option.checked = false;
	}	
	
	// Require users to check terms of use each time
	if ((Ka.Info.PAGE == 'pages/newVideoUpload.jsp' || 
			 Ka.Info.PAGE == 'pages/newAudioUpload.jsp' ||
			 Ka.Info.PAGE == 'pages/addBlog.jsp') 
			&& document.getElementById('termsOfUse')){
			
		 var terms_checkbox = $('termsOfUse');
		 terms_checkbox.checked = false;
			
	}
	
	// Disable selection of audio or video blogging formats
	if (Ka.Info.PAGE == 'pages/addBlog.jsp' && 
	document.getElementById('ka_chooseFormat')) {
	
		var blog_format_options = $('ka_chooseFormat');
		blog_format_options.className = "hideme";
		
		$('ka_fieldBlogReq').getElementsByTagName('label')[1].innerHTML = "";
		
		// Enforce title length for blog posts
		if (document.getElementById('name')){
			$('name').maxLength = 35;
		}

	}

}


/* PLAY PAGE */
function fixPlayPage(){
	
	if (document.getElementById('ka_mainContainer').className.indexOf('ka_mediaPlayPage') >= 0){
		
		// What are tags link
		$('ka_tagsInput').innerHTML = $('ka_tagsInput').innerHTML + "(<a href=\"javascript:void(0);\" onclick=\"document.getElementById('tagpop2').style.display='block';\" class=\"whataretags2\">What are tags?</a>)<div class=\"tagpopwrap\"><div id=\"tagpop2\"><a href=\"javascript:void(0);\" onclick=\"document.getElementById('tagpop2').style.display='none';\" class=\"closetagpop\">close</a><h4>What Are Tags?</h4><p>Tags are words or phrases that describe a commentary. Tags are added by commentary authors or by <em>Get My Vote</em> administrators. Click on a tag to see all commentaries on a similar theme.</p></div><div class=\"spacer\">&nbsp;</div></div>";

		// post your commentary link instead of add video
		$('ka_playPageStats').getElementsByTagName('div')[1].innerHTML = "<a id=\"playPageUpload\" href=\"http://www.npr.org/getmyvote/sharestory.html\">Post Your Commentary Now</a>";

		// disclaimer text
		$('ka_playPageStats').innerHTML = $('ka_playPageStats').innerHTML + "<div style=\"font-style:italic;font-size:11px;line-height:13px;\">The views expressed in these commentaries belong to the commentators only and are not those of NPR, NPR's member stations or its public-media partners.  See our <a href=\"http://www.npr.org/getmyvote/terms.html\">Terms of Service</a>.</div>";
		
	}	
}


/*
$j(document).ready(function() {
	fixRegistrationPage();
});*/

Ka.addDOMLoadEvent(fixRegistrationPage);
Ka.addDOMLoadEvent(fixUploadPages);
Ka.addDOMLoadEvent(fixPlayPage);
Ka.addDOMLoadEvent(setMaxLength);


// More helper functions for textareas
function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxLength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = 'Characters used: <span>0</span>/'+x[i].getAttribute('maxLength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxLength');
	var currentLength = this.value.length;
	this.value = this.value.substring(0, maxLength);
	if (currentLength > maxLength-10)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}




