jQuery(document).ready(function() {
	//adresse du theme :
	var urlTheme = jQuery('head link[href*=grid/style.css]');
	urlTheme = jQuery(urlTheme).attr('href');
	urlTheme = urlTheme.substr(0,urlTheme.length-9);
	
	//fonctions de gestion des couleurs dynamiques	
	function hueToRGB(p, q, t){
		if(t < 0) t += 1;
		if(t > 1) t -= 1;
		if(t < 1/6) return p + (q - p) * 6 * t;
		if(t < 1/2) return q;
		if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
		return p;
	};
	
	function randomColor(){
	
		var h = Math.random();
		var s = 1; //0.8 + (Math.random()/5);
		var l = 0.5;// + (Math.random()/5);
		
		var r, g, b;

		if(s == 0){
			r = g = b = l; // gris
		}else{
			var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
			var p = 2 * l - q;
			r = hueToRGB(p, q, h + 1/3);
			g = hueToRGB(p, q, h);
			b = hueToRGB(p, q, h - 1/3);
		}
		/*var r=55+Math.round(Math.random()*145);
		var g=55+Math.round(Math.random()*145);;
		var b=55+Math.round(Math.random()*145);;*/
		r = Math.round(r*255);
		g = Math.round(g*255);
		b = Math.round(b*255);
		
		
		var color = 'rgb('+r+', '+g+', '+b+')';
		//console.log(color);
		return color;
	}
	
	//couleur de base
	
	var gridColor1 = jQuery('body').css('color');
	var gridColor2 = randomColor();
	var gridColor3 = jQuery('body').css('backgroundColor');
	
	//temporisation de base
	var minTime = 150;
	var medTime = 250;
	var maxTime = 500;
	var hugTime = 3500;
	
	
	
	
	//gestion #header h1 a
	logoDom = '<img src="' + urlTheme + 'images/logo-03.png" title="Vonwa" alt="Vonwa" style="margin: 0 0 0 -30px ; background-color:' + gridColor2 + '" />';
	jQuery('#header h2').css('margin','0');
	jQuery('#header h1 a').empty().append(logoDom);
	jQuery('#header h1 a, #footer h2 a').css('color',gridColor2);
	
	//gestion div.warning	
	jQuery('div.warning').css({
		'border':'1px solid '+gridColor2,
		'border-left':'none',
		'border-right':'none'
	});
	
	//gestion .field
	jQuery('#content .field').css({
		'color':'#000',
		'background':'none',
		'border':'1px solid '+gridColor2
	});
	jQuery('#header .field').css({
		'color':'#000',
		'background':'#FFF',
		'borderColor':'#FFF',
		'color':gridColor1
	}).hover(
		function(){
			jQuery(this).animate({
				'backgroundColor':gridColor2,
				'borderColor':gridColor2,
				'color':'#FFF'
			}, { queue:false, duration:medTime });
		},function(){
			jQuery(this).animate({
				'backgroundColor':'#FFF',
				'borderColor':'#FFF',
				'color':gridColor1
			}, { queue:false, duration:medTime });
		}
	).focus(function() {
		if( this.value == "Recherche" ) {
			this.value = "";
		}
	}).blur(function() {
		if( this.value == "" ) {
			this.value = "Recherche";
		}
	});
	jQuery('img.medium').css('background',gridColor2);
	
	//gestion #nav2 rollover
	jQuery('#nav1 li a, #nav2 li a, .entry a').each(function(){
		jQuery(this).hover(
			function(){
				jQuery(this).animate({
					'color':gridColor2
				}, { queue:false, duration:medTime });
			},function(){
				jQuery(this).animate({
					'color':gridColor1
				}, { queue:false, duration:medTime });
			}
		)
	});
	
	//gestion .navigation a rollover
	jQuery('.navigation a').each(function(){
		switch(jQuery(this).parent().attr('class')){
			case 'top':
				var direction = 'top';
			break;
			case 'right':
				var direction = 'right';
			break;
			case 'bottom':
				var direction = 'bottom';
			break;
			case 'left':
				var direction = 'left';
			break;
		}
		//console.log(gridColor1);
		jQuery(this).css({
			'background-image':'url('+urlTheme+'/images/icons/32-arrow-'+direction+'.png)',
			'backgroundColor':gridColor1
		}).hover(
			function(){
				jQuery(this).css({
					'background-image':'url('+urlTheme+'/images/icons/32-arrow-'+direction+'.png)',
					'backgroundColor':gridColor1
				}).animate({
					'backgroundColor':gridColor2
				}, { queue:false, duration:medTime });
			},function(){
				jQuery(this).animate({
					'backgroundColor':gridColor1
				}, { queue:false, duration:medTime });
			}
		)
	});
	
	//gestion button rollover
	jQuery('button').hover(
		function(){
			jQuery(this).animate({
				'color':gridColor2
			}, { queue:false, duration:medTime });
		},function(){
			jQuery(this).animate({
				'color':gridColor1
			}, { queue:false, duration:medTime });
		}
	)
	
	//gestion .biglist rollover
	jQuery('#content.bigList .post').each(function(){
		//shadow(jQuery('a.thumbnails',this));
		jQuery(this).hover(
			function(){
				jQuery('a.thumbnails img.shadow',this).css({
					'backgroundColor' : '#FFF'
				}).animate({
					'backgroundColor':gridColor2
				}, { queue:false, duration:medTime });
				jQuery('*',this).animate({
					'color':gridColor2
				}, { queue:false, duration:medTime });
			},function(){
				jQuery('a.thumbnails img.shadow',this).animate({
					'backgroundColor' : '#FFF'
				}, { queue:false, duration:medTime });
				jQuery('*',this).animate({
					'color':gridColor1
				}, { queue:false, duration:medTime });
			}
		)
	});/**/
	
	//gestion .stats
	jQuery('.stats dt').css({
		'margin':'8px 0 0 0'
	})
	jQuery('.stats dd').each(function(){
		var ratio = parseInt(jQuery(this).html());
		var cursor = '<div class="cursor"></div>';
		if(!jQuery(this).hasClass('text')){
			jQuery(this).empty().append(cursor).css({				
				'background':'#CCC'
			})
		}
		jQuery(this).css({
			'height':'14px',
			'margin':'8px 0 0 92px',
			'padding':'0',
			'width':'100px'
		})
		jQuery('div.cursor',this).css({
			'width':'0',
			'height':'100%',
			'background':gridColor2
		}).animate({
			'width':ratio+'px'		
		},600+Math.round(Math.random()*600))
	});
	
	//gestion a[] .jpg
	jQuery("a[href$='.jpg']").each(function(){
		jQuery(this).lightBox({
			overlayBgColor: gridColor2,
			overlayOpacity: 0.8,
			containerBgColor:'#FFF',
			imageLoading: '',
			imageBtnClose: urlTheme + 'images/icons/16-close.gif',
			imageBtnPrev: '',
			imageBtnNext: '',
			containerResizeSpeed: 200,
			txtImage: '',
			txtOf: ''
		});
	});
	
	
	//gestion #gallery
	if(1 < jQuery('#gallery li').length){
		var index = 0;
		var next = 1;
		jQuery('#gallery').css({
			'margin':'32px 0 0 92px',
			'height':'256px',
			'width':'640px',
			'background':gridColor2
		});
		jQuery('#gallery li').css({
			'margin':'0 0 0 0',
			'position':'absolute',
			'top':'0',
			'left':'0',
			'height':'256px',
			'width':'640px'		
		});
		jQuery('#gallery img.medium').css({
			'display' : 'none'
		});
		jQuery('#gallery img.medium.current').css({
			'display' : 'block'
		});
		jQuery('#gallery img.medium').css({
			'margin':'0 0 0 0',
			'position':'absolute',
			'top':'0',
			'left':'0',
			'height':'256px',
			'width':'640px'		
		});
		//shadow(jQuery('#gallery'));
		jQuery('#gallery')
		.hover(function(){
			//console.log('hover : ' + this);
			jQuery('img.shadow',this).css({
				'backgroundColor' : gridColor3
			}).animate({
				'backgroundColor': gridColor2
			}, { queue:false, duration:medTime });			
		}, function(){
			jQuery('img.shadow',this).css({
				'backgroundColor': gridColor2
			}).animate({
				'backgroundColor' : gridColor3
			}, { queue:false, duration:medTime });
		})
		.click(function(){
			nextImage();
			jQuery(this).stopTime('switch');
			jQuery(this).everyTime(hugTime, 'switch', function(){
				nextImage();
			}, 0, false)
		})
		.everyTime(hugTime, 'switch', function(){
			nextImage();
		}, 0, false);
	}
	
	
	function nextImage(){
		jQuery('img.medium','#gallery').eq(index).removeClass("current").fadeOut(maxTime);
		jQuery('img.medium','#gallery').eq(next).addClass("current").fadeIn(maxTime);		
		
		index++;
		next++;
		if(next == 1){
			index=0;
		}
		if(jQuery('img.medium:last','#gallery').hasClass('current')){
			next = 0;
		}		
	}
	
	//function de gestion des ombre portées :
	function shadow(jQueryObj){
		//console.log('fun shadow called for : ' + jQueryObj);
		var wideness = "4px"; //largeur de l'ombre (taille d'un cote de l'image shadow-TL par exemple);
		var imgDir = urlTheme + 'images/';
		var imgName = "4-corner";
		var containerWidth = jQueryObj.outerWidth();
		var containerHeight = jQueryObj.outerHeight();
		var shadowDOM = "<img src='" + imgDir + imgName + "-TL.png' class='shadow' id='TL' />";
		shadowDOM += "<img src='" + imgDir + imgName + "-T.png' class='shadow' id='T' />";
		shadowDOM += "<img src='" + imgDir + imgName + "-TR.png' class='shadow' id='TR' />";
		shadowDOM += "<img src='" + imgDir + imgName + "-R.png' class='shadow' id='R' />";
		shadowDOM += "<img src='" + imgDir + imgName + "-BR.png' class='shadow' id='BR' />";
		shadowDOM += "<img src='" + imgDir + imgName + "-B.png' class='shadow' id='B' />";
		shadowDOM += "<img src='" + imgDir + imgName + "-BL.png' class='shadow' id='BL' />";
		shadowDOM += "<img src='" + imgDir + imgName + "-L.png' class='shadow' id='L' />";
		
		jQueryObj.append(shadowDOM)
		jQuery('.shadow').each(function (i) {
			jQuery(this).css({
				position:'absolute',
				backgroundColor : 'transparent'
			})
			var idTest = jQuery(this).attr("id");
			switch(idTest){
				case 'TL':
					jQuery(this).css({
						top:'-'+wideness,
						left:'-'+wideness,
						width:wideness,
						height:wideness
					});
				break;
				case 'T':
					jQuery(this).css({
						top:'-'+wideness,
						right:0,
						width:containerWidth,
						height:wideness
					});
				break;
				case 'TR':
					jQuery(this).css({
						top:'-'+wideness,
						right:'-'+wideness,
						width:wideness,
						height:wideness
					});
				break;
				case 'R':
					jQuery(this).css({
						top:0,
						right:'-'+wideness,
						height:containerHeight,
						width:wideness
					});
				break;
				case 'BR':
					jQuery(this).css({
						bottom:'-'+wideness,
						right:'-'+wideness,
						width:wideness,
						height:wideness
					});
				break;
				case 'B':
					jQuery(this).css({
						left:0,
						bottom:'-'+wideness,
						width:containerWidth,
						height:wideness
					});
				break;
				case 'BL':
					jQuery(this).css({
						bottom:'-'+wideness,
						left:'-'+wideness,
						width:wideness,
						height:wideness
					});
				break;
				case 'L':
					jQuery(this).css({
						left:'-'+wideness,
						top:0,
						height:containerHeight,
						width:wideness
					});
				break;
			}
		});
	}
	
	
});