// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function toggle_visible(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

function activity_show(show) {
  document.getElementById('activity_plan').innerHTML = show
}

// Default jQuery AJAX Setup:
//  - 1.4.2: Default request header is set to */* if dataType isn't script, so default to script type
// TODO Figure out if this is what we want, are we missing functionality and/or should we
//  be solving this rails side (and if so how?)
jQuery.ajaxSetup({
  dataType: 'script'
});

// All non-GET requests will add the authenticity token if not already present in the data packet
// TODO Consider if we can/should move this to ajaxSetup and beforeSend?
//  Then we could let settings.data be an object, and encode it appropriately
jQuery(document).ajaxSend(function(event,request,settings){
    // Fix issue with DELETE requests?
//    request.setRequestHeader("Accept", "text/javascript");
//    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    // For JSON also set the Accept header so that we get it back too
    if (settings.dataType && (settings.dataType.toUpperCase() == 'JSON'))
        request.setRequestHeader('Accept', 'application/json');
    // Check if auth-token isn't set, or this is a GET request, and that one isn't already set
    if (typeof(window._auth_token) == 'undefined' || settings.type.toUpperCase() == "GET") return;
    if (settings.data && settings.data.match(new RegExp("\\b" + window._auth_token_name + "="))) return;

    // Determine if any data is present
    var hasData = !!settings.data;
    
    // If there is no data set the content type as it isn't set by default
    if (!hasData) request.setRequestHeader('Content-Type', settings.contentType);

    //see WIKI for more details about this code: http://madrid/index.php/Javascript_gotchas
    switch(settings.contentType){
      case 'application/json':
        // TODO Use a different libarary aside from JSJaC, one that we'll always load
        dataObj = JSJaCJSON.parse(settings.data);
        dataObj.authenticity_token = encodeURIComponent(window._auth_token);
        settings.data = JSJaCJSON.toString(dataObj);
        break;
      case 'application/x-www-form-urlencoded':
        settings.data += "&authenticity_token=" + encodeURIComponent(window._auth_token);
        break;
      default: 
        console.error('Unknown contentType for this request.')
        settings.data = "authenticity_token=" + encodeURIComponent(window._auth_token);
        break;
    }
    
});

function flashMessage(selector, msg){ 
  jQuery(selector).html(msg).effect('highlight', { color:'#FCF8C4'}, 4000);
}

//flash notice

// function notify(flash_message) {
// 	var flash_div = $("#flash_message");
// 	flash_div.html(flash_message);
// 	flash_div.fadeIn(400);
// 	
// 	setTimeout(function(){
// 		flash_div.fadeOut(500,
// 	function(){
// 		flash_div.html("");
// 		flash_div.hide()})},1400);
// }
// 
// $(function(){
// 
// 	$("#flash_message").hide();
// 	var flash_message = $("#flash_message").html().trim();
// 	
// 	if(flash_message != "")
// 	{
// 	notify(flash_message);
// 	}
// 
// });

// $(document).ready(function() {
// 	function flash(flash_messages) {
// 		var flash_div = $("#flash_messages");
// 		flash_div.html(flash_messages);
// 		flash_dev.fadeIn(400);
// 		
// 		setTimeout(function(){
// 			flash.div.fadeOut(500, function(){
// 				flash_div.html("");
// 				flash_div.hide()})},1400);
// 			})
// 		})
// 	}
// 	
// 	$(function(){
// 		$("#flash_messages").hide();
// 		var message = $("#flash_messages").html().trim();
// 		
// 		if(message != "")
// 		{
// 			flash(message)
// 		}
// 	})
// 	
// 	// $("#flash_messages").hide("slow");
// 	// $("#flash_messages").hide(3000);
// 	// $("#flash_messages").show("fast");
// 	// $("#flash_messages").show("2000");
// });
