var gothamrounded = { src: '/~optwork/wp-content/themes/optionline/js/gothamrounded.swf' };
var gothamMedium = { src: '/~optwork/wp-content/themes/optionline/js/gothamMedium.swf' };
var gothamBook = { src: '/~optwork/wp-content/themes/optionline/js/gothamBook.swf' };

sIFR.activate(gothamrounded, gothamMedium, gothamBook);



$(document).ready(function() {
	
	
	/************************************************************
	 * sIFR config, added onRelease for "for friends" questions.
	 ************************************************************/

	sIFR.replace(gothamrounded, {
	  selector: '#topNav ul li'
	  , css: '.sIFR-root { text-transform:uppercase; font-size:13px; } .sIFR-root a { color: #ffffff; font-size:13px; letter-spacing:0.2; text-decoration:none; height:36px; display:block; background-color:#326880; } .sIFR-root a:hover { color:#ffffff; background-color:#285366; cursor:pointer; } .sIFR-root a.active { background-color:#1E3E4D; }'
	  , offsetTop: 10
	  , offsetLeft: 27
	  , tuneWidth: 54
	  , tuneHeight: 20
	  , forceSingleLine: true
	  , wmode: 'transparent'
	});
	
	sIFR.replace(gothamBook, {
	  selector: '#page-header h2'
	  , css: '.sIFR-root { color: #326880; font-size:24px; }'
	  , wmode: 'transparent'
	});
	
	sIFR.replace(gothamBook, {
	  selector: '.block:not(.for-friends):not(.get-help-block):not(.find-a-center) h3'
	  , css: '.sIFR-root { color: #326880; font-size:22px; }'
	  , wmode: 'transparent'
	});
	
	sIFR.replace(gothamBook, {
	  selector: '#get-help-blocks h3'
	  , css: '.sIFR-root { color: #326880; font-size:18px; }'
	  , wmode: 'transparent'
	});
	
	sIFR.replace(gothamBook, {
	  selector: '#find-a-center h3 span.normal-fac'
	  , css: '.sIFR-root { color: #333333; text-transform:uppercase; font-size:24px; }'
	  , forceSingleLine: true
	  , wmode: 'transparent'
	});
	sIFR.replace(gothamBook, {
	  selector: '#find-a-center h3 span.smaller-fac'
	  , css: '.sIFR-root { color: #333333; text-transform:uppercase; font-size:16px; }'
	  , wmode: 'transparent'
	});
	
	
	sIFR.replace(gothamBook, {
	  selector: '#friends-list h3'
	  , css: '.sIFR-root { color: #326880; font-size:22px; cursor:pointer; line-height:22px; }'
	  , forceSingleLine: true
	  , wmode: 'transparent'
	  , onRelease: function(fi) { handleFlashClick(fi.getFlashElement()); }
	});
	
	sIFR.replace(gothamrounded, {
	  selector: '#get-help h4'
	  , css: '.sIFR-root { color: #ffffff; text-transform:uppercase; font-size:19px; }'
	  , wmode: 'transparent'
	});
	
	sIFR.replace(gothamrounded, {
	  selector: '#fac-sidebar h4 span.normal-fac'
	  , css: '.sIFR-root { color: #333333; text-transform:uppercase; font-size:20px; }'
	  , wmode: 'transparent'
	  , forceSingleLine: true
	  , tuneWidth: 5
	});
	sIFR.replace(gothamrounded, {
	  selector: '#fac-sidebar h4 span.smaller-fac'
	  , css: '.sIFR-root { color: #333333; text-transform:uppercase; font-size:12px; }'
	  , wmode: 'transparent'
	  , forceSingleLine: true
	  , tuneWidth: 5
	});
	
	sIFR.replace(gothamrounded, {
	  selector: '.caption h4'
	  , css: '.sIFR-root { color: #333333; font-size:22px; }'
	  , wmode: 'transparent'
	});
	
	sIFR.replace(gothamrounded, {
	  selector: 'div.browser-not-msie-6 #side-phone'
	  , css: '.sIFR-root { color: #ffffff; font-size:19px; letter-spacing: -0.5; }'
	  , wmode: 'transparent'
	});
	
	sIFR.replace(gothamrounded, {
	  selector: 'div.browser-not-msie-6 #get-help-phone'
	  , css: '.sIFR-root { color: #326880; font-size:19px; letter-spacing: -1; }'
	  , wmode: 'transparent'
	});
	
	
	/************************************************************
	 * TOPNAV FUNCTIONALITY with sIFR
	 ************************************************************/
	
	$('#topNav ul').delegate('li', 'click', function() {
		var link = $(this).children('span.sIFR-alternate').children('a').attr('href');
		window.location = link;
	});
	
	
	
	
	/************************************************************
	 * HOMEPAGE SLIDES
	 ************************************************************/
	
	var hsIdx = 0;
	var timeoutID;
	
	if($('body').hasClass('home-page') || $('body').hasClass('principal-page')) {
		timeoutID = setTimeout(switchSlide, 5000);
	}
	
	$('#homepage-slides .slide-buttons a').click(function(ev) {
		ev.preventDefault();
		slideNumber = $(this).attr('href').substr(7);
		
		if(!$(this).hasClass('focus')) {
			hsIdx = fadeOutFocus();
			fadeInFocus(slideNumber);
			
			clearTimer();
		}
	});
	
	function switchSlide() {
		index = $('#homepage-slides .slide.focus').index();
		total = $('#homepage-slides .slide').size();
		
		if (index == (total - 1)) { next = 0; } else { next = index + 1; }
		
		fadeOutFocus();
		fadeInFocus(next);
		
		timeoutID = setTimeout(switchSlide, 5000);
	}
	
	function clearTimer() {
		clearTimeout(timeoutID);
		timeoutID = setTimeout(switchSlide, 10000);
	}
	
	function fadeOutFocus() {
		index = $('#homepage-slides .slide.focus').index();
		
		$('#homepage-slides .slide:eq('+index+')').animate({'opacity':'0'}, 500).removeClass('focus');
		$('#homepage-slides .slide-buttons a.focus').animate({'opacity':'0.6'}, 500).removeClass('focus');
		
		return index;
	}
	
	function fadeInFocus(cnt) {
		$('#homepage-slides .slide:eq('+cnt+')').animate({'opacity':'1'}, 500).addClass('focus');
		$('#homepage-slides a.slide-button:eq('+cnt+')').animate({'opacity':'1'}).addClass('focus');
	}
	
	
	/************************************************************
	 * COLLAPSIBLE DEFINITION LIST
	 ************************************************************/
	
	$('dl.collapsible dd').addClass('collapsed');
	$('dl.collapsible dt').css({'background-position':'0 -16px'});
	
	var selectedText;
	
	$('dl.collapsible').delegate('dt', 'hover', function() {
		$(this).toggleClass('hover');
	}).delegate('dt', 'click', function() {
		clicked = $(this).next();
		if(clicked.hasClass('collapsed')) {
			$('dl.collapsible dt').removeClass('active');
			$(this).addClass('active');
			
			// get current text
			selectedText = $(this).html();
			var closeText = (language == 'es') ? 'Cerrar' : 'Close this';
			$(this).html(closeText);
			
			$('dl.collapsible dd').addClass('collapsed');
			clicked.removeClass('collapsed');
			
		} else {
			$(this).html(selectedText);
			$(this).removeClass('active');
			clicked.addClass('collapsed');
		}
	});
	
	
	/************************************************************
	 * FOR FRIENDS - Styles & Accordion 
	 ************************************************************/
	
	if($('body').hasClass('for-friends-page') || $('body').hasClass('para-los-amigos-page')) {
		
		$('.sIFR-active #friends-list .block h3').css({'cursor':'pointer'});
		
		$('#friends-list .for-friends .block-inner p:first-child').addClass('first');
		
		var ffIdx;									// index of "For Friends" blocks
		var slug = window.location.hash.substr(1);	// hash to link straight to a 
		var ffHeights = new Array();
		
		if(slug == '') {
			ffIdx = 0;
		} else {
			ffIdx = $('a#'+slug).parent('.for-friends.block').index();
		}
		
		// get heights
		for(var ffI=0; ffI < $('.for-friends.block').size(); ffI++) {
			ffHeights[ffI] = $('.for-friends.block:eq(' + ffI + ') .block-inner-wrapper').height() + 40;
		}
		
		$('#friends-list .block-inner-wrapper:not(:eq('+ffIdx+'))').addClass('collapsed');
		$('#friends-list h3').css({'background-position':'0 -23px'});
		
		$('#friends-list h3:eq('+ffIdx+')').addClass('active');
		
		
		
		
		
		/* $('#friends-list').delegate('h3', 'hover', function() {
				$(this).toggleClass('hover');
			}).delegate('h3', 'click', function() {
			
			
			blockWrapper = $(this).next();
			ffIdx = $(this).parent().index();
			
			if(blockWrapper.hasClass('collapsed')) {
				$('#friends-list h3').removeClass('active');
				$(this).addClass('active');
				
				
				$('#friends-list .block-inner-wrapper:not(.collapsed)').animate({'height':'0'}, 400).addClass('collapsed');
				blockWrapper.animate({'height':ffHeights[ffIdx]+'px'}, 400).removeClass('collapsed');
				
			} else {
			//	$(this).removeClass('active');
			//	blockWrapper.css({'height':'0'}).addClass('collapsed');
				
				//	do nothing
			}
		});
		*/
		
		
		
		// if h3 clicked
		$('#friends-list h3').click(function(){
			blockWrapper = $(this).next();
			handleListClick(blockWrapper);
		});
		
		
		// if flash object clicked
		function handleFlashClick(el) {
			var flElement = $(el);
			blockWrapper = $(el).parent().next();
			handleListClick(blockWrapper);
		}
	
		
		function handleListClick(el) {
			ffIdx = el.parent().index();	
			
			if(el.hasClass('collapsed')) {
				$('#friends-list h3').removeClass('active');
				$(this).addClass('active');
				
				$('#friends-list .block-inner-wrapper:not(.collapsed)').animate({'height':'0'}, 400).addClass('collapsed');
				blockWrapper.animate({'height':ffHeights[ffIdx]+'px'}, 400).removeClass('collapsed');
				
				$('#friends-list .for-friends:eq('+ffIdx+') h3').addClass('active');
			}
		}
		
		
		
		/*$('#friends-list').delegate('h3', 'hover', function() {
				$(this).toggleClass('hover');
			}).delegate('h3', 'click', function() {
			blockWrapper = $(this).next();
			ffIdx = $(this).parent().index();
			
			if(blockWrapper.hasClass('collapsed')) {
				$('#friends-list h3').removeClass('active');
				$(this).addClass('active');
				
				
				$('#friends-list .block-inner-wrapper:not(.collapsed)').animate({'height':'0'}, 400).addClass('collapsed');
				blockWrapper.animate({'height':ffHeights[ffIdx]+'px'}, 400).removeClass('collapsed');
				
			} else {
			//	$(this).removeClass('active');
			//	blockWrapper.css({'height':'0'}).addClass('collapsed');
				
				//	do nothing
			}
		}); */
	
	}
	
	/************************************************************
	 * FIND A PREGNANCY CENTER - Form
	 ************************************************************/
	
	//var zipText = 'zip / postal code';
	
	$('input#fac-s-zip').focus(function() {
		if ($(this).attr('value').search(searchable_zip_default) != -1) $(this).attr('value','');
	}).blur(function() {
		if ($(this).attr('value') == '') $(this).attr('value', replaceable_zip_default);
	});
	
	$('input#fac-gh-zip').focus(function() {
		if ($(this).attr('value') == 'zip / postal code' || $(this).attr('value').search(/digo postal$/) != -1) $(this).attr('value','');
	}).blur(function() {
		if ($(this).attr('value') == '') $(this).attr('value', replaceable_zip_default);
	});

/*
	var zipText = 'zip / postal code';
	
	$('input#fac-s-zip').focus(function() {
		if ($(this).attr('value') == zipText) $(this).attr('value','');
	}).blur(function() {
		if ($(this).attr('value') == '') $(this).attr('value', zipText);
	});
	
	$('input#fac-gh-zip').focus(function() {
		if ($(this).attr('value') == zipText) $(this).attr('value','');
	}).blur(function() {
		if ($(this).attr('value') == '') $(this).attr('value', zipText);
	});
*/
	
	
	
	/********************************************************
	 * CONTACT FORM
	 ******************************/
	
	var overOpacity=0.5,
		messagePrompt = (language=='es') ? 'Escribe tu mensaje aqui...' : 'Write your message here...';
	var regexp_es = /Escribe tu mensaje aqu.\.\.\./;
	
	/* Open & Close the feedback box */
	$('.browser-not-msie-6 a.email-us-link').click(function (event) {
		event.preventDefault();
		$('#contact-form-wrapper').removeClass('hide').animate({'opacity':'1'},400);
		$('#overlay').removeClass('hide').animate({'opacity':overOpacity},400);
	});
	$('a#contact-close').click(function( event ) {
		event.preventDefault();
		$('#contact-form-wrapper').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide').removeClass('submitted');
		});
		$('#overlay').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide').removeClass('submitted');
		});
	});
	$('#overlay').click(function( event ) {
		$('#contact-form-wrapper').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide').removeClass('submitted');
		});
		$('#overlay').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide').removeClass('submitted');
		});
	});
	
	
	$('textarea#fmMessage').focus(function() {
		if($(this).val() == messagePrompt) {
			$(this).val('');
		} else if ($(this).val().search(regexp_es) == 0 && $(this).val().length == 26) {
	    	$(this).val('');		
		}
	}).blur(function() {
		if($(this).val() == '') {
			$(this).val(messagePrompt);
		}
	});
	
	var contactErrors = false;
	var contactErrorMsg;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	var emailFilter=/^.+@.+\..{2,6}$/;
	
	$('input#send-message-button').click(function( event ) {
		event.preventDefault();
		contactErrors = false;
		
		if($('input#fmEmail').val() != '') {
			if ($('#fmEmail').attr('value').match(illegalChars))  {
				$('#fmEmail').addClass('errorRed');
				contactErrorMsg = '<span class="errorRed">You cannot include any illegal characters &mdash; ( ) < > , ; : \ / " [ ] &mdash; in your email address.</span>';
				contactErrors = true;
				$('#fmEmail').focus();
			}
			if (!(emailFilter.test($('#fmEmail').attr('value'))) && !contactErrors) {
				$('#fmEmail').addClass('errorRed');
				contactErrorMsg = '<span class="errorRed">Be sure to enter a valid email address.</span>';
				contactErrors = true;
				$('#fmEmail').focus();
			}	
		} else {
			$('#fmEmail').addClass('errorRed');
			contactErrorMsg = '<span class="errorRed">Be sure to enter an email address.</span>';
			contactErrors = true;
			$('#fmEmail').focus();
		}
		
		if(($('textarea#fmMessage').val() == '' || $('textarea#fmMessage').val() == messagePrompt) && !contactErrors) {
			$('#fmMessage').addClass('errorRed');
			contactErrorMsg = '<span class="errorRed">Don\'t forget to write your message.</span>';
			contactErrors = true;
			$('#fmMessage').focus();
		}
		
		if (!contactErrors) {
			$('form#contact-form').trigger('submit');
		} else {
			$('#contact-form-header p').html(contactErrorMsg);
		}
	});
	
	$('input#cancel-button').click(function( event ) {
		$('#contact-form-wrapper').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide').removeClass('submitted');
		});
		$('#overlay').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide').removeClass('submitted');
		});
		$('#fmEmail').removeClass('errorRed');
		$('#fmMessage').removeClass('errorRed');
		$('#contact-form-header p').html('<strong>New Email Message to: mail@option-line.org</strong>');
	});
	
	
	/************************************************************
	 * START CHAT - Open in new window
	 ************************************************************/
	
	
	$('.white-button, .blue-button, #start-chat-wrapper, #gh-start-chat-wrapper').delegate('a.start-chat', 'click', function(ev) {
		ev.preventDefault();
		
		window.open($(this).attr('href'), "Chat", "width=500,height=440,resizable=yes");
		
		if ($(this).parent().parent('div').attr('id') == 'gh-start-chat-wrapper') {
			pageTracker._trackEvent('Chat', 'Start', 'Location: Get Help page');
		} else {
			pageTracker._trackEvent('Chat', 'Start', 'Location: Sidebar');
		}
		
		return false;
	});
	
	
//	$('a.start-chat').click(function(ev) {
//		ev.preventDefault();
		
//		alert('Hey, Adam: This is blocked by Javascript in custom.js!');
		
		//alert(psMerVol);
		
		//window.open($(this).attr('href'),'Option Line &raquo; Live Chat','width=500,height=440,resizable=yes')
//	});
	
	
	
	/************************************************************
	 * CHAT STATUS - Figure out if chat is online
	 ************************************************************/
	 
	 
	var seMerVs=(location.protocol.indexOf("https")==0?"https":"http")+"://image.providesupport.com/js/optionline/safe-textlink.js?ps_h=MerV&ps_t="+new Date().getTime()+"&online-link-html=&offline-link-html=";
	
	$.getScript(seMerVs, function() {
		var start_chat_text = (language=='es') ? 'Iniciar Chat' : 'Start Chat';
		var chat_offine_text = (language=='es') ? 'Fuera de L&iacute;nea' : 'Chat Offline';
	
		if(psMerVol) {
			$('.question .blue-button').show();
			$('#start-chat-wrapper').append('<span class="white-button"><a class="start-chat" href="http://www.providesupport.com?messenger=optionline">'+start_chat_text+'</a></span>');
			$('#gh-start-chat-wrapper').append('<p class="blue-button bubble"><a class="start-chat" href="http://www.providesupport.com?messenger=optionline">'+start_chat_text+'</a></p>');
		} else {
			$('#start-chat-wrapper').append('<p class="sbOffline"><span id="chat-offline">'+chat_offine_text+'</span></p>');
			$('#gh-start-chat-wrapper').append('<p class="sbOffline"><span id="gh-chat-offline">'+chat_offine_text+'</span></p>');
			$('#start-chat-wrapper').append('<div class="clear"></div>');
			
			$('.question .blue-button').remove();
			
		}
		 
	});



	/************************************************************
	 * TEMPORARY - Chat Button for AIM
	 ************************************************************/
/*	
	var tmpOverOpacity=0.5;
	
	$('.browser-not-msie-6 a[href=#start-chat]').click(function(ev) {
		ev.preventDefault();
		
		$('#aim-popup-wrapper').removeClass('hide').animate({'opacity':'1'},400);
		$('#overlay').removeClass('hide').animate({'opacity':overOpacity},400);
//		alert("Option Line consultants are available via AOL's Instant Messenger during limited hours of the day. Our screen name is 'AnswersatOL1.'");
	});
	$('a#aim-close').click(function( event ) {
		event.preventDefault();
		$('#aim-popup-wrapper').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide');
		});
		$('#overlay').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide');
		});
	});
	$('#overlay').click(function( event ) {
		$('#aim-popup-wrapper').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide');
		});
		$('#overlay').animate({'opacity':'0'},400,function() {
			$(this).addClass('hide');
		});
	});
*/	
	
});




