/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/10/16
 *
 * @author Blair Mitchelmore
 * @version 1.2
 *
 **/

jQuery.fn.extend({
	everyTime: function(interval, label, fn, times) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval < 0)
				return;

			if (typeof times != 'number' || isNaN(times) || times < 0) 
				times = 0;
			
			times = times || 0;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});


$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len = elems.length;
      
  elems.bind('load',function(){
      if (--len <= 0){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }
  });

  return this;
};



$(document).everyTime(5000,"mytimer", function(i) {
  cycleBack();
}, 0);


		function cycleBack()
		{
			if($('.btnAcc.active').next().length == 0) $('.btnAcc:first').click();
			else $('.btnAcc.active').next().click();
		}

 $(document).ready(function(){
		 
		 $('#navigation h3').click(function(event) {
			  
			  $('ul.menu').removeClass("selected");
			  
			  $(event.currentTarget).parent().find('ul.menu').addClass("selected");
			  
			  $('ul.menu').each(function(i,e){
			  
			  if(!$(e).hasClass('selected'))$(e).slideUp();
			 else $(e).slideDown();
			  });

		  $("h3").each(function(i,e){

				  if($(e).parent().find('ul.menu').hasClass('selected')){			  
					$(e).animate({paddingLeft: '25'});
				}
				else
				{
					$(e).animate({paddingLeft: '10'});
				}
	
		  });		  	  
		});	
		/*
		$('#btn1').click(function() {	
			
			$('#wrapper').animate({backgroundColor: "#27292b"},900);
			$('.background_image').animate({opacity: 0},500)
			.queue(function(next) { $('.background_image').attr({src: "http://vincentdamerique.ca/wordpress/wp-content/themes/vd/images/bg_test.jpg"}); next(); })			
			$('.background_image').load(function () {
				$('.background_image').animate({opacity: 1},500); 
			});		
			 
		});	
		$('#btn2').click(function() {
			
			$('#wrapper').animate({backgroundColor: "#c69b56"},900); 
			$('.background_image').animate({opacity: 0},500)
			.queue(function(next) { $('.background_image').attr({src: "http://vincentdamerique.ca/wordpress/wp-content/themes/vd/images/bg_test2.jpg"}); next(); })	
			$('.background_image').load(function () {
				$('.background_image').animate({opacity: 1},500); 
			});		
			
			
		});
		$('#btn3').click(function() {	
			
			$('#wrapper').animate({backgroundColor: "#4272ab"},900); 
			$('.background_image').animate({opacity: 0},500)
			.queue(function(next) { $('.background_image').attr({src: "http://vincentdamerique.ca/wordpress/wp-content/themes/vd/images/bg_test3.jpg"}); next(); })			
			$('.background_image').load(function () {
				$('.background_image').animate({opacity: 1},500); 
			});		
						
		});
		$('#btn4').click(function() {	
			
			$('#wrapper').animate({backgroundColor: "#7d262b"},900); 
			$('.background_image').animate({opacity: 0},500)
			.queue(function(next) { $('.background_image').attr({src: "http://vincentdamerique.ca/wordpress/wp-content/themes/vd/images/bg_test4.jpg"}); next(); })			
			$('.background_image').load(function () {
				$('.background_image').animate({opacity: 1},500); 
			});		
						
		});
		$('#btn5').click(function() {	
			
			$('#wrapper').animate({backgroundColor: "#5f5c60"},900); 
			$('.background_image').animate({opacity: 0},500)
			.queue(function(next) { $('.background_image').attr({src: "http://vincentdamerique.ca/wordpress/wp-content/themes/vd/images/bg_test5.jpg"}); next(); })			
			$('.background_image').load(function () {
				$('.background_image').animate({opacity: 1},500); 
			});		
						
		});
		*/
		
		/* BACKGROUND CHANGER */
		
		$(window).blur(function(){
		$(document).stopTime("mytimer");
		});
		
		$(window).focus(function(){
		$(document).everyTime(5000,"mytimer", function(i) {
  cycleBack();
}, 0);
		});
		
		$('.btnAcc:first').css('backgroundPosition','bottom');
		$('.btnAcc:first').css('opacity',1);
		$('.btnAcc:first').addClass('active');
		$('.btnAcc:first')
		
		$('.btnAcc').click(function(e) {
$(document).stopTime("mytimer");
		$(document).everyTime(5000,"mytimer", function(i) {
  cycleBack();
}, 0);
		
		
		$('.btnAcc').each(function(i,el){
			$(el).css('backgroundPosition','top');
			$(el).css('opacity',0.7);
			$(el).removeClass('active');
		});
		
			$(e.currentTarget).css('backgroundPosition','bottom');
			$(e.currentTarget).css('opacity',1);
			$(e.currentTarget).addClass('active');
		});
		
		$('.changeFond').click(function(e) {
			e.preventDefault();
			e.stopPropagation();
			
			//console.warn("Element qui chauffe : "+$(e.currentTarget).attr('href'));
			
			
			
 			$('.background_image').animate({opacity: 0},500,function(){
				
				
				
				//$('.background_image').attr('src', $(e.currentTarget).attr('href'));
				
				
				var img = new Image();
				$(img).css('opacity',0);
				$(img).css('background','none !important');
				
				$(img).attr('src', $(e.currentTarget).attr('href'))
                      .imagesLoaded(function() {
						$(img).addClass("background_image");
						  $(".wrapper_bg_image").html(img);
						  
						  $(".wrapper_bg_image").each(function(i,e){
						  if($(e).is('img')==true)
						  {
							$(e).empty();
						  }
						  });
						  
						  
						 $('.background_image').animate({opacity: 1},500);
						  $('#wrapper').animate({backgroundColor: "#"+$(e.currentTarget).attr('rel')},500,false);
					  
					  }); 
			}); 
			
			
			
			
			
			//$('.wrapper_bg_image').animate({opacity: 0},500,function(){
			
/* 			var img = $("<img class='background_image' />").attr('src', $(e.currentTarget).attr('href'))
                      .load(function() {
                         if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
                             //alert('broken image!');
                         } else {
                             $(".wrapper_bg_image").html(img);
							// $('.wrapper_bg_image').animate({opacity: 1},500);
							 $('#wrapper').animate({backgroundColor: "#"+$(e.currentTarget).attr('rel')},500,false);
                         }
                      }); */
			
			//});
/* 			$('.wrapper_bg_image img').load($(e.currentTarget).attr('href'), function(){

			});
			 */
/* 			$.ajax({
                url: $(e.currentTarget).attr('href'),
                dataType: 'html',
                cache: false,
                success: function (response) {
				var test = new Image(response);
                    $(".wrapper_bg_image").html(test);
								$('.wrapper_bg_image').animate({opacity: 1},500);
			$('#wrapper').animate({backgroundColor: "#"+$(e.currentTarget).attr('rel')},900);
                }
            }); */
			
			


			
			
		});
		
  	 });
