function getById(el) {
	return document.getElementById(el);
}

function getByTag(par,el) {
	var par = (par == '') ? document.body : par;
	return par.getElementsByTagName(el);
}

function addClass(obj,newClass) {
	if(!obj.className.match(new RegExp(newClass)))
		obj.className+=(obj.className.length>0? " ": "") + newClass;
}
	
function removeClass(obj,oldClass) {
	obj.className=obj.className.replace(new RegExp("( ?|^)"+oldClass+"\\b"), "");
}

function setLinks(linkTags){
	var linkCollection = [];
	for (var j = 0; linkTags[j]; j++) {
		linkCollection[j] = getByTag('',linkTags[j]);
		for (var i = 0; linkCollection[j][i]; i++) {
			if(/bookmark|external|corporate|sponsor/.test(linkCollection[j][i].getAttribute('rel'))) {
				linkCollection[j][i].onclick = function(e) {
					if (!e) var e = window.event;
					if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey)
						return true;
					window.open(this.href);
					return false;
				}
			}
		}
	}
}

var addEvent = function() {
  if (window.addEventListener) {
    return function(el, type, fn) {
      el.addEventListener(type, fn, false);
    };
  } else if (window.attachEvent) {
    return function(el, type, fn) {
      var f = function() {
        fn.call(el, window.event);
      };
      el.attachEvent('on' + type, f);
    };
  }
}();

function swapImg(obj) {
	var tgt = obj.href;
	var db = getById('displayBox');
	var old = getByTag(db,'img')[0];
	var img = document.createElement('img');
	img.src = tgt;
	var desc = getByTag(obj.parentNode,'div')[0];
	var inf = getById('info');
	if(inf && desc && desc.innerHTML)
		inf.innerHTML = desc.innerHTML;
	else
		inf.innerHTML = '';
	if(old)
		db.replaceChild(img,old);
	else
		db.appendChild(img);
}

// function getByClass(cla,par,el) {
// 	var par = (par == '') ? document.body : par;
// 	var el = (el == '') ? '*' : el;
// 	if(document.getElementsByClassName) {
// 		return par.getElementsByClassName(cla);
// 	}
// 	else {
// 		var tagColl = par.getElementsByTagName(el);
// 		trimedColl = new Array;
// 		for (var i = 0; tagColl[i]; i++) {
// 			if(tagColl[i].className.match(new RegExp(cla))) {
// 				trimedColl[trimedColl.length]=tagColl[i];
// 			}
// 		}
// 		return trimedColl;
// 	}
// }


function setScroller() {
	var sb = getById('scrollerBox');
	var si = getById('scrollerItems');
	var db = getById('displayBox');
	var inf = getById('info');
	if(!sb || !si || !db || !inf) return;
	
	// var testColl = getByClass('item',si,'div');

	
	if(document.getElementsByClassName) {
		var siColl = si.getElementsByClassName('item');
	}
	else {
		si = si.getElementsByTagName('div');
		siColl = [];
		for (var i = 0; si[i]; i++) {
			if(/item/.test(si[i].className)) {
				siColl[siColl.length]=si[i];
			}
		}
	}
	
	var sbW = sb.offsetWidth;
	var siW = 0;
	for(var i = 0; siColl[i]; i++) {
		var a = getByTag(siColl[i],'a');
		if(a && a[0]) {
			a[0].onclick = function () {
				swapImg(this);
				return false;
			}
		}
		siW = siW + siColl[i].offsetWidth + 15;
	}
	siColl[siColl.length-1].style.marginRight = 0;
	siW = siW - 15;
	getById('scrollerItems').style.width = siW+'px';
	
	if(siW > sbW) {
		addClass(sb,'scrollable');
		var d = document.createElement('div');
		d.setAttribute('id','scrollBar');
		var s = document.createElement('div');
		s.setAttribute('id','scroller');
		d.appendChild(s);
		sb.appendChild(d);
		var itemScroller = new ScrollItems("scroller","scrollerItems");
	}

}


function ScrollItems(id,box) {
	this.scrl = document.getElementById(id);
	this.box = document.getElementById(box);
	this.interstice = this.box.parentNode;
	this.w = this.scrl.offsetWidth;
	this.bW = this.box.offsetWidth;
	this.prt = this.scrl.parentNode;
	this.prtX = findPosition(this.prt)[0];
	this.prtW = this.prt.offsetWidth;
	this.prtZ = this.prtX + this.prtW;
	this.ratio =  (this.bW - this.prtW) / (this.prtW - this.w);
	this.state = null;
    this.prevX = null;
	this.curOffset = null;
	this.scrolled = null;
	this.makeDraggable();
}

ScrollItems.prototype.makeDraggable = function () {
	this.scrl.onmousedown = function (that) {
        return function (e) {
            that.mousedown(e);
 			return false;
        }
    }(this);
	document.onmousemove = function (that) {
        return function (e) {
            that.mousemove(e);
 			return false;
        }
    }(this);
    document.onmouseup = function (that) {
        return function (e) {
            that.mouseup(e);
        }
    }(this);
	// this.box.onmousewheel = function (that) {
	//         return function (e) {
	//             that.mousewheel(e);
	//         }
	//     }(this);
	this.interstice.onscroll = function (that) {
        return function (e) {
            that.scrollH(e);
        }
    }(this);
}

// ScrollItems.prototype.mousewheel = function (e) {
// 	e = e ? e : window.event;
// 	this.scrl.style.left = (this.box.parentNode.scrollLeft / this.ratio) + 'px';
// 
// }

ScrollItems.prototype.scrollH = function (e) {
	e = e ? e : window.event;
	this.scrl.style.left = ((this.interstice.scrollLeft - this.box.offsetLeft) / this.ratio) + 'px';
	//this.prevX = this.scrl.offsetLeft;
	this.curOffset = this.w/2;
}

ScrollItems.prototype.mousedown = function (e) {
	e = e ? e : window.event;
	var x = e.clientX ? e.clientX : e.pageX;
	if(!this.curOffset) {
		this.curOffset = x - this.prtX;
	}
	else if(this.prevX) {
		this.curOffset = x - (this.prtX + this.prevX);
	}
	this.state = "drag";
	addClass(this.scrl,'scrolling');
}

ScrollItems.prototype.mousemove = function (e) {
	e = e ? e : window.event;
	var x = e.clientX ? e.clientX : e.pageX;
	if(this.state === "drag") {
		if( (x > this.prtX) && (x < this.prtZ)) {
			var scrollerX = (x - this.curOffset) - this.prtX;
			var scrollItemsX = (this.prtX - (x - this.curOffset)) * this.ratio;
			if( (scrollerX > -1) && ( (x - this.curOffset) < (this.prtZ - this.w) )) {
				this.scrl.style.left = scrollerX + 'px';
				//this.box.style.left = scrollItemsX + 'px';
				this.interstice.scrollLeft = - scrollItemsX;
				//this.interstice.scrollTop = -100;
				//document.getElementById("scrollInterstice").scrollTo(100,100);
				
			}
			else if(scrollerX < 0) {
				this.scrl.style.left = 0 + 'px';
				//this.box.style.left = 0 + 'px';
				this.interstice.scrollLeft = 0;
			}
			else if (scrollerX > (this.prtW - this.w)) {
				this.scrl.style.left = (this.prtW - this.w) + 'px';
				//this.box.style.left = (this.prtW - this.bW) + 'px';
				this.interstice.scrollLeft = - (this.prtW - this.bW);
			}
		}
	}
}

ScrollItems.prototype.mouseup = function (e) {
	e = e ? e : window.event;
	this.state = '';
	this.prevX = this.scrl.offsetLeft;
	removeClass(this.scrl,'scrolling');
}


function fixIE() {
	var h1Coll = getByTag(getById('content'),'h1');
	for(var i = 0; h1Coll[i]; i++) {
		var st = getByTag(h1Coll[i],'strong')[0];
		var sp = getByTag(h1Coll[i],'span')[0];
		if(st)
			st.innerHTML = '> ' + st.innerHTML;
		if(sp)
			sp.innerHTML = '> ' + sp.innerHTML;
	}
	var h2Coll = getByTag(getById('content'),'h2');
	for(var i = 0; h2Coll[i]; i++) {
		if(/expositions|news/.test(h2Coll[i].parentNode.className))
			h2Coll[i].innerHTML = '> ' + h2Coll[i].innerHTML;
	}
	var pColl = getByTag(getById('content'),'p');
	for(var i = 0; pColl[i]; i++) {
		if(/extra/.test(pColl[i].className) && getByTag(pColl[i],'a')[0]) {
			var a = getByTag(pColl[i],'a')[0];
			a.innerHTML = '> ' + a.innerHTML;
		}
	}
	var liColl = getByTag(getById('footer'),'li');
	for(var i = 1; liColl[i]; i++) {
		liColl[i].innerHTML = ' | ' + liColl[i].innerHTML;
	}
}

function toggleColl() {
	var coll = getById('collection');
	if(/collOpen/.test(coll.className))
		removeClass(coll,'collOpen');
	else
		addClass(coll,'collOpen');
}

function setCollectionForm() {
	var collCall = getById('collectionCall');
	if(!collCall) return;
	var collForm = getByTag(getById('collection'),'form')[0];
	if(!collForm) return;
	if(!/oeuvres/.test(getById('page').className) ) {
		collCall.onclick = function () {
			toggleColl();
			return false;
		}
	}
}

function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}


function fixLowIE() {
	
}

/* Init
-------------------- */
var init = function() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if(!document.getElementsByTagName)
		return;
	addClass(getById('page'),'scripted');
	setLinks(['a','area']);
	setCollectionForm();
	// setTimeout for safari
	setTimeout("setScroller();",100);
	/*@cc_on @*/
	/*@if (@_win32)
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) {
			rv = parseFloat( RegExp.$1 );
			if(rv < 7) fixLowIE();
			if(rv < 8) fixIE();
		}
	/*@end @*/
}


if (/WebKit/i.test(navigator.userAgent)) {
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			clearInterval(_timer);
			init();
		}
	}, 10);
}
else if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, null);
}
else {
	/*@cc_on @*/
	/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init();
		}
	};
	/*@end @*/
}
window.onload = init;
