(function(){
    
	var n=0;

	var timeout = 500;
	
	var closetimer = 0;
	
	var ddmenuitem = 0;

	var init = function() {
        
        
        $('#twit-sidecarousel').cycle({ 
            fx:     'scrollUp', 
            speed:   500, 
            timeout: 8900, 
            pause:   1,
            fit:    'true',
            height: '79px',
            random: 'true'
        });          
        
        $('.print_icon').click(function() {
           window.print(); 
        });

		$('#JoinForm').each(function() {

			window.onbeforeunload = function() { return "You will lose any changes if you leave this page." };

			$(this).parents('form').submit(function(){
															
				window.onbeforeunload = null;

				return true;

			});

		});

		$('#jsddm > li').bind('mouseover', jsddm_open);
		
		$('#jsddm > li').bind('mouseout',  jsddm_timer);

		document.onclick = jsddm_close;

		$('form').bind('submit', function() {

			$('input', this).each(function() {

				if ($(this).hasClass('default')) {
					
					$(this).val('');
					
				} // end if
			
			}) // end if
			
			return true;
			
		});

		$('#payment').bind('change', function() {
																
			var form = $(this).parents('form')[0];
								
			var worldpay_url = $('#js_worldpay_url', form).val();

			var local_url = $('#js_local_url', form).val();
			
			if ($(this).val() == 1) {
				
				$(form).attr('action', worldpay_url);
				
			} else {
				
				$(form).attr('action', local_url);

			}

		});
		
		$('#close-window').click(function(){window.close();});
		
		$('.gallery').each(function(){
																
			var options = {
				animationSpeed: 'fast', /* fast/slow/normal */
				padding: 10, /* padding for each side of the picture */
				showTitle: false, /* true/false */
				allowresize: true,
				counter_separator_label: ' of ', /* The separator for the gallery counter 1 "of" 2 */
				theme: 'dark_rounded' /* light_rounded / dark_rounded / light_square / dark_square */
			};
		
			$("a[rel^='prettyPhoto']", this).prettyPhoto(options);
																
		});
		
		$('#contact_type').each(contactTypes);
		
		$('#contact_type').change(contactTypes);
		
		$('#enquiry_type').each(function(){
			
			$('label[for=contact_details]').text(ucfirst($(this).val()) + ': *');
				
		});
		
		$('input[name=enquiry_type]').click(function(){

			$('label[for=contact_details]').text(ucfirst($(this).val()) + ': *');
																									
		});
		
		$('.journal-list').each(initJournals);

		$('input').each(setDefault);
		
		$('#memberornot').each(updateMemberNo);

		$('#memberornot').bind('change', updateMemberNo);

		initLanguages();
		
	} // end init

	var updateMemberNo = function() {
	
		var val = $(this).val();
		
		if (val == 0) {
			
			$('#membership_no').parent().hide();
			
		} else {
			
			$('#membership_no').parent().show();
			
		}
																	
	}

	var jsddm_open = function() {  
	
		jsddm_canceltimer();
	
		jsddm_close();
	
		ddmenuitem = $(this).find('ul');
		
		ddmenuitem.css('visibility', 'visible');
		
		ddmenuitem.show();

		/*

		ddmenuitem = $(this).find('ul').css('display', 'block');

		ddmenuclose = $(this).find('a.closeNavigation').css('display', 'block');
		
		ddmenuclose.bind('click', jsddm_close);
		
		*/

	} // end jsddm_open

	var jsddm_close = function() {  
	
		if (ddmenuitem) {
			
				ddmenuitem.hide();
			
				// ddmenuitem.css('display', 'none');
				
				// ddmenuclose.css('display', 'none');
				
		} // end if
		
	} // end if
	
	var jsddm_timer = function() {  
	
		closetimer = window.setTimeout(jsddm_close, timeout);
		
	}
	
	var jsddm_canceltimer = function() {  
	
		if(closetimer) {  
	
			window.clearTimeout(closetimer);
	
			closetimer = null;
	
		}
	
	}

	var removeDefault = function() { 
				
		$(this).val(''); 
		
		$(this).removeClass('default');
		
		$(this).unbind('focus', removeDefault);

		$(this).unbind('blur', setDefault);
	
		$(this).bind('blur', setDefault);

	} // end removeDefault

	var setDefault = function() {
		
		var debug = $(this).attr('type') == "hidden";
		
		if (debug) {
		
			return false;

		}

		if ($('label[for="' + $(this).attr('id') + '"]').css('display') == 'none' && $(this).val() == "") {
		
			var name = $(this).attr('name');
			
			var text = $('label[for="' + $(this).attr('id') + '"]').text();

			if ($(this).attr('type') == "password") {

				var html = '<input id="' + name + '" type="text" value="' + text + '" name="' + name + '" />';

				$(this).replaceWith(html);

				$('#' + name).addClass('default');

				$('#' + name).bind('focus', function(){
				
					var html = '<input id="' + name + '" type="password" value="" name="' + name + '" />';

					$(this).replaceWith(html);
					
					$('#' + name).get()[0].focus();
					
					$('#' + name).removeClass('default');
				
					$('#' + name).bind('blur', setDefault);
			
				});
				
			} else {
				
				$(this).val(text);	
				
				$(this).addClass('default');

				$(this).bind('focus', removeDefault);

			} // end if
	
		} // end if
		
		if ($(this).hasClass('default-value')) {
			
			var val = $(this).val();
			
			var text = $(this).data('default');
			
			if (!text || val == "") {
			
				if (!text)
					
					text = val;
	
				$(this).val(text);
			
				$(this).data('default', text);

				$(this).bind('focus', removeDefault);
				
				$(this).addClass('default');
			
			}
			
		}
		
	} // end setDefault

	var selectJournal = function(id) {
		
		var checked = $("#" + id + ":checked").length;
		
		var list = $('#' + id).parents('ul.journal-list');
		
		var extras = $('#journals-max').val() - $('li.checked,li.extra', list).length > 0 ? false : true;
		
		var cls = extras ? "extra" : "checked";
		
		if (checked) {

			var name = $('#' + id).attr('name', name);

			$('#' + id).parents('li.journal').addClass(cls);

		} else {
			
			if ($('#' + id).parents('li.journal').hasClass('checked') && extras) {
				
				var extra = $('li.extra:first', list);
				
				extra.removeClass('extra');

				extra.addClass('checked');

			}
		
			$('#' + id).parents('li.journal').removeClass('extra');
			
			$('#' + id).parents('li.journal').removeClass('checked');

		} // end if
		
	}
	
	var refreshJournals = function(list) {
		
		var total = $('li.checked,li.extra', list).length;

		var extras = $('li.extra', list).length;

		var maximum = $('#journals-max').val();
		
		var cost = $('#journals-price').val();
		
		if (total >= maximum) {
	
			$('p.instructions-1').hide();

			$('p.instructions-2').show();

		} else {
			
			$('p.instructions-1').show();

			$('p.instructions-2').hide();

		}
		
		if (extras) {
			
			$('p.journal-messages').show();
			
			$('p.journal-messages').text('You have selected ' + extras + ' additional journal subscriptions, which comes to an extra £' + (extras * cost) + '.');
			
		} else {
			
			$('p.journal-messages').hide();
			
			$('p.journal-messages').text('');
			
		}
		
		var remaining = maximum - total;
		
		var output = remaining == 1 ? remaining + " journal" : remaining + " journals";
		
		var extras = [];
		
		$('.journal-list li.extra').each(function(){
			
			extras.push($('input[name="journals[]"]', this).attr('value'));
			
		});
		
		if (extras.length > 0) {
			
			$('#journals-extras').val(extras.join(','));
			
		}
		
		$('span.journal-count').text(output);

	}

	var initJournals = function() {

		var count = 0;

		var maximum = $('#journals-max').val();

		$("input[type=checkbox]:checked", this).each(function(){
																					 
			count++;
		
			if (count > maximum)
			
				$(this).parents('li.journal').addClass('extra');
			
			else
			
				$(this).parents('li.journal').addClass('checked');

		});
		
		refreshJournals(this);

		$('input[type=checkbox]', this).click(function(){
																						 
			// check to see if this box is checked
			
			var id = $(this).attr('id');
			
			var list = $('#' + id).parents('ul.journal-list');

			selectJournal(id);
			
			refreshJournals(list);
			
		});
				
	}

	var initLanguages = function() {
		
		$('input[name="languages[]"][value="Other"]').click(function(){

			var checked = $('input[name="languages[]"][value="Other"]:checked').length;

			if (checked)
			
				$('input#language-other').show();
				
			else
			
				$('input#language-other').hide();

		});

		$('input[name="memberornot[]"][value="Yes"]').click(function(){

			var checked = $('input[name="memberornot[]"][value="Yes"]:checked').length;

			if (checked)
			
				$('input#member-other').show();
				
			else
			
				$('input#member-other').hide();

		});
		
		$('input[name="sectors[]"][value="Other"]').click(function(){

			var checked = $('input[name="sectors[]"][value="Other"]:checked').length;

			if (checked)
			
				$('input#sector-other').show();
				
			else
			
				$('input#sector-other').hide();

		});
		
		$('input[name="age_groups[]"][value="Other"]').click(function(){

			var checked = $('input[name="age_groups[]"][value="Other"]:checked').length;

			if (checked)
			
				$('input#age_group-other').show();
				
			else
			
				$('input#age_group-other').hide();

		});		

	}

	var contactTypes = function() {
																			 
		var optgrp = $('option:selected', this).parents('optgroup');
		
		if (optgrp.length && optgrp.attr('label') == "Journals") {
			
			$('#contact_type_journals').parent().show();
			
		} else {
			
			$('#contact_type_journals').parent().hide();
			
		}
																		 
	} // end contactTypes

	function ucfirst (str) {
			// http://kevin.vanzonneveld.net
			// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
			// +   bugfixed by: Onno Marsman
			// +   improved by: Brett Zamir (http://brett-zamir.me)
			// *     example 1: ucfirst('kevin van zonneveld');
			// *     returns 1: 'Kevin van zonneveld'
	 
			str += '';
			var f = str.charAt(0).toUpperCase();
			return f + str.substr(1);
	}

	var cancelForm = function(url) {
															
		window.onbeforeunload = null;

		location.href = url;

	}

	window.cancelForm = cancelForm;

	$(document).ready(init);

})();


if ($.browser.version > 7 || !$.browser.msie) {

	Cufon.replace('h2.homepage');
    

} else {
	
	Cufon.replace('.grey-small-panel-bd h2');
	
}

Cufon.replace('.content h2');

Cufon.replace('.content h3:not(.content .large-panel-bd h3)');

Cufon.replace('.sidepanel h3');

Cufon.replace('h3.showreel_text', {    textShadow: '4px 4px 4px #333' });



function showDelegates(){
	var number = parseInt($('#no_delegates').val());		
	if (number > 1){
		var hidenu = number + 1;
		for (z = hidenu ; z <= 6; z ++){
			$('#delegate_details_'+z).hide('medium');
		}
		for (i=2; i <= number; i++){
			$('#delegate_details_'+i).show('medium');
		}
		
	} else {
		for (z = 2 ; z <= 6; z ++){
			$('#delegate_details_'+z).hide('medium');
		}	
	}
}

function showTerms (url){
	window.open (url+"terms_and_conditions","mywindow");	
	
}
