
$(function() {

    // init facebox
    $.facebox.settings.loading_image = respath + 'p/facebox/loading.gif';
    $.facebox.settings.close_image = respath + 'p/facebox/closelabel.gif';
    $.facebox.settings.faceboxHtml = '\
<div id="facebox" style="display:none;"> \
    <div class="popup"> \
        <table> \
            <tbody> \
                <tr> \
                    <td class="tl"/><td class="b"/><td class="tr"/> \
                </tr> \
                <tr> \
                    <td class="b"/> \
                    <td class="body"> \
                        <div class="content"> \
                        </div> \
                        <div class="footer"> \
                            <a href="#" class="close"> \
                                <img src="' + $.facebox.settings.close_image + '" title="close" class="close_image" /> \
                            </a> \
                        </div> \
                    </td> \
                    <td class="b"/> \
                </tr> \
                <tr> \
                    <td class="bl"/><td class="b"/><td class="br"/> \
                </tr> \
            </tbody> \
        </table> \
    </div> \
</div>';

    // proper taget _blank
    jQuery('a[rel*=external]').click(function() {
        this.target = '_blank';
    });

    raise_popup = function(level, msg) {

        popup = '\
    <div id="popup-content"> \
        <img src="' + respath + 'p/facebox/icon-' + level + '.gif" alt="' + level + '" width="49" height="49" /> \
        <p>' + msg + '</p> \
    </div>';

        jQuery.facebox(popup);
    };

    // forms
    check_email = function(email) {
        filterEmail = /^[\-+_\.A-Za-z0-9]+\@((([A-Za-z0-9]|[A-Za-z0-9][\-A-Za-z0-9]*[A-Za-z0-9])\.)+(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/;
        if (email == '') {
            return false;
        } else if (! filterEmail.test(email)) {
            return false;
        } else {
            return true;
        }
    }

    check_date = function(day, month, year) {
        if (day == 0 || month == 0 || year == 0) {
            return false;
        }

        //Check if the date is after the current date
        var now = new Date();
        var date = new Date(); date.setDate(day); date.setMonth(month-1); date.setFullYear(year);
        if (date >= now) {
            return false;
        }

        //Calculate the number of days in the month
        if (month == 2) {
            nb_of_days = ( (year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            nb_of_days = 30;
        } else {
            nb_of_days = 31;
        }

        if (day > nb_of_days) {
            return false;
        } else {
            return true;
        }
    }

    get_radios_value = function(id) {
        tmp = "";

        for (var i = 0; (elmt = document.getElementById(id + i)) ; i++) {
            if (elmt.checked) {
                tmp = elmt.value;
            }
        }

        return tmp;
    }

    get_checkbox_value = function(id) {
        if (document.getElementById(id).checked) {
            tmp = true;
        } else {
            tmp = false;
        }
        return tmp;
    }

    get_value = function(id) {
        return  document.getElementById(id).value ;
    }

    give_focus_to_first_elmt = function() {
        var input_elmts=document.getElementsByTagName("input");
        for (var i=0; i<input_elmts.length; i++) {
            if (input_elmts[i].type == "text") {
                input_elmts[i].focus();
                break;
            }
        }
    }
    // end forms

    // visualisation
/*
    preload = function(img_path) {
        var images = new Array("table_left.gif", "mr0.gif", "mr1.gif", "mr2.gif", "mr3.gif", "ms0.gif", "ms1.gif", "ms2.gif", "ms3.gif", "table_right.gif");

		for (var i = 0 ; i < images.length; i++) {
			image = new Image();
			image.src = img_path + images[i];
		}
   }
*/
    draw = function(img_path, is_validate) {
        nb_mr_images = 10;
		nb_ms_images = 10;
		image_width = 100;

		// Init the variables
		id_mr_image = 0;
		id_ms_image = 0;

		// Setting up the list and changing the images
		if (is_validate) {
            list = validated_participants;
            document.getElementById("validated_participants_image").className = "on";
            document.getElementById("non_validated_participants_image").className = "off";
            incr = 1;
            //img_type = '_conf';
            img_type = '';
		} else {
            list = non_validated_participants;
            document.getElementById("validated_participants_image").className = "off";
            document.getElementById("non_validated_participants_image").className = "on";
            incr = -1;
            img_type = '';
		}

		var objects = new Array();

		// Add the images if at least one element in the list
		if (list.length != 0) {
			for (var i = 0; i < list.length; i++) {
				// Calculate the image name
				switch (list[i]['civility_godchild']) {
					case 'MR':
						img_name = 'mr' + id_mr_image + img_type;
						id_mr_image = (nb_mr_images + id_mr_image + incr) % nb_mr_images;
						break;
					case 'MME':
                    case 'MISS':
						img_name = 'ms' + id_ms_image + img_type;
						id_ms_image = (nb_ms_images + id_ms_image + incr) % nb_ms_images;
						break;
				}
				objects[objects.length] = '<img src="' + img_path + img_name + '.png' + '" class="vtop tooltip" alt="' + list[i]['email_godchild'] + '" width="100px" height="100px" />';
			}

		// Add an empty chair if no element
		} else {
			objects[objects.length] = '<img src="' + img_path + 'empty.png' + '" width="100px" height="100px" style="margin-left: 83px;" alt="" />';
		}
		object_div = objects.join("");
		jQuery("#guest").html(object_div);
        jQuery("#guest").css("width", image_width * objects.length);
        tooltip();

        // recall pngfix.js...
        var arVersion = navigator.appVersion.split("MSIE")
        var version = parseFloat(arVersion[1])

        if ((version >= 5.5) && (document.body.filters)) 
        {
           for(var i=0; i<document.images.length; i++)
           {
              var img = document.images[i]
              var imgName = img.src.toUpperCase()
              if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
              {
                 var imgID = (img.id) ? "id='" + img.id + "' " : ""
                 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                 var imgStyle = "display:inline-block;" + img.style.cssText 
                 if (img.align == "left") imgStyle = "float:left;" + imgStyle
                 if (img.align == "right") imgStyle = "float:right;" + imgStyle
                 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                 var strNewHTML = "<span " + imgID + imgClass + imgTitle
                 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
                 img.outerHTML = strNewHTML
                 i = i-1
              }
           }
        }
        // end recall pngfix
        
   }

    tooltip = function() {
        // tooltip configuration
        // these 2 variable determine popup's distance from the cursor
        // you might want to adjust to get the right result
        xOffset = 10;
        yOffset = 20;
        // end tooltip configuration
        jQuery("img.tooltip").hover(function(e) {
            this.t = this.alt;
            this.alt = "";
            jQuery("body").append("<p id='tooltip'>"+ this.t +"</p>");
            jQuery("#tooltip")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .fadeIn("fast");
        },
        function() {
            this.alt = this.t;
            jQuery("#tooltip").remove();
        });
        jQuery("img.tooltip").mousemove(function(e) {
            jQuery("#tooltip")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
        });
    };

   // end visualisation

});

