var w = 0;
var ImgNew = null;
var div = null;
var agt=navigator.userAgent.toLowerCase();
var is_gecko = (agt.indexOf('gecko') != -1);
var is_ie    = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_opera = (agt.indexOf("opera") != -1);
var offSetImg = 100;
var isOnClick = false;
var rightover = false;
var leftover = false;
var MaxImageWidth = 720;
var NavigatorWidth = 658;

onload_functions.push("InitGallery()");

function InitGallery(){
	div = document.getElementById('imggallery');
	var nav = document.getElementById('navigator');
	
	var img = nav.getElementsByTagName("IMG");
	if(img.length > 0)
	{
		w=0;
		for(j = 0; j<= img.length -1; j++)
		{
			w += img[j].width + 2;
		}
		if(w <= NavigatorWidth)
		{
			w = NavigatorWidth
		} 
		nav.style.width = w + 'px';
		clickImgae(img[0], img[0].src.replace('mini', 'normal'));
	}
}

function opacityOff()
{
	if((div.style.opacity!=0) && (parseFloat(div.style.opacity)>0))
	{
		if (is_ie)
		{
			div.filters[0].opacity=div.filters[0].opacity-5;
		} 
		div.style.opacity=parseFloat(div.style.opacity)-0.05;
		setTimeout('opacityOff()', 2);
	} else {
		setImage();
	}
}

function opacityOn()
{
	if((div.style.opacity!=1) && (parseFloat(div.style.opacity)!=1))
	{
		if (is_ie)
		{
			div.filters[0].opacity=div.filters[0].opacity+5;
		} 
		div.style.opacity=parseFloat(div.style.opacity)+0.05;
		setTimeout('opacityOn()', 1);
	} else {
		isOnClick = false;
		ImgNew = null;
	}
}

function setImage()
{
	div.innerHTML = "";			
	div.appendChild(ImgNew);
	setTimeout('opacityOn()', 0);
}

function clickImgae(imgsrc, pathNewImage)
{
	if(!isOnClick)
	{
		isOnClick = true;
		ImgNew = document.createElement("IMG");
		
		ImgNew.onload = function() { 		
			if(parseInt(ImgNew.width) > MaxImageWidth)
			{
				ImgNew.width = MaxImageWidth;
			}
			opacityOff();
		};
		ImgNew.src = pathNewImage;
	} 
}

function MouseHoverRigth()
{
	rightover = true;
	rightoffset();
}

function MouseRightLeave()
{
	rightover = false;
}

function MouseHoverLeft()
{
	leftover = true;
	leftoffset();
}

function MouseLeftLeave()
{
	leftover = false;
}

function rightoffset()
{
	if(rightover)
	{
		var oldOffSet = parseInt(document.getElementById('navigator').style.left.replace('px', ''));
		var offSet = oldOffSet  - offSetImg;
		if( offSet + w - NavigatorWidth < 0 )
			offSet = (w - NavigatorWidth) * -1;
		OffSet(oldOffSet, offSet);
	}
}

function leftoffset()
{
	if(leftover)
	{
		var oldOffSet = parseInt(document.getElementById('navigator').style.left.replace('px', ''))
		if(oldOffSet == 0) return;
		var offSet = oldOffSet + offSetImg;
		if(offSet > 0 )
			offSet = 0;
		OffSet(oldOffSet, offSet);
	}
}

function offSetMin(Values, stopper)
{
	if(Values < stopper)
		Values = stopper;

	if(is_ie)
		document.getElementById('navigator').style.left = Values;		
	else
		document.getElementById('navigator').style.left = Values + 'px';	

	if(Values != stopper)
	{
		Values -= 5;		
		setTimeout('offSetMin(' + Values + ',  ' + stopper + ')', 1);
	} else {
		rightoffset();
	}
}

function offSetMax(Values, stopper)
{
	if(Values > stopper)
		Values = stopper;
		
	if(is_ie)
		document.getElementById('navigator').style.left = Values;
	else 
		document.getElementById('navigator').style.left = Values + 'px';		
	if(Values != stopper)
	{
		Values += 5;
		setTimeout('offSetMax(' + Values + ',  ' + stopper + ')', 1);
	} else {
		leftoffset();
	}
}

function OffSet(Old, New)
{
	if(Old > New)
	{
		offSetMin(Old, New);
	} else {
		offSetMax(Old, New);		
	}
}

function findPosX(objtesto)
{
	var obj = document.getElementById(objtesto);
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(objtesto)
{
	var obj = document.getElementById(objtesto);
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}