var active = false;
var current;
var other;

function initSwap() {
	other.style.zIndex = 100;
	current.style.zIndex = 0;
}

function doFade(opacity) {
	other.style.opacity = opacity / 100;
	other.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="
+ (opacity) + ");";
	if (opacity >= 100) {
		other.style.filter = '';
		active = false;
	} else {
		setTimeout('doFade(' + (opacity + 10) + ')', 50);
	}
}

function swapPhotos(which) {
	if (active == true) return false;
	active = true;
	if (which.getElementsByTagName("div")[0].style.zIndex == '' ||
		which.getElementsByTagName("div")[0].style.zIndex == 0) {
		current = which.getElementsByTagName("div")[1];
		other = which.getElementsByTagName("div")[0];
	} else {
		current = which.getElementsByTagName("div")[0];
		other = which.getElementsByTagName("div")[1];
	}

	initSwap();
	doFade(10);
}

//////Rating functions

function displayRating(e, which) {
 changeRatingClass(which, 'r' + calculateClass(e, which));
}

function changeRatingClass(which, cls) {
 which.className = which.className.split(' ')[0] + ' ' + cls;
}

function resetRating(which, id) {
 rating = document.getElementById('rating' + id).value;
 changeRatingClass(which, 'r' + (rating == '' ? '0stars' : rating));
}

function setRating(e, which, id) {
 document.getElementById('rating' + id).value = calculateClass(e, which);
}

function calculateClass(e, which) {
 x = ((!e.layerX) ? window.event.x : e.layerX);
 x = x - which.offsetLeft + 1 - getPadding(which);

 rating = Math.ceil(x / ((which.offsetWidth-getPadding(which) * 2) / 10)) / 2;
 if (rating < 1) rating = 1;
 if (rating > 5) rating = 5;
 cls = (rating == 1) ?  rating + 'star' : rating + 'stars';
 cls = cls.replace('.', '');
 return cls;
}

function getPadding(obj) {
 if (obj.currentStyle) {
  return parseInt(obj.currentStyle.paddingLeft) + 
	 parseInt(obj.currentStyle.borderLeftWidth);
 } else {
  var s = document.defaultView.getComputedStyle(obj, '');
  return parseInt(s.getPropertyValue('padding-left')) +
	 parseInt(s.getPropertyValue('border-left-width'));
 }
}

 function pp_image_popup() {
   return false;
 }

 var allowToggle = true;
 function toggle(which) {
  if (!allowToggle) return;
  which.className = (which.className == 'hidden' ? 'visible' : 'hidden');
 }

 function togglecomments (postid) { 
  var whichpost = document.getElementById(postid); 
  if (whichpost.className=="comment_shown") { 
     whichpost.className="comment_hidden"; 
  } else { 
   whichpost.className="comment_shown"; 
  } 
 } 

 function onPage(which) {
  which.className = 'on';
  which.getElementsByTagName('a').item(0).className += ' on';
 }

 function offPage(which) {
  which.className = '';
  var cls = which.getElementsByTagName('a').item(0).className;
  which.getElementsByTagName('a').item(0).className = cls.split(' ')[0];
 }
