var timer;


function initAll(){
	var par = new Parallel();
	var seq = new Sequence();
	var l1 = new Tween(document.getElementById('prLogo').style,'top',Tween.strongEaseOut,-350,0,1,'px');
	var l2 = new OpacityTween(document.getElementById('prLogo'),Tween.strongEaseOut,0,100,1);
	var b1 = new OpacityTween(document.getElementById('bar'),Tween.strongEaseOut,0,100,1);
	par.addChild(l1);
	par.addChild(l2);
	seq.addChild(par);
	seq.addChild(b1);
	seq.start();
	seq.onMotionFinished = function() {
		getTheImages();
		(document.all) ? timer = setTimeout('MSfix()', 6000) : timer=null;
	}
}

function Preloader(){ 
	this.images = [];
	this.imagesSrc = [];
	this.divFactor = null;
	this.preload = function() {
		var x = this.divFactor;
		for(i=0;i<this.imagesSrc.length;i++){ 
			this.images[i] = new Image();
			this.images[i].onload = function(){ progressBar(x);};
			this.images[i].src = this.imagesSrc[i];
			if(document.all){	//fix for IE cashing issue
				this.images[i].style.position = 'absolute';
				this.images[i].style.left = '-800px';
				document.getElementsByTagName('body')[0].appendChild(this.images[i]);
			}
		}
	}
}



function progressBar(x){
	var y = document.getElementById('progress').parentNode.offsetWidth - 8; // - 5 in case it's a couple of pixels short
	var preloaderWidth = parseInt(document.getElementById('progress').offsetWidth);
	if( preloaderWidth  >= y) {
		(document.all) ? clearTimeout(timer) : timer=null;
		var preloader = document.getElementById('preLoader');
		preloader.parentNode.removeChild(preloader);
		initPages();
	} else {
	(document.all) ? clearTimeout(timer) : timer=null;

	var exp = parseFloat(y / x);
	var y = document.getElementById('progress').offsetWidth; 
	document.getElementById('progress').style.width = exp + parseInt(y) + 'px';
	
	(document.all) ? timer = setTimeout('MSfix()', 4000) : timer=null;
	}
}

function MSfix() {
	(document.all) ? clearTimeout(timer) : timer=null;
	var w = document.getElementById('progress').offsetWidth;
	var l1 = new Tween(document.getElementById('progress').style,'width',Tween.strongEaseOut,w,163,1,'px');
	l1.start();
	l1.onMotionFinished = function() {
		var preloader = document.getElementById('preLoader');
		preloader.parentNode.removeChild(preloader);
		initPages();
	}
}

function testPl(){
	cachedGallery = xmldata;
	var allImgs = [];
	var thumbs = xmldata.getElementsByTagName('thumbnail');
	var images = xmldata.getElementsByTagName('img');
	//alert(allImgs);
	for(i=0;i<thumbs.length;i++){
		allImgs.push("images/gallery/new/" + thumbs[i].getAttribute('src'));
	}
	for(j=0;j<images.length;j++){
		allImgs.push("images/gallery/new/" + images[j].getAttribute('src'));
	}
	var tst = new Preloader(tst);
	tst.imagesSrc = allImgs;
	tst.divFactor = tst.imagesSrc.length - 1;
	tst.preload();
}


function getTheImages(){
	getData("images/gallery/gallery.xml",testPl, 'responseXML'); 
}

window.onload = initAll;
