// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

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 hideDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'none';
}

function showDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'block';
}

var commenter_name, mtcmtemail, mtcmtauth, mtcmthome;

function individualArchivesOnLoad(commenter_name) {




    if (commenter_name) {
        hideDocumentElement('name-email');
        showDocumentElement('comments-open-text');
        showDocumentElement('comments-open-footer');
    } else {
        hideDocumentElement('comments-open-data');
        hideDocumentElement('comments-open-text');
        hideDocumentElement('comments-open-footer');
    }




    if (document.comments_form) {
        if (!commenter_name && (document.comments_form.email != undefined) &&
            (mtcmtmail = getCookie("mtcmtmail")))
            document.comments_form.email.value = mtcmtmail;
        if (!commenter_name && (document.comments_form.author != undefined) &&
            (mtcmtauth = getCookie("mtcmtauth")))
            document.comments_form.author.value = mtcmtauth;
        if (document.comments_form.url != undefined && 
            (mtcmthome = getCookie("mtcmthome")))
            document.comments_form.url.value = mtcmthome;
        if (document.comments_form["bakecookie"]) {
            if (mtcmtauth || mtcmthome) {
                document.comments_form.bakecookie.checked = true;
            } else {
                document.comments_form.bakecookie.checked = false;
            }
        }
    }
}

function writeTypeKeyGreeting(commenter_name, entry_id) {

    if (commenter_name) {
        document.write('<p>Thanks for signing in, ' + commenter_name +
          '. '+
          '(<a href="http://plugins.movabletype.org/cgi-bin/mt4community/mt-comments.cgi?__mode=handle_sign_in&amp;static=1&amp;logout=1&entry_id=' + entry_id + '">sign out</a>)</p>');
    } else {

        document.write('<p>Please sign in with '+
          '<a href="https://www.typekey.com/t/typekey/login?&amp;lang=en_US&amp;t=HsQ0UuNt30DxCXMW4lbr&amp;v=1.1&amp;_return=http://plugins.movabletype.org/cgi-bin/mt4community/mt-comments.cgi%3f__mode=handle_sign_in%26key=TypeKey%26static=1%26entry_id=' + entry_id + '">TypeKey</a> or <a href="#" showDocumentElement(\'openid-form\');">OpenID</a></p>');


    }

}


if ('plugins.movabletype.org' != 'plugins.movabletype.org') {
    document.write('<script src="http://plugins.movabletype.org/cgi-bin/mt4community/mt-comments.cgi?__mode=cmtr_name_js"></script>');
} else {
    commenter_name = getCookie('commenter_name');
}


/* AJAX Rating */

function updateRating(request) {
	/* alert(request.responseText);  // DEBUGGING */

	var response = eval('(' + request.responseText + ')');
	var status = response.error ? 2 : 1;
	var total = parseInt(response.total_score) - response.r;
    var count = parseInt(response.vote_count) -1;

	var indicator = document.getElementById('indicator_' + response.obj_type + response.obj_id);
	if(indicator)
		indicator.style.display = 'none';

	updatePage(response.obj_type, response.obj_id, response.r, response.blog_id, total, count, response.a, status, response.error);
	
	if(status == 1) {
		var voted = getCookie('ajaxrating_' + response.obj_type);
		if(!voted)
			voted = new Array();
		else
			voted = voted.split(',');
		voted[voted.length] = response.obj_id;
		var cookie = voted.join(',');
		var now = new Date();
	    fixDate(now);
	    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
	    now = now.toGMTString();
	    setCookie('ajaxrating_' + response.obj_type, cookie, now, '/', '', '');		
	}	
}

function pushRating(type,id,r,b,total,count,a) {
	if(!commenter_name) {
		alert('Please sign in to cast your vote!');
		document.location.hash='#comments-open';
		Fat.fade_element('typekey-greeting', 90, 2000, "#FDFF2F", "#FFFFFF")
		return;
	}
	
	var voted = getCookie('ajaxrating_' + type);
	if(voted) {
		var objs = voted.split(',');
		for (var i = 0; i < objs.length; i++) {
			if(objs[i] == id) {
				alert('You have already cast your vote!');
				updatePage(type,id,r,b,total,count,a, 2);
				return;				
			}
		}
	}
	
	var indicator = document.getElementById('indicator_' + type + id);
	if(indicator)
		indicator.style.display = 'block';
		
	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>'; }
    new ajax('/cgi-bin/mt4community/plugins/AjaxRating/mt-vote.cgi', {postBody: 'obj_type='+type+'&r='+r+'&obj_id='+id+'&blog_id='+b+'&a='+a, onComplete: updateRating});
}

function updatePage (type, id, r, b, total, count, a, status, error) {
	var tick = document.getElementById('tick_' + type + id);
	var cross = document.getElementById('cross_' + type + id);	
	
	if(status == 1) {
		
		var avg = Math.round(((total + r)/(count + 1))*10)/10;
		if (type == "comment") {
			var new_width =  Math.round(avg / 1 * 16);
		} else {
			var new_width =  Math.round(avg / 5 * 80);
		}
		var span_avg = document.getElementById("ajaxrating_" + type + "_" + id + "_avg");
		if (span_avg) {span_avg.innerHTML = avg; }
		var span_ttl = document.getElementById("ajaxrating_" + type + "_" + id + "_ttl");
		if (span_ttl) {span_ttl.innerHTML =  total + r; }
		var span_cnt = document.getElementById("ajaxrating_" + 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 =  ""; }
	
		if(tick)
			tick.style.display = 'block';
		if(cross)
			cross.style.display = 'none';
	} 
	
	if(status == 2) {
		if(tick)
			tick.style.display = 'none';
		if(cross) {
			cross.style.display = 'block';
			cross.setAttribute('alt', error);
			cross.alt = error;
			cross.setAttribute('title', error);
			cross.title = error;
		}
				
		alert(error);	
	}
}

function showComment(id) {
   var threshold = document.getElementById("threshold-" + id);
   if (threshold) {threshold.innerHTML =  ""; }
   var comment = document.getElementById("comment-" + id);
   if (comment) {comment.style.display = "block"; }
}

function hideComments(comments) {
      var threshold = getCookie("threshold");
      if (!threshold) { threshold = -9999; }
      for (var i=0; i < comments.length - 1; i++) {
          var comment = document.getElementById("comment-" +  comments[i]);
          var total = document.getElementById("ajaxrating_comment_" +  comments[i] + "_ttl").innerHTML;
          if (parseInt(total) < parseInt(threshold)) { 
              comment.style.display = "none"; 
              var threshold_span = document.getElementById("threshold-" + comments[i]);
              if (threshold_span) {threshold_span.style.display =  "block"; }
          }
      }
      if (document.threshold_form) { document.threshold_form.threshold.value = threshold; }
}

function setThreshold (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    setCookie('threshold', f.threshold.value, now, '/', '', '');
}
 
function reportComment (id) {
    new ajax('/cgi-bin/mt4community/plugins/AjaxRating/mt-report.cgi', {postBody: 'id='+id, onComplete: updateRating});
}

var scrolldelay;
function scrollForward() {
          document.getElementById('scrolling-frame').scrollLeft += 20;
          scrolldelay = setTimeout('scrollForward()',40);
}

function scrollBackward() {
          document.getElementById('scrolling-frame').scrollLeft -= 20;
          scrolldelay = setTimeout('scrollBackward()',40);
}

function cancelscroll() {
       clearTimeout(scrolldelay);
}

function resizeScreenshots() {
	var themelist = document.getElementById('screenlist');
	var wrap = document.getElementById('screens-wrap');
	var width = 0;
	if(themelist) {
		var imgs = themelist.getElementsByTagName('img');
		for (var i = 0; i < imgs.length; i++) {
			width = width + imgs[i].width;
			width = width + 40;
		}
		wrap.style.width = width + "px";
	}
}
