
// xDef r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xDef()
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}

// xStr r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xStr(s)
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
  return true;
}

// xNum r2, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xNum()
{
  for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
  return true;
}

// xGetElementById r2, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}

// xGetElementsByTagName r5, Copyright 2002-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xGetElementsByTagName(t,p)
{
  var list = null;
  t = t || '*';
  p = xGetElementById(p) || document;
  if (typeof p.getElementsByTagName != 'undefined') { // DOM1
    list = p.getElementsByTagName(t);
    if (t=='*' && (!list || !list.length)) list = p.all; // IE5 '*' bug
  }
  else { // IE4 object model
    if (t=='*') list = p.all;
    else if (p.all && p.all.tags) list = p.all.tags(t);
  }
  return list || [];
}

// xGetElementsByClassName r7, Copyright 2002-2011 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xGetElementsByClassName(c,p,t,f)
{
  var r=[], re, e, i, l;
  re = new RegExp("(^|\\s)"+c+"(\\s|$)");
//  var e = p.getElementsByTagName(t);
  e = xGetElementsByTagName(t,p); // See xml comments.
  for (i=0, l=e.length; i<l; ++i) {
    if (re.test(e[i].className)) {
      r[r.length] = e[i];
      if (f) f(e[i]);
    }
  }
  return r;
}

// xParent r2, Copyright 2001-2010 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xParent(e, s)
{
  e = xGetElementById(e);
  if (e) {
    e = e.parentNode;
    if (s) {
      while (e && e.nodeName.toLowerCase() != s) e = e.parentNode;
    }
  }
  return e;
}

// xInnerHtml r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xInnerHtml(e,h)
{
  if(!(e=xGetElementById(e)) || !xStr(e.innerHTML)) return null;
  var s = e.innerHTML;
  if (xStr(h)) {e.innerHTML = h;}
  return s;
}

// xStyle r2, Copyright 2007,2010 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xStyle(p, v)
{
  var i, e;
  for (i = 2; i < arguments.length; ++i) {
    e = xGetElementById(arguments[i]);
    try { e.style[p] = v; }
    catch (ex) {
/*@cc_on
@if (@_jscript_version <= 5.7) // IE7 and down
if(p!='display'){continue;}var s='',t=e.tagName.toLowerCase();switch(t){case'table':case'tr':case'td':case'li':s='block';break;case'caption':s='inline';break;}e.style[p]=s;
@end @*/
    }
  }
}

// xLoadScript r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xLoadScript(url)
{
  if (document.createElement && document.getElementsByTagName) {
    var s = document.createElement('script');
    var h = document.getElementsByTagName('head');
    if (s && h.length) {
      s.src = url;
      h[0].appendChild(s);
    }
  }
}

// xOpacity r1, Copyright 2006-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

// Modified to handle IE6+ separately
// René Seindal (rene@seindal.dk)

function xOpacity(e, o)
{
  var set = xDef(o);
  //  if (set && o == 1) o = .9999; // FF1.0.2 but not needed in 1.5
  if(!(e=xGetElementById(e))) return 2; // error
  if (xStr(e.style.opacity)) { // CSS3
    if (set) e.style.opacity = o + '';
    else o = parseFloat(e.style.opacity);
  }
  if (e.filters && e.filters[0]) {
    if (xDef(e.filters[0].opacity)) {//if IE6+
      if (set) e.filters[0].opacity=o*100
      else o = e.filters[0].opacity/100
    }
    else { //else if IE5.5-
      if (set) e.style.filter = 'alpha(opacity=' + (100 * o) + ')';
      else if (e.filters && e.filters.alpha) { o = e.filters.alpha.opacity / 100; }
    }
  }
  else if (xStr(e.style.filter)) { // IE5.5+
    if (set) e.style.filter = 'alpha(opacity=' + (100 * o) + ')';
    else if (e.filters && e.filters.alpha) { o = e.filters.alpha.opacity / 100; }
  }
  else if (xStr(e.style.MozOpacity)) { // Gecko before CSS3 support
    if (set) e.style.MozOpacity = o + '';
    else o = parseFloat(e.style.MozOpacity);
  }
  else if (xStr(e.style.KhtmlOpacity)) { // Konquerer and Safari
    if (set) e.style.KhtmlOpacity = o + '';
    else o = parseFloat(e.style.KhtmlOpacity);
  }
  return isNaN(o) ? 1 : o; // if NaN, should this return an error instead of 1?
}

// xCamelize r1, Copyright 2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xCamelize(cssPropStr)
{
  var i, c, a = cssPropStr.split('-');
  var s = a[0];
  for (i=1; i<a.length; ++i) {
    c = a[i].charAt(0);
    s += a[i].replace(c, c.toUpperCase());
  }
  return s;
}

// xGetComputedStyle r7, Copyright 2002-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xGetComputedStyle(e, p, i)
{
  if(!(e=xGetElementById(e))) return null;
  var s, v = 'undefined', dv = document.defaultView;
  if(dv && dv.getComputedStyle){
    s = dv.getComputedStyle(e,'');
    if (s) v = s.getPropertyValue(p);
  }
  else if(e.currentStyle) {
    v = e.currentStyle[xCamelize(p)];
  }
  else return null;
  return i ? (parseInt(v) || 0) : v;
}

// xWidth r6, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xWidth(e,w)
{
  if(!(e=xGetElementById(e))) return 0;
  if (xNum(w)) {
    if (w<0) w = 0;
    else w=Math.round(w);
  }
  else w=-1;
  var css=xDef(e.style);
  if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    w = xClientWidth();
  }
  else if(css && xDef(e.offsetWidth) && xStr(e.style.width)) {
    if(w>=0) {
      var pl=0,pr=0,bl=0,br=0;
      if (document.compatMode=='CSS1Compat') {
        var gcs = xGetComputedStyle;
        pl=gcs(e,'padding-left',1);
        if (pl !== null) {
          pr=gcs(e,'padding-right',1);
          bl=gcs(e,'border-left-width',1);
          br=gcs(e,'border-right-width',1);
        }
        // Should we try this as a last resort?
        // At this point getComputedStyle and currentStyle do not exist.
        else if(xDef(e.offsetWidth,e.style.width)){
          e.style.width=w+'px';
          pl=e.offsetWidth-w;
        }
      }
      w-=(pl+pr+bl+br);
      if(isNaN(w)||w<0) return;
      else e.style.width=w+'px';
    }
    w=e.offsetWidth;
  }
  else if(css && xDef(e.style.pixelWidth)) {
    if(w>=0) e.style.pixelWidth=w;
    w=e.style.pixelWidth;
  }
  return w;
}

// My little utilities

// Grab first image in content area for replacement by slideshow
function getContentImg(n) {
	if (n === undefined) n = 0
	var content = xGetElementById("content")
	if (content) {
		var imgs = xGetElementsByTagName("IMG",content)
		if (imgs && imgs.length > n)
			return imgs[n]
	}
	return null
}

var vkSlideData

function vkCheckForSlide() {
	var content = xGetElementById("content")
	if (!content) return

	// Webcam check
	var webcam = document.getElementById("webcam")
	if (webcam) {
		setInterval(function() {
			var i =  document.getElementById("webcam")
			var e = i.src.lastIndexOf('?')
			i.src = i.src.substring(0,(e<0?i.src.length:e))+'?'+new Date().getTime()
		}, 8000)
	}


	var imgs = xGetElementsByTagName("IMG",content)
	if (!imgs || imgs.length == 0) return

	var slideshows = new Object()
	for (var i=0; i<imgs.length; i++) {
		var img = imgs[i]

		if (!img.alt) continue
		var argstart = img.alt.lastIndexOf('//slide album:')
		if (argstart < 0) continue
		var argv = img.alt.substring(argstart+8).split(/ +/)
		var args = new Object;
		for (var a in argv) {
			var m = argv[a].match(/([a-z]+):(.+)/)
			if (m) {
				var v = parseInt(m[2]);
				args[m[1]] = isNaN(v) ? m[2] : v
			}
		}
		if (xDef(args.album) && args.album)
			slideshows[args.album] = [ img, args ]
	}
	if (!slideshows) return

	vkSlideData = slideshows

	// Load slideshow code
	xLoadScript('/local/slideshow.js')
	// Load slideshow data with callback
	for (var i in slideshows) {
		var args = slideshows[i][1]
		var script = '/local/picasa.php/'+args.album+'?cb=1'
		if (xDef(args.size))
			script += '&size='+args.size
		xLoadScript(script)
	}
}

// this is called from picasa.php?cb=1
function vkSlideStart(album, data) {
	if (!vkSlideData[album])
		return

	var img = vkSlideData[album][0]
	var args = vkSlideData[album][1]

	// Trailing comma might lead to null element at the end
	if (!data[data.length-1])
		data.pop()

	// Adjust delay to msec
	if (xDef(args.delay))
		args.delay *= 1000

	// Site policy - large slideshows are with captions
	if (!xDef(args.showcaption) && xDef(args.size) && args.size >= 400)
		args.showcaption = 'below'

	if (xNum(args.wait))
		setTimeout(function(){new fadeshow(data, img, args)}, args.wait*1000)
	else
		new fadeshow(data, img, args)
}

var vkSiteHeaderImages=[
"/wp-content/themes/venice/images/kubrickheader.jpg",
"/upload/header/IMG_2422.jpg",
"/upload/header/IMG_2626.jpg",
"/upload/header/IMG_2637.jpg",
"/upload/header/IMG_2742.jpg",
"/upload/header/IMG_2864.jpg",
"/upload/header/IMG_2978.jpg",
"/upload/header/IMG_3078.jpg",
"/upload/header/IMG_3320.jpg",
"/upload/header/IMG_3322.jpg",
"/upload/header/IMG_3561.jpg",
"/upload/header/IMG_3769.jpg",
"/upload/header/IMG_3819.jpg",
"/upload/header/IMG_4050.jpg",
"/upload/header/IMG_4053.jpg",
"/upload/header/IMG_4061.jpg",
"/upload/header/IMG_4216.jpg",
"/upload/header/IMG_4376.jpg",
"/upload/header/IMG_4383.jpg",
"/upload/header/IMG_4408b.jpg",
"/upload/header/IMG_4408.jpg",
"/upload/header/IMG_4413.jpg",
"/upload/header/IMG_4414.jpg",
"/upload/header/IMG_4459b.jpg",
"/upload/header/IMG_4459.jpg",
"/upload/header/IMG_5284.jpg",
"/upload/header/IMG_5289.jpg",
"/upload/header/IMG_5294.jpg",
"/upload/header/IMG_5559.jpg",
"/upload/header/IMG_5739.jpg",
"/upload/header/IMG_6451.jpg",
"/upload/header/IMG_6453.jpg",
"/upload/header/IMG_6482.jpg",
"/upload/header/IMG_6487.jpg",
"/upload/header/IMG_6489.jpg",
"/upload/header/P1080126.jpg",
"/upload/header/P1080128.jpg",
"/upload/header/P1080207.jpg"
];

function vkSiteHeaderSwap() {
	var n = Math.floor(Math.random() * vkSiteHeaderImages.length)
	xStyle("backgroundImage", "url("+vkSiteHeaderImages[n]+")", "header");
}



