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

function openDialogBox(id) {
  $(id).slideDown('fast').fadeTo('fast', '0.95');
}
function closeDialogBox(id) {
  $(id).slideUp('fast');
}
function setFrenchDatePicker(selector) {
    $(selector).datepicker({ dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'] });
    $(selector).datepicker('option', 'dayNamesMin', ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa']);
    $(selector).datepicker('option', 'dayNamesShort', ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam']);
    $(selector).datepicker('option', 'monthNames', ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Decembre']);
    $(selector).datepicker('option', 'monthNamesShort', ['Jan','Fev','Mar','Avr','Mai','Juin','Juil','Août','Sept','Oct','Nov','Déc']);
    $(selector).datepicker('option', 'dateFormat', 'mm/dd/yy');
    
    
}

function showResaCalendar(selector, dates) {
  var dates_array = dates.split(";");
  $(selector).datepicker({ 
      beforeShowDay: function(date) {
        //if dates_array.index() !== null;
        d = new Date();        
        day = getDate(date);
        month = getMonth(date);
        if(dates_array.indexOf(date.getFullYear().toString() + "-" + month + "-" + day) < 0)
          return [false > d, 'calendar_avalaible','Disponible']; 
        else
          return [false > d, 'calendar_not_avalaible','Non Disponible']; 
        end
      }, 
      numberOfMonths: [2, 2]
    }
  );
}

function editResaCalendar(selector, start, id, post_id, dates) {
  //$(selector).datepicker( 'disable' );
  var dates_array = dates.split(";");
  $(selector).datepicker({ 
      beforeShowDay: function(date) {
        //if dates_array.index() !== null;
        d = new Date();     
        day = getDate(date);
        month = getMonth(date);
        if(dates_array.indexOf(date.getFullYear().toString() + "-" + month + "-" + day) < 0)          
          return [date > d, 'calendar_avalaible','Disponible']; 
        else
          return [date > d, 'calendar_not_avalaible','Non Disponible']; 
        end
      }, 
      defaultDate: start,
      onSelect: function(dateText) { 
        $.post("/admin/resas/swap", { id: id, date: dateText, post_id: post_id }, null, "script" );
      },
      onChangeMonthYear: function(year, month, inst) {
        $('#datefield').val("" + year + "/" + month + "/01");
      }
      
    }
  );
  
}
function getDate(date){
	d = (date.getDate()).toString();
	if(d.length < 2){d = '0'+d}
	return d
}
function getMonth(date){
	m = (date.getMonth()+1).toString();
	if(m.length < 2){m = '0'+m}
	return m
}
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
	  for(var i=0; i<this.length; i++){
      if(this[i]==obj){
        return i;
      }
    }
    return -1;
  }
}
function toggleSubMenu(id) {
  if($(id).css("display") == "none") {
    $(id).fadeIn();
  } else {
    $(id).fadeOut();
  }
}
function equalHeight(group) {
  tallest = 0;
  group.each(function() {
    thisHeight = $(this).height();
    if(thisHeight > tallest) {
      tallest = thisHeight;
    }
  });
  group.height(tallest);
}

function bp_serialize(id, ta_id, h_id) {
 	var output = $(id).layoutEditor('serialize');
	$(h_id).val(JSON.stringify(output.json));
	$(ta_id).val(output.html.html());
	alert($(h_id).val());
}
function bp_update() {
  var output = $("#layoutEditor").layoutEditor('serialize');
  $("#post_json").val(JSON.stringify(output.json));
	$("#post_text").val(output.html.html());  
}
/**
 * Implements JSON stringify and parse functions
 * v1.0
 *
 * By Craig Buckler, Optimalworks.net
 *
 * As featured on SitePoint.com
 * Please use as you wish at your own risk.
*
 * Usage:
 *
 * // serialize a JavaScript object to a JSON string
 * var str = JSON.stringify(object);
 *
 * // de-serialize a JSON string to a JavaScript object
 * var obj = JSON.parse(str);
 */

var JSON = JSON || {};

// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
	var t = typeof (obj);
	if (t != "object" || obj === null) {
		// simple data type
		if (t == "string") obj = '"'+obj+'"';
		return String(obj);
	}
	else {
		// recurse array or object
		var n, v, json = [], arr = (obj && obj.constructor == Array);
		for (n in obj) {
			v = obj[n]; t = typeof(v);
			if (t == "string") v = '"'+v+'"';
			else if (t == "object" && v !== null) v = JSON.stringify(v);
			json.push((arr ? "" : '"' + n + '":') + String(v));
		}
		return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
	}
};


// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
	if (str === "") str = '""';
	eval("var p=" + str + ";");
	return p;
};

function change_xenon_image() {
  $('#xenon').attr("src", "/images/ca/xenon/" + $('#strip').val() + "_" + $("#xenon_color").val() + ".png");
}
