
function sitq_ready() {
	sitq.imagePreview();
	sitq.positionComplexe();
	sitq.titleTips();
	sitq.certifications();
	sitq.formulaires.champs();
}

var sitq = {

	imagePreview: function() {
		xOffset = 10;
		yOffset = 10;
		
		$jq('a.imm_link').hover(
			function(e)
			{
				if ( this.rel != "" )
				{
					this.t = this.title;
					this.title = "";
					var c = ''//(this.t != "") ? "<br />" + this.t : "";
					$jq("body").append('<p id="preview"><img src="' + this.rel + '" alt="Image preview" />'+ c +'</p>');
					$jq("#preview")
						.css("top",(e.pageY - xOffset) + "px")
						.css("left",(e.pageX + yOffset) + "px")
						.fadeIn("fast");
				}
			},
			function()
			{
				if ( this.rel != "" )
				{
					this.title = this.t;	
					$jq("#preview").remove();
				}
			});
		
		$jq("a.imm_link").mousemove(function(e){
			$jq("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});
	},
	
	positionComplexe: function() {
		if ($jq('.complexe_button').length < 1) return false;
		
		$jq('.complexe_button').each(function(i,e) {
			var pictureWidth = $jq(this).next().width();
			var wrapperWidth = $jq(this).parent().width();
			var posComplexe = Math.ceil((wrapperWidth - pictureWidth) / 2);
			$jq(this).children('img').css('right', posComplexe + 'px');
		});
	},


	titleTips: function() {

		xOffset2 = 10;
		yOffset2 = -20;

		$jq('a.tip').hover(
			function(e) {

				if ( this.title != "" ) {
	
					this.ori = this.title;
					this.infos = this.title.split(/\|/);
					this.title = "";

					var c = "a";		// contenu
					
					$jq.each(this.infos, function(index, val) {
						if (index == 0) {
							c = '<h6>'+val+'</h6>';
						} else {
							c = c+val+'<br />';
						}
					});

					$jq("body").append('<div id="titleTip">'+c+'</div>');

					$jq("#titleTip")
						.css("top",(e.pageY - yOffset2) + "px")
						.css("left",(e.pageX + xOffset2) + "px")
						.fadeIn("normal");
				}
			},
			function() {
				this.title = this.ori;
				$jq("#titleTip").remove();
			});
		
		$jq("a.tip").mousemove(function(e){
			$jq("#titleTip")
				.css("top",(e.pageY - yOffset2) + "px")
				.css("left",(e.pageX + xOffset2) + "px");
		});
	},
	
	certifications: function() {
		$jq('img.boma_best').parent('a').hover(function() {
			if ($jq('.pop_boma_best').length > 0)
				$jq('.pop_boma_best').show();
		},function() {
			if ($jq('.pop_boma_best').length > 0)
				$jq('.pop_boma_best').hide();
		});
	},
	
	table_decoration: function() {
		var tr = 1;
		$jq("table.decorate tbody").each(function() {
			tr = 1;
			$jq(this).children("tr").each(function() {
				if ((tr % 2) == 0)
					$jq(this).addClass("paire");
				tr++;
			});
		});
		sitq.formulaires.champs();
	},
	
	formulaires: {
		attach_detail_autre_click: function(a) {
			var autre_id_prefix = $jq(a).attr('id').replace(/f_mca_/,"f_mca_carteopt_");
			var autre_label = langue == 'en' ? 'Other' : 'Autre';
			$jq('#'+autre_id_prefix+'_details_'+autre_label).click(function() {
				if ($jq(this).attr('checked'))
					$jq('#'+autre_id_prefix+'_detail_autre').show();
				else 
					$jq('#'+autre_id_prefix+'_detail_autre').hide();
			});
		},
		carteopts: function(a) {
			if (typeof actions == "undefined" || typeof actions[$jq(a).attr('id')] == "undefined")
				return false;

			$jq.ajax({
				type: "POST",
				url: actions[$jq(a).attr('id')],
				data: "",
				dataType: "html",
				success: function(resultat){
					if (resultat) {
						$jq('#carteopt').html(resultat);
						sitq.formulaires.attach_detail_autre_click(a);
						sitq.table_decoration();
					} else {
						alert("<?php echo __('Erreur'); ?>");
					}
				},
				error: function(e) {
					alert("<?php echo __('Erreur'); ?> : " + e);
				}
			});
			return false;
		},
		erreur: function(e) {
			if (typeof e == "string") {
				alert(e);
			} else if (typeof e == "object") {
				if (e.statusText && e.status)
					alert(e.status + ' - ' +e.statusText);
			} else {
				alert("Erreur indéfinie")
			}
		},
		champs: function() {
			$jq('form table tr.requis th label').each(function() {

				if (!$jq(this).children('span.requis').length)
					$jq(this).append('<span class="requis">*</span>');
			});
			$jq('form table tr.requis_choices td').each(function() {
				if (!$jq(this).children('span.requis_choices').length)
					$jq(this).prepend('<span class="requis_choices">*</span>');
			});
		}
	}
}
