

$(document).ready( function() {
	
	externalLinks();
	
	//removes the border from the last item
	$(".upcoming a:last-child").css("border","none");
	$(".directions a:first-child").css("border","none");

	$("#toggle").click(function() {
		$(".cms_all").toggle();
	});
	
	//remove underlines from links that contain strong or bold tags
	$('a strong').parent().css('text-decoration', 'none');
	//$('a strong').css('text-decoration', 'none');

	
	/*------------------------------------------------------------------------------
		admin accordians
	------------------------------------------------------------------------------*/	

		jQuery('#admin_accordian').accordion({ 
			autoheight: false,
			active: true, 
			alwaysOpen: false
		});
	
	
		jQuery('.accordion').accordion({ 
			autoheight: false,
			active: true, 
			alwaysOpen: false
		});

	
	
	/*------------------------------------------------------------------------------
		form submission and light validation
	------------------------------------------------------------------------------*/	

		$(".form_to_email #submit").click(function(){

			error = '<strong>Please fill in these required fields<\/strong>:<br />';
			formIsValid = true;
			//--------------------------------------------
			// validation
				required_fields = $('#required').val();
				var required_fields = required_fields.split(',');

				for( i=0; i < required_fields.length; i++){
					ffield = required_fields[i];
	
					if($("#" + ffield).val() == "" || $("#" + ffield).val() == undefined){
						error = error + required_fields[i] + ', '; 
						formIsValid = false;
					}
					
				}
	
				strLen = error.length; 
				error = error.slice(0,strLen-2);
				error = error.replace('_', ' ');
	
			//--------------------------------------------
			// submit the form if all the validation passed
			if(formIsValid){
			
				submit_to = $(".form_to_email").attr("action");
				$(".form_to_email").css('display', 'none');
				
				$.post(submit_to, $(".form_to_email").serialize(), function(data){
					$("#error_message").css('display', 'none');
					$('#success_message').css('display', 'block');
					$('#success_message').html(data);
				});
				
			}else{
				location.href = location.href+"#error_message";
				$("#success_message").css('display', 'none');
				$("#error_message").css('display', 'block');
				$("#error_message").html(error);
			}
		
			return false;

		});

});

/*------------------------------------------------------------------------------
	make slug (url friendly string) from a dirty string
------------------------------------------------------------------------------*/	

	function convertToSlug(inputText){
		return inputText
		.toLowerCase()
		.replace('&', 'and') 		// & becomes "and"
		.replace(/^\s+|\s+$/g, "") 	// remove whitespace from beginning or end
		.replace(/[^\w ]+/g,'')		// removes anything that isn't alphanumeric, underscore, hyphen
		.replace(/ +/g,'_') 		// spaces become underscores
		;
	}


/*------------------------------------------------------------------------------
	Popup Windows
------------------------------------------------------------------------------*/	

	var myWin = null;
	function popup(theUrl, width, height) {
		if ((! myWin) || (myWin.closed)) {
			theDim = 'width='+width+',height='+height+',left=5,top=5,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,';
			myWin = window.open(theUrl, '', theDim);
			myWin.opener = window;
		} else {
			myWin.location = theUrl;
			myWin.focus();
			myWin.opener = window;
		}
	}
	
	
	function p_link(mylink, closeme){
		if ( (window.focus && window.opener)){
			if (closeme){ // ADDED BY NR
				window.opener.focus();
			}
			window.opener.location.href=mylink;
		}
		if (closeme){
			window.close();
		}
	}


/*------------------------------------------------------------------------------
	XHTML valid external links
------------------------------------------------------------------------------*/	

	function externalLinks() { 
		if (!document.getElementsByTagName) return; 
		var anchors = document.getElementsByTagName("a"); 
		for (var i=0; i<anchors.length; i++) { 
			var anchor = anchors[i]; 
			if (anchor.getAttribute("href") && 
					anchor.getAttribute("rel") == "external") 
				anchor.target = "_blank"; 
		} 
	} 


