function light(img, o) {
	r = /(^|\b)on(\b|$)/;
	if (r.test(img.className)) return;
	if (!img.hi) {
		img.style.top = '-' + o + 'px';
		img.hi = true;
	} else {
		img.style.top = '0px';
		img.hi = false;
	}
	return true;
}

function navlight(img) {
	return light(img, 11);
}

function rotatebg(e, n, choices) {
    e = document.getElementById(e);
	var i = parseInt(Math.random()*n);
	i = Math.min(choices.length-1, i);
	var c = choices[i];
	e.style.backgroundImage = 'url(' + c.img + ')';
	e.style.backgroundPosition = c.left + 'px '+ c.top + 'px';
	e.style.width = c.width + 'px';
	e.style.height = c.height + 'px';
}

// function plugin(img) {
// 	var p1 = img.src.split('/');
// 	var bn = p1[p1.length - 1];
// 	var p2 = bn.split('.');
// 	var n = p2[0];
// 	var ext = p2[1];
// 	p1[p1.length - 1] = n +'-o' + '.' + ext;
// 	img.hisrc = p1.join('/');
// 	img.losrc = img.src;
// }


var intWW = 520 ;
var intWH = 490 ;
var intWT = 0;              // Popup Top Position
var intWL = 0;              // Popup Left Position
var intSWT = 0;              // Popup Top Position for Shop
var intSWL = 0;              // Popup Left Position for Shop
var intSW = screen.width;   // Screen Width
var intSH = screen.height;  // Screen Height

if (screen) {
// 1.33_ is a typical screen ratio (width divided by the height)
// But we'll use 1.4 just to be safe
if (intSW / intSH > 1.4) {
	intWL = (intSW - intWW) / 4;
	intWT = (intSH - intWH) / 2;
}
else {
	intWL = (intSW - intWW) / 2;
	intWT = (intSH - intWH) / 2;
}
}

if (screen) {
	intSWL = (intSW / 2) -379;
	intSWT = (intSH / 2) -245;
}

function goToTheDesignMuseumShop() {
strURL = 'http://www.designmuseumshop.com/index2.html';
winWid = 757;
winHei = 490;
var strName = 'shop';
var strOptions = ' scrollbars=0,resizable=0,menubar=0,toolbar=0,status=1,location=0,directories=0';
windowb = window.open(strURL, strName, 'width=' + winWid + ',height=' + winHei + ',left=' + intSWL + ',top=' + intSWT + ',' + strOptions);
 if (windowb.focus) {
 windowb.focus();
 }
}


function openDOYWindow() { //v2.1
  awindow = window.open('http://www.designmuseum.org/designeroftheyear/html/detect.html','doy','width=780, height=520, dependent=1,directories=0,location=0,menubar=0,resizable=0,scrollbars=0,toolbar=0');
  var x = 780; 
  var y = 520; 
  awindow.moveTo(parseInt((screen.availWidth - x) / 2), parseInt((screen.availHeight - y) / 2)); 
  awindow.focus();
	return false;
}




function openDOYWindow() { //v2.1
	var theURL = '/designeroftheyear/html/detect.html'

  awindow = window.open(theURL,'doy','width=780, height=520,dependent=1,directories=0,location=0,menubar=0,resizable=0,scrollbars=0,toolbar=0');
  var x = 780; 
  var y = 520; 
  awindow.moveTo(parseInt((screen.availWidth - x) / 2), 
  parseInt((screen.availHeight - y) / 2)); 
   awindow.focus();

}

leave_effect = Effect.BlindUp;
arrive_effect = Effect.BlindDown;
function norm_duration(d, to) {
	if (!d) d = (to ? to : 0.4); 
	return d; 
}

function show_optional_form(name, in_duration, out_duration) {
	in_duration = norm_duration(in_duration);
	out_duration = norm_duration(out_duration, in_duration);
	
	leave_effect(name+'-inactive', {duration:out_duration});
	arrive_effect(name+'-active', {duration:in_duration});

}

function hide_optional_form(name, in_duration, out_duration) {
	in_duration = norm_duration(in_duration);
	out_duration = norm_duration(out_duration, in_duration);
	leave_effect(name+'-active', {duration:out_duration});
	arrive_effect(name+'-inactive', {duration:in_duration});
}


function init_optional_form(name) {
	$(name+'-active').hide();
	$(name+'-inactive').show();
}
function searchbox(){
	if($$('input.query')){	
		var sinput = $$('input.query')[0];
		sinput.value = 'Search';
		sinput.onfocus = function() {
		  // if already cleared, do nothing
		  if (this._cleared) return
	  
		  this._value = this.value;
		  this.clear();
		}
	
		sinput.onblur = function() {
		  // if already cleared, do nothing
		  if (this._cleared) return
	  
		  if(this.value==''){
			this.value = this._value;
		  }else{
			this._cleared = true;
		  }
		}
	}
}

function selectReplacement(obj) {
	obj.className += ' replaced';
	var ul = document.createElement('ul');
	ul.className = 'selectReplacement';
	var opts = obj.options;
	var selectedOpt = (!obj.selectedIndex) ? 0 : obj.selectedIndex;
	for (var i=0; i<opts.length; i++) {
		var li = document.createElement('li');
		var txt = document.createTextNode(opts[i].text);
		li.appendChild(txt);
		li.selIndex = i;
		li.selectID = obj.id;
		li.onclick = function() {
		  selectMe(this);
		};
		
		if (i == selectedOpt) {
		  li.className = 'selected';
		  li.onclick = function() {
		    this.parentNode.className += ' selectOpen';
		    this.onclick = function() {
		      selectMe(this);
		    };
		  };
		}
		if (i+1 == opts.length) {
			li.className = li.className + ' last';
		}
		if (window.attachEvent) {
		  li.onmouseover = function() {
		    this.className += ' hover';
		  };
		  li.onmouseout = function() {
		    this.className = 
		      this.className.replace(new RegExp(" hover\\b"), '');
		  };
		}
		ul.appendChild(li);
	}
	obj.onfocus = function() {
		ul.className += ' selectFocused';
	};
	obj.onblur = function() {
		ul.className = 'selectReplacement';
	};
	obj.onchange = function() {
		var idx = this.selectedIndex;
		selectMe(ul.childNodes[idx]);
	};
	obj.onkeypress = obj.onchange;
	obj.parentNode.insertBefore(ul,obj);
}
function selectMe(obj) {
	var lis = obj.parentNode.getElementsByTagName('li');
	for (var i=0; i<lis.length; i++) {
		if (lis[i] != obj) {
			lis[i].className='';
			if (i+1 == lis.length) {
				lis[i].className = 'last';
			}
			lis[i].onclick = function() {
			selectMe(this);
			};
		} else {
		  setVal(obj.selectID, obj.selIndex);
		  obj.className='selected';
		  obj.parentNode.className = 
		    obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
		  obj.onclick = function() {
		    obj.parentNode.className += ' selectOpen';
		    this.onclick = function() {
		      selectMe(this);
		    };
		  };
		}
	}
}

function setVal(objID,val) {
	var obj = document.getElementById(objID);
	obj.selectedIndex = val;
	if($(obj).value!=''){
		document.location.href = $(obj).value;
	}
}

function quicklinks(){
	if($('quicklinks')){	
		$('submit_quicklinks').hide();
		var select = $('quicklinks');
		select.onchange = function() {
			if(select.value!=''){
				document.location.href = select.value;
			}
		};
		(document.all && !window.XMLHttpRequest) ? null : selectReplacement(select);
	}
}

Event.observe(window, 'load', function() {
  searchbox();
  quicklinks();
});

/*
 *************************immersive360***********************************
 */

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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 = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function immersive360() {

	var body = document.getElementsByTagName("body").item(0);
	var o = $('overlay');
	if (!o) {
		o = document.createElement("div");
		o.setAttribute('id','overlay');
		o.style.display = 'none';
	}
	var arrayPageSize = getPageSize();
	//Element.setWidth(o, arrayPageSize[0]);
	//Element.setHeight(o, arrayPageSize[1]);
	body.appendChild(o);

	var l = $('lightbox');
	o.appendChild(l);
	l.show();

	jQuery("#overlay").animate({opacity: "show"}, 600, function (){console.log('overlay is visible')});
	/*
	new Effect.Appear(o, { duration: 0.2, from: 0.0, to: 1, afterFinish: function() {
		//show_immersive360(link);
		console.log('done appearing');
	} });
	*/
	return false;
}

function close_immersive360() {
	var l = $('lightbox');
	l.hide();
	new Effect.Fade('overlay', { duration: 0.2, afterFinish: function() {
		//showFlash2();
		$('overlay').hide();
		//var canvas = $('fullscreen_canvas');
		//canvas.innerHTML = '';
	}});
}

function show_immersive360(link) {
	//var canvas = $('fullscreen_canvas');
	//canvas.innerHTML = '<iframe id="iPano" name="iPano" src="http://www.designmuseum.immersive360.com/'+link+'" frameborder="0" width="475" height="400" scrolling="no"></iframe>';
	//canvas.appendChild(img);
}
