(function($) {
// Wrapper for SimpleModal plugin
// http://www.ericmmartin.com/projects/simplemodal/
$.fn.overlay = function(options) {

	if (!this.length) { 
		console.log('Overlay: ERROR. Stopping. - no element selected');
		return this; 
	}
	var $this = $(this);	
	var opts = $.extend(true, {}, $.fn.overlay.defaults, options);
	var content = "no content";
	var $success = false;
	var href = $this.attr('href');

	if(href) {
		$.ajax({
			url:href,
			dataType: 'html',
			beforeSend: function() {
				$.modal.close(); //close any modals if opened
			},
			success: function(data) {
				if(opts.scroll) data = scrollPrep(data);		
				$.modal( data , opts );
			},
			complete: function() {
				if(opts.scroll){
					var scrollarea = $('#simplemodal-data')
					scrollarea.tinyscrollbar();
				} 
			},
		});
	}else{
		data = $this[0];
		if(opts.scroll) data = scrollPrep(data);		
		$.modal( data , opts );
		if(opts.scroll){
			var scrollarea = $('#simplemodal-data')
			scrollarea.tinyscrollbar();
		}
	}
	
	//update scroll after search so images have a chance to load
	if($this.attr('id') == 'searchresults') {
		setTimeout(function() {
			scrollarea.update(); 
		}, 1000);
	}
	
	//////	
	if($this.attr('class') == 'nav_contact') {	
		setTimeout(function() {
			//console.log($("#ContactsIndexForm"));	
			var ContactsIndexForm = $("#ContactsIndexForm").ajaxForm({ 
				beforeSubmit: function( $opts ) { 
					//console.log('ajaxForm::beforeSubmit()');
					//http://bassistance.de/jquery-plugins/jquery-plugin-validation/
					//valiation occurs here. validate() returns true on success. false prevents submission
					var validation_options = {
						rules:{
							name: {
								required: true,
								minlength: 2,
							},
							email: {
								required: true,
								email: true
							},
							subject: {
								required: true,
								minlength: 2,
							},
							message: {
								required: true,
								minlength: 2,
							},
							optin: false,
						},
						messages:{
							name: "(Required)",
							email: "(Valid Email)",
							subject: "(Required)",
							message: "(Required)",
						},
					};
					$success = $("#ContactsIndexForm").validate(validation_options).form();
					if($success){
						
					}
				},
		        success: function() {
					if($success){
						$('.contacts table').toggle('fast');
						$('.contacts .success').toggle('fast');
					}
				},
		        url: '/contacts',
		        type: 'post'
			});
			//console.log(ContactsIndexForm);
		}, 500);
		
	}
	//////
	
	return true; //return this;
};

/**
* add divs required for scrollbar plugin
*/
function scrollPrep(data){	
	data = $(data).html(); //used to get html from DIV Object returned during search results //wrap an extra div around incoming content, will get removed by JQuery.html()
	
	return '<div id="overlay_scroll"><div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div><div class="viewport"><div class="overview">' + data + '</div></div></div>';
}

// default options
$.fn.overlay.defaults = {
	opacity:0, //background that covers rest of site
	minHeight:525,
	maxHeight:525,
	minWidth:600,
	maxWidth:600,
	modal:false, //false=allow background to be clickable
	position:[150], //top:150
	scroll:1,
};

})(jQuery);


