function cleanLists() {
	$("ul").each(function() {
		for (var i = 0; i < this.childNodes.length; i++) {
			var node = this.childNodes[i];
			if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
				this.removeChild(node);
		}
		return this;
	});
}

function dropNav() {
	// When the child UL is visible, change the class of the parent link.
	$("div#nav ul ul").hover(function() {
		$(this).parents("li").children("a").addClass("selected");
		},function() {
		$(this).parents("li").children("a").removeClass("selected");
	});
	// Add the hover class to the child LIs to shift the left attribute.
	$("div#nav > ul > li").hover(function() { 
		$(this).children("ul:eq(0)").css("left", "auto");
	},function() {
		$(this).children("ul:eq(0)").css("left", "-9999px");
	});
}

function swapDefault() {
	$("input[type=text]").each(function() {
		$(this).focus(function() {
			if(this.defaultValue && this.value == this.defaultValue) this.value = "";
			$(this).addClass("focus");
		}).blur(function() {
			if(this.defaultValue && !this.value.length) this.value = this.defaultValue;
			$(this).removeClass("focus");
		});
	});
}

function targetLinks() {
	$("a").click(function() {
		if (this.getAttribute("href") && this.getAttribute("rel") == "external") this.target = "_blank";
	});
}

// Shows the appropriate content when properties tabs are clicked

function toggleProperties() {
	var a = $("div.property div.box-body > ul li a");
	if (a.length == 0) {
	  setTimeout("toggleProperties()", 5000);
	}
  a.click(function() {
		var anchors		= $(this).parents("ul").find("a");
		var container	= $(this).parents("div.box-body");
		container.children("div").hide();
		for (i = 0; i < anchors.length; i++) {
			if (anchors[i] == this) container.children("div:eq("+i+")").show();
		}
		anchors.removeClass("selected");
		$(this).addClass("selected");
		if (window.event) window.event.returnValue = false;
		return false;
	});
}

// Gallery Thumbnail View

function galleryThumbs() {
	$("ul.thumbs li").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	$("ul.thumbs li").click(function() {
		loc	= $(this).children("a:first").attr("href");
		location.href	= loc;
	});
	$("ul.thumbs-small li").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	$("ul.thumbs-small li").click(function() {
		loc	= $(this).children("a:first").attr("href");
		location.href	= loc;
	});
}

// Gallery Medium Image View

function resizeMed() {
	var x, y;
	var box				= $("div.image-med");
	var image			= $("div.image-med img");
	var boxPad			= parseInt(box.css("paddingLeft")) + parseInt(box.css("paddingRight"));
	var imagePad		= parseInt(image.css("paddingLeft")) + parseInt(image.css("paddingRight")) + ( parseInt(image.css("borderLeftWidth")) * 2);
	var boxWidth		= box.width() - boxPad;
	var imgWidth		= image.attr("width") - imagePad;
	var imgHeight		= image.attr("height");
	x	= boxWidth / imgWidth;
	
	if ( x < 1 ) {
		y	= Math.round( x * imgHeight );
		x	= boxWidth - imagePad;
		image.css( { width : x, height : y } );	
	}
}

//Movement APi

var tickMoveStep = false;
var now = (new Date()).getTime();;
var lasttick = now;
var delta_t = 0;

function get_coords(d) {
  var x = 0;
  var y = 0;
  var p = d;
  while (p.offsetParent) {
      x += p.offsetLeft;
      y += p.offsetTop;
    p = p.offsetParent;
  }
  var n = new Object();
  n.x = x;
  n.y = y;
  return n;
}


function getscrollpos() {
  var r = new Object();
  if (window.innerWidth == undefined) {
    r.w = document.body.clientWidth;
    r.h = document.body.clientHeight;
    r.x = document.body.scrollLeft;
    r.y = document.body.scrollTop;
    r.max_y = document.body.scrollHeight;
  }
  else {
    r.w = window.innerWidth;
    r.h = window.innerHeight;
    r.x = window.scrollX;
    r.y = window.scrollY;
    r.max_y = window.scrollMaxY;
  }
  return r;
}

function findChildOfClass(el, childClass) {
	var a = el.childNodes;
	var i, p;
	for (i = 0; i < a.length; i++) {
		p = a[i];
		if (p.className == childClass) return p;
		p = findChildOfClass(p, childClass);
		if (p) return p;
	}
	return null;
}

function wrap(val, lim) {
  while (val < 0) val += lim;
  return val % lim;
}

var movings = new Array();
function movePropertyTo(property, target, post, delta, integral, donefunc) {
  if (Math.abs(parseFloat(eval(property)) - target) < delta) {
    eval(property + " = " + target);
    if (donefunc) donefunc();
    return undefined;
  }
  var m = new Object();
  var mnew = true;
  for (var i = 0; i < movings.length; i++) {
    if (movings[i].property == property) {
      m = movings[i];
      mnew = false;
    }
  }
  m.property = property;
  m.delta = delta;
  m.target = target;
  m.orig = parseFloat(eval(property));
  m.dist = Math.abs(m.orig - m.target);
  m.post = post;
  m.integral = integral;
  m.donefunc = donefunc;
  m.on = true;
  m.curval = m.orig;
  if (mnew) {
   movings[movings.length] = m;
   }
  if (!tickMoveStep) {
    tickMoveStep = true;
    now = (new Date()).getTime() - 30;
    tick();
  }
  return m;
}
function moveStep(delta) {
  var i, m, o, c = 0, t, l;
  for (i = 0; i < movings.length; i++) {
    m = movings[i];
    if (m.on == false) continue;
    c++;
    o = m.curval;
    if (m.target == m.orig) t = 0.5;
    else t = (o - m.orig) / (m.target - m.orig);
    if (t < 0.1) l = 0.9;
    else if (t > 0.6) l = 0.7;
    else {
      if (t > 0.5) t = 0.5 - (t - 0.5);
      l = 0.9 - (0.3 * t * 2);
      }
    l = Math.pow(l, delta / 30);
  	outVal = o * l + m.target * (1 - l);
  	m.curval = outVal;
  	if (m.integral) outVal = Math.round(outVal);
  	if (isNaN(outVal)) {
  	    movings[i].on = false;
  	    try {
  	     eval(m.property + " = " + m.target + (m.post ? (" + '" + m.post + "'") : "") + ";");
  	     if (m.donefunc) m.donefunc();
  	    } catch (e) {}
  	    continue;
  	}
    try {
  	    var s = m.property + " = " + outVal + (m.post ? (" + '" + m.post + "'") : "") + ";";
  	    eval(s);
    }
  	catch (e) {}
    if (Math.abs(o - outVal) < m.delta) {
      try {
      	s = m.property + " = " + m.target + (m.post ? (" + '" + m.post + "'") : "") + ";";
      	eval(s);
        m.on = false;
  	    if (m.donefunc) m.donefunc();
        } catch (e) {}
      }
    }
  if (c) {
  	tickMoveStep = true;
  }
  else {
    tickMoveStep = false;
  	movings = new Array();
  }
}
function tick() {
  lasttick = now;
	now = (new Date()).getTime();
	if (tickMoveStep) moveStep(now - lasttick);
	if (tickMoveStep) {
    setTimeout('tick()', 10);
	}
}

var backdrop, playerdiv, backimage;

function videoPlay(flv, el) {
  backimage = document.createElement("div");
  backimage.className = 'videoplayerimage';
  backimage.style.opacity = 0.01;
  backimage.style.filter = "alpha(opacity=80)";
//  backimage.style.display = 'none';
  backimage.onclick = videoClose;
  backdrop = document.createElement("div");
  backdrop.className = 'videoplayerbackdrop';
  backdrop.style.opacity = 0.01;
  backdrop.style.filter = "alpha(opacity=80)";
  backdrop.onclick = videoClose;
  playerdiv = document.createElement("div");
  playerdiv.className = 'videoplayerdiv';
  playerdiv.style.position = 'fixed';
  playerdiv.style.left = (document.body.scrollWidth - 30) / 2 + "px";
  playerdiv.style.top = "-30px"; //"150px";
  playerdiv.style.width = "30px"; //"540px";
  playerdiv.style.height = "30px"; //"334px";
  playerdiv.style.verticalAlign = "middle";
  el = findChildOfClass(el.parentNode, "playerhtml");
  if (el) playerdiv.innerHTML = ""  
//    + "<div class='vpul'>BEEES</div><div class='vpur'></div><div class='vpupper'></div><div class='vpcontent'>"
    + el.innerHTML
//    + "</div><div class='vpbl'></div><div class='vpbr'></div><div class='vplower'></div>"
    ;  
  document.body.appendChild(backdrop);
  document.body.appendChild(backimage);
  document.body.appendChild(playerdiv);
  movePropertyTo("playerdiv.style.top", 300, "px", 0.1, false, function () {
    movePropertyTo("playerdiv.style.width", 860, "px", 0.1, false, false);
    movePropertyTo("playerdiv.style.left", (document.body.scrollWidth - 860) / 2, "px", 0.1, false, function () {
      movePropertyTo("playerdiv.style.top", 150, "px", 0.1, false, false);
      movePropertyTo("playerdiv.style.height", 478, "px", 0.1, false, function () {
        movePropertyTo("backimage.style.opacity", 0.99, '', 0.01, false, function () {
          movePropertyTo("backdrop.style.opacity", 0.8, '', 0.01, false, function () {
            });
          });
        });
      });
    });
  if (window.event) window.event.returnValue = false;
  return false;
}

function videoClose() {
    playerdiv.innerHTML = '';
    movePropertyTo("backimage.style.opacity", 0.01, '', 0.01, false, function () {
      document.body.removeChild(backimage); backimage = undefined;
      });
    movePropertyTo("backdrop.style.opacity", 0.01, '', 0.01, false, function () {
      document.body.removeChild(backdrop); backdrop = undefined;
      });
    movePropertyTo("playerdiv.style.height", 30, 'px', 0.1, false, false);
    movePropertyTo("playerdiv.style.top", 300, 'px', 0.1, false, function () {
      movePropertyTo("playerdiv.style.left", (document.body.scrollWidth - 30) / 2, "px", 0.1, false, false);
      movePropertyTo("playerdiv.style.width", 30, 'px', 0.1, false, function () {
          movePropertyTo("playerdiv.style.top", -90, 'px', 0.1, false, function () {
            document.body.removeChild(playerdiv); playerdiv = undefined; });
        });
      });
}

var playinfo = new Object();
function getUpdate(type, arg1, arg2) {
  if (type == 'item') {
    var the_object;
    var http_request = new XMLHttpRequest();
    http_request.open("GET", "/viproxy.php?idx=" + arg1 + "&blog=" + window.vblog, true);
    http_request.onreadystatechange = function () {
      if (http_request.readyState == 4) {
        if (http_request.status == 200) {
          playinfo = new Object(); 
          eval(http_request.responseText);
          infoUpdated();
        }
        http_request = null;
      }
    };
    http_request.send();
  }
}

function infoUpdated() {
  for (var i in playinfo) {
    var el = document.getElementsByName('vp_' + i);
    if (el.length) {
      el = el[el.length - 1];
      if (el.tagName == 'A') {
        //Set HREF for links
        el.href = playinfo[i];
      }
      else {
        el.innerHTML = playinfo[i];
      }
    }
  }
}

//	-------------------------------------
//	Favorites
//	-------------------------------------
	
function favorites ()
{
	$("li.favorites a").click( function(event) {
		var id		= $(this);
		var del		= "";

		if ( $(this).attr("class") == "delete" )
		{
			del	= "/delete";
		}

		$(id).addClass("loading");

		$.post( $(this).attr("href") + del, {}, function(data)
		{
			if ( data.substr(0,1) == "!" )
			{
				alert( data );
				
				return;
			}
			else
			{
				alert( data );
				if ( del == "" )
				{
					$(id).text("Unfavorite it!");
					$(id).addClass("delete");
				}
				else
				{
					$(id).text("Favorite it!");
					$(id).removeClass("delete");
				}
			}

			$(id).removeClass("loading");
		});
		
		return false;
	});
}

//	-------------------------------------
//	Stickies
//	-------------------------------------

function stickies ()
{
	$("a.stickies").click( function(event)
	{
		$.post( "/ajax/stickies/" + $(this).attr("alt"), {}, function(data)
		{
			if ( data.substr(0,1) == "!" )
			{
			}
			else
			{
			}
		});
		
		$("div#stickies").fadeOut("slow");
		
		return false;
	});
}

var footer_loaded = 0;
function footer_ajax() {
	var el = document.getElementById('footer_placeholder');
	if (!el) return;
	var a = getscrollpos();
	var b = get_coords(el);
	if (a.y + a.h > b.y - 500 && !footer_loaded) {
		footer_loaded = 1;
		el.innerHTML = '';
	    var http_request = new XMLHttpRequest();
	    http_request.open("GET", "/viproxy.php?func=footer_ajax", true);
	    http_request.onreadystatechange = function () {
	      if (http_request.readyState == 4) {
	        if (http_request.status == 200) {
	          el.innerHTML = http_request.responseText;
	        }
	        http_request = null;
	      }
	    };
	    http_request.send();		
	}
}
window.onscroll = footer_ajax;

sponsorclear = function () { 
    var el = document.getElementById('sponsored-linksbutton');
    if (!el) setTimeout("sponsorclear()", 1000);
    else {
        if (el.scrollHeight == 24) el.style.display = 'none';
    }
    };
$(document).ready(sponsorclear);


iphonecheck = function() {
    if (navigator.platform == 'iPhone') {
        $("span.ifiphone").css('display', 'inline');
        $("span.notiphone").css('display', 'none');
    }
}
$(document).ready(iphonecheck);

//Get out of here, you!