// 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";
		
	}
	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";
		
	}
	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";
		
	}
	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 == 'viewed') {
		document.getElementById('tab-comments-content').style.display="none";
		document.getElementById('tab-comments').className="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://dealbreaker.com/images/side-arrow-up.gif"; arrowDown.src =
"http://dealbreaker.com/images/side-arrow-down-on.gif"; }

if (fci > 0 && fci < (commentslist.length - 3)) {
arrowUp.src = "http://dealbreaker.com/images/side-arrow-up-on.gif";
arrowDown.src = "http://dealbreaker.com/images/side-arrow-down-on.gif"; }

if (fci == (commentslist.length - 3)) { arrowUp.src =
"http://dealbreaker.com/images/side-arrow-up-on.gif"; arrowDown.src =
"http://dealbreaker.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 top stories in the sidebar



var ts = 0; // total stories count

var fsi = 0; // The first story to be displayed



function topStories(inst) {



	if (inst == "start") {

		fsi = 0; 

		arrowNext = document.getElementById('arrow-next');

		arrowPrevious = document.getElementById('arrow-previous');

		x = document.getElementById('top-stories').childNodes;

		storieslist = new Array();

		for (i=0; i < x.length; i++) {

			if (x[i].className == "top-story") { 

				storieslist[ts] = x[i].id;

				ts++;

			}

		}

	}

	if (storieslist.length > 3) {

		if (inst == "previous" && fsi != 0) { fsi--; }

		if (inst == "next" && (fsi < (storieslist.length - 3))) { fsi++; }

		lsi = fsi + 3; // Last comment index

		hiddenStories = storieslist.slice();

		hiddenStories.splice(fsi,3);

		

		for (b=0; b < hiddenStories.length; b++) {

			document.getElementById(hiddenStories[b]).style.display="none";

		}

if (fsi == 0) { arrowPrevious.src =
"http://dealbreaker.com/images/button-back.gif"; arrowNext.src =
"http://dealbreaker.com/images/button-forward-on.gif"; }

if (fsi > 0 && fsi < (storieslist.length - 3)) {
arrowPrevious.src = "http://dealbreaker.com/images/button-back-on.gif";
arrowNext.src = "http://dealbreaker.com/images/button-forward-on.gif"; }

if (fsi == (storieslist.length - 3)) { arrowPrevious.src =
"http://dealbreaker.com/images/button-back-on.gif"; arrowNext.src =
"http://dealbreaker.com/images/button-forward.gif"; }

	} else {

		lsi = storieslist.length;

	}

	for (c=fsi; c < (lsi); c++) {

		document.getElementById(storieslist[c]).style.display="block";

	}

							

	return false;

}




function topStories2(inst) {



	if (inst == "start") {

		fsi = 0; 

		arrowNext = $('arrow-next');

		arrowPrevious = $('arrow-previous');

		x = $('top-stories').childNodes;

		storieslist = new Array();

		for (i=0; i < x.length; i++) {

			if (x[i].className == "top-story") { 

				storieslist[ts] = x[i].id;

				ts++;

			}

		}

	}

	if (storieslist.length > 2) {

		if (inst == "previous" && fsi != 0) { fsi--; }

		if (inst == "next" && (fsi < (storieslist.length - 2))) { fsi++; }

		lsi = fsi + 2; // Last comment index

		hiddenStories = storieslist.slice();

		hiddenStories.splice(fsi,2);

		

		for (b=0; b < hiddenStories.length; b++) {

			$(hiddenStories[b]).style.display="none";

		}

if (fsi == 0) { arrowPrevious.src =
"http://dealbreaker.com/images/button-back.gif"; arrowNext.src =
"http://dealbreaker.com/images/button-forward-on.gif"; }

if (fsi > 0 && fsi < (storieslist.length - 2)) {
arrowPrevious.src = "http://dealbreaker.com/images/button-back-on.gif";
arrowNext.src = "http://dealbreaker.com/images/button-forward-on.gif"; }

if (fsi == (storieslist.length - 2)) { arrowPrevious.src =
"http://dealbreaker.com/images/button-back-on.gif"; arrowNext.src =
"http://dealbreaker.com/images/button-forward.gif"; }

	} else {

		lsi = storieslist.length;

	}

	for (c=fsi; c < (lsi); c++) {

		$(storieslist[c]).style.display="block";

	}

							

	return false;

}





// Get the current date and time

function showDate() {

	var currentTime = new Date();

var months = new
Array('January','February','March','April','May','June','July','August','September','October','November','December');

	var month = months[currentTime.getMonth()];

	var day = currentTime.getDate();

	var year = currentTime.getFullYear();

	var time = "";

	var pmam = "";

	var hours = currentTime.getHours();

	var minutes = currentTime.getMinutes();

	if (minutes < 10) {

		minutes = "0" + minutes;

	}

	if(hours > 11){

	pmam = "PM";

	} else {

	pmam = "AM";

	}

	if(hours >12) {

		hours -= 12;

	}

	time = hours + ":" + minutes + "&nbsp;"+ pmam;

	return(month + "&nbsp;" + day + ",&nbsp;" + year + "&nbsp;" + time);

}



// jumpmenu for footer

function MM_jumpMenu(targ,selObj,restore){ //v3.0

  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");

  if (restore) selObj.selectedIndex=0;

}



// field focus reset

function field_focus_reset(element, defaultString) {

if (element.value==defaultString) {

element.value="";

element.style.color="#000000";

}else{

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')) {
		$('basicCommentForm').hide();
		$('accountCommentForm').show();
		var override = '';
	} else if (getCookie("mtcmtauth") || (override == 'logout')) { 
		document.comments_form.bakecookie.checked = true;
		$('basicCommentForm').show();
		var override = '';
	} else {
		$('basicCommentForm').hide();
		$('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://dealbreaker.com/check-username.php?username=' + username, {
			asynchronous:true,
			onComplete: function() {
				if(checkResult.innerHTML != '') {
					submit.disabled = 'disabled';
					submit.hide();
				} else {
					submit.disabled = '';
					submit.show();
				}
			}
		});
}


