/*String.prototype.replaceAll = function(strTarget, strSubString)
{
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	
	while (intIndexOfMatch != -1)
	{
		strText = strText.replace( strTarget, strSubString )
		intIndexOfMatch = strText.indexOf( strTarget );
	}

	return( strText );
}
    
var dwProgressBar = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		container: $$('body')[0],
		boxID:'',
		percentageID:'',                                            
		displayID:'',
		startPercentage: 0,
		displayText: false,
		speed:10
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//create elements
		this.createElements();
	},
	
	//creates the box and percentage elements
	createElements: function() {
		var box = new Element('div', { id:this.options.boxID });
		var perc = new Element('div', { id:this.options.percentageID, 'style':'width:0px;' });
		perc.inject(box);
		box.inject(this.options.container);
		if(this.options.displayText) { 
			var text = new Element('div', { id:this.options.displayID });
			text.inject(this.options.container);
		}
		this.set(this.options.startPercentage);
	},
	
	//calculates width in pixels from percentage
	calculate: function(percentage) {
		return ($(this.options.boxID).getStyle('width').replace('px','') * (percentage / 100)).toInt();
	},
	
	//animates the change in percentage
	animate: function(to) {
		$(this.options.percentageID).set('morph', { duration: this.options.speed, link:'cancel' }).morph({width:this.calculate(to.toInt())});
		if(this.options.displayText) { 
			$(this.options.displayID).set('text', 'Loading : ' + to.toInt() + '%'); 
		}
	},
	
	//sets the percentage from its current state to desired percentage
	set: function(to) {
		this.animate(to);
	}
	
});*/

var diapoffre = new Class({
  initialize: function(src, titre, legende, credit){
	this.src = src;
	this.titre = titre;
	this.legende = legende;
        this.credit = credit;
  }
});

var diapoffre_loading_count = 0;

var startslideshowoffre = function() {
	
	if (currentdiapoffre >= 0) {
		$(_DIAPOFFRE_DIV_ID + currentdiapoffre).fade(1, 0);
	} else {
		currentdiapoffre = 0;
	}
	
	if (currentdiapoffre >= (diapoffre_loading_count-1)) {
		currentdiapoffre = 0;
	} else {
		currentdiapoffre++;
	}
	
	$(_DIAPOFFRE_DIV_ID + currentdiapoffre).getElement('div').setStyle('display', '');
	$(_DIAPOFFRE_DIV_ID + currentdiapoffre).fade(0, 1);
	$$('#'+_DIAPOFFRE_RACINE_ID+' img').setStyle('display', 'block');

}
/*
var loadSettings = function()
{
	var tempSetting = _COOKIE_DEFAULT;

	// Cookie pour les blocs sortables 
	var myCookie = Cookie.read(_COOKIE_NAME);
	
	if (myCookie) {tempSetting = JSON.decode(myCookie);}
	
	//alert("Load : " + JSON.encode(tempSetting));
	
	return tempSetting;
}

var saveSettings = function(mySettings)
{
	//alert("Save & dispose : " + JSON.encode(mySettings) + " _COOKIE_NAME : " + _COOKIE_NAME);
	var myCookie = Cookie.write(_COOKIE_NAME, JSON.encode(mySettings), {duration: 1000, path: "/"});
}

var setColonneStyles = function(id)
{	
	if (!id) {id = 'white';}
	
	var current_style = eval('_COLONNE_OFF_'+id);
	var selected_style = eval('_COLONNE_ON_'+id);
	
	$(_COLONNE_SORTABLE).getElements(_COLONNE_SORTABLE_BLOCK).each(function(element, index) {
		element.setStyles(current_style);
		
		element.removeEvents();
		element.addEvents({
			'mouseover': function(){
				this.setStyles(selected_style);
			},
			
			'mouseleave': function(){
				this.setStyles(current_style);
			},
			
			'click': function(){
				this.setStyles(selected_style);
			}
		});
	});
	
	
}*/
/*
var addCommentEvents = function()
{
	// Comments form control 
	if ($(_COMMENT_FORM_ID)) {
		
		var getColorBack = $(_COMMENT_FORM_ID).getElement('input').getStyles('background-color', 'border-color');
			
		$(_COMMENT_FORM_ID).getElement('input[id=submit]').addEvent('click', function(e) {
			
			var validOk = true;

			$$('#'+_COMMENT_FORM_ID+' input.required', '#'+_COMMENT_FORM_ID+' textarea.required').each(function(el){
				var verifString = ( (el.value.trim() == "") && ( (el.hasClass('ezstring')) || (el.hasClass('eztext')) ) );
				var verifEmail = ( (!el.value.test("^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,4}$")) && (el.hasClass('ezemail')) );
				if (verifString || verifEmail) {
					el.setStyle('background-color', _COMMENT_BACKGROUND_ERROR);
					el.setStyle('border-color', _COMMENT_BORDER_ERROR);

					validOk = false;					
				}
				else {
					el.setStyles(getColorBack);
				}
			});
							
			return validOk;
		});
		
	}
}*/

window.addEvent('domready', function() {  
	
	/*var test = "<div><form action=\"/layout/set/ajax/content/action\" method=\"post\"></div>";
	
	
	
	// progress bar 
	$(_BANDEAU_RACINE_ID).setStyle('display', '');
	
	var progressBar = new dwProgressBar({  
		container: $(_LOADIND_ID),  
		startPercentage: 0,  
		speed:_LOADING_SPEED,  
		boxID: _LOADIND_BOX_ID,  
		percentageID: _LOADIND_PERC_ID,  
		displayID: _LOADIND_TEXT_ID,  
		displayText: true  
	});*/
	
	
	/* preloading */  
	/*var loader = new Asset.images(images_files, { 
		
		onProgress: function(counter,index) {
			//Mise Ã  jour de la barre de progression
			progressBar.set((counter + 1) * (100 / images_files.length));
			
			//CrÃ©ation du bandeau chargÃ©
			var mydiv = new Element('div', {id:_BANDEAU_DIV_ID+counter}).inject($(_BANDEAU_RACINE_ID));
			mydiv.setOpacity(0); //Bandeau invisible par dÃ©faut
			
			//CrÃ©ation de la lÃ©gende
			var mydivtitre = new Element('div',{'class': _BANDEAU_TITRE_ID, 'style':'display:none' }).inject(mydiv);
			mydivtitre.setOpacity(_BANDEAU_TITRE_OPACITY); // OpacitÃ© hors CSS pour validation W3C
			mydivtitre.set({html: '<b>' + tab_bandeaux[counter].titre + '</b><br />' + tab_bandeaux[counter].legende });
			
			//CrÃ©ation de l'image
			var myimg = new Element('img',{ src:tab_bandeaux[counter].src, 'style':'display:none'}).inject(mydiv);
			
			bandeaux_loading_count = counter+1;
			
			if (counter == 0) {
				startslideshow();
				startslideshow.periodical(_SLIDESHOW_TIMING);
			}
		},
		
		onComplete: function() {  
			
		}  
	});*/
        // ########## VAR LOADER modifié pour Weekend-esprit-de-picardie.com #################
              var loaderoffre = new Asset.images(images_filesdiapoffre, {   
              onProgress: function(counteroffre,indexoffre) {
              //Mise à jour de la barre de progression
              //progressBar.set((counter + 1) * (100 / images_files.length));
           
              //Création du conteneur pour le bandeau chargé
              var mydivoffre = new Element('div', {id:_DIAPOFFRE_DIV_ID+counteroffre}).inject($(_DIAPOFFRE_RACINE_ID));
              mydivoffre.setOpacity(0); //Bandeau invisible par défaut
           
              //Création de l'ombre du logo
              var mydivombreoffre = new Element('div',{'class': 'ombre'}).inject(mydivoffre);  
              // Création de la promesse
              //var mydivpromesse = new Element('div',{'class': 'promesse'}).inject(mydiv);  
              //Création de la légende
              //var mydivtitre = new Element('div',{'class': _BANDEAU_TITRE_ID, 'style':'display:none' }).inject(mydiv);
              //mydivtitre.setOpacity(_BANDEAU_TITRE_OPACITY); // Opacité hors CSS pour validation W3C
              //mydivtitre.set({html: '<b>' + tab_bandeaux[counter].titre + '</b><br />' });
           
              //Création de l'image
              var myimgoffre = new Element("img",{ src:tab_diapoffre[counteroffre].src, "style":"display:none", height:200, width:340, alt:tab_diapoffre[counteroffre].legende, title:tab_diapoffre[counteroffre].legende}).inject(mydivoffre);
              var mycreditoffre = new Element('p',{ 'class':'creditsphotos' }).inject(mydivoffre);
              mycreditoffre.set({html: '' + tab_diapoffre[counteroffre].credit + '<br />' });
          
              diapoffre_loading_count = counteroffre + 1;
           
              if (counteroffre == 0) 
              {
                 startslideshowoffre();
                 startslideshowoffre.periodical(_DIAPOFFRE_TIMING);
              }
              }
              });        
	
	/*
	var mySettings = loadSettings();
	
	// Gestion des blocs ordonnÃ©s 
	if (mySettings.sortable) 
        {
		
		arraySort = eval(mySettings.sortable);
		
		var mynewlayer = $(_COLONNE_SORTABLE).clone(false, true);

		var isSortable = true;
		arraySort.each(function(item, index) {
			if ($(item)) {
				var myitem = $(item).clone(true, true).inject(mynewlayer);
			} else
			{
				isSortable = false;
			}
			
		});
		
		if (isSortable) {
			mynewlayer.replaces($(_COLONNE_SORTABLE));
		}
		else {
			mySettings.sortable = null;
			saveSettings(mySettings);
		}
	}
	
	
	// Effets sur les blocs sortables 
	setColonneStyles(mySettings.style);
	
	$$('.style_block').each(function(element, index) 
        {
		element.addEvents({
			'click': function(){
				var css = new Asset.css(element.getElement('span').get('html'), { });
				setColonneStyles(element.get('id'));
				mySettings.style = element.get('id');
				
				saveSettings(mySettings);
			}
		});
	});
	
	// blocs sortables 
	var mySortables = new Sortables(_COLONNE_SORTABLE, {
		opacity: 0,
		clone: true,
		revert: { duration: 1500, transition: 'elastic:out' },
		
		onComplete: function(el) {  		
			mySettings.sortable = JSON.encode(this.serialize(0));
			
			saveSettings(mySettings);
		}
		
	});

	// ToolTips 
	var myTipList = [];
	$$('a', 'li', 'div', 'img').each(function(element, index){
		if (element.getAttribute('title')) {
			var content = element.get('title').split(' // ');
			element.store('tip:title', content[0]);
			element.store('tip:text', content[1]);
			myTipList.push(element);
			
		}
	});
	var myTips = new Tips(myTipList, {hideDelay:0, showDelay: 0, onShow: null, onHide: null});
	
	myTips.addEvents({  
		'show': function(tip) {
			tip.fade(0.9);
		},
		'hide': function(tip) {
			tip.fade('out');
		}
	});
	
	// Comments AJAX context 
	if ($(_COMMENT_FORM_ACTION)) 
        {
		var myform = $(_COMMENT_FORM_ACTION);
		myform.addEvent( 'submit', function(evt) {
				new Event(evt).stop();
				new Request({
					method: myform.get('method'),
					url: _AJAX_LAYOUT+myform.get('action'),
					
					onRequest: function() {
						$(_COMMENT_DIV_ACTION).set('html', '<div id="ajax-loader"><h1>Chargement en cours...</h1><img src='+images_url+'ajax-loader.gif'+'></div>');
					},
					
					onSuccess: function(responseText, responseXML) {
						$(_COMMENT_DIV_ACTION).set('html', responseText.replaceAll(_AJAX_LAYOUT, ''));
						addCommentEvents();
					}
				}).send(this.toQueryString());
		});
	}
	
	// Comments form control 
	addCommentEvents();*/
	
});
 