$(function() {
	
	// START THE SLIDESHOW
	var refreshIntervalId = setInterval("slideSwitch()", 5000);
	
	
	// ALLOW MENU DRILL-DOWNS
	
	// hide 'em by default
	$('#sidebar-menus ul#menu-selector li ul').hide();
	
	// show the ones you're in
	$('#sidebar-menus ul#menu-selector li.menu-live ul').show();
	
	// show the ones you click on, too
	$('#sidebar-menus ul#menu-selector li').click(function() {
		$(this).addClass('menu-live').find('ul').show();
	});
	
    
    // MAKE LINKS POINTING ELSEWHERE OPEN ELSEWHERE
    $("a[href*='http://']:not([href*='"+location.hostname+"'])").click( function() {
    window.open(this.href);
    return false;
    });
    
    
    // HANDLE THE MODAL WINDOW
    
    // show it on button click
    $('#diners-club-button').click(function(e) {
    	e.preventDefault();
    	$('#modal-container').show();
    });
    
    // hide it on background or close click
    $('#modal-background, #modal-close').click(function() {
    	$('#text-fields input').val('');
    	$('#modal-container').hide();
    });
    
    // disable the submit button until proven otherwise
    $('#msubmit').attr('disabled', 'disabled');
    
    // prevent submitting w/o req'd fields
    $('#modal-content input').keyup(function() {
    	
    	if (
    		($('#mname').val().length>0) && 
    		($('#mstreet').val().length>0) && 
    		($('#mcity').val().length>0) && 
    		($('#mstate').val().length>0) && 
    		($('#mzip').val().length>0) && 
    		($('#mphone').val().length>0) && 
    		($('#memail').val().length>0)
    		)
    		
    	{
    		
		    // disable the submit button until proven otherwise
    		$('#msubmit').attr('disabled', '');
    
    	} else {
    		
		    // disable the submit button until proven otherwise
    		$('#msubmit').attr('disabled', 'disabled');
    
    	}
    	
    });
	
	
	// HANDLE THE MODAL POST SUBMIT
	
	/* attach a submit handler to the form */
	$("#modal-content form").submit(function(event) {
		
		/* stop form from submitting normally */
		event.preventDefault(); 
		
		/* get some values from elements on the page: */
		var
			$form = $(this),
			term = $form.find('input[name="s"]').val(),
			url = $form.attr('action');
		
		/* Send the data using post and put the results in a div */
		$.post(
			url,
			{
				Name : $('#mname').val(),
				Street : $('#mstreet').val(),
				City : $('#mcity').val(),
				State : $('#mstate').val(),
				Zip : $('#mzip').val(),
				Phone : $('#mphone').val(),
				Email : $('#memail').val(),
				Twitter : $('#mtwitter').val(),
				Facebook : $('#mfacebook').val()
			},
			function(data) {
				$("#modal-content form").html(data);
			}
		);
		
	});

});


function slideSwitch(prev) {
	
	// initiate the variable
	var $active = $('#slideshow-slides div.active');
	
	// make the last one "active" if there isn't one already
	if ($active.length == 0) $active = $('#slideshow-slides div:last');
	
	// make the first one "next" if it's at the end
	var $next = $active.next().length ? $active.next() : $('#slideshow-slides div:first');
	
	// set the "active" one a step back so the other can appear over it
	$active.css({'z-index' : 24});
	
	// let the other one materialize in front of it
	$next.css({'opacity' : 0.0, 'z-index' : 25}).addClass('active').animate({opacity: 1.0}, 1000, function() {
		// clean up after it
		$active.css({'opacity' : 0.0}).removeClass('active');
	});
	
}


