// default

//
var browser		= $(window);
var dataSent 	= 0;

//
$.postJSON = function (url, data, callback) {
	$.post(url, data, callback, 'json');
};

$(function(){
	
	//
	$.ajaxSetup({ 
			scriptCharset: "utf-8"
	});
	
	$(document).pngReplace();
	
	$.upgrade();
	
	// login/member tab
	$('#right>#header>#login:not(.open)>a').click(function(){
		$(this).parent('div').animate({'height': '120px'}, 1000, function(){ $('#login_form').fadeIn(); });	
		return false;
	});
	
	$('.open_me').animate({'height': '120px'}, 1000, function(){ $('#login_form').fadeIn(); });
	
	// contact
	$('#userForm').ajaxForm({
        beforeSubmit:  	function(formData, form, options){
							$('#error').slideUp();
							return true;
						}, 
        success:       	function(data, status, xhr, form){
							if(data.success == true){
								$('#userForm').slideUp('slow', function(){ $('#thankYou').slideDown(); });
							} else {
								$('#error').slideDown();
							}
						},
		url:			'/ajax.php',
		type:			'post',
		dataType:		'json'
	});
	
	$('#userForm').bind('form-pre-serialize', function(e) {
    	//tinyMCE.triggerSave();
	});
	
	// survey 
	$('.pencil').each(function(){
		$(this).click(function(){
			$(this).parent('div').parent('div').find('textarea').slideDown();	
			$(this).fadeOut();
			return false;
		});					   
	});
	
	$('textarea').focus(function(){ if($(this).val() == 'Description'){$(this).val('');}});
	$('.row textarea:not(.required)').hide();
	
	$('[id^=points_],[id^=description_]').each(function(){
	
		$(this).change(function(){
			
			dataSent++;
			dataCheck();
			
			$.postJSON('/ajax.php',{'action':'post', 'id':$(this).attr('id'), 'value':$(this).val()},function(data){
				if(data.success == true){
					//
				} else {
					//
				}
				dataSent--;
				dataCheck();
			});
			
		});
		
	});
	
	$('[id^=upload_]').each(function(){
		$(this).ajaxForm();				   
	});
	
	$('[id^=file_]').each(function(){
	
		$(this).change(function(){
			
			dataSent++;
			dataCheck();
								
			$(this).parent('form').ajaxSubmit({
				url: '/ajax.php',
				type: 'get',
				dataType:  'json',
				resetForm: true,
				success: function(data, status){
					if(data.success == true){
						$('#attached_'+data.id).html('Attached File: <a href="/cache/uploads/' + data.file_name + '">' + data.file_name + '</a>').fadeIn();
					} else {
							
					}
					dataSent--;
					dataCheck();
				}
			});					
		});
		
	});
	
	$('html').mousemove(function(e){
		if(dataSent > 0){
			if(e.pageX < browser.width() - $('#sendingData').width() - 40){
				$('#sendingData').css({'left': e.pageX + 20, 'top':e.pageY});
			} else {
				$('#sendingData').css({'left': e.pageX - 40 - $('#sendingData').width(), 'top':e.pageY});
			}
		}
	});
	
	// calendar
	$.fn.qtip.styles.cal = { 
	   width: 280,
	   textAlign: 'left',
	   border: {
		  width: 1,
		  radius: 2,
		  color: '#cccccc'
	   },
	   tip: 'bottomLeft',
	   name: 'light'
	}
	
	$('.event>a').each(function(){
		$(this).qtip({
		  content: $(this).attr('meta'),
		  position: {corner: {target: 'rightTop',tooltip: 'bottomLeft'}},
		  style: 'cal'
		});
	});
	
	// gallery
	$('a.group').fancybox(); 

});

function dataCheck(){
	if(dataSent > 0){
		$('#sendingData').wait(1000).fadeIn();
	} else {
		$('#sendingData').stop().css({'left':0, 'top':0}).hide();
	}
}

window.onbeforeunload = function(e){
	var e = e || window.event;

	if(dataSent > 0){
		// For IE and Firefox
		if (e) {
			e.returnValue = 'The form is currently processing, leaving the page will result with loss of data.';
		}
		
		// For Safari
		return 'The form is currently processing, leaving the page will result with loss of data.';
	}
};

