/**
 * main script for www.chateaudeboudry.ch
 * author : amin ladhani, 2010, amin@as-o.ch
 * toutes les fonctions et méthodes de ce script sont libres d'utilisation
 * amin ladhani
 */
var 	site = {
		init : function(){
			info.init();
			new showImage('contenu');
			document._getById('copy').onmousedown = function(){
				if(!document.getElementById('__cms') 
				&& (!window._browser().isIE ||window._browser().isIE9)){
					new connectBox();
				}
			};
		}
	};

// --------------------------------------------------------------------------- INFO
var 	info = {
		init : function(){
			this.infoContainer = document._getById('infoWrapper');
			this.searchButton = document._getById('searchButton');
			this.mapButton = document._getById('mapButton');
			this.btArray = [this.searchButton,this.mapButton];
			this.btFunction = [this.search,this.map];
			this.containerHeight = 350;
			this.innerContainerBorder = 1;
			this.innerContainerMargin = 2;
			this.isOpen = false;
			this.initButton();
		},
		initButton : function(){
			var bt = this.btArray, _this = this;
			bt._each(function(itm,i){
				itm.onclick = function(){return false;};
				itm.action = _this.btFunction[i];
				itm._addEvent('mousedown',function(){
					var _self = window._browser().isIE ? event.srcElement : this ;
					_this.setContainer(_self);
				});
			});
			this.createContainer();
		},
		createContainer : function(){
			var m = window._browser().isIE7 ? 10 : 0 ;
			this.container = new _element('div')
				._setClass('infoWrapperOut')
				._setAttributes({'id':'infoBox'})
				._setStyle({
					'position':'absolute',
					'z-index':'10',
					'top':(this.infoContainer.offsetTop+this.infoContainer.offsetHeight-2)+'px',
					'left':(this.infoContainer.offsetLeft-m)+'px',
					'width':(this.infoContainer.offsetWidth-2)+'px',
					'height':'1px',
					'background-color':'#111',
					'background-color':'#000',
					'border':'solid 1px #000',
					'overflow':'hidden',
					'display':'none'
				})._appendTo(document._getById('wrapperIn'));
			this.innerContainer = new _element('div')
				._setClass('infoWrapperIn')
				._setStyle({
					'background-color':'#111',
					'border':'solid '+info.innerContainerBorder+'px #222',
					'margin':info.innerContainerMargin+'px',
					'height':(this.containerHeight-((info.innerContainerBorder*2)
						+(info.innerContainerMargin*2)))+'px',
					//'padding':'10px',
					'overflow':'auto'
				})._appendTo(this.container);
		}, 
		setContainer : function(current){
			var bt = this.btArray, _this = this, currCls = current.className;
			for(var i=0;i<bt.length;i++) bt[i]._removeClass('select');
			if(currCls=='select'){
				current._removeClass('select');
				this.close();
			}else{
				current._setClass('select');
				if(!this.isOpen)
					this.open(current);
				else
					current.action();
			}
		},
		setLoader : function(){
			info.innerContainer._setHtml('');
			var loader = new _element('div')
				._setStyle({
					'position':'absolute',
					'top':'50%',
					'left':'50%',
					'width':'32px',
					'height':'32px',
					'margin-top':'-16px',
					'margin-left':'-16px',
					'background-image':'url(__layout/ajax-loader2.gif)'
				})._appendTo(info.innerContainer);
		},
		open : function(current){
			var trans = new _basicFx.transition(this.container,{
				duration:200,
				onComplete:function(){current.action();}
			}).start(['height',0,info.containerHeight]);
			this.container.style.display = '';
			this.isOpen = true;
		},
		close : function(){
			var _this = this;
			var trans = new _basicFx.transition(this.container,{
				duration:200,
				onComplete:function(){
					_this.innerContainer._setHtml('');
					_this.container.style.display = 'none';
					document.onkeypress = function(){};
				}
			}).start(['height',info.containerHeight,0]);
			this.isOpen = false;
		},
		search : function(){
			new _request.get({
				'file':'main.xhr.php',
				'param':'?type=search_form',
				'onSend':function(){ info.setLoader(); },
				'onReceive':function(){
					info.innerContainer._setHtml(this.responseText);
					info.setSearchAction();
				}
			});
		},
		map : function(){
			document.onkeypress = function(){};
			new _request.get({
				'file':'main.xhr.php',
				'param':'?type=map',
				'onSend':function(){ info.setLoader(); },
				'onReceive':function(){
					info.innerContainer._setHtml(this.responseText);
				}
			});
		},
		setSearchAction  : function(){
			var	sInput = document._getById('searchInputText'),
				sSubmitButton = document._getById('searchSubmitButton'),
				sDisplay = document._getById('infoSearchDisplayResult'),
				getResult = function(){
					new _request.get({
						'file':'main.xhr.php',
						'param':'?keyword='+sInput.value,
						'onSend':function(){},
						'onReceive':function(){
							sDisplay._setHtml('');
							if(this.responseText){
								var ul = new _element('ul')
									._appendTo(sDisplay)
									._setHtml(this.responseText);
							}
						}
					});
				};
			sSubmitButton._addEvent('mousedown',function(){getResult();});
			document.onkeypress = function(e){
				if(e.which==13 || e.keycode==13) getResult();
			};
		},
		getSearchResult : function(){
		
		}
	};
	
// --------------------------------------------------------------------------- CONNECTBOX
var 	connectBox = function(){
		var _this = this;
		this.plane = new _element('div')
			._addClass('__connectPlane')
			._appendTo(document.body);
		this.container = new _element('div')
			._addClass('__connectBox')
			._appendTo(document.body);
		this.title = new _element('h2')
			._addClass('__title')
			._setHtml('<h2>CMS identification</h2>')
			._appendTo(this.container);
		this.p1 = new _element('p')
			._setHtml('<label>Utilisateur</label>')
			._appendTo(this.container);
		this.p2 = new _element('p')
			._setHtml('<label>Mot de passe</label>')
			._appendTo(this.container);
		this.p3 = new _element('p')
			._addClass('__buttonsWrapper')
			._appendTo(this.container);
		this.inputId = new _element('input')
			._setAttributes({'type':'text','id':'usr','name':'usr'})
			._appendTo(this.p1);
		this.inputPsw = new _element('input')
			._setAttributes({'type':'password','id':'psw','name':'psw'})
			._appendTo(this.p2);
		this.submitButton = new _element('input')
			._setAttributes({'type':'button','value':'ok','name':'connectButton'})
			._setStyle({'float':'right'})
			._addEvent('click',_this.send)
			._implement({'usr':this.inputId,'psw':this.inputPsw,'container':this.p3})
			._appendTo(this.p3);
		this.cancelButton = new _element('input')
			._setAttributes({'type':'button','value':'annuler','name':'connectButton'})
			._addEvent('mousedown',function(){
				_this.plane._removeElement();
				_this.container._removeElement();
			})._appendTo(this.p3);
		this.container._setStyle({
			'margin-left':'-'+(this.container.offsetWidth/2)+'px',
			'margin-top':'-'+(this.container.offsetHeight/2)+'px'
		});
		this.inputId.focus();
	};
	connectBox.prototype.send = function(){
		var 	cnt = this.container,
			usr = this.usr.value,
			psw = this.psw.value;
		if(!document.getElementById('__info'))
			var info = new _element('p')
				._setAttributes({'id':'__info'})
				._addClass('__info')
				._setHtml('connect...')
				._appendBefore(cnt);
		_request.post({
			file : '__php/conn.xhr.php',
			param : 'usr='+usr+'&psw='+psw,
			onSend : function(){},
			onReceive : function(){
				if(this.responseText=='1') location.reload();
				else
					document.getElementById('__info')
					._addClass('__warning')
					._setHtml('Utilisateur ou mot de passe incorrect.');
			}
		});
	};

// --------------------------------------------------------------------------- SHOWIMAGE
var 	showImage = function(container){
		if(!container) return false;
		this.imageArray = [];
		this.container = typeof container == 'string' 
			? document._getById(container) 
			: container ;
		if(!this.container) return false;
		this.pic = this.container._getByTagName('img');
		this.setAction();
	};
	showImage.prototype.setAction = function(){
		var 	im = this.pic,
			_this = this;
		im._each(function(img){
			if(img.className.match('inserted_image')){
				img['target'] = img.getAttribute('src')
					.replace('http://www.chateaudeboudry.ch/','')
					.replace('http://chateaudeboudry.ch/','')
					.replace('http://as-o.ch/sites/chateaudeboudry/','')
					.replace('http://www.as-o.ch/sites/chateaudeboudry/','')
					.replace(/mini_/,'');
				_this.imageArray.push(img);
				img._addEvent('mousedown',function(){
					var _self = window._browser().isIE ? event.srcElement : this ;
					_this.createImageWrapper();
					_this.setCurrent(_self);
					_this.getImageSize(_self.target);
				});
			}
		});
	};
	showImage.prototype.setCurrent = function(current){
		for(var i=0;i<this.imageArray.length;i++)
			if(this.imageArray[i]==current) 
				this.current = i;
	};
	showImage.prototype.createImageWrapper = function(){
		var _this = this;
		this.plane = new _element('div')
			._setAttributes({'title':'cliquer pour fermer'})
			._setClass('popImageBackgroundPlane')
			._addEvent('mousedown',function(){_this.removeImage();})
			._appendTo(document.body);
		this.wrapper = new _element('div')
			._setClass('popImageImageWrapper')
			._appendTo(document.body);
		this.loader = new _element('div')
			._setAttributes({'id':'__ajax_loader'})
			._setClass('popImageLoader')
			._appendTo(document.body);
		if(window._browser().isIE6){
			var stl = {'top':(document.documentElement.scrollTop+(Math.round(window._getWindowSize().y/2)))+'px'};
			this.plane._setStyle({'opacity':'0.8'});
			this.wrapper._setStyle(stl);
			this.loader._setStyle(stl);
		}
	};
	showImage.prototype.getImageSize = function(file){
		_this = this;
		new _request.get({
			'file':'__php/xhr.getimagesize.php',
			'param' : '?file='+file,
			'onSend' : function(){},
			'onReceive' : function(){
				_this.setImage(_jsonParse(this.responseText),file);
			}
		}); 
	};
	showImage.prototype.resizeToWindow = function(size){
		var	ws = window._getWindowSize(),
			w = parseInt(size.width),
			h = parseInt(size.height),
			h1,r1,w1;
		if(parseInt(h)>parseInt(ws.y)){
			h1 = parseInt(ws.y);
			r1 = h/parseInt(ws.y);
			w1 = Math.round(w/r1);
			if(w1>parseInt(ws.x)){
				w1 = ws.x;
				r1 = w1/parseInt(ws.x);
				h1 = Math.round(h1/r1);
			}
		}
		if(parseInt(w)>parseInt(ws.x)){
			w1 = w1 ? w1 : parseInt(ws.x);
			r1 = w/parseInt(ws.x);
			h1 = Math.round(h/r1);
			if(h1>parseInt(ws.y)){
				h1 = ws.y;
				r1 = h1/parseInt(ws.y);
				w1 = Math.round(w1/r1);
			}
		}
		return {'height':(h1 ? h1-50 : h),'width':(w1 ? w1-50 : w)};
	};
	showImage.prototype.setImage = function(rsize,image){
		var 	_this = this,
			el = this.wrapper,
			size = this.resizeToWindow({'width':rsize['width'],'height':rsize['height']}),
			marginLeft = parseInt(el._getStyle('margin-left')),
			marginTop = parseInt(el._getStyle('margin-top')),
			newMarginLeft = '-'+Math.floor(size.width/2),
			newMarginTop = '-'+Math.floor(size.height/2),
			transH = new _basicFx.transition.sineInOut(this.wrapper,{onComplete:function(){
				_this.img = new _element('img')
					._setAttributes({'title':'cliquer pour fermer'})
					._setClass('popImage')
					._setStyle({
						'display':'none',
						'width':size.width+'px',
						'height':size.height+'px'})
					._setAttributes({'src':image})
					._addEvent('mousedown',function(){_this.removeImage();})
					._appendTo(el);
				if(rsize.legend){
					var legend = new _element('div')
						._setAttributes({'id':'__image_legend'})
						._setClass('popImageLegend')
						._setHtml(rsize.legend)
						._appendTo(el);
					var closeLegend = new _element('div')
						._setAttributes({'title':'masquer la légende'})
						._setHtml('x')
						._setClass('popImageLegendClose')
						._addEvent('mousedown',function(){
							legend._removeElement();})
						._appendTo(legend);
				}
				_this.isLoaded(_this.img);
			}}),
			transW = new _basicFx.transition.sineInOut(this.wrapper,{onComplete:function(){
				transH.start(
					['height',el.offsetHeight,size.height],
					['margin-top',marginTop,newMarginTop]
				);
			}});
			transW.start(
				['width',el.offsetWidth,size.width],
				['margin-left',marginLeft,newMarginLeft]
			);
	};
	showImage.prototype.setNavigButton = function(){
		if(this.imageArray.length<2) return false;
		var _this = this;
		this.next = new _element('div')
			._setAttributes({'title':'image suivante'})
			._setClass('popImageNavigButton')
			._setStyle({'right':'5px'})
			._setHtml('&raquo;')
			._addEvent('click',function(){_this.nextImage();})
			._appendTo(this.wrapper);
		this.previous = new _element('div')
			._setAttributes({'title':'image précédante'})
			._setClass('popImageNavigButton')
			._setStyle({'left':'5px'})
			._setHtml('&laquo;')
			._addEvent('click',function(){_this.previousImage();})
			._appendTo(this.wrapper);
	};
	showImage.prototype.isLoaded = function(image){
		var trans = new _basicFx.transition(image);
		if(image.complete){
			image._setStyle({'display':'','opacity':'0'});
			document._getById('__ajax_loader')._setStyle({'display':'none'});
			trans.start(['opacity',0,100]);
			_this.setNavigButton();
		}else{
			image.onload = function(){
				document._getById('__ajax_loader')._setStyle({'display':'none'});
				image._setStyle({'display':'','opacity':'0'});
				trans.start(['opacity',0,100]);
				_this.setNavigButton();
			}
		}
	};
	showImage.prototype.nextImage = function(){
		var prv = this.imageArray[this.current+1] 
			? this.current+1 
			: 0 ;
		this.navigButtonAction(prv);
	};
	showImage.prototype.previousImage = function(){
		var prv = this.imageArray[this.current-1] 
			? this.current-1 
			: this.imageArray.length-1;
		this.navigButtonAction(prv);
	};
	showImage.prototype.navigButtonAction = function(i){
		this.current = i;
		this.img._removeElement();
		this.next._removeElement();
		this.previous._removeElement();
		if(document.getElementById('__image_legend')) 
			document.getElementById('__image_legend')._removeElement();
		this.getImageSize(_this.imageArray[i].target);
		document._getById('__ajax_loader')._setStyle({'display':''});
	};
	showImage.prototype.removeImage = function(){
		this.plane._removeElement();
		this.wrapper._removeElement();
		this.loader._removeElement();
	};
