$(document).ready(function() {

	var first_run = true;
	$('div#search-box-category select#search-box-category-select').change(function() {
	
		switch ($('select#search-box-category-select').val()) {
			case '1':
			case '2':
				$('div#search-box-missions').hide();
				$('div#search-box-events').show();
				if ($('select#search-box-category-select').val() != $('input#value-type').val()) {
					var _url = '?module=Events&typeID='+$('select#search-box-category-select').val()+'&view='+$('input#value-view').val()+'&filter-tags='+$('input#value-tags').val();
						_url+= '&y='+$('input#value-year').val()+'&m='+$('input#value-month').val();
						_url+= '&speakerID='+$('select#search-box-events-select-speaker').val();
					
					if (!first_run) {
						window.location = _url;
					}
				}
			break;
			case '50':
				$('div#search-box-events').hide();
				$('div#search-box-missions').show();
				if ($('select#search-box-category-select').val() != $('input#value-type').val() && first_run != true) {
					var _url = '?module=Events&typeID='+$('select#search-box-category-select').val()+'&view='+$('input#value-view').val()+'&filter-tags='+$('input#value-tags').val();
						_url+= '&y='+$('input#value-year').val()+'&m='+$('input#value-month').val();
						_url+= '&countryID='+$('select#search-box-missions-select-country').val();
						_url+= '&coordinatorID='+$('select#search-box-missions-select-coordinator').val();
					if (!first_run) {
					window.location = _url;
					}
				}
				
			break;
			default:
				$('div#search-box-events').hide();
				$('div#search-box-missions').hide();
				
				if ($('select#search-box-category-select').val() != $('input#value-type').val() && first_run != true) {
					var _url = '?module=Events&typeID='+$('select#search-box-category-select').val()+'&view='+$('input#value-view').val()+'&filter-tags='+$('input#value-tags').val();
						_url+= '&y='+$('input#value-year').val()+'&m='+$('input#value-month').val();
					if (!first_run) {
					window.location = _url;
					}
				}
			break;
		}
		first_run = false;
	}).trigger('change');
	
	$('select#search-box-events-select-speaker').change(function(){
		//if ($(this).val() != 0) {
			var _url = '?module=Events&typeID='+$('select#search-box-category-select').val()+'&view='+$('input#value-view').val()+'&filter-tags='+$('input#value-tags').val();
				_url+= '&y='+$('input#value-year').val()+'&m='+$('input#value-month').val();
				_url+= '&speakerID='+$('select#search-box-events-select-speaker').val();
			window.location = _url;
		//}
	});
	
	$('select#search-box-missions-select-country').change(function(){
		
			var _url = '?module=Events&typeID='+$('select#search-box-category-select').val()+'&view='+$('input#value-view').val()+'&filter-tags='+$('input#value-tags').val();
				_url+= '&y='+$('input#value-year').val()+'&m='+$('input#value-month').val();
				_url+= '&countryID='+$('select#search-box-missions-select-country').val();
				_url+= '&coordinatorID='+$('select#search-box-missions-select-coordinator').val();
			window.location = _url;
		
	});
	
	$('select#search-box-missions-select-coordinator').change(function(){
		//if ($(this).val() != 0) {
			var _url = '?module=Events&typeID='+$('select#search-box-category-select').val()+'&view='+$('input#value-view').val()+'&filter-tags='+$('input#value-tags').val();
				_url+= '&y='+$('input#value-year').val()+'&m='+$('input#value-month').val();
				_url+= '&countryID='+$('select#search-box-missions-select-country').val();
				_url+= '&coordinatorID='+$('select#search-box-missions-select-coordinator').val();
			window.location = _url;
		//}
	});
	
    $('.filter-tags').change(
        function() {
            window.location = '?module=Events&filter-tags='+$('.filter-tags option:selected').text()+'&y='+$('.y2').val()+'&m='+$('.m2').val();
        }
    );
    
    $('.event-popup').click(function(i) {
    	
        $('#mod_window').html(''); 
        $('#mod_window').hide();
        
        if (this.id != undefined && this.id != '') {
        	var _extra = '';
        	if (this.name == 'mission') {
        		_extra = '&missionID='+this.id;
        	}
            $.get("?module=Events&event=View&eventID="+this.id+_extra,
                function(data){
                	$('#mod_window').html(data);
                	$('#mod_window').css('position', 'absolute');
                	$('#mod_window').css('z-index', 999);
                	$('#mod_window').css('left', 60);
                	$('#mod_window').css('top', 120);
                    $('#mod_window').fadeIn('slow');
                    doAjaxRebind();
                }
            );
        } else {
            window.location = i.target;
        }
        return false;
    });
    
    doAjaxRebind();

});


function doAjaxRebind()
{
	$('a.email_to').click(function() {
		$(this).hide();
		
		var eventID = $(this).children('img').attr('id');
		
		$('div#email_open_'+eventID).css('display', 'block');
		
		$('div#email_open_'+eventID+' input#sendEmailTo').click(function() {
			var _mission = $('div#email_open_'+eventID+' input[name="ismission"]').val();
			var _email = $('div#email_open_'+eventID+' input[name="emailto"]').val();
			var _msg = $('div#email_open_'+eventID+' textarea[name="emailmsg"]').val();
			if (isValidEmail(_email)) {
					$.post("?module=Events&event=SendEvent&eventID="+eventID, {email:_email,msg:_msg,mission:_mission}, $.SendEmailDataReturn);
					
			} else {
				alert("Please enter a valid email address");
			}
			return false;
		});
		
		return false;
	});
}


function isValidEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}


$.SendEmailDataReturn = function (data)
{   
	if (data.length > 0) {
	
		var _d = data.split('|');
		if (_d[0] == '200') {
			alert(_d[1]);
		} else {
			alert("Error: "+_d[1]);
		}
		
	} else {
	   alert('Error, please try again.');
	}
}
