/*!
 * GesWeb jquery Library for chables using jQuery v1.4.2
 * http://www.obtic.ch/
 *
 * Copyright (c) 2011 Obtic Sarl
 *
 * Date: 2011-06-10 (Fri, 10 June 2011)
 */
$(function() {

// news scroll (only if display news)
	if ($("#gw-news-scroller").length > 0)
  {
    $('#gw-news-scroller').theatre({
      selector: 'div', 		
      effect: 'vertical',
      controls: 'none',
      still: 3000,
      speed: 800,
    });
  }

  //show-hide greather than 3 direct acces
  $('.gw-connexe-direct li:gt(1)').hide();
  $('.gw-connexe-direct').hover(
    function(){
      $('.gw-connexe-direct p').slideToggle();
      $('.gw-connexe-direct li:gt(1)').slideToggle('slow');
  });


  // Add picto to link
  $('#gw-content a:not(:has(img)), .gw-connexe-content a:not(:has(img))').filter(function() {
      return this.hostname && this.hostname !== location.hostname;
  }).after(' <img src="/Themes/chables/images/external.png" style="border:none;padding:0;margin:0;" alt="external link" />');   
        
  $(".gw-connexe-content a[href$='.pdf']").before('<img src="/Themes/chables/images/pdf.png" style="border:none;padding:0;margin:0;margin-left:-18px;margin-top:5px;" alt="fichier acrobat pdf" />&nbsp;');   
  $("#gw-normal a[href$='.pdf']").after('<img src="/Themes/chables/images/pdf.png" style="border:none;padding:0;margin:0;" alt="fichier acrobat pdf" />&nbsp;');   

  // Header Carousel
	$('#gw-carousel').waitForImages(
			function() {
				$('#gw-carousel').show().theatre({
					selector: 'img', 		
					effect: '3d',
					controls: 'none',
					still: 1000,
					speed: 1000,
				});
			}
//			,function(loaded, total) {
//				$('body').append('<p>Image ' + loaded + ' of ' + total + ' loaded.</p>');
//			}
	);
	
//	$('#gw-carousel img').cacheImage();

  // Header Fancy
  $('#gw-carousel a').fancybox();
		

});
//JQuery
			
/*
* waitForImages 1.2.2
* -----------------
* Provides a callback when all images have loaded in your given selector.
* http://www.alexanderdickson.com/
*
*
* Copyright (c) 2011 Alex Dickson
* Licensed under the MIT licenses.
* See website for more info.
*
*/

;(function($) {
    $.fn.waitForImages = function(finishedCallback, eachCallback, waitForAll) {

        // Handle options object.
        if (typeof finishedCallback === 'object') {
            eachCallback = finishedCallback.each;
            waitForAll = finishedCallback.waitForAll;
            finishedCallback = finishedCallback.finished;
        }

        // Handle missing callbacks.
        finishedCallback = finishedCallback || function() {};
        eachCallback = eachCallback || function() {};

        // Convert waitForAll to Boolean
        waitForAll = !! waitForAll;

        // Ensure callbacks are functions.
        if (!$.isFunction(finishedCallback) || !$.isFunction(eachCallback)) {
            throw new TypeError('An invalid callback was supplied.');
        };

        return this.each(function() {
            // Build a list of all imgs, dependent on what images will be considered.
            var obj = $(this),
                allImgs = [];

            if (waitForAll) {
                // CSS properties which may contain an image.
                var hasImgProperties = $.fn.waitForImages.hasImgProperties || [
                    'backgroundImage',
                    'listStyleImage',
                    'borderImage',
                    'borderCornerImage'
                    ];
                   
                var matchUrl = /url\(['"]?(.*?)\1\)/g;

                // Get all elements, as any one of them could have a background image.
                obj.find('*').filter(function() {
                    var element = $(this);

                    // If an `img` element, add it. But keep iterating in case it has a background image too.
                    if (element.is('img')) {
                        allImgs.push({
                            src: element.attr('src'),
                            element: element[0]
                        });
                    }

                    $.each(hasImgProperties, function(i, property) {
                        var propertyValue = element.css(property);
                        // If it doesn't contain this property, skip.
                        if ( ! propertyValue) {
                            return true;
                        }

                        // Get all url() of this element.
                        var match;
                        while (match = matchUrl.exec(propertyValue)) {
                            allImgs.push({
                                src: match[1],
                                element: element[0]
                            });
                        };
                    });
                });
            } else {
                // For images only, the task is simpler.
                obj.find('img').each(function() {
                    allImgs.push({
                        src: this.src,
                        element: this
                    });
                });
            };

            var allImgsLength = allImgs.length,
                allImgsLoaded = 0;

            // If no images found, don't bother.
            if (allImgsLength == 0) {
                finishedCallback.call(obj[0]);
            };

            $.each(allImgs, function(i, img) {

                var image = new Image;

                image.onload = function() {
                    allImgsLoaded++;
                    eachCallback.call(img.element, allImgsLoaded, allImgsLength);
                    if (allImgsLoaded == allImgsLength) {
                        finishedCallback.call(obj[0]);
                        return false;
                    };
                };

                image.src = img.src;
            });
        });
    };
})(jQuery);

/*
 * cacheImage: a jQuery plugin
 *
 * cacheImage is a simple jQuery plugin for pre-caching images.  The
 * plugin can be used to eliminate flashes of unstyled content (FOUC) and
 * improve perceived page load time.  Callbacks for load, error and abort
 * events are provided.
 *
 * For usage and examples, visit:
 * http://github.com/alexrabarts/jquery-cacheimage
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2008 Stateless Systems (http://statelesssystems.com)
 *
 * @author   Alex Rabarts (alexrabarts -at- gmail -dawt- com)
 * @requires jQuery v1.2 or later
 * @version  0.2.1
 */

(function ($) {
  $.extend($, {
    cacheImage: function (src, options) {
      if (typeof src === 'object') {
        $.each(src, function () {
          $.cacheImage(String(this), options);
        });

        return;
      }

      var image = new Image();

      options = options || {};

      $.each(['load', 'error', 'abort'], function () { // Callbacks
        var e = String(this);
        if (typeof options[e] === 'function') { $(image).bind(e, options[e]); }

        if (typeof options.complete === 'function') {
          $(image).bind(e, options.complete);
        }
      });

      image.src = src;

      return image;
    }
  });

  $.extend($.fn, {
    cacheImage: function (options) {
      return this.each(function () {
        $.cacheImage(this.src, options);
      });
    }
  });
})(jQuery);

