// © Marc Diethelm, boo@chic-happens.ch
// Generic DHTML Library Cross-Browser Cross-Platform


// Platform Sniff
var UAString;
var is_windows;
var is_mac;
	
UAString = navigator.userAgent.toLowerCase();
if (UAString.indexOf('windows') != -1) {
	is_windows = true;
} else if (UAString.indexOf('mac') != -1) {
	is_mac = true;
}

//alert(UAString);

// Browser Sniff / Layer Stuff: DOM, IE4 or Netscape 4?

var is_DOM = true;
var is_ie4 = true;
var is_nav4 = true;
var is_gecko = false;
var is_firefox = false;

/*if(UAString.indexOf('gecko') != -1) {
	is_gecko = true;	
	if (UAString.indexOf('firefox') != -1) {
		is_firefox = true;
	}
}*/

if (!document.getElementById) is_DOM = false;
if (typeof document.all == 'undefined') is_ie4 = false;
if (typeof document.layers == 'undefined') {is_nav4 = false}else{document.location='nav4.html';}

var is_ie = false;
if (UAString.indexOf('msie') != -1) {
	is_ie = true;
}


/* COOKIE LIB
setCookie(name, value, days, path, domain, secure), 
readCookie(name), 
killCookie(name, path, domain)
*/	
// This next little bit of code tests whether the user accepts cookies.

if (typeof cookies == "undefined") { // if not checked yet (no cookie info in session / no session)
  //alert("testing cookies");
  var acceptsCookies = 0;
  if (document.cookie == '') { // if no cookie for domain exists
  	document.cookie = 'acceptsCookies=1'; // Try to set a cookie.
  	if (document.cookie.indexOf('acceptsCookies=1') != -1) {
  	acceptsCookies = 1; // If it succeeds, set variable
  	killCookie('acceptsCookies');
  	}
  }else { // there was already a cookie
  
  	acceptsCookies = 1;
  	//killCookie('acceptsCookies');
  }
  
  if ( !acceptsCookies == 1 ) {
  	/*alert('Your browser does not allow cookies to be set.\n\n'+
  	'To enjoy all features of this site you need a cookies enabled browser.');*/
  } else {
    //alert("munch, munch!");
  }
} else { // already set in session
  acceptsCookies = 1;
}

var hours = 24;
function setCookie(name, value, days, path, domain, secure) {

	if (acceptsCookies == 1) { // Don't waste your time if the browser doesn't accept cookies.
	
		path = "/";
		if (hours) {
			var expires;
			if ( (typeof(days) == 'string') && Date.parse(days) ) { // already a Date string
				expires = days;
			} else if (typeof(days) == 'number') { // calculate Date from number of days
				expires = (new Date((new Date()).getTime() + days*3600000*24)).toGMTString();
			}
		}
		//alert(name + '=' + escape(value) + ((expires)?(';expires=' + expires):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''));
		document.cookie = name + '=' + escape(value) + ((expires)?(';expires=' + expires):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.

	} else {
  	/*alert('Your browser does not allow cookies to be set.\n\n'+
  	'To enjoy all features of this site you need a cookies enabled browser.');*/
  }
} // setCookie

		
function readCookie(name) {
	if (document.cookie === '') { // there's no cookie, so go no further
		return false;
	} else { // there is a cookie
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
		var NN2Hack = firstChar + name.length;
		if ((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
			firstChar += name.length + 1; // skip 'name' and '='
			lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
			if(lastChar == -1) lastChar = theBigCookie.length;
			return unescape(theBigCookie.substring(firstChar, lastChar));
		}else { // If there was no cookie of that name, return false.
			return false;
		}
    }	
}


function killCookie(name, path, domain) {
	var theValue = readCookie(name); // We need the value to kill the cookie
	if(theValue) {
		document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
	}
}

/********** Assign Specific Browser and Platform Style Sheets  ************/


var user_style_pref;

function getUserStylePref() {

	if (user_style_pref = readCookie('userStylePref'))  {
		//alert('cookie user_style_pref = '+ user_style_pref);
		if (user_style_pref != 'default') {
			loadStyle(user_style_pref);
		}
		return user_style_pref;
	} else {
		// alert('no cookie'); // user_style_pref is now = false
		user_style_pref = "default";
		return 'default';
	}
	
}

getUserStylePref();

// ***************** Dynamic Stylesheets Code *****************************
	
function loadStyle(url) {

	//url = (url == 'new_dark' ? 'default': url);

	if (is_ie) { // IE Hack: 'disabled' property: proprietary implementation.
	
		//disable all selectable (i.e. non-specific) ss with a title
		var linkElements;
		if (opener) {
			linkElements = opener.top.document.getElementsByTagName('link');
		} else {
			linkElements = document.getElementsByTagName('link');
		}
		for (i=0;i<linkElements.length;i++) {
			if (linkElements[i].rel == 'alternate stylesheet' && linkElements[i].title) {
				linkElements[i].disabled = true;
				document.getElementsByTagName('link')[i].disabled = true;
			}
		}
		
		//enable desired ss
		if (opener) {
			// main browser window (IF called from styler window)
			opener.top.document.getElementById(url).disabled = false;
		}
			// caller window (styler or main window)
		document.getElementById(url).disabled = false;
		
	} else { // not IE:
		if (document.getElementById('default')) {
			if (opener) {
				opener.top.document.getElementById('default').href = 'CSS.'+url+'.css';
			}
			document.getElementById('default').href = 'CSS.'+url+'.css';
		}
	}
	if (acceptsCookies) {
		saveStyle(url);
	} else {
		alert('Your browser does not allow cookies to be set!');
	}
}


function saveStyle(url) {
	var days_to_live = 90;
	
	if (document.getElementById('savePref')) {
	
		var save_today_only = document.getElementById('savePref').checked;
		if (save_today_only) {
			var now = new Date();
			var year = now.getFullYear();
			var month = now.getMonth();
			var tomorrow = 1+now.getDate();
			
			var days_to_live = new Date(year,month,tomorrow).toGMTString();
		}
	}
	
	if (url) {
		setCookie('userStylePref',url,days_to_live);
	}
}

// ***************** Layer Visibility *****************************

// Layer Visibility Switch (id/name, true/false)
// Cross Browser Script (DOM, ie4 are supported)
// use: (id/name, true/false)

function showDIV(div_name,on) {
	if(on) {
		if(is_DOM && document.getElementById(div_name)) {//check: is object still available? (eg.: onunload)
			document.getElementById(div_name).style.visibility = "visible"; return "on";
		}else if(is_ie4 && document.all[div_name]) {
			document.all[div_name].style.visibility = "visible"; return "on";
		}
	}else {
		if(is_DOM && document.getElementById(div_name)) {//check: is object still available? (eg.: onunload)
			document.getElementById(div_name).style.visibility = "hidden"; return "off";
		}else if(is_ie4 && document.all[div_name].style) {
			document.all[div_name].style.visibility = "hidden"; return "off";
		}
	}
	return false;
}

/********** new windows **********/

var transmit = null; // the newly opened window will fetch the picture url onload() from here.
var popup_counter = 0;

function picViewer(w,h) {
	transmit = filename;
	viewerWin = window.open('picture_viewer.html','popup_win_01','toolbar=no,location=no,status=no,resizable=no,directories=no,scrollbars=no,width='+w+',height='+h);
	centerWin('viewerWin',w,h);
}

function openNewWin(filename,windowName,w,h,win_type,statusbar,scrollbars) {

	if (typeof statusbar == 'undefined') {
		status_str = "statusbar=no,";
	} else if (statusbar === 1 && is_ie) {
		status_str = "status=yes,";
	} else if (statusbar === 1) {
		status_str = "statusbar=yes,";
	} else {
		status_str = "";
	}
	if (scrollbars == null) {
		var scroll_str = "scrollbars=no,";
	} else if (scrollbars == true) {
		var scroll_str = "scrollbars=yes,";
	} else if (scrollbars == 'auto') {
		var scroll_str = "scrollbars=auto,";
	} else {
		var scroll_str = "";
	}
	
	if (win_type == "modal") {
		win_type = "modal=yes,";
	} else if (win_type == "dependent") {
		win_type = "dependent=yes,";
	} else {
		win_type = "";
	}
	
	var win_str = 'toolbar=no,location=no,'+status_str+'resizable=no,directories=no,'+scroll_str+win_type+'width='+w+',height='+h;
	//alert(win_str);
	var newWin = window.open(filename,windowName,win_str);
	
	newWin.focus();
	centerWin(newWin,w,h);
}

function centerWin(myWin,w,h) {
	myWin.moveTo(screen.width/2-w/2,screen.height/2-h/2);
}