var $sshow0606 = {
swap: function(){
var $ = arguments.callee.$;
var use = ($.current.className.indexOf('optionA')+1)? $.current.option[1] : $.current.option[0];
//if(use.sound){ $.soundctrl('audiofile='+use.sound); }
use = use.className;
$.current.parentNode.className = $.current.className = use;
},

prev: function(){
var $ = arguments.callee.$;
var n = ( ($.current.no - 1) < $.hasintro)? ($.elements.length - 1):($.current.no - 1);
$.go(n);
return false;
},
next: function(){
var $ = arguments.callee.$;
var n = ( ($.current.no + 1) >= $.elements.length)? $.hasintro : $.current.no + 1;
$.go(n);
return false;
},

go: function(n){
	var $ = arguments.callee.$;
	if($.hasintro && $.tools){
		$.btnswap.style.display = $.tools.style.display = (n == 0)? 'none':'block';
	}
	if(typeof $.elements[n] == 'object' && $.elements[n].nodeName){

	if(n != $.current.no){
	$.soundctrl();
		$.current.style.display = 'none';
		if(typeof $.current.a != 'undefined') $.current.a.className = '';
	}
	$.elements[n].style.visibility = 'hidden';
	$.elements[n].style.display = 'block';
	$.current = $.elements[n];
	if(!$.hasintro || ($.hasintro && n != 0) ){
	var o1 = $.current.option[0];
	var o2 = $.current.option[1];
	if(!$.current.imgset){
	o1.img.className = (o1.img.width/o1.img.height <= 1)? 'vert':'horiz';
	o2.img.className = (o2.img.width/o2.img.height <= 1)? 'vert':'horiz';
	o1.className = 'optionA '+o1.img.className;
	o2.className = 'optionB '+o2.img.className;
	$.current.imgset = true;
	}
	var check = ($.current.className.indexOf('optionA')+1)? o1 : o2;
	$.root.className = $.current.className = check.className;
	if(check.sound){ $.soundctrl('audiofile='+check.sound); }
	//if(check.sound){ $.soundctrl('audiofile='+check.sound); }
	}
	$.current.style.visibility = 'visible';
	if(typeof $.current.a != 'undefined') $.current.a.className = 'currentimg';
	}
},

build: function(){
var d = document;
/* read elements and widths/heights, compute positions, classnames */
this.elements = [];
var els = this.root.childNodes;
var intro = d.getElementById('intro');
this.hasintro = (intro && intro.parentNode == this.root)? 1:0;
	if(this.hasintro){
d.getElementById('btnbegin').no = 1;
d.getElementById('btnbegin').className = 'btn';
d.getElementById('restart').no = 0;
var r = this;
d.getElementById('btnbegin').onclick = d.getElementById('restart').onclick = function(){ r.go(this.no); return false; };
	}
var request = (typeof location.search == 'string' && location.search.search(/\d+/)+1)? ( parseInt(location.search.match(/(\d+)/)[0])):false;
request = (!(isNaN(request)) && request > 0)? request:0;
this.imagelist = d.getElementById('images');
var r = this;
var c = -1;
	for(var i = 0;i<els.length;i++){
var el = els[i];
if(el.parentNode != this.root || el.nodeName.toLowerCase() != 'div') continue;
c++;
el.no = c;
el.style.display = 'none';
this.elements.push(el);
if(el.id && el.id == 'intro') continue;

el.option = el.getElementsByTagName('div');
var img1 = el.option[0].img = el.option[0].getElementsByTagName('img')[0];
var img2 = el.option[1].img = el.option[1].getElementsByTagName('img')[0];

el.className = el.option[0].className = 'optionA '+ img1.className;
el.option[1].className = 'optionB '+ img2.className;
img1.no = 0;
img2.no = 1;
//el.onclick = function(){ r.swap(); };
if(el.style.cursor) el.style.cursor = 'pointer';

for(var s = 0;s<2;s++){
var option = el.option[s];
option.sound = false;
var spans = el.option[s].getElementsByTagName('span');
for(var so = 0;so<spans.length;so++){
if(spans[so].className.indexOf('sound')+1){
	option.sound = spans[so].innerHTML;
	break;
	};
}
}

var link = d.createElement('a');
link.href = '?' + c;
link.no = c; var r = this;
link.onclick = function(){ r.go(this.no); return false; };
link.innerHTML = '<span class="btn">'+c+'</span>';
this.imagelist.appendChild(link);
el.a = link;
	};
this.btnswap.onclick = function(){ r.swap(); return false; };
this.current = this.elements[((request < this.elements.length)? request : 0)];
if(d.getElementById('sound')){
	this.sound = d.getElementById('sound');
	this.sound.file = this.sound.innerHTML;
	this.sound.style.display = 'inline';
	this.soundctrl('jscallback=$sshow0606.soundcallback()');
}
this.go(this.current.no);
},

soundcallback: function(){
// setup sound control buttons
$sshow0606.soundenabled = true;
$sshow0606.root.parentNode.className += ' soundenabled';
return;
},
soundctrl: function(msg){
if(typeof this.sound == 'undefined' || (this.sound && msg != '' && document.getElementById('soundctrl') && document.getElementById('soundctrl').className.indexOf('off')+1) ) return;
var fv = (msg)? msg:'';
var f = this.sound.file;//+'?'+(new Date()).getTime();
this.sound.innerHTML = '<object type="application/x-shockwave-flash" data="'+f+'" width="100" height="100"><param name="movie" value="'+f+'" /><param name="flashVars" value="'+fv+'"></object>';
},

init: function(){
var d = document;
if(!d.getElementById || !d.createElement) return;

this.root = d.getElementById('show');
this.root.home = this;
this.tools = d.getElementById('tools');
this.swap.$ = this.go.$ = this.prev.$ = this.next.$ = this;
this.btnswap = d.getElementById('swap');
this.sound = (d.getElementById('sound'))? d.getElementById('sound') : false;
this.build();
// setup events on toolbar
d.getElementById('prev').onclick = this.prev;
d.getElementById('next').onclick = this.next;
},
custom: {
	audio: function(el, mp3){
	if(!el.defaultClass) el.defaultClass = el.className;
	var test = (el.className.indexOf('on')+1)? true:false;
	el.className = el.defaultClass + ((test)? ' off':' on');
	$sshow0606.soundctrl( ((test)? '':('audiofile='+mp3)) );
	}
}
};
window.onload = function(){ $sshow0606.init(); };