$.fn.placeholdr = function(val) {
	
	$(this).val(val);
	
	$(this).click(function(){
		if($(this).val() == val){	
			$(this).val('');
		}
	});
}

$.fn.hide_tools = function() {
	if(!$('#group-footer-tools').length) {
		return;
	}
	
	var w = $(window).height();
	var c = $('#primary-content').height() + $('#unit-header').height();
	
	if(w > c) {
		$('#group-footer-tools').css({display: 'none'});
	} else {
		$('#group-footer-tools').css({display: 'block'});
	}
}

$(document).ready(function(){
	
	$('.clickable').clickable().css('cursor','pointer');
		
	$('.text.placeholder').placeholdr('Enter email address');
	$('.search_placeholder').placeholdr('Search Film Birmingham&hellip;');
	
	$('#back-to-top').click(function(){
		$(document).scrollTo( $(this).attr('href'), 800, {easing:'easeOutCubic'} );
		return false;
	});
	
	$("#field-site-search").placeholdr();
	$("#field-subscribe").placeholdr();
	
	//do the tabbing (once the dom has loaded!)
	$("ul.request-forms-tabs li").click(function(){
		if (!$(this).hasClass("selected"))
		{
			$("ul.request-forms-tabs li").removeClass("selected");
			$(this).addClass("selected");
			$("div.tab-panel").removeClass("selected");
			//work out the tab to show
			$(".tab-panel." + $(this).attr("id")).addClass("selected");	
		}
		return false;
	});
	// Datepicker
	if($.browser.msie && parseInt($.browser.version, 10) == 6) {
		
	} else {
		$(".datepicker").datepicker({
			showOn: 'button',
			buttonImage: '/mmlib/images/filmbirmingham/calendar.gif',
			buttonText: 'Choose Date',
			buttonImageOnly: true		
		});
	}

	//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').val('Enter email address').placeholdr();
		$('#share-email-list').append(newAddr);
		return false;
	});
	
	$('#btn-share-by-email').click(function(){
		var emailAddress = [];
		$('#share-email-list input').each(function(){
			emailAddress.push($(this).val());
		});
			
		$(this).attr('value', 'Sending...');
			
		var data = {
			"object-type": $('#object-type').val(), 
			"object-id": $('#object-id').val(),
			"email": emailAddress
		}
		$.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 email address');
			$('#unit-social-media-bookmarks').fadeOut('fast');
			alert('Your message has been sent');
		});
		return false;
	});
	
	$('#group-footer-tools').hide_tools();
	
	$('.unit-site-feature').each(function(){
		var innerWrapper = $(this).find('.site-feature-inner-wrapper');
		var offsetTop = (innerWrapper.height() - 25);
		var initialInfoOffset = {
			"top": (offsetTop - 11),
			"left": 0
		};
		
		var infoBox = $(this).find('.info');
		
		infoBox.css({
			"top": initialInfoOffset['top']
		});
		
		$(this).hover(function() {
			infoBox.stop().animate({
				"top": (initialInfoOffset['top'] - (infoBox.height() - 20))
			}, 500);
		}, function() {
			infoBox.stop().animate({
				"top": initialInfoOffset['top']
			}, 500);
		});
	});
	
	$('body').addClass('js-enabled');
	//	tab switching
	$("ul.request-forms-tabs li").click(function(){
		if (!$(this).hasClass("selected")) {
			$("ul.request-forms-tabs li").removeClass("selected");
			$(this).addClass("selected");
			$("div.tab-panel").removeClass("selected");
			//work out the tab to show
			$(".tab-panel." + $(this).attr("id")).addClass("selected");	
		}
		return false;
	});
	
	$('.form-actions-back, .form-actions-next').click(function(){
		$("div.tab-panel, .request-forms-tabs li").removeClass("selected");
		//work out the tab to show
		$($(this).attr("href")).addClass("selected");
		$('.request-forms-tabs li a[href=' + $(this).attr('href') + ']').parent().addClass('selected');
		
		var $inputs = $(this).parent().parent().find(':input');
		$inputs.each(function() {
			
		});
		
		$("body").scrollTop(180);
		
		return false;
	});
	
});


