$().ready(function() {
	
	jQuery.fn.clear = function () {
		$(':input', this).each(function() {
			var type = this.type;
			var tag = this.tagName.toLowerCase(); // normalize case
			// it's ok to reset the value attr of text inputs,
			// password inputs, and textareas
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = "";
			// checkboxes and radios need to have their checked state cleared
			// but should *not* have their 'value' changed
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			// select elements need to have their 'selectedIndex' property set to -1
			// (this works for both single and multiple select elements)
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
	}
	jQuery.fn.center = function ( myTarget ) { 
		this.css('position','absolute'); 
		
		if ( myTarget == "window" ) {
			this.css('top', ($(window).scrollTop()+200)+'px');
			this.css('left', ( $(window).scrollLeft() + ( ( $(window).width() - this.width() ) / 2 ) ) + 'px');
		} else {
			if ( $(myTarget).exists() ) {
				this.css('top', ( $(myTarget).offset().top + ( ( $(myTarget).height() - this.height() ) / 2 ) ) + 'px');
				this.css('left', ( $(myTarget).offset().left + ( ( $(myTarget).width() - this.width() ) / 2 ) ) + 'px');
			} else {
				this.css('top', ($(window).scrollTop()+200)+'px');
				this.css('left', ( $(window).scrollLeft() + ( ( $(window).width() - this.width() ) / 2 ) ) + 'px');
			}
		}
		
		return this; 
	} 
	jQuery.fn.exists = function(){
		return jQuery(this).length>0;
	}
	$.extend({
		getUrlVars: function(){
			var vars = [], hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			
			for( var i = 0; i < hashes.length; i++ ) {
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
			return vars;
		},
		getUrlVar: function(name){
			return $.getUrlVars()[name];
		}
	});
	function closeAllLayers() {
		// clear form
		$('#widget_form').clear();
		
		// if popup is present
		if ( $('#widget_popup').exists() ) { $('#widget_popup').remove(); };
		// if bg is present
		if ( $('#widget_bg').exists() ) { $('#widget_bg').remove(); };
		// if loader is present
		if ( $('#widget_loader').exists() ) { $('#widget_loader').remove(); };
		// if loader is present
		if ( $('#widget_message').exists() ) { $('#widget_message').remove(); };
	}
	
	var btn_close = "<div id=\"frm_close\"><img src=\"/images/templates/widget-close.gif\" border=\"0\" /></div>";
	var btn_close_sub = "<div id=\"frm_close_sub\"><img src=\"/images/templates/widget-close.gif\" border=\"0\" /></div>";
	
	$('span').click(function() {
		var personalID = $('#'+this.id);
		
		if ( personalID.attr('form') != undefined ) {
			frm_open( personalID );
		};
	});
	
	// on click open layer
//	$('#frm_open').click(function() {
	function frm_open( clickID ) {
		
		closeAllLayers();
		
		$('body').prepend("<div id=\"widget_popup\"></div>");
		$('#widget_popup').hide(); // automatically hide to populate
		
		
//		var gatherData = "form=" + $('#frm_open').attr('form');
//		if ( ($('#frm_open').attr('event') != "undefined") ) { gatherData = gatherData + "&event=" + $('#frm_open').attr('event'); };
		var gatherData = "form=" + clickID.attr('form');
		if ( (clickID.attr('event') != undefined) ) { gatherData = gatherData + "&event=" + clickID.attr('event'); };
		if ( $.getUrlVar('site') != undefined ) { gatherData = gatherData + "&site=" + $.getUrlVar('site'); };
		
		// call form into layer
		var html = $.ajax({
			url: "/create_form.php",
			data: gatherData,
			dataType: "html",
			cache: false,
			async: false
		}).responseText;
		
		//console.log('here');
		
		// populate div via AJAX
		$('#widget_popup').html(html).center( 'window' );
		// create close button
		if ( !$('#frm_close').exists() ) { $('#widget_popup').prepend(btn_close); }
		// once populated show
		$('#widget_popup').show();
		
		
		// create a close event for that layer
		// placed here because until the "open" click 
		// the id didn't exist
		$('#frm_close').click(function() { closeAllLayers(); });
		
	};
//	});
	
	
	$.validator.setDefaults({
		submitHandler: function() {
			// show extra layers
			$('body').append("<div id=\"widget_bg\"></div>");
			$('body').append("<div id=\"widget_loader\"><div class=\"inner\"><img src=\"/images/templates/loading.gif\" alt=\"loading...\" /> processing...</div></div>");
			$('body').append("<div id=\"widget_message\"></div>");
			
			// hide extra layers
			$("#widget_bg").css('height', $(document).height() + "px");
			$("#widget_bg").css('width', $(document).width() + "px");
			$("#widget_loader").hide();
			$("#widget_message").hide();
			
			// position layer
			$("#widget_loader").center( '#widget_popup' );
			
			// show the alyers now
			$("#widget_loader").show();
			
			// successful submit return message
			$("#widget_form").ajaxSubmit({
				target: "#widget_message"
			});
			
			
			
			// listen
			$("#widget_form").ajaxStop(function() {
				// remove loader
				$("#widget_loader").remove();
				
				// create close button
				if ( !$('#frm_close_sub').exists() ) { $('#widget_message').prepend(btn_close_sub); }
				// position layer
				$("#widget_message").center( '#widget_popup' );
				// show the alyers now
				$("#widget_message").show();
				
				$('#frm_close_sub').click(function() { closeAllLayers(); });
			}).ajaxError(function(a, b, e) {
				throw e;
				closeAllLayers();
			});
			
		}
	});
	
});
