//	$Id: shoppage.js,v 1.2 2011/11/02 12:51:37 beta Exp $
//	old css/scripts.js for non-order shop pages (i.e. go/place)
//	requires res/shop.js

var dyntags = [];

function init()
{
	try { shopinit(); } catch (e) {}
	groupsel_show();
	if (!move_banner('init')) swf_show();

	var hash = document.location.hash; // #PRAE7 links from tag cloud
	if (hash) {
		hash = 'group'+hash.substring(1);
		var chk = document.getElementById(hash);
		if (chk) {
			chk.checked = true;
			groupsel_change(chk);
		}
	}

	var tagcloud = I('tagcloud');
	if (!tagcloud) return;
	var t = tagcloud.getElementsByTagName('span');
	var tagclouddyn = text('tagclouddyn')-0;
	if (tagclouddyn > t.length) tagclouddyn = t.length;
	while (tagclouddyn--) dyntags.push(t[Math.floor(Math.random()*t.length)]);
	dyntag();
}

function openwin(url, name, feat){
	win = window.open(url, name, feat);
	win.focus();
}

function infoClose(){
	self.close();
}
function infoGo(href){
	var href = href || '/go/place';
	win = window.open(href);
	win.focus();
	self.close();
}

function popup(breite,hoehe,name) // popup-Funktion mit allen Browsermenus weggenommen
{
  fenster=window.open('',name,'width=' + breite + ',height=' + hoehe +',resizable=yes,scrollbars=no,toolbar=no,status=no,menubar=no,location=no');
  fenster.focus();
} 

function popup2(breite,hoehe,name) // popup-Funktion mit allen Browsermenus weggenommen
{
  fenster=window.open('',name,'width=' + breite + ',height=' + hoehe +',resizable=yes,scrollbars=yes,toolbar=no,status=no,menubar=no,location=no');
  fenster.focus();
}                      


function groupsel_show()
{
	var hide=document.getElementById('groupSelHide');
	var show=document.getElementById('groupSelForm');
	if (null!=hide){hide.className='hidden';}
	if (null!=show){show.className='';}
}

function groupsel_check(hash) {
	var chk = document.getElementById(hash);
	if( !chk ) return;
	var formElems = chk.form.elements;
	for ( var i = 0; i < formElems.length; ++i ) {
		var elem = formElems[i];
		if ( "checkbox" != elem.type ) continue;
		if ( "groupALL" == elem.id ) continue;
		elem.checked = elem.id == hash ? true : false;
	}
	groupsel_change(chk);
}

function groupsel_change(that)
{
	if ( null == that && null == id ) return;
	var groupList=document.getElementById('groupList');
	if( null == groupList )
		groupList=document.getElementById('groupList2');
	if ( null == groupList ) return;
	var items = groupList.childNodes;
	if ( 0 == items.length ) return;
	var groupId = that.id;
	var selAll = that.checked && ("groupALL" == groupId);
	var formElems = that.form.elements;
	var selGroup = "";
	for ( var i = 0; i < formElems.length; ++i ) {
		var elem = formElems[i];
		if ( "checkbox" != elem.type ) continue;
		if ( "groupALL" == elem.id ) continue;
		if ( elem.checked ) {
			if ( 0 != selGroup.length ) selGroup = selGroup.concat(" ");
			selGroup = selGroup.concat( elem.id );
		}
	}
	var forceAll = 0;
	// none is all
	if ( "" == selGroup ) { forceAll = 1; selAll = true; }
	else if ( 1 == selGroup.split(" ").length && "groupALL" != groupId ) forceAll = 2;
	if ( 0 != forceAll ) {
		for ( var i = 0; i < formElems.length; ++i ) {
			var elem = formElems[i];
			if ( "groupALL" == elem.id ) {
				elem.checked = 1 == forceAll ? true : false;
				break;
			}
		}
	}
	var selItems = [];
	for ( var i = 0; i < items.length; ++i ) {
		var item = items[i];
		if ( null == item.className ) continue;
		var classes = item.className.split(" ");
		var cn = classes[0];
		if ( selAll || (0 <= selGroup.indexOf(cn)) ){
			item.className = cn;
			selItems.push(item);
		}	
		else
			item.className = cn + " hidden";
	}
	if( groupList.id == 'groupList2' ) {
		for( var i = 0; i<selItems.length; ++i ) {
			var item = selItems[i];
			if( (i%2) == 1 ){
				item.style.borderLeft = "1px solid white";
			}
			else{
				item.style.borderLeft = "1px dotted #ccc";
			}
			if( i > 1 ) {
				item.style.borderTop = "1px solid white";
			}
			else {
				item.style.borderTop = "1px dotted #ccc";
			}
		}
	}
}



var swfVersion;
function swf_show () {
	if (!swfVersion) swfVersion = swf_version();
	if (6 > swfVersion[0]) return;
	var div = document.getElementsByTagName("div");
	var swfImg = new Array();
	var swf = new Array();
	var cnt = 0;
	for ( var i = 0; i < div.length; ++i ) {
		var did = div[i].id;
		if ( null == did || 0 == did.length ) continue;
		if ( 6 < did.length && "swfImg" == did.substring(0,6) ) swfImg[cnt] = div[i];
		else if ( 3 < did.length && "swf" == did.substring(0,3) ) swf[cnt++] = div[i];
	}
	for ( var i = 0; i < swf.length; ++i ) {
		var cn = swfImg[i].className;
		if ( null == cn ) continue;
		swfImg[i].className = cn + " invisible";
		cn = swf[i].className;
		if ( null == cn ) continue;
		var cls = cn.split(" ");
		swf[i].className = cls[0];
	}
	var bannerSwf = document.getElementById('banner_right_swf');
	if( bannerSwf ) {
		bannerSwf.style.display = "block";
	}
	var bannerImg = document.getElementById('banner_right_img');
	if( bannerImg ) {
		bannerImg.style.display = "none";
	}
}


function swf_version () {
	var ver = new Array(3);
	if ( navigator.plugins && navigator.mimeTypes.length ) {
		var x = navigator.plugins["Shockwave Flash"];
		if ( x && x.description ) 
			return x.description.replace(/([a-zA-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".").split(".");
	} else {
		var x = null;
		try { 
			var x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		} catch( e ) { 
			try { 
				var x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				ver[0] = 6; ver[1] = 0; ver[2] = 21;
				x.AllowScriptAccess="always";
			} catch(e) {
				if ( 6 == ver[0] ) { return ver; } 
			}
			try {
				x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} catch(e) {}
		}
		if ( null != x ) return x.GetVariable("$version").split(" ")[1].split(",");
	}
	ver[0] = -1; ver[1] = -1; ver[2] = -1;
	return ver;
}


function close_banner ()
{
	var div = document.getElementById('banner_content');
	if( div ) {
		div.style.display = 'none';
	}
	swf_show();
}


var layer, layeron; // set in db/layer.js
function move_banner (init)
{
	var div = document.getElementById('banner_content');
	if (!div || !layer) return;
	if ('init' === init) { // FF calls timeout with params
		if (!layeron) layeron = 'HOME';
		var path = location.pathname;
		if ('/' == path) path = '/go/place';
		if (0 > path.indexOf('/go/place')) return;
		var i = path.indexOf('!');
		var place = 0>i ? 'HOME' : path.substr(i+5, 4);
		if (layeron != place) return;
		var visits = 1;
		var match = /layer=(\d+):(\w+)/.exec(document.cookie || '');
		if (match && layeron == match[2] && 3 < (visits += +match[1])) return;
		// console.log('visits='+visits+' init='+init);
		var newcookie = 'layer='+visits+':'+layeron+'; path=/; expires='+
			(new Date(6*7*86400*1000 + (new Date()).getTime())).toUTCString();
		document.cookie = newcookie;
		if ('/shop' == layer.substr(0,5)) {	// -> https
			var protohorst = 'https://'+location.hostname;
			if (location.port) protohorst += ':'+location.port.replace('080','443');
			layer = protohorst+layer;
		}
		document.getElementById('layerlink').href = layer;
		div.style.display = 'block';
	}
	var left = div.style.left.replace('px', '') / 1;
	if (left >= 50) {
		div.style.left = '50px';
		return;
	}
	left = left + 10;
	
	div.style.left = left + 'px';
	setTimeout(move_banner, 50);
	return true;
}


function dyntag ()
{
	if (!dyntags) return;
	var e = dyntags.pop();
	if (!e) return;
	e.className = ''; // strip the dyn
	setTimeout(dyntag, 900);
}

