// JavaScript Document
var dhNull;
var n4 = !!(document.layers && typeof document.classes != dhNull);
var px = (n4) ? '' : 'px';
var documentW;
var documentH;
var viewportW;
var viewportH;
var xScroll;
var yScroll;
var currentImage = '';
var currentGallery = '';
var currentSizes;
var seeitnow;
var imageObj;
function menuHover( elem , name, hovering ){ elem.className = (hovering) ? "menuItem " + name + "Hover" : "menuItem"; }
function subMenuHover( elem , name, hovering ){ elem.className = (hovering) ? "subMenu " + name + "Hover" : "subMenu"; }
function gotoURL( page ) { window.location = 'http://jordan.robotmojo.com/' + page; }
function isObject(a) { return (a && typeof a == 'object') || isFunction(a); }
function resizeFunc(){
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
			// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	documentW = pageWidth;
	documentH = pageHeight;
	viewportW = windowWidth;
	viewportH = windowHeight;
}
function getObject(objectid){
	try	{
	  if (document.getElementById) {
			return document.getElementById(objectid);
		  }
		  else if (document.all) {
			return document.all[objectid];
		  }
		  else if (document.layers) {
			return document.layers[objectid];
		  }
		}
	catch(e) {
		alert("Error: "+objectid+"\n"+e);
	}
}
function createObject(tag, id, classname, parent, innerHTML, type){
	try{
		var tmpObject = document.createElement(tag);
		if (tag == "input" && type != dhNull) tmpObject.type = type;
		if(classname != dhNull) tmpObject.className = classname;
		if(id != dhNull){
			tmpObject.id = id;
			tmpObject.name = id;
		}
		if(parent != dhNull) parent.appendChild(tmpObject);
		if(innerHTML != dhNull) tmpObject.innerHTML = innerHTML;
	}
	catch(e) {  }
	return tmpObject;
}
//-----------------------------------------
// visually Hide a DOM Element
//-----------------------------------------
 function hideObject(obj) {
	if(!isObject(obj) ) return;
	obj.style.visibility = 'hidden';
	obj.style.display = 'none';
}

//-----------------------------------------
// visually Show a DOM Element
//-----------------------------------------
function showObject(obj) {
	if(!isObject(obj) ) return;
	obj.style.visibility = 'visible';
	obj.style.display = 'block';
}

function openimage( gallery , image, screenSizes, sizes, infoIndex ){
	
	resizeFunc();
	var imageObj;
	if( !getObject('matte') ) {
		
		var matte = 		createObject("div", 'matte', dhNull, getObject('main'), dhNull);
		var matte_bg = 		createObject("div", 'matte_bg', 'matte_inner', matte, dhNull);
		var matte_content = createObject("div", 'matte_content', 'matte_inner', matte, dhNull);
				
		var imageFrame = createObject("div", 'image_frame', 'imageFrame', matte_content, dhNull);
		
		var imageHeader = 	createObject('div', 'imgHeader', 	dhNull,		imageFrame, dhNull);
			var imgPrev = 	createObject('div', 'popupPrev',	'popupNav',	imageHeader, dhNull);
			var imgOpen = 	createObject('div', 'popupOpen',	'popupNav',	imageHeader, dhNull);
			var imgNext = 	createObject('div', 'popupNext',	'popupNav',	imageHeader, dhNull);
			
		imageObj = createObject("img", "imageImage", 'popupImage',	imageFrame, dhNull);
		
		imgPrev.onclick = image_Prev;
		imgOpen.onclick = image_Open;
		imgNext.onclick = image_Next;
	
		imageHeader.onclick = stopEvent;
		
	
		matte.onclick = destroyMatte;
		matte_bg.onclick = destroyMatte;		
	}
	
	if(!matte) matte = getObject('matte');
	matte.style.width = documentW;
	matte.style.height = documentH;
		
	var currentSelection = { i:-1, difference:-1,size:0 };	
	for( var i  = 0; i < sizes.length ; i++ ){
		var difference = viewportH - sizes[i] - 20;
		if( currentSelection.difference == -1){
			currentSelection.difference = difference;
			currentSelection.size = sizes[i];
			currentSelection.i = i;
		} else if( difference < currentSelection.difference && difference > 0 ){
			currentSelection.difference = difference;
			currentSelection.size = sizes[i];
			currentSelection.i = i;
		}
	}

	if( !imageObj ) imageObj = getObject("imageImage");
	imageObj.src = "/images/fullsize/" + gallery + "/" + screenSizes[currentSelection.i] + "/" + image;
	
	//if( !imageObj.onclick) imageObj.onclick = stopEvent;
	
	var seeLive = getObject('popupOpen');
	if( images[image].seeitnow ){
		seeitnow = images[image].seeitnow;
		showObject(seeLive);		
	} else {
		hideObject(seeLive);
	}
	
	currentImage = image;
	currentGallery = gallery;
	currentSizes = sizes;
}

function stopEvent(e){
	if (!e) var e = window.event;
	try{
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		e.preventDefault();
	}catch(ex){
		//alert(ex);
	}
	return false;
}

function destroyMatte(){
	var matte = getObject('matte');
	//delete matte.onclick;
	matte.parentNode.removeChild(matte);
}

function getW() {	
	return (n4) ?
		(this.obj) ?
			this.obj.clip.width : 0
		: (this.obj) ?
			(this.obj.offsetWidth || this.style.pixelWidth || this.style.width || 0) : 0;
}
function getH() { 
	return (n4) ?
		(this.obj) ? 
			this.obj.clip.height : 0 
		: (this.obj) ?
			(this.obj.offsetHeight || this.style.pixelHeight || this.style.height || 0) : 0;
}


function image_Prev(){
	var found = false;
	var lastKey = "";
	var getLast = false;
	
	for( var x in images){
		
		if(x == currentImage && !getLast){
			if( lastKey == "" ){
				getLast = true;
			} else {
				openimage( images[lastKey].gallery , lastKey, images[lastKey].screenSizes, images[lastKey].sizes);
				return;
			}
		}
		
		lastKey = x;
	}
	openimage( images[x].gallery , x, images[x].screenSizes, images[x].sizes);	
}

function image_Open(){
	newwindow = window.open(seeitnow, 'popuplink');
	if (window.focus) {newwindow.focus()}
}

function image_Next(){
	var found = false;
	for( var x in images){
		if(found == true){
			openimage( images[x].gallery , x, images[x].screenSizes, images[x].sizes);
			return;
		}
		if(x == currentImage){
			found = true;
		}
	}
	for( var x in images){
		openimage( images[x].gallery , x, images[x].screenSizes, images[x].sizes);	
		return;
	}
}