function SmoothScroll() {
	$('.scroll').click(function() {
	  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	  && location.hostname == this.hostname) {
		var $target = $(this.hash);
		$target = $target.length && $target
		|| $('[name=' + this.hash.slice(1) +']');
		if ($target.length) {
		  var targetOffset = $target.offset().top;
		  $('html,body')
		  .animate({scrollTop: targetOffset}, 1000);
		 return false;
		}
	  }
	});
}

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
   };

// Accordion
function Accordion() {

   	var togglers = $('.accordion .toggler');
   	var elements = ".accordion .element";

   var elementsWithErrors = $("ul.accordion li div.element:has(ul li div.error)");

   if (elementsWithErrors.size() > 0) {
   		$(elements).hide();   
   		elementsWithErrors.show();
   	}
   	else {
   	$(elements).hide();
   	$(elements + ':first').show()
   	$('.accordion .toggler a span').addClass('active');
   	}


   	togglers.click(
	function() {
		var checkElement = $(this).next();
		if ((checkElement.is(elements)) && (checkElement.is(':visible'))) {
			$(elements + ':visible').slideUp(300);
			$(this).children().removeClass('active');
			return false;
		}
		if ((checkElement.is(elements)) && (!checkElement.is(':visible'))) {
			$(elements + ':visible').slideUp(300);
			checkElement.slideDown(300);
			$(this).children().addClass('active');
			return false;
		}
	});

}

$(document).ready(function() {

	$(document).pngFix();
	SmoothScroll();
	Accordion();

	$("a[rel*='external']").attr({ target: "_blank" });

	var volatile = $(".field input")
	volatile.focus(function(event) {
		if ($(this).val() == $(this).attr("title")) {
			$(this).val("");
		} 
	}, volatile.blur(function(event) {
		if ($(this).val() == "") {
			$(this).val($(this).attr("title"));
		}
	}
    ));

	$(".barToggler").click(function() {
		e.preventDefault();
		$("#barAccordian").slideToggle("fast");
	});

	// Now get the font changed: http://cufon.shoqolate.com/generate/
	Cufon.replace('h1, #nav li, .menuList span, .voucherDownload span, #content .participantList h2, #content .contact h2, #content .alertMail h2', { fontFamily: 'ShEuropa', hover: true });

	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({ opacity: 'toggle' }, speed, easing, callback);
	};

	var fadeToggler = $(".loginToggler")
	var fadeElement = $(".loginOverlay")

	$(fadeToggler).click(function(event) {
		event.preventDefault();
		fadeElement.fadeToggle("normal");
	});

	$('#overlayMap').empty();
	$('#overlayMap').flash({
		src: '/_images/varsity/flash/test.swf',
		wmode: 'transparent',
		width: 440,
		height: 280
	});

	//Custom selectboxes

	var selectBoxWidth;

	if ($('.galleryFilter .select select')) {
		selectBoxWidth = '250px';
	}
	else {
		selectBoxWidth = '160px';
	}

	$('#content .bookingForm .select select').selectmenu({ style: 'dropdown', width: '303px', maxHeight: 200 });
	$('#content .bookingForm .dateSelect select').selectmenu({ style: 'dropdown', width: '95px', maxHeight: 200 });
	$(' #content .contact .select select').selectmenu({ style: 'dropdown', width: '299px', maxHeight: 200 });
	$('#content .contact .dateSelect select').selectmenu({ style: 'dropdown', width: '93px', maxHeight: 200 });

	$('input').customInput();

	$('#homeFlash').empty();
	$('#homeFlash').flash({
		src: '/_images/varsity/flash/Varsity-hompage.swf',
		width: 698,
		height: 363,
		WMODE: 'transparent'
	});

	$('#varsityFlash').empty();
	$('#varsityFlash').flash({
		src: '/_images/varsity/flash/Call-to-Action-pod.swf',
		width: 248,
		height: 202,
		WMODE: 'transparent',
		flashvars: { xmlPath: "/_images/varsity/flash/xml.xml", configPath: "/_images/varsity/flash/config.xml" }

	});

	jQuery('#vcardCarousel').jcarousel({
		auto: 3,
		scroll: 1,
		wrap: 'last',
		initCallback: mycarousel_initCallback
	});

	$("ul.barGallery li a").click(function() {
		var mainImage = $(this).attr("href"); //Find Image Name
		$("#barMain img").attr({ src: mainImage });
		return false;
	});







});

