jQuery.fn.voxCarousel = function(options) {
	var defaults = {
		slideList: '.namelist'
	}
	var options = $.extend(defaults, options);

	return this.each(function() {
		var activeSlide = $(this).find('.slide li:first');
		var slideList = $(this).find(options.slideList);

		//$(this).find('.slide-holder shape').remove();
		if (slideList.length > 0) {
			function updateSlideList() {
				$(slideList).find('a').each(function(e) {
					// Custom modif Qc média
					//if (activeSlide[0] == $($(this).attr('href'))[0]) {
					if (activeSlide[0] == $($(this).attr('rev'))[0]) {
						$(this).parent().addClass('current');
					} else {
						$(this).parent().removeClass('current');
					}
					Cufon.replace('.namelist li a', {
						fontFamily: 'titillium',
						hover: true
					});
				});
			}
			$(slideList).find('a').click(function(e) {
				try {
					e.preventDefault()
					// Custom modif Qc média
					activeSlide = jQuery(jQuery(this).attr('href'));
					//activeSlide = jQuery(jQuery(this).attr('rev'));
					updateNav();
					$(this).closest('.carousel').find('.slide-holder').scrollTo(activeSlide, 500, { easing:'swing' });
				}
				catch(err) {}
			});
		}

		function updateNav() {
			if (slideList.length > 0) {
				updateSlideList();
			}
		}

		// Custom modifs to these two functions by Qc média. The first-child and last-child properties that were used dont exist accrding to the jquery doc.
		$(this).find('.prev').click(function(e) {
			e.preventDefault();
			activeSlide = $(activeSlide).prev().length > 0 ? $(activeSlide).prev() : $('.carousel .slide li:last');
			updateNav();
			$(this).closest('.carousel').find('.slide-holder').scrollTo(activeSlide, 500, { easing:'swing' });
		});
		$(this).find('.next').click(function(e) {
			e.preventDefault();
			activeSlide = $(activeSlide).next().length > 0 ? $(activeSlide).next() : $('.carousel .slide li:first');
			updateNav();
			$(this).closest('.carousel').find('.slide-holder').scrollTo(activeSlide, 500, { easing:'swing' });
		});

		$(this).find('.slide-holder').scrollTo(activeSlide, 500, { easing:'swing' });
		updateNav();

	});
};

$(document).ready(function() {
	$('.carousel').voxCarousel();

	if ($('body.contact').length > 0 && GBrowserIsCompatible()) {
		var voxIcon = new GIcon();
		voxIcon.image = "http://www.voxdata.com/fileadmin/templates/images/ico_map-pin.png";
		voxIcon.iconSize = new GSize(47, 35);
		voxIcon.iconAnchor = new GPoint(16, 34);
		var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));

		var montreal = new GMap2(document.getElementById("montreal-map"));
		montreal.addControl(new GSmallZoomControl3D(), topRight);
		montreal.setCenter(new GLatLng(45.49993650946158, -73.57148587703705), 15);
		montreal.addOverlay(new GMarker(new GLatLng(45.499951, -73.570257), {icon:voxIcon}));

		var toronto = new GMap2(document.getElementById("toronto-map"));
		toronto.addControl(new GSmallZoomControl3D(), topRight);
		toronto.setCenter(new GLatLng(43.74443684930602, -79.40736651420593), 15);
		toronto.addOverlay(new GMarker(new GLatLng(43.744413,-79.405717), {icon:voxIcon}));
	}

	// Accessible input values
    $("form.extended input[type=text], form.extended textarea").each(function(){
      var id = $(this).attr("id");
      var val = $(this).prev().html();
      $(this).prev().hide();
      if(this.value == '') this.value = val;
      $(this).focus(function(){
        if(this.value == val) this.value = '';
        $(this).addClass('focus');
        // Custom modif Qc média. Error manipulation
        if($(this).hasClass('error'))
        	$(this).removeClass('error');
        // End custom
       }).blur(function(){
        if(this.value == '') this.value = val;
        $(this).removeClass('focus');
      })
     });

	// Input type file customization
  $('input[type=file]').each(function(){
    $(this).addClass('file').addClass('hidden');
    // Custom modif by Qc média. Changed the path of the image and added language support.
    $(this).parent().append($('<div class="fakefile" />').append($('<input type="text" value="'+$(this).prev().html()+'" />').attr('id',$(this).attr('id')+'__fake')).append($('<img src="fileadmin/templates/images/browse.png" alt="Browse" />')));

    $(this).bind('change', function() {
      $('#'+$(this).attr('id')+'__fake').val($(this).val());;
    });
    $(this).bind('mouseout', function() {
      if ( $(this).val() != '' ) {
        $('#'+$(this).attr('id')+'__fake').val($(this).val());;
      }
    });
  });

	// Custom modif qc média. We add simple validation to the form
	$("form.extended").bind('submit',function(e) {
		$(this).data("submit",true);
		// We purposely ommit to include the textarea because the text in this field is optional.
		$("form.extended input[type=text]").each(function(i) {
			if(this.value == $(this).prev().html() || this.value == '') {
				$(this).addClass('error');
				$("form.extended").data("submit", false);
			}
		});

		return $(this).data("submit");
	});
	// End custom
	
	// Custom modif qc média. Number removal form
	$("#validate_step1").click(function(e) {
		$("#messages_step1").fadeOut();
		$("#loading_step1").fadeIn();

		$.get(
			$("#qm_voxdatanumberremoval_1").attr("action"),
			$("#qm_voxdatanumberremoval_1").serialize()+'&tx_qmvoxdatanumberremoval_pi1%5Bstep%5D=1',
			function(data) {
				$("#loading_step1").fadeOut();

				var ar = data.split("||");
				
				if(ar[0] == "1") {
					$(".qm_voxdatanumberremoval_step2").removeClass("disabled");
					$(".qm_voxdatanumberremoval_step2 input").removeAttr("disabled");
					$("#messages_step1").hide();
					
					$("#validate_step2").click(function(e) {
						$("#messages_step2").fadeOut();
						$("#loading_step2").fadeIn();
						
						$.get(
							$("#qm_voxdatanumberremoval_1").attr("action"),
							$("#qm_voxdatanumberremoval_1").serialize()+'&tx_qmvoxdatanumberremoval_pi1%5Bstep%5D=2',
							function(data) {
								$("#loading_step2").fadeOut();
							
								var ar = data.split("||");
								
								if(ar[0] == "1") {
									$("#messages_step2")
										.removeClass("error")
										.addClass("success");
								} else {
									$("#messages_step2")
										.removeClass("success")
										.addClass("error");
								}
								
								$("#messages_step2")
									.html(ar[1])
									.fadeIn();
							}
						);
					
						return false;
					});
					
				} else {
					$("#messages_step1")
						.html(ar[1])
						.fadeIn();
				}
			}
		);
	
		return false;
	});
	// End custom


	// Namelist hover
  $('.namelist li a').hover(function(){
    $(this).addClass('hover');
    Cufon.replace('.namelist li a', {
			fontFamily: 'titillium',
			hover: true
		});
  },function() {
    $(this).removeClass('hover');
		Cufon.replace('.namelist li a', {
			fontFamily: 'titillium',
			hover: true
		});
  });

  $('body').addClass('js');

  Cufon.now();
  Cufon.refresh('#navigation a');

});
