$(document).ready(function(){
	try{
		if($.fn.supersized){
			$.fn.supersized.options = {
				startwidth: 1024,
				startheight: 768,
				minsize: .5,
				slideshow: 1,
				slideinterval: 5000
			};
		    $('#supersize').supersized();
		}
	}catch(e){}

	$('.clickable').clickable().css('cursor','pointer');
	$("#field-site-search").placeholdr();
	$("#field-subscribe").placeholdr();
	$('.placeholder').placeholdr();
	
	$("#unit-navigation ul").lavaLamp({
        fx: "easeout",
        speed: 333
    });

	var productionthumbnails = $('.production-thumbnail');
	
	$('.production-thumbnail').live('mouseover mouseout', function(event){
		var active = $(this);
		if(event.type == 'mouseover'){
			$('.production-thumbnail').each(function(){
				if($(this).attr('id') != active.attr('id')){
					$(this).stop().fadeTo(300, 0.3);
				}
			});
		}else{
			$('.production-thumbnail').each(function(){
				if($(this).attr('id') != active.attr('id')){
					$(this).stop().fadeTo(300, 0.95);
				}
			});
		}
	});
	
	var pagethumbnails = $('.page-thumbnail');
	
	$('.page-thumbnail').live('mouseover mouseout', function(event){
		var active = $(this);
		if(event.type == 'mouseover'){
			$('.page-thumbnail').each(function(){
				if($(this).attr('id') != active.attr('id')){
					$(this).stop().fadeTo(300, 0.3);
				}
			});
		}else{
			$('.page-thumbnail').each(function(){
				if($(this).attr('id') != active.attr('id')){
					$(this).stop().fadeTo(300, 0.95);
				}
			});
		}
	});

	$('#back-to-top').click(function(){
		$(document).scrollTo( $(this).attr('href'), 800, {easing:'easeout'} );
		return false;
	});
	
	//Social media bookmarks
	$('a.share-button').click(function(){
		if( $('#unit-social-media-bookmarks').css('display') == 'block' ){
			$('#unit-social-media-bookmarks').fadeOut('fast');
		}else{
			$('#unit-social-media-bookmarks').fadeIn('fast');
		}
		return false;
	});
	$('#social-media-close').click(function(){
		$('#unit-social-media-bookmarks').fadeOut('fast');
		return false;
	});
	$('#add-another-email-address').click(function(){
		var newAddr = $('#share-email-list li:first').clone();
		newAddr.children('input').addClass('placeholder').each(function(){
			if($(this).hasClass('input-name')){
				$(this).val('Enter friend\'s name');
			}else{
				$(this).val('Enter friend\'s email address');
			}
			$(this).placeholdr();
		})
		$('#share-email-list').append(newAddr);
		return false;
	});
	
	$('#btn-share-by-email').click(function(){
		var emailAddress = [];
		var names = [];
		var error = 0;
		
		$('#share-email-list input').each(function(){
			if($(this).hasClass('input-name')){
				names.push($(this).val());
			}else{
				if(validate($(this).val())) {
					emailAddress.push($(this).val());
				} else {
					alert('You have not entered a valid email address');
					error = 1;
					return false;
				}
			}
		});
			
		$(this).attr('value', 'Sending...');
			
		var data = {
			"object-type": $('#object-type').val(), 
			"object-id": $('#object-id').val(),
			"receiverEmail": emailAddress,
			"receiverNames": names,
			"senderName": $('#yourname').val()
		}
		if(error < 1) {
			$.post('/mmlib/includes/ajax/ajax.sendtofriend.php/', data, function(){
				$('#share-email-list li:gt(0)').remove();
				$('#btn-share-by-email').attr('value', 'Send');
				$('#share-email-list li input').addClass('placeholder').val("Enter friend's email address");
				$('#unit-social-media-bookmarks').fadeOut('fast');
			});
		} else {
			error = 0;
		}
		return false;
	});

	
	$('.unit-site-feature').each(function(){
		var infoBox = $(this).find('.info');
		var infoBoxHeight = infoBox.height() - 20; //20 is height of first line of header

		var currentTop = parseInt(infoBox.css('top'));
		$(this).data('defaultTop', currentTop);
		
		$(this).hover(
			function(){	//over	
				var newTop = currentTop - infoBoxHeight;
				infoBox.stop().animate({top:  newTop}, 500, 'easeout');
			},
			function(){ //out
				var infoBox = $(this).find('.info');
				infoBox.stop().animate({top: $(this).data('defaultTop')}, 500, 'easeout');
			}
		);
	});
	
	
	
	//Track outgoing links
	$('a[href^="http"]').click(function() {
		try{
			pageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));
		}catch(e){}
	});
	
	// Validate emails
	function validate(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   if(reg.test(email) == false) {
	      return false;
	   } else {
		  return true;
	   }
	}
	
});

