if(typeof window.innerHeight == "number"){
	var InnerHeight = window.innerHeight;
	var InnerWidth = window.innerWidth;
} else if(typeof document.documentElement.clientHeight == "number"){
	var InnerHeight = document.documentElement.clientHeight;	
	var InnerWidth = document.documentElement.clientWidth;
}

if(document.all && !window.opera && !window.XMLHttpRequest){
	var Position = 'absolute';
} else {
	var Position = 'fixed';	
}

/* --- Define loading graphic here --- */

var loadGraphicURL = '';//'NOWloading.gif';

/* ----------------------------------- */

if (loadGraphicURL != '') {
	var loadGraphic = new Image();
} else {
	var loadGraphic = document.createElement('div');
}
	
	NOWbox = {};
	
/************************
CORE METHODS
************************/
/* ---------------------------------------
Initiate overlay
--------------------------------------- */
NOWbox.initOverlay = function(){
	
	if(!document.getElementById(NOWbox.ID.Overlay)){		
		var overlay = document.createElement('div');					   
		overlay.id = NOWbox.ID.Overlay;
		for(var s in NOWbox.CSS.Overlay){
			overlay.style[s] = NOWbox.CSS.Overlay[s];
		}
		
		//check if body is not height 100%
		if(document.body.style.height != '100%' || document.body.style.overflow != 'hidden'){
			NOWbox.bodyOriginalHeight = document.body.style.height;
			NOWbox.bodyOriginalOverflow = document.body.style.overflow;
			NOWbox.bodyOriginalMargin = document.body.style.margin;
			NOWbox.bodyAugmented = true;
			document.body.style.height = '100%';
			document.body.style.overflow = 'hidden';
			document.body.style.margin = 0;
		}
		
		document.body.appendChild(overlay);
		document.getElementById(NOWbox.ID.Overlay).onclick = function(){
			NOWbox.close();	
		}
		
		//Continue
		NOWbox.initLoadGraphic();
		
	}
						
};

/* ---------------------------------------
Initiate loading graphic
--------------------------------------- */
NOWbox.initLoadGraphic = function(run){
	
	if (loadGraphicURL != '') {
		loadGraphic.src = loadGraphicURL;
		loadGraphic.id = NOWbox.ID.LoadImg;
	} else {
		loadGraphic.id = NOWbox.ID.LoadImg;
	}
	
	for(var s in NOWbox.CSS.LoadImg){
		loadGraphic.style[s] = NOWbox.CSS.LoadImg[s];
	}
	document.body.appendChild(loadGraphic);
	
	//Continue
	NOWbox.loadImg(NOWbox.imgSource);
	
}

/* ---------------------------------------
Load image from source
--------------------------------------- */
NOWbox.loadImg = function(source){
	//image = new Image();
	image = document.createElement('img');
	image.src = source;
	if(window.innerHeight){
		image.onload = function(){
				image.onload = null;
				
				if(image.width > 0 && image.height > 0){
					NOWbox.image = image;
					image.style.width = image.width + 'px';
					image.style.height = image.height + 'px';
					
					NOWbox.resize(image);
					
					//Continue
					NOWbox.initContainer();
				} 
		}
	} else {
		var t = setInterval(function(){
			if(image.complete){
				clearInterval(t);
				if(image.width > 0 && image.height > 0){
							NOWbox.image = image;
							image.style.width = image.width + 'px';
							image.style.height = image.height + 'px';
							
							NOWbox.resize(image);
							
							//Continue
							NOWbox.initContainer();
				}				
			}
									 }, 100);
	}
		
	
};

/* ---------------------------------------
Initiate container
--------------------------------------- */
NOWbox.initContainer = function(){
	var container = document.createElement('div');
	container.id = NOWbox.ID.Container;
	for(var s in NOWbox.CSS.Container){
		container.style[s] = NOWbox.CSS.Container[s];
	}
	
	document.body.appendChild(container);
	container.style.marginTop = -(parseInt(image.style.height)/2) + 'px';
	container.style.marginLeft = -(parseInt(image.style.width)/2) + 'px';
	
	//Continue
	NOWbox.initImg()
		
};

/* ---------------------------------------
Initiate image
--------------------------------------- */
NOWbox.initImg = function(){
	//document.body.removeChild(document.getElementById(NOWbox.ID.LoadImg));
	if(!document.getElementById(NOWbox.ID.Img)){
		image.id = NOWbox.ID.Img;
		
		for(var s in NOWbox.CSS.Img){
			image.style[s] = NOWbox.CSS.Img[s];
		}		
		
		document.getElementById(NOWbox.ID.Container).appendChild(image);
		
		//Continue
		NOWbox.attachFooter();
		
	}
};

/* ---------------------------------------
Attach footer
--------------------------------------- */
NOWbox.attachFooter = function(){
	var footer = document.createElement('div');
	footer.id = NOWbox.ID.Footer;
	document.getElementById(NOWbox.ID.Container).appendChild(footer);
	
	if(NOWbox.caption != ''){
		NOWbox.attachCaption(NOWbox.caption, footer.id);
	}
		
		
		if(NOWbox.isGallery(NOWbox.rel).length > 1){
			if(NOWbox.Imgdex(NOWbox.imgSource) != 0){
				NOWbox.attachButton('Back', 'nowboxback', footer.id, 'javascript: NOWbox.back();');
			}
			if(NOWbox.Imgdex(NOWbox.imgSource) != (NOWbox.currentGroup.length - 1)){
				NOWbox.attachButton('Next', 'nowboxnext', footer.id, 'javascript: NOWbox.next();');
			}
		}
		
		NOWbox.attachButton('Close', 'nowboxclose', footer.id, 'javascript: NOWbox.close();');
		
};

/* ---------------------------------------
Attach buttons
--------------------------------------- */
NOWbox.attachButton = function(linkText, id, attachToId, href){
	var elText = document.createTextNode(linkText);
	var el = document.createElement('a');
	el.href = href;
	el.id = id;
	document.getElementById(attachToId).appendChild(el).appendChild(elText);
};

/* ---------------------------------------
Attach caption
--------------------------------------- */
NOWbox.attachCaption = function(caption, parentId){
	var captiontext = document.createTextNode(caption);
	var caption = document.createElement('p');
	document.getElementById(parentId).appendChild(caption).appendChild(captiontext);
};

/************************
METHODS & PROPERTIES
************************/
/* ---------------------------------------
Initiate image (0 = back, 1 = forward)
--------------------------------------- */
NOWbox.change = function(direction){	
	
	NOWbox.Imgdex(NOWbox.imgSource);
	
	if(direction == 0){
		NOWbox.imgSource = NOWbox.currentGroup[(NOWbox.currentGroup.length - 1) - 1];
	} else if(direction = 1){
		NOWbox.imgSource = NOWbox.currentGroup[(NOWbox.currentGroup.length - 1) + 1];
	}
	
	console.log(NOWbox.imgSource)
	NOWbox.initLoadGraphic();
	
	
};

/* ---------------------------------------
Get images index in group array
--------------------------------------- */
NOWbox.Imgdex = function(source){
	
	var needle = 0;
	
	for(i = 0, l = NOWbox.currentGroup.length; i < l; i++){
		if(source == NOWbox.currentGroup[i]){
			needle = i;
		}
	}
	
	return needle;
}

/* ---------------------------------------
Properties
--------------------------------------- */
NOWbox.imgSource = '';
NOWbox.image = '';
NOWbox.caption = '';
NOWbox.rel = '';

/* ---------------------------------------
View
--------------------------------------- */
NOWbox.view = function(source, caption, group){
	NOWbox.caption = caption;
	NOWbox.imgSource = source;
	NOWbox.rel = group;
	this.initOverlay();
};

/* ---------------------------------------
Close
--------------------------------------- */
NOWbox.close = function(){
	
	image = null;
	NOWbox.currentGroup = null;
	if(document.getElementById(NOWbox.ID.Container)){
		document.body.removeChild(document.getElementById(NOWbox.ID.Container));	
	}
	
	if(document.getElementById(NOWbox.ID.Img)){
		document.body.removeChild(document.getElementById(NOWbox.ID.Img));	
	}
		
	if(document.getElementById(NOWbox.ID.Overlay)){
		document.body.removeChild(document.getElementById(NOWbox.ID.Overlay));
	}
	
	if(document.getElementById(NOWbox.ID.LoadImg)){
		document.body.removeChild(document.getElementById(NOWbox.ID.LoadImg));
	}
	
	if(NOWbox.bodyAugmented){
		document.body.style.height = NOWbox.bodyOriginalHeight;
		document.body.style.overflow = NOWbox.bodyOriginalOverflow;
		document.body.style.margin = NOWbox.bodyOriginalMargin;
		NOWbox.bodyAugmented = false;
	}
	
};

/* ---------------------------------------
Back
--------------------------------------- */
NOWbox.back = function(){
	NOWbox.change(0);
};

/* ---------------------------------------
Next
--------------------------------------- */
NOWbox.next = function(){
	NOWbox.change(1);
};

/* ---------------------------------------
Resize
--------------------------------------- */
NOWbox.resize = function(image){
					   
	var x = InnerWidth - 100,					   
		y = InnerHeight - 250;
	
	if (image.width > x) {	
		image.style.height = image.height * (x / image.width) + 'px';
		image.style.width = x + 'px';	
		if (image.height > y) {
			  image.style.width = image.width * (y / image.height) + 'px';
			  image.style.height = y + 'px';
		}	
	} else if (image.height > y) {	
		image.style.width = image.width * (y / image.height) + 'px';
		image.style.height = y + 'px';	
	} 
	
};

/* ---------------------------------------
Image group counter
--------------------------------------- */
NOWbox.isGallery = function(group){
	if(group != '' && group != null){		
		NOWbox.currentGroup = [];
		for(var i = 0, l = NOWbox.linksArray.length; i < l; i++){
			if(NOWbox.linksArray[i].rel.indexOf(group) != -1){
				
				NOWbox.currentGroup[i] = NOWbox.linksArray[i];
				
			}
		}
		
		return NOWbox.currentGroup;
		
	} else {
		return false;
	}
};

/* -------------------------
	NOWbox.ID
------------------------- */
NOWbox.ID = {
	Container: 'nowbox-container',
	Overlay: 'nowbox-overlay',
	LoadImg: 'nowbox-load',
	Img: 'nowbox-image',
	Footer: 'nowbox-footer'
};

/* -------------------------
	NOWbox.CSS
------------------------- */
NOWbox.CSS = {
	Overlay: {
		width: '100%',
		height: '100%',
		position: Position,
		top: 0,
		left: 0,
		zIndex: 10000,
		backgroundColor: '#000',
		filter: 'alpha(opacity = 90)',
		opacity: 0.9
	},
	Img: {
		display: 'block',
		marginBottom: '10px'
	},
	LoadImg: {
		position: Position,
		top: '50%',
		left: '50%',
		marginLeft: '-100px',
		zIndex: 10001
	},
	Container: {
		position: Position,
		top: '50%',
		left: '50%',
		padding: '10px',
		zIndex: 10001,
		backgroundColor: '#fff'
	}	
};

/*
Checks if last 4 chars of href contains an image type (entered as a param)
*/

function strMatch(type, where){
	if(where.href.substr((where.href.length - 4), where.href.length).indexOf(type) != -1){
		return true;
	} else {
		return false;
	}
}

/*
 * Attach the click event to the nowbox links
 */
jQuery(document).ready(function(){
 
	var links = document.getElementsByTagName('a');
	NOWbox.linksArray = [];
	for(var i = 0, l = links.length; i < l; i++){
		if(links[i].className != 'undefined' && links[i].className != ''){			
			if(links[i].className.indexOf('nowbox') != -1){				
				NOWbox.linksArray[i] = links[i];
				jQuery(links[i]).click(function(){					
					if(strMatch('jpg', this) || strMatch('gif', this) || strMatch('png', this)){
						NOWbox.view(this.href, this.title, this.rel);						
					}
					return false;
				});
			}
		}
	}
	
});	