/**
 * ...
 * @author Matthieu Chavigny
 */

$(function() {
	// wallpaper selector
	var wallpaperPath= "/img/rvb/wallpaper/";
	var wallpaperList = [
	{url:"grey1.jpg", repeat:""},
	{url:"denim-daisies-2.png", repeat:""},
	{url:"charcoal.jpg", repeat:""},
	{url:"cuir.png", repeat:""},
	{url:"WP_SO_Ukr0123.png", repeat:""}
	];
	var wallpaperIndex = 0;
	$("body").append('<div id="wpselector"><div class="thumb"><img src="" alt=""/></div><img src="/img/rvb/reload.png" alt="" class="reload"/><img src="/img/rvb/corner.png" alt="" class="corner"/><img src="/img/rvb/wallpaper.png" alt="" class="wallpaper"/></div>');
	$("#wpselector").css("position", "fixed");
	$("#wpselector").css("z-index", 50);
	$("#wpselector").css("cursor", "pointer");
	$("#wpselector img.reload, #wpselector img.corner, #wpselector img.wallpaper").css("position", "fixed");
	$("#wpselector img.reload, #wpselector img.corner, #wpselector img.wallpaper").css("top", 0);
	$("#wpselector img.reload, #wpselector img.corner, #wpselector img.wallpaper").css("left", 0);
	$("#wpselector div.thumb").css("width", 32);
	$("#wpselector div.thumb").css("height", 32);
	$("#wpselector div.thumb").css("overflow", "hidden");
	$("#wpselector div.thumb").css("position", "fixed");
	$("#wpselector div.thumb").css("top", 0);
	$("#wpselector div.thumb").css("left", 0);
	$("#wpselector div.thumb img").css("position", "absolute");
	$("#wpselector div.thumb img").css("top", 0);
	$("#wpselector div.thumb img").css("left", 0);
	$("#wpselector div.thumb img").attr("src", wallpaperPath+wallpaperList[0].url);
	$("#wpselector img.corner").css("width", 45);
	$("#wpselector img.corner").css("height", 48);
	$("#wpselector img.wallpaper").css("opacity", 0);
	
	$("#wpselector").mouseenter(function() {
		$("#wpselector img.corner").animate({width:110, height:117}, 200);
		$("#wpselector div.thumb").animate({width:82, height:79}, 200);
		$("#wpselector img.wallpaper").delay(50).animate({margin:7, opacity:1}, 150);
		$("#wpselector img.reload").animate({margin:2}, 200);
	});
	$("#wpselector").mouseleave(function() {
		$("#wpselector img.corner").animate({width:45, height:48}, 100);
		$("#wpselector div.thumb").animate({width:32, height:32}, 100);
		$("#wpselector img.wallpaper").animate({margin:0, opacity:0}, 150);
		$("#wpselector img.reload").animate({margin:0}, 200);
	});
	$("#wpselector").click(function() {
		createCookie("ts_wallpaper",wallpaperIndex,100);
		selectWp();
	});
	if(readCookie("ts_wallpaper")!=null) {
		wallpaperIndex = readCookie("ts_wallpaper");
		if(wallpaperIndex>wallpaperList.length-1) wallpaperIndex = 0;
		selectWp();
	}
	function selectWp() {
		$("html").css("background", "url('"+wallpaperPath+wallpaperList[wallpaperIndex].url+"') "+wallpaperList[wallpaperIndex].repeat);
		wallpaperIndex++;
		if(wallpaperIndex==wallpaperList.length) wallpaperIndex=0;
		$("#wpselector div.thumb img").attr("src", wallpaperPath+wallpaperList[wallpaperIndex].url);
	}
});
