var flobu ;
var flopanel;
$(document).ready(function() {
		// Agir dès que le DOM est prêt
//$('.rounded').corners();
//$('.rounded').corners(); /* test for double rounding */
$('table', $('#featureTabsContainer .tab')[0]).each(function(){$('.native').hide();});
$('#featureTabsContainer').show();
//tab(0);
initialiser();
initPlanteTitles();
initPlantePanel();
init_panel ();
init_attente ();
//$('img.btn').leviTip({sourceType: 'attribute', source: 'alt'});

});

function init_attente () {
			$(function(){
			flobu = new flower_bubble ({
				base_obj: $( '#content' ),
				base_dir:  baseurl+'/public/images/',
				background: { css: 'white', opacity: 0.78 },
				flower: { image: 'load.gif', width: 32, height: 32 },
				block_mode: 'base_obj'
			}) ;
		}) ;
		
		$(function(){
			flopanel = new flower_bubble ({
				base_obj: $( '#content' ),
				base_dir:  baseurl+'/public/images/',
				background: { css: 'white', opacity: 0.78 },
				flower: { image: 'load.gif', width: 32, height: 32 },
				block_mode: 'base_obj'
			}) ;
		}) ;
}


function initialiser () {
		// initialisation de la boite de message
	$("#messageDialog").dialog({
		bgiframe: true,
		autoOpen:false,
		height: 300,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			"OK" : function() {
				$("#messageDialog").dialog('close');
			}
		}
		});
		
		$("#dlgConfirmer").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 400,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			}
		});
	





		// mise en page des tableaux
		$("table.zebra thead > tr:first-child()").addClass("zSelected");
		$("table.zebra tbody > tr:nth-child(odd)").addClass("zOdd");
		$("table.zebra tbody > tr:nth-child(even)").addClass("zEven");


		//coins arrondis




}


function init_panel () {
	$('table.zebra thead > tr:first-child()').addClass('zSelected');
	$('table.zebra tbody tr:not([th]):odd').addClass('zOdd');
	$('table.zebra tbody tr:not([th]):even').addClass('zEven');
	
	
	
	$(".panellist").css("display","block");
}
function initPlantePanelOld () {
	var toggleMinus = baseurl+'/public/images/toggleminus.png';
	var togglePlus = baseurl+'/public/images/toggleplus.png';
	var $subHead = $('table.zebra tbody th:first-child');
	$subHead.prepend('<img src="' + togglePlus + '"title="Expand this section" />');
	$subHead.parents('tr').siblings().fadeOut('fast');
	$('img', $subHead).addClass('clickable').click(function() {
		var toggleSrc = $(this).attr('src');
		if ( toggleSrc == toggleMinus ) {
			$(this).attr('src', togglePlus).parents('tr').siblings().fadeOut('fast');
		} else {
			$(this).attr('src', toggleMinus).parents('tr').siblings().fadeIn('fast');
		}
	});
}

function initPlantePanel () {
	var toggleMinus = baseurl+'/public/images/toggleminus.png';
	var togglePlus = baseurl+'/public/images/toggleplus.png';
	var $subHead = $('li.page_item div ul li.famille');
	$subHead.prepend('<img src="' + togglePlus + '"alt="Expand this section" />');
	//$subHead.parents('ul').siblings().fadeOut('fast');
	$subHead.children('ul').fadeOut('fast');
	$('img:first', $subHead).addClass('clickable').click(function() {
		var toggleSrc = $(this).attr('src');
		if ( toggleSrc == toggleMinus ) {
			//$(this).attr('src', togglePlus).parents('li').siblings('ul').fadeOut('fast');
			$(this).attr('src', togglePlus);
			$(this).next().fadeOut('fast');
		} else {
			//$(this).attr('src', toggleMinus).parents('li').siblings('ul').fadeIn('fast');
			$(this).attr('src', toggleMinus)
			$(this).next().fadeIn('fast');
		}
	});
}

function initPlanteTitles() {
	var toggleMinus = baseurl+'/public/images/toggleminus.png';
	var togglePlus = baseurl+'/public/images/toggleplus.png';
	var $subHead = $('div.widget-centre h3');
	$subHead.prepend('<img class="tgl" src="' + toggleMinus + '"alt="Cacher cette section" />');
	//$subHead.parents('ul').siblings().fadeOut('fast');
	//$subHead.children('ul').fadeOut('fast');
	$('img.tgl', $subHead).addClass('clickable').click(function() {
		var toggleSrc = $(this).attr('src');
		if ( toggleSrc == toggleMinus ) {
			$(this).attr('src', togglePlus).parents('h3').siblings('ul').fadeOut('fast');
			//$(this).attr('src', togglePlus);
			//$(this).next().fadeOut('fast');
		} else {
			$(this).attr('src', toggleMinus).parents('h3').siblings('ul').fadeIn('fast');
			//$(this).attr('src', toggleMinus)
			//$(this).next().fadeIn('fast');
		}
	});
}


function messageDialog(message) {
	// appel du mlessage avec le bon texte
	$("#dlgMessage").html(message);
	$("#messageDialog").dialog("open");
}

function getContenu(adresse) {
	getContenuParam(adresse,{});
}

function getContenuParam(adresse, params) {
	flobu.enable();
	updateMessage ("");
        $.get(adresse,params,  
     function success(data){  
				$("#mainContent").html(data);
				initialiser();
				flobu.disable();
     }); // on passe en paramètre optionnel le type de retour ici JSON  
     // j'enregistre pour ma navigation
  }
  
  
  // raffraichissement d'un panel
  function updatePanel(panel,url) {
        $.get(url,{},  
     function success(data){  
				$(panel).html(data);
				init_panel();
     }); // on passe en paramètre optionnel le type de retour ici JSON  
     
  }
  
  function updateMessage (data) {
	$("#ajxmessage").html(data);
}
  
  
  
function active_tools (tools) {
	$(".btn").css("display","none");
	$(tools).css("display","inline");
}



function creerPlantation(id) {
		flopanel.enable();
        $.post(baseurl+"/plantation/ajoutauto",{ref_plante:id},  
     function success(data){
     	updatePanel ("#plantationpanel",baseurl+"/plantation/panel");
     	updateMessage(data);
     	flopanel.disable();
     }); // on passe en paramètre optionnel le type de retour ici JSON  
  }


function ajouterPanier(id) {
	flopanel.enable();
        $.post(baseurl+"/panier/ajouter",{ref_plante:id},  
     function success(data){
     	updateMessage(data);
     	updatePanel ("#panierpanel",baseurl+"/panier/panel");
     	flopanel.disable();
     }); // on passe en paramètre optionnel le type de retour ici JSON  
  }
  


function supprimerPanier(id) {
	plantenom=$("#pan_"+id).html();
	 if (confirm("Voulez-vous retirer "+plantenom+" du panier ?")) {
	 	flopanel.enable();
        $.post(baseurl+"/panier/supprimer/",{id:id,del:"oui"},  
     function success(data){
     	//messageDialog (data.message);
     	updateMessage(data);
     	updatePanel ("#panierpanel",baseurl+"/panier/panel");
     	flopanel.disable();
     }); // on passe en paramètre optionnel le type de retour ici JSON  
    }
  }
  
  
  function viderPanier() {

	 if (confirm("Voulez-vous vide totalement le panier ?")) {
	 	flopanel.enable();
        $.post(baseurl+"/panier/vider/",{del:"oui"},  
     function success(data){
     	//messageDialog (data.message);
     	updateMessage(data);
     	updatePanel ("#panierpanel",baseurl+"/panier/panel");
     	flopanel.disable();
     }); // on passe en paramètre optionnel le type de retour ici JSON  
    }
  }
  
  
  function ajouterCommentaire (controller,refext) {
  		titre = "Nouveau commentaire";
  		message="Entrez votre commentaire ci-dessous";
  		$("#dlgConfirmMessage").html(message);
		$("#dlgConfirmer").attr( "title", titre);
		$("#dlgConfirmer").dialog('option', 'buttons',
			 {
				"Confirmer" : function() {
					$(this).dialog('close');
					commentaire = $("#dlgInput").val();
					$.post(baseurl+"/"+controller+"/commentaire/",{ref_ext:refext,commentaire:commentaire},  
					function success(data){
						updateMessage(data);
						whereAmI = $("#whereami").html();
						getContenu(whereAmI);
					});   
				},
				"Annuler": function() {
					$(this).dialog('close');
				}
			}
		);
		$("#dlgConfirmer").dialog("open");
	}
  	


  function ajouterCommentaireMulti (controller,refext) {
  		var listecarres="";
		$(".selecteditem").each(function(i) {
			listecarres =  listecarres +$(this).attr("id") +"\n";         
		});  
		
  		
  		
  		titre = "Nouveau commentaire";
  		message="Entrez votre commentaire ci-dessous";
  		$("#dlgConfirmMessage").html(message);
		$("#dlgConfirmer").attr( "title", titre);
		$("#dlgConfirmer").dialog('option', 'buttons',
			 {
				"Confirmer" : function() {
					$(this).dialog('close');
					commentaire = $("#dlgInput").val();
					$.post(baseurl+"/"+controller+"/commentaireMulti/",{ref_ext:refext,commentaire:commentaire,liste:listecarres},  
					function success(data){
						updateMessage(data);
					});   
				},
				"Annuler": function() {
					$(this).dialog('close');
				}
			}
		);
		$("#dlgConfirmer").dialog("open");
	}




function getLune() {
	datelune = $("#datepotager").val();
    adresse = baseurl+"/lune/index";
     getContenuParam(adresse,{datelune:datelune});
  }













/**
 * jquery flower bubble
 *
 * blocks and tints the screen, draws a bubble and a flower inside of it
 *
 * @author Oktay Acikalin ( ok@ryotic.de )
 * @version 0.4
 * @license MIT / GPL v2
 * @copyright Oktay Acikalin, 7 October, 2008
 * @package jquery_flower_bubble
 * 
 * example call:
 * 
 * var flobu ;
 * 
 * $(function(){
 * 	flobu = new flower_bubble ({
 * 		base_obj: $( 'div#some_obj' ),
 * 		base_dir: _appldir + '/images',
 * 		background: { css: 'white', opacity: 0.78 },
 * 		bubble: { image: 'bubble.png', width: 130, height: 98 },
 * 		flower: { image: 'flower.gif', width: 32, height: 32 }
 * 	}) ;
 * }) ;
 * 
 * flobu . enable () ;
 * flobu . disable () ;
 * 
 **/
var flower_bubble = function ( options ) {
	
	// begin: define defaults
	this . defaults = {
		base_obj: null, // which (jquery-)object to block
		
		container_id: 'flower_bubble_container',
    
		fade_speed: 'normal',
		zindex: 1000,
		
		// full = start at base_obj.top and cover whole screen
		// base_obj = only block this specific object
		block_mode: 'full',
		// block_mode: 'base_obj',
    
		base_dir: 'images',
		background: { css: 'white', opacity: 0.5 },
		bubble: { image: 'bubble.png', width: 'auto', height: 'auto' },
		flower: { image: 'flower.gif', width: 'auto', height: 'auto' }
	} ;
	// end: define defaults
	
	// begin: add some timestamp to the id to make it quite unique
	var t = new Date () ;
	this . defaults . container_id += '_' + t . getTime () ;
	// end: add some timestamp to the id to make it quite unique
	
	// begin: define container vars
	this . base_obj ;
	this . container_id ;
	this . fade_speed ;
	this . zindex ;
	this . block_mode ;
	this . base_dir ;
	this . background ;
	this . bubble ;
	this . flower ;
	// end: define container vars
	
	// begin: setup container vars
	jQuery . extend ( this . defaults, options ) ;
	jQuery . extend ( this, this . defaults ) ;
	// end: setup container vars
	
	// begin: preload images
	var bubble_img = $( '<img src="' + this . base_dir + '/' + this . bubble . image + '">' ) ;
	var flower_img = $( '<img src="' + this . base_dir + '/' + this . flower . image + '">' ) ;
	if ( this . bubble . width == 'auto' || this . bubble . width == undefined )
		this . bubble . width = bubble_img . get ( 0 ) . width ;
	if ( this . bubble . height == 'auto' || this . bubble . height == undefined )
		this . bubble . height = bubble_img . get ( 0 ) . height ;
	if ( this . flower . width == 'auto' || this . flower . width == undefined )
		this . flower . width = flower_img . get ( 0 ) . width ;
	if ( this . flower . height == 'auto' || this . flower . height == undefined )
		this . flower . height = flower_img . get ( 0 ) . height ;
	// end: preload images
	
	this . flowers = 0 ; // unblock will only occur if no flowers are left
	
	// block ui and show flower
	this . enable = function ()
	{
		this . flowers++ ;
		if ( $( 'body > div#' + this . container_id ) . length != 0 ) return ;
		
		var pos = this . base_obj . offset () ;
		
		var container = $( '<div></div>' ) ;
		container . attr ( 'id', this . container_id ) ;
		container . css ({
			position: 'absolute',
			'z-index': this . zindex,
			top: pos . top,
			left: 0,
			width: '100%',
			overflow: 'hidden',
			height: Math . max ( $( window ) . height (), $( 'body' ) . height () ) - pos . top
		}) ;
		if ( this . block_mode == 'base_obj' )
		{
			container . css ({
				left: pos . left,
				width: this . base_obj . width (),
				height: this . base_obj . height ()
			}) ;
		}
		container . hide () ;
		
		var background = $( '<div></div>' ) ;
		background . css ({
			position: 'absolute',
			width: '100%',
			height: '100%',
			background: this . background . css,
			opacity: this . background . opacity
		}) ;
		container . append ( background ) ;
		
		var bubble = $( '<div></div>' ) ;
		bubble . css ({
			position: 'relative',
			background: 'url("' + bubble_img . attr ( 'src' ) + '") no-repeat',
			width: this . bubble . width,
			height: this . bubble . height
		}) ;
		var flower = flower_img ;
		flower . css ({
			position: 'relative',
			left: this . bubble . width / 2 - this . flower . width / 2,
			top: this . bubble . height / 2 - this . flower . height / 2 - 2
		}) ;
		flower . mousedown ( function () { return false ; } ) ;
		bubble . append ( flower ) ;
		container . append ( bubble ) ;
		
		$( 'body' ) . append ( container ) ;
		
		bubble . css ( 'left', container . width () / 2 - this . bubble . width / 2 ) ;
		bubble . css ( 'top', container . height () / 2 - this . bubble . height / 2 ) ;
		
		if ( container . pngFix )
		  container . pngFix () ;
		
		container . mousedown ( function () { return false ; } ) ;
		container . fadeIn ( this . fade_speed ) ;
	}
	
	// unblock ui
	this . disable = function ()
	{
		if ( $( 'body > div#' + this . container_id ) . length == 0 ) return ;
		this . flowers = Math . max ( 0, this . flowers - 1 ) ;
		if ( this . flowers > 0 ) return ;
		
		$( 'body > div#' + this . container_id ) . fadeOut ( this . fade_speed, function () {
			$( this ) . remove () ;
		} ) ;
	}
	
}

