
jQuery.fn.wait = function (MiliSeconds) {
    $(this).animate({ opacity: '+=0' }, MiliSeconds);
    return this;
};

function Blinker(div, css) {
	
	var self = this;
	var int = 0;
	var rst = 7;
	var col = 'red';
	
	self.blink = function() {
		if (int == 0) {
			int = setInterval(function() {
				if (!rst--) {
					clearInterval(int);
					rst = 7;
					int = 0;
				}
				col = col == 'red' ? 'transparent' : 'red';
				$(div).css(css, col);
			}, 80);
		}
	};
	
}

function caroussel() {
    $('#right ul#caroussel').moodular({
        effects: 'fade',
        auto: true
    });
    $('#right ul#caroussel').css('visibility', 'visible');
}

function menu() {
	$("#nav ul").css({display: "none"});
	$("#nav li").hover(
		function() {
			$(this).find('ul:first').css({visibility: "visible"}).show();
		},
		function() {
			$(this).find('ul:first').css({visibility: "hidden"}).hide();
		}
	);
}

function normalize(str) {
	str = str.replace(/[ÈÉÊËèéêë]/g, "e");
	str = str.replace(/[@ÀÁÂÃÄÅàáâãäå]/g, "a");
	str = str.replace(/[ÌÍÎÏìíîï]/g, "i");
	str = str.replace(/[ÙÚÛÜùúûü]/g, "u");
	str = str.replace(/[ÒÓÔÕÖØòóôõöø]/g, "o");
	str = str.replace(/[Çç]/g, "c");
	str = str.toLowerCase();
	str = str.replace(/[\s\'\"-_]+/g, "_");
	str = str.replace(/[^a-z0-9-_]/gi, '');
	str = str.replace(/[-_]+$/gi, '');
	str = str.replace(/^[-_]+/gi, '');
	return str;
}

function bandeau() {
	AC_FL_RunContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
		'width', '598',
		'height', '378',
		'src', 'flash/bando_543',
		'quality', 'high',
		'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		'align', 'middle',
		'play', 'true',
		'loop', 'true',
		'scale', 'showall',
		'wmode', 'transparent',
		'devicefont', 'false',
		'id', 'bandeau',
		'name', 'bando_543',
		'menu', 'false',
		'allowFullScreen', 'false',
		'allowScriptAccess','sameDomain',
		'movie', 'flash/bando_543',
		'salign', ''
	);
}

function Slates(s, w) {
	
	var self = this;
	var slates = s;
	var wanted = w;
	var bought = 0;
	var percent = Math.floor((slates/wanted) * 1000);
	var blink = new Blinker('#validate', 'color');
	
	self.init = function() {
		$('#roof').width(percent);
		$('.slate').draggable({
			cursor: 'move',
			drag: function() {$('#messagebox, #arrow').fadeOut('slow');},
			revert: true
		}).dblclick(function() {
			if (self.addSlate())
				$(this).hide();
		});
		$('#castel').droppable({
			//hoverClass: 'roofing',
			drop: self.dragSlate
		});
	};

	self.compute = function(callback) {
		$('#roof').slideHorizontal(
			(5 * percent) + 500,
			function() {self.setSlates(Math.ceil(($(this).width()/1000) * wanted));},
			function() {self.setSlates(slates); 
            	try {callback();} catch(e) {}}
		);
	};
	
	self.addSlate = function() {
		if (slates == wanted) return false;
		self.setSlates(++slates);
		$('#number').text(++bought);
		$('input[name=quantity]').val(bought);
		$('#roof').width(Math.floor((slates/wanted) * 1000));
		blink.blink();
		//setTimeout(function() {$('#validate').css('border-color', 'gray');}, 100);
		return true;
	};

	self.setSlates = function(num) {
		$('#current').html(num >= wanted ? wanted : num);
		$('#objective').html(wanted - num);
	};

	self.dragSlate = function(event, ui) {
		if (!self.addSlate()) return false;
		ui.draggable.hide();
		return true;
	};
    
    $(self.init);
	
}

function Loader(s, callback) {

    var self = this;
    var cpt = -1;
    var ie = $.browser.msie;
    var slates = s;
    var loaded = false;
    var call = callback;
    
    self.init = function() {
        if (!ie) {
            cpt = $('img.cache').length;
            $('img.cache').load(self.loadCache);
        }
        else {
            self.loadCache();
        }
    };
    
    self.loadCache = function() {
        if (!loaded) {
            if (ie || --cpt <= 0 || $(this).attr('id') == undefined) {
                loaded = true;
            	$('#counter, #donateurs').css('opacity', 0.8);
                $('#castel').css('visibility', 'visible');
                $('#heap').fadeIn('fast');
                $('#load').fadeOut('normal', function() {
                    slates.compute(call);
                });
            }
        }
    };
    
    $(self.init);
    
}

