function include(file)
{

  var script  = document.createElement('script');
  script.src  = file;
  script.type = 'text/javascript';
  script.defer = true;

  document.getElementsByTagName('head').item(0).appendChild(script);

}

include('jquery-1.2.2.min.js');

function getName(element)
{
	var src= element.getAttribute("src");
	
	var n = src.substr(src.lastIndexOf("/") + 1, src.length - 1);
	
	return n;
}


var H =(document.innerHeight)?(document.innerHeight):(document.documentElement)?document.documentElement.clientHeight:document.body.clientHeight;
var W =(document.innerWidth)?(document.innerWidth):(document.documentElement)?document.documentElement.clientWidth:document.body.clientWidth;

var max_height = 600;
var max_width = 800;

var lb_height = max_height;
var lb_width =  max_width;

var pic;

var timeout_active;

function showLightBox(element,path)
{
	// fade in the empty light box

	pic =new Image();
	pic.name = "fullImage"
	pic.style.display = 'none'; //hide the Image until it's completely loaded
	
	// tweak dynamic height of faded layer
	var fader_height = document.getElementById('wrapper').offsetHeight;
	fader_height += 40;
	document.getElementById('fade').style.height = fader_height + "px";	
	document.getElementById('fade').style.display = 'block';
	
	// get the image from source
	if(path == undefined)
	{
		pic.src = 'images/vertical_image_list/full/' + getName(element);
	}
	else
	{
		pic.src = path + getName(element);
	}
	
	var scroll = (window.pageYOffset)?(window.pageYOffset):(document.documentElement)?document.documentElement.scrollTop:document.body.scrollTop;
	
	document.getElementById('light').style.top = (H/2) - (lb_height/2) + scroll + 'px';
	document.getElementById('light').style.left = (W/2) - (lb_width/2) + 'px';
	document.getElementById('light').style.width = lb_width + 'px';
	document.getElementById('light').style.height = lb_height + 'px';	

	$("#light").fadeIn("fast");	
	
	// call the image loader
	timeout_active = window.setTimeout('loader()',500);
	

	
}

function resizeLightbox()
{
	
	var img_h = pic.height;
	var img_w = pic.width;
	
	if(img_h > max_height || img_w > max_width)
	{
		var ratio = img_h / img_w;
		
		if(img_h > max_height)
		{
			img_h = max_height;
			img_w = img_h / ratio;  
		}
		else
		{
			if(img_w > max_width)
			{
				img_w = max_width;
				img_h = ratio * img_w;	
			}
			else
			{
				img_h = max_height;
				img_w = img_h;
			}
		}
		
		pic.height = img_h ;
		pic.width = img_w ;
		
	}
	
	lb_height = img_h + 60;
	lb_width  = img_w + 50;	
	
	var scroll = (window.pageYOffset)?(window.pageYOffset):(document.documentElement)?document.documentElement.scrollTop:document.body.scrollTop;
	
	document.getElementById('light').style.top = (H/2) - (lb_height/2) + scroll + 'px';
	document.getElementById('light').style.left = (W/2) - (lb_width/2) + 'px';
	document.getElementById('light').style.width = lb_width + 'px';
	document.getElementById('light').style.height = lb_height + 'px';
	
	//document.getElementById('light').style.display = 'block';
	pb.innerHTML = '';
	pb.appendChild(pic);
	//pb.style.display = 'block'; //show the picpox
	pic.style.display = 'block'; // show the completely loaded Image
	$("#PicBox").fadeIn("slow");
	
	
}

function closeLightBox()
{
	pb.style.display = 'none';
	$("#light").fadeOut("slow",close());
}

function close()
{
	//document.getElementById('light').style.display='none';
	//document.getElementById('fade').style.display='none';
	$("#fade").fadeOut("slow");
	if(window.timeout_active)
	{
		clearTimeout(timeout_active);
	}
}

function getElementStyle(elemID, IEStyleProp, CSSStyleProp) {
    var elem = document.getElementById(elemID);
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

function returnNumber(z)
{
	var str ='';
	var i;
	for(i=0;i<z.length; i++)
	{
		if(parseInt(z.charAt(i)) > -1)
		{
			str += z.charAt(i);
		}
	}
	if(z.charAt(0) == "-")
	{
		return parseInt("-" + str);
	}
	else
	{
		return parseInt(str);
	}
	
}

function loader()
{
	var ld = document.getElementById('loading');
	
	if(pic.complete == false)
	{
		ld.innerHTML = '<span style="font-family:Arial;font-size:12px;color:#CCC;">LOADING</span>';
		timeout_active = window.setTimeout('loader()',500);
	}
	else
	{
		ld.innerHTML ='<span style="font-family:Arial;font-size:12px;color:#CCC;">&nbsp;</span>';
		if(timeout_active)
		{
			window.clearTimeout(timeout_active);
		}
		
		resizeLightbox();
	}
	
	
}






