/* ****************************************************

	@file		  global.js
	@description  Comportements globaux
	@author		  remi (ixmedia.com)
	@version	  20090410

***************************************************** */

(function($){

	$(function() {

		$('a').liensExternes({
			exclude : window.location.host
		});

		function updateAutres() {
			$('tr.autre').each(function() {
				var $this = $(this);
				if ($this.prev().find('select').val() == 'autre') {
					$this.show();
				} else {
					$this.hide();
				}
			});
		}
		$('#statut, #source').change(updateAutres);
		updateAutres();

		$('#utilitaires ul ul').hide();
		$('#utilitaires > ul > li > a').click(function() {
			switch ($(this).parent().attr('class')) {
				case 'print' :
					window.print();
					return false;
				case 'partager' :
					$('#utilitaires ul ul').slideToggle(200);
					return false;
				case 'question' :
					ixbox.toggle('question');
					return false;
				case 'contact' :
					ixbox.toggle('envoyer');
					return false;

			}
		});
		
		$('.erreur .question').click(function() {
			ixbox.toggle('question');
			return false;
		});

		var partager_urls = {
			'delicious': function() {
				window.open('http://del.icio.us/post?v=4;url=' + encodeURIComponent(location.href) + ';title=' + encodeURIComponent(document.title), "", "width=1000,height=600,menubar=1,resizable=1,scrollbars=1");
			},

			'publieca': function() {
				window.open('http://publie.ca/submit/?url='+encodeURIComponent(window.location.href.substr(0,1024))+'&title='+encodeURIComponent(document.title.substr(0,64))+'&body='+encodeURIComponent(document.getSelection().substr(0,255)));
			},

			'facebook': function() {
				var d = document,
				f = 'http://www.facebook.com/share',
				l = d.location,
				e = encodeURIComponent,
				p = '.php?src=bm&v=4&i=0&u=' + e(l.href) + '&t=' + e(d.title);
				a = function() { if (!window.open(f + 'r' + p, 'sharer', 'toolbar=0,status=0,resizable=1,width=626,height=436')) l.href = f + p };
				if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0);
				else { a() }
			},

			'google': function() {
				window.open('http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title), "", "width=1000,height=600,menubar=1,resizable=1,scrollbars=1");
			}
		}

		$('#utilitaires ul ul a').click(function() {
			var classe = this.parentNode.className;
			partager_urls[classe].call();
			$('#utilitaires ul ul').hide();
			return false;
		});

		$(document).click(function() {
			$('#utilitaires ul ul').hide();
		});

		$('a.wii').click(function() {
			ixbox.toggle('wii');
			return false;
		});

		$('a.video').click(function() {
			var movie = $(this).attr('href');
			var meta = $.metadata.get(this);

			$('#video .video').html('');
			$('#video').css({
				width : meta.largeur+'px',
				height : (meta.hauteur + 25) + 'px',
				marginLeft : '-' + (meta.largeur/2)+'px'
			})
			ixbox.toggle('video', null, function() {
				paramFV = '';
				if (meta.fvVideo) {
					paramFV = '<param name="FlashVars" value="fvVideo='+meta.fvVideo+'" />';
				}
				$('#video .video').html('<object type="application/x-shockwave-flash" data="'+movie+'" width="'+meta.largeur+'" height="'+meta.hauteur+'"><param name="bgcolor" value="#000000" /><param name="movie" value="'+movie+'" />'+paramFV+'</object>');
			});
			return false;
		});
		
		$('#video .fermer a').click(function() {
			$('#video .video').html('')
			ixbox.toggle('video')
		})


var ixbox = {
	init : function() {

		$('.form-overlay .fermer').click(function() {
			ixbox.toggle($(this).parent().parent().attr('id'));
		})
		$('#overlay').css('opacity', 0.8);
		$('.form-overlay').find('.loading, .confirmation, .form-erreur').hide();

		$('#envoyer form').submit(function() {

			$(this).find('input, textarea').removeClass('erreur');
			var valide = true;

			var auteur_nom 				= $('#auteur-nom').val();
			var auteur_courriel 		= $('#auteur-courriel').val();
			var destinataire_nom 		= $('#destinataire-nom').val();
			var destinataire_courriel 	= $('#destinataire-courriel').val();
			var commentaires 			= $('#commentaires').val();

			if (auteur_nom == "") {
				$('#auteur-nom').addClass('erreur');
				valide = false;
			}

			if (!auteur_courriel.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
				$('#auteur-courriel').addClass('erreur');
				valide = false;
			}

			if (destinataire_nom == "") {
				$('#destinataire-nom').addClass('erreur');
				valide = false;
			}

			if (!destinataire_courriel.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
				$('#destinataire-courriel').addClass('erreur');
				valide = false;
			}

			if (valide === true) {
				$(this).find('.champs, .confirmation').hide().parent().find('.loading').show();

				// Envoi du courriel!
				$.ajax({
					type: 'POST',
					url : '/envoyer-ami.php',
					data : {
						'auteur-nom' : auteur_nom,
						'auteur-courriel' : auteur_courriel,
						'destinataire-nom' : destinataire_nom,
						'destinataire-courriel' : destinataire_courriel,
						'commentaires' : commentaires
					},
					success : function(data) {
						// Affichage de confirmation
						if (parseInt(data) == 1) {
							$('#envoyer form').find('.confirmation').show().parent().find('.loading, .champs').hide();
						} else {
							$('#envoyer form').find('.form-erreur').show().parent().find('.loading, .champs').hide();
						}
					}
				})
			}
			return false;
		})

		$('#question form').submit(function() {

			$(this).find('input, textarea').removeClass('erreur');
			var valide = true;

			var courriel 		= $('#question-courriel').val();
			var question		= $('#laquestion').val();

			if (!courriel.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
				$('#question-courriel').addClass('erreur');
				valide = false;
			}

			if (question == "") {
				$('#laquestion').addClass('erreur');
				valide = false;
			}

			if (valide === true) {
				$(this).find('.champs, .confirmation').hide().parent().find('.loading').show();

				// Envoi du courriel!
				$.ajax({
					type: 'POST',
					url : '/envoyer-question.php',
					data : {
						'courriel' : courriel,
						'question' : question
					},
					success : function(data) {
						// Affichage de confirmation
						if (parseInt(data) == 1) {
							$('#question form').find('.confirmation').show().parent().find('.loading, .champs').hide();
						} else {
							$('#question form').find('.form-erreur').show().parent().find('.loading, .champs').hide();
						}
					}
				})

			}

			return false;

		})

		$(document).keyup(function(e){
			keycode = (e == null) ? event.keyCode : e.which;
			if (keycode == 27 && $('#overlay').is(':visible')) { // close
				var id = $('.form-overlay:visible, .form-overlay-alt:visible, .form-overlay-video:visible').attr('id');
				ixbox.toggle(id);
			}
		});

	},
	toggle : function(id, defaultvalue, callback) {
		$overlay = $('#overlay');
		$overlay.bgiframe();
		$form = $('#'+id);
		if ($overlay.is(':animated')) {
			$overlay.stop();
		}
		
		if ($overlay.is(':visible')) {
			$overlay.stop().animate({
				opacity: 0
			},{
				duration: 400,
				complete : function() {
					$(this).hide();
				}
			});
			$form.slideUp();
			$form.find('input, textarea').val("").removeClass('erreur');
			$form.find('.loading, .confirmation, .form-erreur').hide().parent().find('.champs').show();
		} else {

			if ($.browser.msie && $.browser.version <= 6) {
				window.scrollTo(0,1);
			}

			$form.slideDown();
			if (id == "question" && defaultvalue!="") {
				$form.find('#laquestion').val(defaultvalue)
			}
			$overlay.css('opacity', 0).show().animate({
				opacity: 0.8
			},{
				duration: 400,
				complete : function() {
					if (callback) {
						callback();
					}
				}
			});
		}
	}
};
ixbox.init();


	});

})(jQuery)