$(document).ready(function(){
	
	// global
		
	$('#header li').each(function(){
		
		if ($(this).attr('rel') != '0' && $(this).attr('rel') != '279' && $(this).attr('rel') != '276'){ // not home or contact
			
			$(this).append('<div class="drop-down" style="display: none;"><div class="inner"></div><div class="bott-cap"></div></div>');

			$.each(map[$(this).attr('rel')], function(index, value){
				
				var html = '<a href="/pages/view/' + value.id + '/' + value.slug + '">' + value.name + '</a>';
				$('#header li[rel="' + value.parent + '"] .drop-down .inner').append(html);
			});
		}	
	});

	$('.drop-down').hover(function(){
		
		$(this).show();
	
	}, function(){
		
		$(this).hide();
	});

	// home

	$('#home-video-ph').click(function(){
		
		$('#home-video').show();
		$(this).hide();
	});

	//$.preloadImages("home-over.png", "about-over.png", "books-over.png", "school-over.png", "multimedia-over.png", "conferences-over.png", "contact-over.png");
	
	//$('#header li.active a').not('.drop-down a').css('background', 'url(/images/nav/' + $('#header li.active a').attr('rel') + '.png)');

	$('#header li a').not('.drop-down a').hover(
							function(){
								
								//$(this).data('bg', $(this).css('background-image'));
								//$(this).css('background', 'url(/images/nav/' + $(this).attr('rel') + '.png)');

								$(this).parent().find('.drop-down').show();
								
							}, 
		
							function(){

								//$(this).css('background', $(this).data('bg'));
								$(this).parent().find('.drop-down').hide();
							}
					);

	$('ul.downloads li').find('a:first').click(function(){
		
		parent = $(this).parent();
		parent.toggleClass('active');
		parent.find('p').toggle();

		return false;
	});
	

	// eletter sign up

	$('#eletter-sign-up').data('init', $('#eletter-sign-up').val());

	$('#eletter-sign-up').focus(function(){
		$('#eletter-sign-up').val($('#eletter-sign-up').val() == $('#eletter-sign-up').data('init') ? '' : $('#eletter-sign-up').val()); 
	});

	$('#eletter-sign-up').blur(function(){
		$('#eletter-sign-up').val($('#eletter-sign-up').val() == '' ? $('#eletter-sign-up').data('init') : $('#eletter-sign-up').val()); 
	});

	$('#eletter-sign-up-butt').click(function(){

		$('#eletter-sign-up-form').ajaxSubmit({ url: 'http://www.thecanarycollective.com/subscribers/add_email/5/' + $('#eletter-sign-up').val() + '/?format=json&callback=?', type: 'GET', dataType: 'json', success: function(e){
			
			$('#eletter-sign-up').val(e.message);
			$('#eletter-sign-up').css('color', e.status > 0 ? '#05931C' : '#FF0000');
		} });

		return false; 
	});

	// testimonials

	if ($('#testimonials').length){
			
		$('#testimonials').cycle({

			fx:     'scrollHorz', 
			prev:   '#testimonials-prev', 
			next:   '#testimonials-next',
			speed:  500, 
			timeout: 0 
		});
	}

	// page tabs

	$('#page-tabs a').click(function(){
		
		$('#page-tabs a').removeClass('active');
		$(this).addClass('active');

		$('div.sub').hide();
		$('#sub-' + $(this).attr('rel')).show();

		$(this).blur();
		return false;
	});

	// pics

	if ($('#pics').length){
			
		$('#pics').cycle({

			fx:     'fade', 
			speed:  500, 
			timeout: 0 
		});

		$('#pics-thumbs img').click(function(){
			
			$('#pics').cycle($(this).index());
		});
	}
});



jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
