// Profile Javascript

req = null;
function getCommenterName() {
	cValue = "";
	dc = document.cookie;
	cookies = dc.split(";");
	for (x=0;x<cookies.length;x++) {
		if (cookies[x].match(/commenter_name=/)) {
		cValue = cookies[x].replace(/commenter_name=/, "");
		}
	}
	if (cValue.length>1) {
		return unescape(cValue);
	} else {
		return "";
	}
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function urlParameter(name)
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}


// Tabbed Sidebar Widgets

function showTabs(tab) {
	if (tab == 'category') {
		document.getElementById('tab-category-content').style.display="block";
		document.getElementById('tab-category').className="active";
		document.getElementById('tab-archives-content').style.display="none";
		document.getElementById('tab-archives').className="last";
		document.getElementById('tab-blogroll-content').style.display="none";
		document.getElementById('tab-blogroll').className="";
	}
	if (tab == 'archives') {
		document.getElementById('tab-category-content').style.display="none";
		document.getElementById('tab-category').className="";
		document.getElementById('tab-archives-content').style.display="block";
		document.getElementById('tab-archives').className="active last";
		document.getElementById('tab-blogroll-content').style.display="none";
		document.getElementById('tab-blogroll').className="";
	}
	if (tab == 'blogroll') {
		document.getElementById('tab-category-content').style.display="none";
		document.getElementById('tab-category').className="";
		document.getElementById('tab-archives-content').style.display="none";
		document.getElementById('tab-archives').className="last";
		document.getElementById('tab-blogroll-content').style.display="block";
		document.getElementById('tab-blogroll').className="active";
	}
	if (tab == 'comments') {
		document.getElementById('tab-comments-content').style.display="block";
		document.getElementById('tab-comments').className="active last";
		document.getElementById('tab-recommended-content').style.display="none";
		document.getElementById('tab-recommended').className="";
	}
	if (tab == 'recommended') {
		document.getElementById('tab-comments-content').style.display="none";
		document.getElementById('tab-comments').className="last";
		document.getElementById('tab-recommended-content').style.display="block";
		document.getElementById('tab-recommended').className="active";
	}
	return false;
}

// Cycles through recent comments in the sidebar

var tc = 0; // total comments count
var fci = 0; // The first comment to be displayed

function recentComments(dir) {
	if (dir == "start") {
		fci = 0; 
		arrowUp = document.getElementById('side-arrows-up');
		arrowDown = document.getElementById('side-arrows-down');
		x = document.getElementById('recent-comments-list').childNodes;
		commentslist = new Array();
		for (i=0; i < x.length; i++) {
			if (x[i].className == "recent-comment") { 
				commentslist[tc] = x[i].id;
				tc++;
			}
		}
	}
	if (commentslist.length > 3) {
		if (dir == "up" && fci != 0) { fci--; }
		if (dir == "down" && (fci < (commentslist.length - 3))) { fci++; }
		lci = fci + 3; // Last comment index
		hiddenComments = commentslist.slice();
		hiddenComments.splice(fci,3);
		
		for (b=0; b < hiddenComments.length; b++) {
			document.getElementById(hiddenComments[b]).style.display="none";
		}
		if (fci == 0) { arrowUp.src = "http://fashionista.com/images/side-arrow-up.gif"; arrowDown.src = "http://fashionista.com/images/side-arrow-down-on.gif"; }
		if (fci > 0 && fci < (commentslist.length - 3)) { arrowUp.src = "http://fashionista.com/images/side-arrow-up-on.gif"; arrowDown.src = "http://fashionista.com/images/side-arrow-down-on.gif"; }
		if (fci == (commentslist.length - 3)) { arrowUp.src = "http://fashionista.com/images/side-arrow-up-on.gif"; arrowDown.src = "http://fashionista.com/images/side-arrow-down.gif"; }
	} else {
		lci = commentslist.length;
	}
	for (c=fci; c < (lci); c++) {
		document.getElementById(commentslist[c]).style.display="block";
	}
							
	return false;
}


// Cycles through events  in the sidebar

var te = 0; // total events count
var fei = 0; // The first event to be displayed

function eventsList(dir) {
	if (dir == "start") {
		fei = 0; 
		eventsArrowUp = document.getElementById('events-arrows-up');
		eventsArrowDown = document.getElementById('events-arrows-down');
		x = document.getElementById('events-scroller').childNodes;
		eventslist = new Array();
		for (i=0; i < x.length; i++) {
			if (x[i].className == "event") { 
				eventslist[te] = x[i].id;
				te++;
			}
		}
	}
	
	if (eventslist.length > 3) {
		if (dir == "up" && fei != 0) { fei--; }
		if (dir == "down" && (fei < (eventslist.length - 3))) { fei++; }
		lei = fei + 3; // Last event index
		hiddenEvents = eventslist.slice();
		hiddenEvents.splice(fei,3);
		
		for (b=0; b < hiddenEvents.length; b++) {
			document.getElementById(hiddenEvents[b]).style.display="none";
		}
		if (fei == 0) { eventsArrowUp.src = "http://fashionista.com/images/side-arrow-up.gif"; eventsArrowDown.src = "http://fashionista.com/images/side-arrow-down-on.gif"; }
		if (fei > 0 && fei < (eventslist.length - 3)) { eventsArrowUp.src = "http://fashionista.com/images/side-arrow-up-on.gif"; eventsArrowDown.src = "http://fashionista.com/images/side-arrow-down-on.gif"; }
		if (fei == (eventslist.length - 3)) { eventsArrowUp.src = "http://fashionista.com/images/side-arrow-up-on.gif"; eventsArrowDown.src = "http://fashionista.com/images/side-arrow-down.gif"; }
	} else {
		lei = eventslist.length;
	}
	for (e=fei; e < (lei); e++) {
		document.getElementById(eventslist[e]).style.display="block";
	}
							
	return false;
}





// 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 = 'guest@fashionista.com';
    //f.author.value = '';
    //f.url.value = '';
}

function fieldDefault(target,text,clear) {
	if (clear) {
		clear = clear;
	} else { 
		clear = 0; 
	}
	
	field = $(target);
	
	if (field.value == '') {
		field.addClassName('fieldDefault');
		field.value = text;
	} else if (field.value == text) {
		
		if (clear == 1) {
			field.value = '';
			field.removeClassName('fieldDefault');
		}	
		
	} else if ( (field.value != '') && (getCookie('mtcmtauth') != text) ) {
		field.removeClassName('fieldDefault');
	}
}

function prepareComments(override) {
	if (getCookie("mtcmtauth") != '') {
		document.comments_form.author.value = getCookie("mtcmtauth");
	} else if (document.comments_form.author.value == '') {
		document.comments_form.author.value = 'guest';
	}
	document.comments_form.url.value = getCookie("mtcmthome");
	
	if ((user.length>0) && (override != 'logout')) {
		$('accountCommentForm').show();
		var override = '';
	} else if (getCookie("mtcmtauth") || (override == 'logout')) { 
		document.comments_form.bakecookie.checked = true;
		$('basicCommentForm').show();
		var override = '';
	} else {
		$('accountLoginForm').show();
	}
	
	fieldDefault('comment-author','guest');
}

function toggleCommentForm(active) {
	if (active == 'basicCommentForm') {
		if (user.length>0) {
			$('accountLoginForm').hide();
			$('accountCommentForm').show();
			
			$('commentText_account').value = $('commentText').value;
			//previewCommentBody('account');
			//previewComment();
		} else {
			$('accountCommentForm').hide();
			$('accountLoginForm').show();
		}
		$('basicCommentForm').hide();
	}	else {
		$('accountCommentForm').hide();
		$('accountLoginForm').hide();
		$('basicCommentForm').show();	
		
		$('commentText').value = $('commentText_account').value;
		//previewCommentBody();
		//previewComment();
	}
	
}

function validateComment(f) {
	if (f.author == '') {
		f.author = 'guest';
	}
	if (f.bakecookie.checked) { 
		rememberMe(f); 
	} else { 
		forgetMe(); 
	}	
}

function previewCommentBody(type) {
	if ($('commentPreview').style.display == 'none') { 
		new Effect.BlindDown('commentPreview');
	}

	if(type == 'account') {
		var text = $('commentText_account').value;
	} else {
  	var text = $('commentText').value;
	}

	text = text.replace(/\n\n/, '<br /><br />');
  splitText = text.split(/\n/);
  var textPrev = $('previewBody');
  var i = 0;
  textPrev.innerHTML = '';
  for(i = 0; i < splitText.length; i++) {
    if(splitText[i].length > 0 ) {
      textPrev.innerHTML += splitText[i] + "<br />";
    }
  }
}

function previewComment(type) {
	if ($('commentPreview').style.display == 'none') { 
		new Effect.BlindDown('commentPreview');
	}
	if (type == 'account') {
		var name = getCommenterName();
		var url = '';
	} else {
		var name = $('comment-author').value;
		var url = $('comment-url').value;
	}

	var authPrev = $('previewAuthor');
	if (url) {
		authPrev.innerHTML = '<a href="' + url + '">' + name + '</a>';
	} else {
		authPrev.innerHTML = name;
	}
}

function checkUsername()
{   
		var checkResult = $('checkResult');
		var submit = $('comment-post');
	
    var username = $('comment-author').value;
    new Ajax.Updater(checkResult, 'http://fashionista.com/check-username.php?username=' + username, {
			asynchronous:true,
			onComplete: function() {
				if(checkResult.innerHTML != '') {
					submit.disabled = 'disabled';
					submit.hide();
				} else {
					submit.disabled = '';
					submit.show();
				}
			}
		});
}