// 
//  application.js
//  FCIC.gov
//  
//  Created by Pete Saia on 2011-01-26
//  Copyright 2010 Home Front Communications. All rights reserved.
// 

$(document).ready(function() {

	// Make the logo clickable
	$('.logo').click(function() { location.href='/'; });
	
	// Clear the default input value on browsers that don't support placeholder
	$('.autoClear').focus(function() { clearInputValue(this); });

	// the resource images
	if ($('#img-resources').length) {
		$(".imgrow").not('.pdf').css('cursor','pointer').click(function() {
			var obj_image = $(this).find('a img'), txt_title = $(this).find('strong').html();
			$.fancybox({
				'padding'		: 20,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: txt_title,
				'href'			: $(obj_image).attr('src').replace('thumb','full')
			});
		});
	}


	// hearings switcher..
	var triggers = $('#pageControlers').find('a'),
		pages = $('.switchPage');

	$(pages).not('.page1').hide();

	$(pages).each(function(i){
		$(triggers[i]).bind('click', function(){
			$(triggers).removeClass('active');
			$(this).addClass('active');
			$(pages).hide();
			if ($(pages[i]).css('display') == 'none') {
				$(pages[i]).css('display', 'block');
			}
			else {
				$(pages[i]).css('display', 'none');
			}
			return false;
		});
	});

	// get the report page
	var chapClass;
	$('#bookChapters').find('.expandable > a').click(function(){
		chapClass = $(this).parent().find('.chapter');

		if ($(this).parent().find('.chapter').css('display') == 'none') {
			$(this).parent().css('background-image','url(/img/icons/gloss-close.gif)');
			$(chapClass).slideDown();
		} else {
			$(this).parent().css('background-image','url(/img/icons/gloss-expand.gif)');
			$(chapClass).slideUp();

		}
		return false;
	});


	$('.resourcetip a').hover(function(){
		$(this).parent().find('p').css({ 'top': $(this).position().top + ($(this).height() + 20), 'left': $(this).position().left - ($(this).parent().find('p').width() / 2) }).fadeIn();
	},function(){
		$(this).parent().find('p').fadeOut();
	});
	
	
	$('.alteringTbl').find('tr:odd').addClass('hl');
	$('.articleList').find('.expandable > div').hide();
	$('.articleList').find('.expandable > a').click(function(){
		if ($(this).parent().find('div').css('display') == 'none') {
			$(this).parent().find('div:first').slideDown();
		} else {
			$(this).parent().find('div:first').slideUp();
		}
		return false;
	});
	
	
	// Magic Charts
	var nav = $('#magic-charts .nav').find('a'),
		panels = $('#magic-charts').find('.panel');
	
	panels.hide();
	$(panels[0]).show();
	
	$(nav[0]).parent().addClass('active');
	
	// set up the nav switcher.
	$(nav).each(function(i){
		$(this).bind({
			click : function(){
				panels.hide();
				$(panels[i]).show();
				$(nav).parent().removeClass('active');
				$(this).parent().addClass('active');
				return false;
			}
		});
	});
	
	// set up checkbox map switchers
	var options = {},
		curPanel = null;
	panels.each(function(panelIndex) {
		options = $(this).find('input:checkbox');
		options.each(function(optIndex){
			$(this)
				.attr('checked','checked')
				.bind({
					click : function(){
						if ($(this).is(':checked')) {
							$(panels[panelIndex])
								.find('.type' + parseInt(optIndex+1))
								.css('background-image', 'url(/img/magic-charts/'+$(panels[panelIndex]).attr('id')+'-'+parseInt(optIndex + 1)+'.png)');
						} else {
							$(panels[panelIndex])
								.find('.type' + parseInt(optIndex + 1))
								.css('background-image','none');
						}
					}
				})
				.trigger('click')
				.attr('checked','checked');
		});
	});
});

