$(document).ready(function() {
	
	// Magpie Update Begin
	
   //Set up rating_stars hover functionality
//    $('#rating_widget li a').each(function() {
//        $(this).hover(
//        function() {
//            var height = $(this).parent().parent().height();
//            var pixels = ($(this).parent().prevAll().size() + 1) * height * 2;
//            $(this).parent().parent().css({
//                "backgroundPosition": "0 " + -pixels + "px"
//            });
//        },
//        function() {
//            $(this).parent().parent().css({
//                "backgroundPosition": "0 0"
//            });
//        }
//        );
//    });
	// Magpie Update End

    //Setup full plan row hover
    $('#full_plan ul:not(.title) li.topic').hover(function() {
        $(this).parent().addClass('hover');
    },
    function() {
        $(this).parent().removeClass('hover');
    });

    //Setup all details panels for full plan items
    $('#full_plan ul li.details').hide();
    $('#full_plan ul:not(.title) li.topic').click(function() {
		
		if(!$(this).parent().hasClass('open')){ 
			$('#full_plan ul.open').each(function() {
				$(this).children('li.details').slideUp('fast');
			 	$(this).removeClass('open');
			});
		}
				
        $(this).parent().children('li.details').slideDown('fast');
        $(this).parent().addClass('open');
    });

	//Setup form panels for editing custom messages
    /* Begin Magpie Update 6/16/09 */
    
	$('#custom_messages ul li form').hide();
	$('#custom_messages ul li p a:not(.delete)').click(function(){
		$(this).parents('li').children('form').slideToggle('fast');
		$(this).parents('li').toggleClass('open');
	});
	$('#custom_messages ul li form ul li a.reset').click(function(){
		$(this).parents('form').slideUp('fast');
		$(this).parents('li').toggleClass('open');
	});
	$('#custom_messages ul li').hover(function(){
		$(this).addClass('active');
	},
	function(){
		$(this).removeClass('active');
	});
	
	$('div.quote_section div.hidden').hide();
	$('div.quote_section div.more').click(function(){
		$(this).parents('div').children('div.hidden').slideToggle('fast');
		$(this).text($(this).text() == 'More...' ? "Less..." :"More...");
		$(this).parents('div').toggleClass('open');
	});

	$('div.comments_section div.comments_hidden').hide();
	$('div.comments_section div.more_less').click(function(){
		$(this).parents('div').children('div.comments_hidden').slideToggle('fast');
		$(this).text($(this).text() == 'More...' ? "Less..." :"More...");
	});

	/* End Magpie Update 6/16/09 */
	/** Begin Magpie update **/
	/* Commenting out this function so we can
	 * handle the characters remaining in the code
	var bindCharactersRemaining = function() {
		var messageType = getTypeOfMessage(this);
		var charactersTyped = $(this).val().length;
		var charactersAllowed = (messageType == 'text') ? 120 : 1000;
		var charactersRemaining = charactersAllowed - charactersTyped;
		$(this).siblings('p.chars').text(charactersRemaining + ' characters remaining');
	}
	$('#custom_messages textarea').keyup(bindCharactersRemaining);
	$('#custom_messages textarea').change(bindCharactersRemaining);
	 */
	/** End Magpie update **/
    // day calendar hover states
    var calendar_nav = $('#calendar_click_targets a');
    var calendar_box = $('#flip_calendar');

    calendar_nav.hover(function() {
        which = $(this).hasClass('left') ? 'left' : 'right';
        calendar_box.addClass(which);
    },
    function() {
        which = $(this).hasClass('left') ? 'left' : 'right';
        calendar_box.removeClass(which);
    })
    // end hover flip calendar

    // Magpie Update Begin
    //To do yes and no checkboxes
//    $('div.checkmarks a span').click(function() {
//        $(this).toggleClass('active');
//		$(this).parent().siblings('a').children('span').removeClass('active');
//        return true;
//    });
    // Magpie Update End

    //Calendar tool tips
    //If Internet Explorer get rid of the fade
    var fadeSpeed = jQuery.support.noCloneEvent ? 125: 0;
    $('#calendar a').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: fadeSpeed,
        top: -117,
        left: -155,
        fixPNG: true
    });

    //Hide submit buttons and activate anchor links to submit the form
    $('form input.submit:not(.nohide)').hide();
    $('form:not(.async) a.grey_button:not(.reset)').click(function() {
        $(this).parents('form').submit();
        return false;
    });
	$('form a.reset').click(function(){
		$(this).parents('form')[0].reset();
		$(this).parents('form').children('ul').children('li').children('p.chars').html("120 characters remaining.");
		return false;
	});
	
	//Marketing - Our Programs detail hovers
	$('#our_programs ul li').hover(function(){
		
		/** Begin Magpie Update **/
		/**
		$(this).children('h4').children('span.links').children('a').click(function(event){
			window.location.href = event.target.href;
			return false;
		});
		$(this).click(function() {
			window.location.href = $(this).children('h4').children('a').attr('href');
		});		
		**/
		/** End Magpie Update **/
		$(this).addClass('active');
	},
	function(){
		$(this).removeClass('active');		
	});
	
	//Hook up the button to print just a challenge
	$('a.print_challenge').click(function(){
		var challenge = $(this).parents('div.challenge');
		var challengeWindow = window.open('', 'printChallengeWindow', 'height=480,width=640,scrollbars=no,status=no,toolbar=no,menubar=no');
		challengeWindow.document.open();
		challengeWindow.document.write($(challenge).html());
		challengeWindow.document.close();
		$('a.print_challenge', challengeWindow.document).remove();
		challengeWindow.window.print();
		return false;
	});
	
	
	//Hook up the button to print just a challenge
//	$('a.print_tool').click(function(){
//		var toolPage = $(this).attr('href');
//		var toolWindow = window.open(toolPage, 'printToolWindow', 'height=480,width=640,scrollbars=no,status=no,toolbar=no,menubar=no');
//		alert('Preparing Tool...');
////		toolWindow.window.resizeTo(645, 485);
//		$('a.print_tool', toolWindow.document).remove();
//		//wait 2 seconds
//		toolWindow.window.print();
//		return false;
//	});
	
		
	
	/** Begin Magpie Update **/
//	var bindCharacterRestrictions = function() {
//		var charactersTyped = $(this).val().length;
//		var charactersAllowed = isTextMessage() ? 144 : 1000;
//		var charactersRemaining = charactersAllowed - charactersTyped;
//		if(charactersRemaining <= 0)
//		{
//			$(this).val($(this).val().substring(0,charactersAllowed));
//		}
//	}
//
//	function isTextMessage() {
//		return $("#messageTypeGroup-textMessage")[0].checked;	
//	}
	/** End Magpie Update **/
});

function setTBDimensions(height,width){
	$("#TB_ajaxContent").height(height);
	$("#TB_ajaxContent").width(width);
	$("#TB_window").height(height);
	$("#TB_window").width(width);
	$("#TB_window").css('margin-left',-1 * width / 2);
	$("#TB_window").css('margin-top',-1 * height / 2);
}


function openPrintTool(url)
{
	var toolWindow = window.open(url, 'printToolWindow', 'height=600,width=800,scrollbars=yes,status=no,toolbar=no,menubar=no');
}

function initFacebookApi(apiKey)
{
	tb_remove();
	FB_RequireFeatures(["XFBML", "Connect"], function()
	    {
	       FB.Facebook.init(apiKey, "/xd_receiver.htm",{"doNotUseCachedConnectState":false});
	       FB.ensureInit(function() {
	    	   FB.Connect.get_status().waitUntilReady( function( status ) {
	    	      switch ( status ) {
	    	      case FB.ConnectState.connected:
	    	         loadStatusMessage();
	    	         break;
	    	      case FB.ConnectState.appNotAuthorized:
	    	      case FB.ConnectState.userNotLoggedIn:
	    	            FB.Connect.requireSession(loadStatusMessage);
	    	         break;
	    	      }
	    	   });
	    	});
	    });
}

var loadStatusMessageCallbackURL = ''; // set via ajax call

function loadStatusMessage()
{
	wicketAjaxGet(loadStatusMessageCallbackURL, function() { }, function() { });
}

function postToFBStreamWithoutPermission(name,description)
{
	FB.ensureInit(function() {
		var success = FB.Connect.streamPublish("",{'name':name,'description':description},null,null,"Share this challenge on Facebook",handlePostError,false,null);
	});
}

function handlePostError(post_id,exception)
{
	//HACKHACK:If we get an error with the post it likely means our session
	//state has changed to logged out, so we will refresh the page
	//and allow FB.Connect to check. 
	if(post_id == null || post_id == "null")
        location.reload(true);
}



