//answer question page
function preventDuplicatePosting(eleSubmitButton, eleAnswerText){
	if(!eleSubmitButton.hasAttribute('disabled') && eleAnswerText.value.length > 0){
		eleSubmitButton.setAttribute('disabled', 'disabled');
	}
}

function resetAnswerForm(eleSubmitButton, eleAnswerText){
	eleAnswerText.clear();
	eleSubmitButton.removeAttribute('disabled');
}


//home & others
function toggleEmailSignup(input){
  if(input.value.toLowerCase() == 'email address'){
    input.value = '';
  }
  else if(input.value == ''){
    input.value = 'Email Address';
  }	
}
function clearEmailSignUp(){
	if($("user_email").value == 'Email Address'){
		$("user_email").value = ""
	}
}


//Global


function toggleSearchInput(input){
  if(input.value.toLowerCase() == 'search'){
    input.value = '';
  }
  else if(input.value == ''){
    input.value = 'Search';
  }
}

function clearDefaultText(){
  if(document.forms['search_form'].query.value.toLowerCase() == 'search')
  {
	document.forms['search_form'].query.value = '';
  }
}


function print_link(url, tag, id) {
    window.open("http://"+url+"/print/"+tag+"/"+id,
                'First30Days',
                'width=600,location=no,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes')
}

//css drop downs
//ie only supports :hover on the "a" element... this fakes it on other elements
//http://htmldog.com/articles/suckerfish/dropdowns/
function SuckerFish(strListSelector) {
	return function() {
		var sfEls = $$(strListSelector);
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover = function() { this.addClassName('sfhover') }
			sfEls[i].onmouseout = function() { this.removeClassName('sfhover') }
		}
	}
}

function LightBoxLogIn(){
/*	if($$('.log_in').length == 0){ return false; } // this method should only run on the new layout*/
	var is_new_layout = false;
	for(i=0;i<=document.styleSheets.length - 1;i++){
		if(document.styleSheets[i] != null && document.styleSheets[i].href != null){
			if(document.styleSheets[i].href.indexOf("v8.css") > 0 || document.styleSheets[i].href.indexOf("ariane.css") > 0){
				is_new_layout=true;
			}
		}
	}

	if(is_new_layout){
		var login = new Control.Modal($$('a.log_in')[0],{className: 'modal_box',width: 425,height: 220, fade:true,iframe:false,iframeshim:false, ajaxMethod:'get'});
		login.container.id = 'login_modal';
		login.observe('afterOpen', function(){ $('email').focus(); });

		//modalize any "log in" links on the page
		$$('a[href="/account/login"]').each(
			function(link){
				if(link.className != 'log_in'){
					link.observe('click', function(e){
					  e.stop();
					  login.open();
					});				
				}
			}
		);
	
		$$('map area[href="/account/login"]').each(
			function(link){
				link.observe('click', function(e){
				  e.stop();
				  login.open();
				});				
			}
		);
	}
}

function ResetLightBoxContents(){
	var is_new_layout = false;
	for(i=0;i<=document.styleSheets.length - 1;i++){
		if(document.styleSheets[i] != null && document.styleSheets[i].href != null){
			if(document.styleSheets[i].href.indexOf("v8.css") > 0 || document.styleSheets[i].href.indexOf("ariane.css") > 0){
				is_new_layout=true;
			}
		}
	}


//	if($$('.log_in').length == 0){ return false; } // this method should only run on the new layout
if(is_new_layout){
	Control.Overlay.observe('afterHide',function(){
	//destroy existing modal content to ensure there is always a new request for the "default" content
		Control.Window.windows.each(
			function(objWindow){
				objWindow.remoteContentLoaded = false;
				$(objWindow.container.id).innerHTML = ' ';
				//$('share_modal').innerHTML=' ';
				//alert('killed ' + objWindow.container.id);
			}
		)
	});
}

}

function fbs_click(){
	if(Control.Modal != "undefined"){
		Control.Modal.close();
	}
	u=location.href;
	t=document.title;

	var height = 436;
	var width = 626;
	var top = (screen.height - height)/2;
	var left = (screen.width - width)/2;

	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','top=' + top + ',left=' + left + ',toolbar=0,status=0,width=' + width + ',height=' + height);

	return false;
}

function new_window_from_modal(anchor){
	if(Control.Modal != "undefined"){
		Control.Modal.close();
	}
	var height = 650;
	var width = 1000;
	var top = (screen.height - height)/2;
	var left = (screen.width - width)/2;

	window.open(anchor.href,'sharer','top=' + top + ',left=' + left + ',scrollbars=1,toolbar=1,status=1,resizable=1,width=' + width + ',height=' + height);

	return false;
}

//Profile edit
function WarnOnCloaking(strPriorCloakPreference, eleRadio){

	frmUpdate = document.forms["update_privacy"]

	if(strPriorCloakPreference != eleRadio.value){ //pref changed
		if(frmUpdate.warning_shown.value == "false"){ //avoid message when clicking on already selected pref
			if(eleRadio.value == "true"){
				strMessage = "ATTENTION:\nYour friends will be removed and no users will be able to identify you or your postings.\n\nClick \"OK\" if you are sure you want to hide your information."
			}
			else{
				strMessage = "ATTENTION:\nYour profile information will be displayed as will your screen name along side your postings.\n\nClick \"OK\" if you are sure you want to make your information publicly visible."
			}

			blnCloak = eleRadio.value == "true";

 			if(confirm(strMessage)){
				ToggleRadios(blnCloak);
				frmUpdate.warning_shown.value = "true";
			}
			else{
				ToggleRadios(!blnCloak);
				frmUpdate.warning_shown.value = "false";
			}
		}
	}
	else{
		frmUpdate.warning_shown.value = "false";
	}
}

function ToggleRadios(blnCloak){
	eleShow = document.getElementById("cloaked_pref_false");
	eleHide = document.getElementById("cloaked_pref_true");
	if(blnCloak){
		eleShow.checked = false;
		eleHide.checked = true;
	}
	else{
		eleShow.checked = true;
		eleHide.checked = false;
	}
}

function textCounter(field, maxlimit){
	if (field.value.length > maxlimit){
		field.value = field.value.substring(0, maxlimit);
	}
}



function copy(inElement) {
  if (inElement.createTextRange) {
    var range = inElement.createTextRange();
    if (range) // && BodyLoaded==1
      range.execCommand('Copy');
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="/media/flash/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}

function edcWidget(){
	copy('<script type="text/javascript" src="http://www.first30days.com/everyday-change/today.js"></script>')
}

function bookWidget(site){
	copy("<script type='text/javascript' src='http://"+site+"/ariane/book_widget.js'></script>")
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Mark O'Sullivan :: http://lussumo.com/
 Jeff Larson :: http://www.jeffothy.com/
 Mark Percival :: http://webchicanery.com/
*/
function copy(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="/media/flash/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}


function ChangeNationPlayer(strType, strPath, strTitle){
	var strPlayerFileName = "change_nation_audio_player.swf";
	var strPathName = "mp3Path";
	var strTitleName = "mp3Title";

	if(strType.toLowerCase() == 'video'){
		strPlayerFileName = "change_nation_video_player.swf";
		strPathName = "flvPath";
		strTitleName = "flvTitle";
	}

	var so = new SWFObject("/media/flash/" + strPlayerFileName, "Change Nation", "400", "350", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addVariable(strPathName, strPath);
	so.addVariable(strTitleName, strTitle);
	so.addParam("wmode", "transparent");
	so.addParam("salign", "t");
	so.write("change_nation_flash_player");
}

function CreateFlashFeature(){
	var so = new SWFObject("/media/flash/home_featured.swf", "flash_feature", "400", "240", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addParam("salign", "tl");
	so.addParam("wmode", "transparent");
	so.addParam("allowScriptAccess", "always");
	so.write("flash_feature");
}

function CreateArianeSpeaking(){
	var so = new SWFObject("/media/flash/ariane_speaking_video_player.swf", "ariane_flash_player", "360", "290", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addParam("salign", "tl");
	so.addParam("wmode", "transparent");
	so.write("ariane_flash_player");
}

function CreateArianeHarper(){
	var so = new SWFObject("/media/flash/ariane_harper.swf", "ariane_harper", "360", "290", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addParam("salign", "tl");
	so.addParam("wmode", "transparent");
	so.write("ariane_flash_harper");
}

function CreateArianeAmazon(){
	var so = new SWFObject("/media/flash/ariane_amazon.swf", "ariane_amazon", "360", "290", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addParam("salign", "tl");
	so.addParam("wmode", "transparent");
	so.write("ariane_flash_amazon");
}

function CreateEndorsements(){
	var so = new SWFObject("/media/flash/endorsements.swf", "Endorsements", "260", "130", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addParam("salign", "tl");
	so.addParam("wmode", "transparent");
	so.write("flash_endorsements");
}

function CreateLowerEndorsements(){
	var so = new SWFObject("/media/flash/endorsements_no_mask.swf", "Endorsements", "260", "130", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addParam("salign", "tl");
	so.addParam("wmode", "transparent");
	so.write("flash_endorsements");
}

function CreateTestimonials(){
	var so = new SWFObject("/media/flash/testimonials.swf", "Testimonials", "260", "130", "8", "#ffffff");
	so.addParam("quality", "high");
	so.addParam("salign", "tl");
	so.addParam("wmode", "transparent");
	so.write("flash_testimonials");
}



//temp fix for cheer ad that had a pop over div written to page. this caused operation aborted errors in IE
// changed the code from dom load to window load so our stuff would fire after the ad finished it's dom manip
if(Prototype.Browser.IE){
	Event.observe(window, 'load', LightBoxLogIn, false);
	Event.observe(window, 'load', ResetLightBoxContents, false);	
}else{
	document.observe("dom:loaded", LightBoxLogIn);
	document.observe("dom:loaded", ResetLightBoxContents);
}





// cookies.js
var Document = {
  cookies: function(document){
    return $A(document.cookie.split("; ")).inject($H({}), function(memo, pair){
      pair = pair.split('=');
      memo.set(pair[0], pair[1]);
      return memo;
    });
  }
};
Object.extend(document, { cookies: Document.cookies.methodize() });

// application.js
function showFlash(){
  if(flash = document.cookies().get('flash')){
    $('cookie_flash').update(flash.gsub(/\+/, " "));
    $('cookie_flash').style.display = ''
  }
}