var active_col="thumbs_1";  // show first set of pictures by default
var started = false;  // not loaded a picture yet
var curl = ""; // no current collection url
var purl = ""; // no current preview url
var z=0;  // zoomed out be default
var scrolling=0;  // not scrolling by default
var preloadimages = ""; // don't preload any images by default

function zoomcursor() {
	document.getElementById('main_pic_container').style.cursor = "-moz-zoom-in, pointer";
	if (document.getElementById('main_pic_container').style.cursor != "-moz-zoom-in, pointer") {
		document.getElementById('main_pic_container').style.cursor = "pointer";
		document.getElementById('main_pic_container').style.cursor = "-moz-zoom-in";
	}
}

function pageload() {
	if (typeof(window.autoload_image) == 'function') {
		mouse();
		document.onmousemove = mouse;
		document.getElementById('main_pic_container').onmouseover = startscrolling;
		document.getElementById('main_pic_container').onmouseout = stopscrolling;
		document.getElementById('main_pic_container').onclick = zoom;
		preloadimagescontent = "";
		for(i=0;i<preloadimages.length;i++) {
			preloadimagescontent += "<img src=\""+preloadimages[i]+"\" />";
		}
		document.getElementById('preloadimages').innerHTML = preloadimagescontent;
		autoload_image();
	}
		
}
function pageunload() {
	
}

function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}

function show_col(s,d) {
	if (active_col!="") {
		document.getElementById(active_col).style.display='none';
	}
	active_col = 'thumbs_'+(s+d);
	document.getElementById(active_col).style.display='block';
}
function show_pic(pic_url, zoom_url,px,py,zx,zy) {
	if (!started) {
		zoomcursor();
		started=true;
	}
	if (z==1) {
		zoom();
	}
	
	time = 250;
	p=1000/20;
	t=0;
	s= 100/(time/p);
	o=100;
	while (o>=0) {
		setTimeout("changeOpac("+Math.round(o)+",'main_pic_container')",t);
		o=o-s;
		t = t+p;
	}
	setTimeout("document.getElementById('main_pic_loader').style.background = 'url(\"http://www.annaperena.co.uk/collections/opc-ajax-loader.gif\") no-repeat center center'",t);
	setTimeout("document.getElementById('main_pic_container').style.backgroundImage = 'url(\""+pic_url+"\")'",t);
	setTimeout("document.getElementById('main_pic_container').style.backgroundPosition = '50% 50%'",t);
	setTimeout("setsize("+px+","+py+")",t);
	while (o<=100) {
		setTimeout("changeOpac("+Math.round(o)+",'main_pic_container')",t);
		o=o+s;
		t = t+p;
	}
	curl = zoom_url;
	purl = pic_url;
	psize=[px,py];
	csize=[zx,zy];
}
function setsize(w,h) {
	document.getElementById('main_pic_container').style.width = w+"px";
	document.getElementById('main_pic_container').style.height = h+"px";
	var obj = document.getElementById('main_pic_container');
	dpos = [0, 0];
	var i = 1;
	while (obj) {
		dpos = [dpos[0]+obj.offsetLeft, dpos[1]+obj.offsetTop];
		obj = obj.offsetParent;
		i++;
	}
}
function posatmouse() {
	corx= (cpos[0]-dpos[0])-(((cpos[0]-dpos[0])/psize[0])*csize[0]);
	cory= (cpos[1]-dpos[1])-(((cpos[1]-dpos[1])/psize[1])*csize[1]);
	
	if (corx>0) corx=0;
	if (cory>0) cory=0;
	
	if (corx<psize[0]-csize[0]) corx=psize[0]-csize[0];
	if (cory<psize[1]-csize[1]) cory=psize[1]-csize[1];
	document.getElementById('main_pic_container').style.backgroundPosition = corx+'px '+cory+'px';
}
function zoom() {
	if (started) {
		if (z==0) {
			
			
			document.getElementById('main_pic_container').style.backgroundImage = 'url(\''+curl+'\')';
			document.getElementById('main_pic_container').style.cursor = "move";
			posatmouse()
			z=1;
			startscrolling();
		} else {
			stopscrolling();
			document.getElementById('main_pic_container').style.backgroundImage = "none";
			document.getElementById('main_pic_container').style.backgroundPosition = '50% 50%';
			document.getElementById('main_pic_container').style.backgroundImage = 'url(\''+purl+'\')';
			zoomcursor();
			z=0;
		}
	}
}

function startscrolling() {
	if (z==1) {
		scrolling=1;
		intId=setInterval('posatmouse()',50);
	}
}
function stopscrolling() {
	if (scrolling==1) {
		clearInterval(intId);
		scrolling=0;
	}
}
	
function mouse(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e) {
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		} else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		cpos = [posx, posy];
	}
}
if( window.addEventListener ) {
    window.addEventListener( 'load', pageload, false );
    window.addEventListener( 'unload', pageunload, false );
} else if( document.addEventListener ) {
    document.addEventListener('load' , pageload, false );
    document.addEventListener( 'unload', pageunload, false );
} else if( window.attachEvent ) {
    window.attachEvent( 'onload', pageload );
    window.attachEvent( 'onunload', pageunload );
} else {
    if( window.onload ) { window.XTRonload = window.onload; }
    if( window.onunload ) { window.XTRonunload = window.onunload; }
    window.onload = pageload;
    window.onunload = pageunload;
}