$( document ).ready( function() {
	/*
		ATTACH THE THROW POPUP EVENT
	*/
	$( ".throwpop" ).click( function( event ) { 
		// Stop the browser from following the link
		event.preventDefault();
		
		// Grab the target of the link
		var sTarget = $( this ).attr( "href" );
		
		// Set the width and height of the popup
		var iWidth = 800;
		var iHeight = 600;
		var iScrollbars = 2;
		
		// Center the window on the clients screen
		var iLeft = (screen.width)?(screen.width-iWidth)/2:100;
		var iTop = (screen.height)?(screen.height-iHeight)/2:100;
		
		// Put the window config together
		var sSettings='width='+iWidth+',height='+iHeight+',top='+iTop+',left='+iLeft+',scrollbars='+iScrollbars+',location=yes,directories=no,status=yes,menubar=yes,toolbar=yes,resizable=yes';
		
		// Create the new window
		win=window.open( sTarget, "", sSettings );
	} );
	
	/*
		SET UP AN ACCORDION
	*/
	function accordionise() {
		// Add the styles to each of the container divs
		$( "#accordion > div" ).each( function(i) {
			var cssObj = {
				overflow: "hidden",
				height: "auto",
				color: "ff0000"
			}
			$(this).css(cssObj);
			
			if( i > 0 ) {
				$(this).css( "display", "none" );
			}

		});
		
		// Make sure the toggler acts like a link
		$( "#accordion > a" ).each( function(i) {
			$(this).css( "cursor", "pointer" );
		});
		
		// Active the accordion
		jQuery( "#accordion" ).accordion({ 
			autoHeight: false ,
			header: "h3.toggler"
		});
	}
	accordionise();
	
	
	// JS to uncheck radio buttons if you click on the selected one
	
	 $( "input[type='radio']" ).each( function(i) {
		$(this).mousedown(function () { 
			var oElement = $( this );
			var sName = oElement.attr( "name" );
			
			var iNewVal = oElement.attr( "value" );
			var iOldVal = $( "input[name='" + sName + "']:checked" ).attr( "value" );
			
			if( iNewVal == iOldVal ) {
				setTimeout( function() {
					oElement.attr( "checked", false );
				}, 100 )
			}
		});
	});
	

	$(".lightbox").lightBox({fixedNavigation:true});
	
	
	/*
		CYCLING IMAGE ON WEDDING PAGE
	*/
	var sPath = "#weddings_index #threeimages .two img";
	var iCurrentImageIndex = 0;
	
	var sImages = new Array();
	sImages[0] = $( sPath ).attr( "src" );
	sImages[1] = "/media/images/weddings/banner_justmarried.jpg";
	
	function cycleImage() {
		$( sPath ).fadeOut( "slow", function() {
			$( sPath ).attr( "src", sImages[ iCurrentImageIndex ] );
			$( sPath ).fadeIn( "slow" );
		});
		
		iCurrentImageIndex++;
		iCurrentImageIndex = ( iCurrentImageIndex < sImages.length ) ? iCurrentImageIndex : 0;
		
		setTimeout( cycleImage, 5000 );
	}
	
	if( $( sPath ).length > 0 ) {
		setTimeout( cycleImage, 5000 );
	}
	
	// Nice reset button
	var aResets =  $( '.form_reset' );
	if( aResets.length > 0 ) {
		aResets.each( function(i) {
			$( this ).replaceWith( '<img src="/media/images/buttons/clear.jpg" alt="Reset all product options" class="form_reset" />' );
		});
		$( 'img.form_reset' ).click( function(i) {
			$( this ).parents( 'form' ).each(function(){
				this.reset();
			});
		});
	}
	
	$('#terms-reveal').hide();
	//$('.reveal').click(function(){
		
		$('.reveal').toggle(function() {
			$('#terms-reveal').slideDown();	
		}, function() {
			$('#terms-reveal').slideUp();	
		});								
	//});
	
	
	
} );

/*
	ATTACH ANY LIGHTBOXES
	This needs to be outside the DOM ready function
*/
function attachLightbox( element ) {
	element.lightBox({fixedNavigation:true});
}

