/*  Prototype JavaScript framework
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 *
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *
 *  For details, see the Prototype web site: http://prototype.conio.net/
 *
/*--------------------------------------------------------------------------*/


//note: this is a stripped down version of prototype, to be used with moo.fx by mad4milk (http://moofx.mad4milk.net).

var Class = {
  create: function() {
    return function() { 
      this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
    return __method.apply(object, arguments);
  }
}

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;

    elements.push(element);
  }

  return elements;
}

//-------------------------

document.getElementsByClassName = function(className) {
  var children = document.getElementsByTagName('*') || document.all;
  var elements = new Array();
  
  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
      if (classNames[j] == className) {
        elements.push(child);
        break;
      }
    }
  }
  
  return elements;
}

//-------------------------

if (!window.Element) {
  var Element = new Object();
}

Object.extend(Element, {
  remove: function(element) {
    element = $(element);
    element.parentNode.removeChild(element);
  },

  hasClassName: function(element, className) {
    element = $(element);
    if (!element)
      return;
    var a = element.className.split(' ');
    for (var i = 0; i < a.length; i++) {
      if (a[i] == className)
        return true;
    }
    return false;
  },

  addClassName: function(element, className) {
    element = $(element);
    Element.removeClassName(element, className);
    element.className += ' ' + className;
  },
  
  removeClassName: function(element, className) {
    element = $(element);
    if (!element)
      return;
    var newClassName = '';
    var a = element.className.split(' ');
    for (var i = 0; i < a.length; i++) {
      if (a[i] != className) {
        if (i > 0)
          newClassName += ' ';
        newClassName += a[i];
      }
    }
    element.className = newClassName;
  },
  
  // removes whitespace-only text node children
  cleanWhitespace: function(element) {
    element = $(element);
    for (var i = 0; i < element.childNodes.length; i++) {
      var node = element.childNodes[i];
      if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) 
        Element.remove(node);
    }
  }
});

//based on prototype's ajax class
//to be used with prototype.lite, moofx.mad4milk.net.

ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};

var gtype = 0;
var gid = 0;
var gr = 0;
var gb = 0;
var gtotal = 0;
var gcount = 0;
var ga = 0;
var gnum_units = 0;
var gimage = '';
var gw = 0;

function updateRating(request) {
	/*alert(request.responseText); // DEBUGGING */
	var resp_arr = request.responseText.split("||");
	if (resp_arr[0] == 'ERR') {
		alert(resp_arr[1]);
	}
	
	if (resp_arr[0] == 'OK') {
		var type = resp_arr[1];
		var id = resp_arr[2];
		var r = parseInt(resp_arr[3]);
		var b = 1;
		var total = parseInt(resp_arr[4]) - r;
		var count = parseInt(resp_arr[5]) -1;
		var a = 0;
		// var theUL = document.getElementById('rater_ul'+type+id); // the UL
		// if (theUL) { theUL.innerHTML = '<li id="rater_li'+type+id+'" class="current-rating" style="width:0px"></div>'; }
		//updatePage(type,id,r,b,total,count,a);
	}
}

function pushRating(type,id,r,b,total,count,a,num_units,image,w) {
	/*alert('here in pushrating, args are: obj_type='+type+'&r='+r+'&obj_id='+id+'&blog_id='+b+'&a='+a+'&num_units='+num_units+'&image_file='+image+'&unit_width='+w);*/
	updatePage(type,id,r,b,total,count,a);
	// var theUL = document.getElementById('rater_ul'+type+id); // the UL
	// if (theUL) { theUL.innerHTML = '<div class="loading"></div>'; }
	//window.setTimeout( 'ajaxWithTimeOut(type,id,r,b,total,count,a,num_units,image,w)', 30 );
	//window.setTimeout( 'ajaxWithTimeOut('+type+','+id+','+r+','+b+','+total+','+count+','+a+','+num_units+','+image+','+w+')', 30 );
gtype = type;
gid = id;
gr = r;
gb = b;
gtotal = total;
gcount = count;
ga = a;
gnum_units = num_units;
gimage = image;
gw = w;
	window.setTimeout( 'ajaxWithTimeOut()', 100 );
}

function ajaxWithTimeOut() {
	new ajax('/cgi-bin/plugins/RatingSystem/mt-vote.cgi', {postBody: 'obj_type='+gtype+'&r='+gr+'&obj_id='+gid+'&blog_id='+gb+'&a='+ga+'&num_units='+gnum_units+'&image_file='+gimage+'&unit_width='+gw, onComplete: updateRating});
}

function updatePage (type,id,r,b,total,count,a) {
	var avg = Math.round(((total + r)/(count + 1))*10)/10;
	var new_width = Math.round(avg / 10 * 300);
	var span_avg = document.getElementById("ratingsystem_" + type + "_" + id + "_avg");
	if (span_avg) {span_avg.innerHTML = avg; }
	var span_ttl = document.getElementById("ratingsystem_" + type + "_" + id + "_ttl");
	if (span_ttl) {span_ttl.innerHTML = total + r; }
	var span_cnt = document.getElementById("ratingsystem_" + type + "_" + id + "_cnt");
	if (span_cnt) {span_cnt.innerHTML = count + 1; }
	var rater_li = document.getElementById("rater_li" + type + id);
	if (rater_li) {rater_li.style.width = new_width.toString() + "px"; }
	var thumb = document.getElementById("thumb" + type + id);
	if (thumb) {thumb.innerHTML = ""; }
	var thanks = document.getElementById("thanks" + type + id);
	if (thanks) {thanks.innerHTML = "Thanks for voting!"; }
}

var commenter_name;

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    if (f.author != undefined)
       setCookie('mtcmtauth', f.author.value, now, '/', '', '');
    if (f.email != undefined)
       setCookie('mtcmtmail', f.email.value, now, '/', '', '');
    if (f.url != undefined)
       setCookie('mtcmthome', f.url.value, now, '/', '', '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '/', '');
    deleteCookie('mtcmthome', '/', '');
    deleteCookie('mtcmtauth', '/', '');
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function writeTypeKeyGreeting(commenter_name, entry_id) {
}

