jQuery(document).ready(function () {
    $('#topbar').dropdown();

    var $tabs_inscript = $("#tabs_inscript");
    $tabs_inscript.tabs();
    $tabs_inscript.children("li").click(function () {});

    var $zone_gmap = $("#zone_gmap");
    if(0 < $zone_gmap.length) {
	$zone_gmap.gmap3({
	    action: 'addMarker',
	    address: "place de l'étoile, Paris",
	    map:{
		center: true,
		zoom: 14
	    }
	});
    }
    /*$("#map_accueil").gMap({
	 zoom : 14,
	 latitude : 48.856578,
	 longitude : 2.351828
	 });*/

    $('.close').click(function () {
	$(this).parent("div").remove();
    });

    var hot_edit_nombre = function () {
	var focus = $(this),
	valeur = focus.text();
	$(this).unbind('click');
	focus.text("").append('<input type="" name="" value="' + valeur + '" style="width: 60px;" id="hot_editing" />');
	$("#hot_editing").focus().blur(function () {
	    var result = valeur,
	    $this = $(this),
	    nValeur = $(this).val(),
	    idLien = $(this).parents("table")[0].id.substring(3),
	    majReq = $.ajax({
		url: "lnkBtlPers/majNbr",
		dataType: "json",
		data: {
		    "cible": idLien,
		    "valeur": nValeur
		}
	    });
	    majReq.done(function (data, status) {
		if (data.success === "false") {
		    alert('Nous rencontrons un problème. Veuillez réessayer plus tard s\'il vous plaît. Si le problème persiste, n\'hésitez pas à nous contacter.');
		}
		else {
		    result = nValeur;
		}
	    });
	    majReq.fail(function () {
		alert('Nous rencontrons un problème. Veuillez réessayer plus tard s\'il vous plaît. Si le problème persiste, n\'hésitez pas à nous contacter.');
	    });
	    majReq.always(function () {
		focus.text(result).click(hot_edit_nombre);
		$this.remove();
	    });
	});
    };
    $('.input_nombre').click(hot_edit_nombre).twipsy({
	"placement":"right"
    });


    $(".couverture").click(function (ele) {
	$(ele.target).mcDiaporama();
    });
/*
	 * Admin
	 */
/* var lieu = 'bouteilles', action = 'list';
    $("ul.menu-dropdown").click(function (event) {
	event.preventDefault();
	var $target = $(event.target);
	lieu = event.target.hash.substring(1);
	action = 'adminlist';
	var onglets = $.ajax({
	    url: window.location.origin + '/' + lieu + '/' + action
	});
	onglets.done(function (data) {
	    $("#realContent").html(data);
	    history.pushState({
		object:"admin_" + lieu + "_" + action
	    }, "admin " + lieu, window.location.origin + "/admin/" + lieu + "/" + action);
	});
	lieu = event.target.hash.substring(1);
	$tabs_admin.find(".active").removeClass("active");
	$tabs_admin.find("li").addClass("active");
    });
    var $tabs_admin = $("#tabs_admin");
    $tabs_admin.tabs();
    $tabs_admin.children("li").click(function (event) {
	event.preventDefault();
	//var $this = $(this);
	var $target = $(event.target);
	$tabs_admin.find(".active").removeClass("active");
	$target.parents("li").addClass("active");
	action = event.target.hash.substring(1);
	var onglets = $.ajax({
	    url: window.location.origin + '/' + lieu + '/' + action
	});
	onglets.done(function (data) {
	    $("#realContent").html(data);
	    history.pushState({
		object:"admin_" + lieu + "_" + action
	    }, "admin " + lieu, window.location.origin + "/admin/" + lieu + "/" + action);
	});
    });*/
});

(function ($) {
    var methods = {
	init : function (options) {
	    return this.each(function (ele) {
		var $this = $(this),
		$diaporama = $(this).parent().next(),
		$background = $diaporama.find(".background");
		$panel = $diaporama.find(".panel");
		$background.click(function () {
		    $diaporama.hide();
		    $panel.find(".photo").remove();
		});
		$panel.click(function (event) {
		    event.preventDefault();
		    //alert(event.target.src);
		    //$background.html('<img src="' + event.target.src + '" />');
		    $panel.find(".photo").remove().css("top", "50%");
		    $panel.prepend('<div class="photo"><img src="' + event.target.src + '" /></div>').css("top", "60%");
		    $img = $panel.find(".photo").find("img");
		    $img.css("margin-top", "-" + ($img.css("width").substring(0, $img.css("width").length - 2) / 2) + "px")
			.css("margin-left", "-" + ($img.css("height").substring(0, $img.css("height").length - 2) / 2) + "px");
		});
		$diaporama.show();
	    });
	},
	destroy : function () {
	    return this.each(function () {});
	}
    };

    $.fn.mcDiaporama = function (method) {
	if (methods[method]) {
	    return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
	} else if (('object' === typeof method) || (!method)) {
	    return methods.init.apply(this, arguments);
	} else {
	    $.error('Method ' +  method + ' does not exist on mc.diaporama');
	}
    };
})(jQuery);
