/* This one time, Luke Giuliani wrote a switch on an enum that either cured cancer, saved Africa from famine, or achieved world peace.
 * There was user error when it got deployed, so it just wrote this code instead as backup.
 */

$(document).ready(function() {

	Shadowbox.init();

	$('.js_text_minimised').show();
	$('.js_text_maximised').hide();

	// do the question expanders
	$('.js_expander .js_expander_doit').click(function(event) {
		event.preventDefault();
		$(event.target).parents('.js_expander').hide();
		$(event.target).parents('.js_expander').siblings('.js_text_maximised').show();
	});

	// some tooltipz
	$(".js_tooltipme").tooltip({
		effect: 'slide'
	});
	
	$(".js_oursay_tooltipme").tooltip({
		effect: 'slide',
		position: 'bottom left',
		offset: [20,140],
		tipClass: 'oursay_tooltip'
	});

	//bdoy gogogogogogoogog oerioh afwoeih
	$('body').addClass('hasjs');

	// top menu drops
	$('.js_drop').addClass('drop');
	
	$('.js_drop').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		
		$('.js_drop').removeClass('active');
		$('.js_drop').each(function(el, i) {
			$('.' + $(this).attr('rel')).hide();
		});

		$(e.originalTarget).addClass('active');
		$('.' + $(e.currentTarget).attr('rel')).show();
		
	
	});

	$('.js_search_questions_input').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		// fuck that. use placeholder now.
		// if ($('.js_search_questions_input').attr('value') == 'Search questions...')
		// {
		// 	$('.js_search_questions_input').attr('value', '');
		// }
	});	
	
	$('body').click(function(e) {
		$('.js_dropdown_user').hide();
		$('.js_dropdown_search').hide();
		$('.m1 .js_drop').removeClass('active');

		$('.js_im_the_oursays').hide();
	});

	// dropdown the oursay nav

	$('.js_nav_current_dropdown').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		$('.js_im_the_oursays').show();
	});
	
	
	// kill sesh mesh 
	
	$('.js_close_session_messages').click(function(e){
		e.preventDefault();
		$('.js_session_messages').slideUp();
	
	});


	$('.js_datepicker').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		yearRange: 'c-100:c'
	});

	// real simple
	// this just selects all the text...
	$('.js_select_me').click(function(){
		$(this).select();
	});


	$('.js_input_placeholder').each(function(){
		if ($(this).attr('title') != '')
		{
			$(this).focus(function(){
				if ($(this).attr('title') == $(this).attr('value'))
				{
					$(this).attr('value', '');
				}
			});
			$(this).blur(function(){
				if ($(this).attr('value') == '')
				{
					$(this).attr('value', $(this).attr('title'));
				}
			});			
		}
	});
	
	/* Video response page!!! whooo */
	// player.seekTo(seconds
	$('.js_response_jump').click(function(e) {
		ytplayer = document.getElementById("js_youtube");
		ytplayer.seekTo($(e.currentTarget).attr('rel'), true);
	 
	});
	
	// embed youtubes if we have them. 
	if ($('#js_youtube').length > 0)
	{
		var params = { allowScriptAccess: "always", wmode: 'opaque'};
	  var atts = { id: "js_youtube" };
	  swfobject.embedSWF("http://www.youtube.com/e/" + app.oursay.video_id + "?enablejsapi=1&playerapiid=js_youtube", "js_youtube", "600", "400", "8", null, null, params, atts);
	}
  

	// add some clicks
	$('.js_countdown').countdown({
		until: new Date(app.finish_timestamp * 1000),
		layout: '<em>{dn}</em><span>:</span><em>{hn}</em><span>:</span><em>{mn}</em><span>:</span><em>{sn}s</em>'
	});
	
	$('.js_leaveabc').click(function() {
		return confirm('You\'re now leaving the ABC website and going over to OurSay.\n\nQ&A and OurSay are cooperating on a project where one of the most popular questions on OurSay will be put to the Q&A panel on May 16th – the first Monday after the Budget. \n\nTo vote on the Q&A OurSay you can register at oursay.org.au. Once you are registered, you can post questions and vote on other OurSay\'s as well. If you do not want to be involved in future OurSays, make sure you tick the box: "Please don’t keep my personal information or contact me in the future" during registration.\n\nClick OK to continue on and have your say.');
	});
});


$(function(){

	// // set up shadow box to show login and register forms, if they exist.
	// Shadowbox.init({
	//     // we skip the automatic setup because we don't have any
	//     // images for shadowbox on the page.
	//     skipSetup: true,
	// 		enableKeys: false // disable keys, as we have forms in the popup
	// });

	// set it up for both the js login and register links

	if (($('.js_register').length > 0) && ($('.js_register_form').length > 0))
	{
		var html = $('.js_register_form').html();
		$('.js_register_form').remove();

		$('.js_register').click(function(e) {
			e.preventDefault();

			Shadowbox.open({
					content:	html,
					player:		"html",
					title:		"Register / Log in",
					height:		400,
					width:		820
			});

		});
	}

	$('.ask_a_question').click(function() {
		$('#oursay_questionbox').focus();
	});
	
	$('.js_scroll_to').click(function(e) {
		e.preventDefault();
		$(document).scrollTo($(e.currentTarget).attr('rel'), 500, {over:-0.5});
	});
	
	// search tabs tabs search 
	$("#search_tabs").tabs({ fx: { opacity: 'toggle' } });

});




var oursay_modal_box = {

	// position before we move it about
	position : 150,

	init: function()
	{
		$('#close_the_popup_plz_kbai, #confirmation_popup_backer').click(function(e){
			e.preventDefault();
			$('#confirmation_popup_backer').fadeOut();
			$('#confirmation_popup').fadeOut();
		});
	},

	set_position : function(position)
	{
		if (position < 100)
		{
			$('#confirmation_popup').css('marginTop', 150);		
		} else
		{
			$('#confirmation_popup').css('marginTop', position);
		}
	},
	
	show_from_event : function(e)
	{
		e.preventDefault();
		var position = $(e.currentTarget).offset().top;
		var content_id = $(e.currentTarget).attr('rel');
		oursay_modal_box.set_position(position);
		oursay_modal_box.show(content_id);
	},

	show : function(content_id)
	{
		$('#confirmation_popup_backer').css("height", $(document).height());
		$('#confirmation_popup_backer').fadeIn();
		$('#confirmation_popup').fadeIn();

		$('.confirmation_popup_possibility').css("display","none");
		$('#' + content_id).css("display", "block");
	}
}


