$(document).ready (function () {

	//    $("#corps").hide ();
	//    $("#logo").show ();

    service_vu = '';
    service_mouse_active = true;
    /*
    $("#logo").css ('left', $(window).width()/2-140);
    $("#logo").css ('top', $(window).height()/2-140);   
    $("#logo").click (function () {
	$("#logo").animate ( {
	    left: "55px",
	    top: "55px"
	}, "fast");
	$("#logo").animate ({
	    height: "120px",
	    width: "120px"
	}, "fast", function () {
	    $("#logo").attr ('src', 'elol.png');
	    $("#corps").show ();
	    $("#logo").css ('cursor', 'auto');
	    $("#logo").hide ();
	});
	$("#logo").unbind ('click');
    });
    */
    $(".service").css ('opacity', '0.5');
    $(".service").hover (on_mouse_over, on_mouse_out);
    $(".service").click (on_service_click);
  
    $("#menu a.interne").click (on_menu_click);
    $("#close").click (on_close_click);

    page = window.location.hash.substring (1);
    switch (page) {
    case 'si':
    case 'devweb':
    case 'devspec':
    case 'reprise':
    case 'admin':
    case 'formation':
	//	$("#logo").click ();
	$("#"+page).click ();
	break;
    case 'liens':
    case 'contact':
    case 'ml':
    case 'references':
	//	$("#logo").click ();
	$("#"+page).click ();
    }
});

function on_mouse_over () {
    if (service_mouse_active) {
	$(this).animate ({opacity: "1", width: "200px", height: "120px"});
    }
}

function on_mouse_out () {
    if (service_mouse_active) {
	$(this).animate ({opacity: "0.5", width: "200px", height: "120px"});
    }
}

function on_service_click () {
    clicke = this;
    service_mouse_active = false;
    service_vu = $(this).attr('id');
    $(".service").each (function () {
	$(this).unbind ('click');
    });

    $(".service").each (function () {
	if (clicke != this) {
	    $(this).hide ("blind", { direction: 'horizontal' }, "slow");
	} else {
	    $(this).animate ({ width: "200px", height: "120px" }, "slow", function () {
		$.ajax ({
		    type: "GET",
		    url: "/"+$(this).attr('id')+".php?web=2",
		    success: function (reponse) {
			$("#retour_services").show ();
			$("#retour_services").unbind ("click");
			$("#retour_services").click (function () {
			    $("#detail_service").slideUp (function () {
				$(".service").each (function () {
				    $(this).show ("blind", { direction: 'horizontal' }, "slow");
				});
			    });
			    $("#retour_services").hide ();
			    $(".service").click (on_service_click);
			    service_mouse_active = true;
			    $(".service").css('opacity', '0.5');
			    service_vu = '';
			    return false;
			});
			$("#detail_service").html (reponse);
			$("#detail_service").slideDown ();
		    }
		});
	    });
	}
    });

    return false;

    /*
	$(this).animate ({
	    width: $(window).width()+'px',
	    height: $(window).height()+'px'
	});
*/
}

function on_menu_click () {
    theid = $(this).attr('id');
    $.ajax ({
	type: "GET",
	url: "/"+$(this).attr('id')+".php?web=2",
	success: function (reponse) {
	    if ($("#popup:visible").length) {
		$("#popup").slideUp ("slow", function () {
		    $("#popup_contenu").html (reponse);
		    $("#popup").slideDown ("slow");
		    if (theid == 'contact') {
			$("#envoi").unbind ('click', on_envoi_click);
			$("#envoi").bind ('click', on_envoi_click);
		    } else if (theid == 'references') {
			$(".ui-accordion-content").hide ();
			$(".ui-accordion-content:first").show ();
			$(".ui-accordion-header").click (on_accordion_click);
		    }
		});
	    } else {
		$("#popup_contenu").html (reponse);
		$("#contenu").slideUp ("slow");
		$("#popup").slideDown ("slow");
//		$("#divclose a").attr('href', '#'+service_vu);
		if (theid == 'contact') {
		    $("#envoi").unbind ('click', on_envoi_click);
		    $("#envoi").bind ('click', on_envoi_click);
		} else if (theid == 'references') {
		    $(".ui-accordion-content").hide ();
		    $(".ui-accordion-content:first").show ();
		    $(".ui-accordion-header").click (on_accordion_click);		    
		}		
	    } 
	    switch (theid) {
	    case 'contact':
		break;
	    case 'references':
		break;
	    }
	}
    });

    return false;
}

function on_close_click () {
    $("#contenu").slideDown ("slow");
    $("#popup").slideUp ("slow");
    return false;
}

function on_envoi_click () {
    xml = '<contact>';
    xml += '<genre>'+$("#genre").val()+'</genre>';
    xml += '<prenom>'+$("#prenom").val()+'</prenom>';
    xml += '<nom>'+$("#nom").val()+'</nom>';
    xml += '<societe>'+$("#societe").val()+'</societe>';
    xml += '<titre>'+$("#titre").val()+'</titre>';
    xml += '<adresse>'+$("#adresse").val()+'</adresse>';
    xml += '<cp>'+$("#cp").val()+'</cp>';
    xml += '<ville>'+$("#ville").val()+'</ville>';
    xml += '<telephone>'+$("#telephone").val()+'</telephone>';
    xml += '<email>'+$("#email").val()+'</email>';
    xml += '<message>'+$("#contact_msg").val()+'</message>';
    xml += '</contact>';
    $.ajax ({
	type: "POST",
	url: "/ajax/envoiContact.php",
	data: xml,
	success: function (reponse) {
	    alert ("Le message a été envoyé");
	    on_close_click ();
	}
    });
}

function on_accordion_click () {
    $(".ui-accordion-content").slideUp ();
    $(this).next (".ui-accordion-content").slideDown ();
}

