var userBlockStatus;

function userBlockToggle(ignoredUser){
	if (userBlockStatus){
		userBlockAjaxCall("/private-messaging/removeUserFromIgnoreList",ignoredUser)
		userBlockStatus = false;
	} else {
		userBlockAjaxCall("/private-messaging/addUserToIgnoreList",ignoredUser)
		userBlockStatus = true;
	}
}
		
function userBlockAjaxCall(url, xIgnoredUser) {	
	new Ajax.Request (url, {
		method: 'get',
		parameters: { user:subjectUser, ignoredUser:xIgnoredUser },
		onSuccess: function() {
			processBlockStatusButton(xIgnoredUser);
		}
	});
}

function getIgnoreList(username) {
	if (subjectUser !== objectUser) {
		var url = '/private-messaging/getIgnoreList';
		new Ajax.Request (url, {
			method: 'get',
			parameters: {user: username},
			onSuccess: function(data) {
				var myXmlDoc = domParseFromString(data.responseText);
				processBlockList(myXmlDoc);
			}
		});
	}
}

function domParseFromString(xmlobject) {
	//DOM Parser object
	// code for IE
	if (window.ActiveXObject) {
	  var doc=new ActiveXObject("Microsoft.XMLDOM");
	  doc.async="false";
	  doc.loadXML(xmlobject);
	}else {// code for Mozilla, Firefox, Opera, etc.
	  var parser=new DOMParser();
	  var doc=parser.parseFromString(xmlobject,"text/xml");
	}
	// documentElement always represents the root node
	var x=doc.documentElement;
	return x;
}

function processBlockList(myobj) {
	userBlockStatus = false;			
	var ignoredUserArray = myobj.getElementsByTagName("ignoredUser");
	for (var i=0;i<ignoredUserArray.length;i++){
		if (objectUser == ignoredUserArray[i].getAttribute("username")) {
			userBlockStatus = true;
		}
	}
	processBlockStatusButton();
}

function processBlockStatusButton() {
	if (userBlockStatus){ // user is blocked, activate the unblock button
		$('request_block_' + objectUser).innerHTML = button('unblock');
	} else { //else activate the block button
		$('request_block_' + objectUser).innerHTML = button('ignoreuser');
	}
//	$("#requestblockhref"+thisuser).fadeIn("slow");
}

function button(text) {
	return '<img src="/cm/marieclaire/images/v01/btn_cp_' + text + '.gif" alt="' + text + '" />';
}

function friend_request(them, act, obj){
	obj.removeAttribute('onclick');
	obj.innerHTML = button('pleasewait');
	obj.style.cursor = 'auto';
	new Ajax.Request ('/community/processfriend.pl', {
		parameters: { their_id:them, process:act },
		onSuccess: function(data){
			requestProcess(them,data.responseText,obj);
		}
	});
}

function requestProcess(them, data, obj){
	var datastring = data.replace(/\s+/g, '');
	switch(datastring.toLowerCase()){
		case "declinesucceed":
			friendsPending--;
			if (friendsPending == 0) {
				obj.parentNode.parentNode.parentNode.innerHTML = '<p style="padding:0 16px;">You have no more pending requests. You are now being taken back to your <a href="/community/profile/">profile</a>.</p>';
				window.location = window.location;
			} else {
				obj.parentNode.parentNode.style.display = 'none';
				$('back_to_profile_requests').setAttribute('onclick', 'window.location=window.location');
			}
//			insertinprofilerequests(friendsPending, subjectUser);
//			insertinloginrequests1(friendsPending, subjectUser);
//			if (friendsPending == 0) {$("#friendspendingcontainer").fadeOut(1000);} else {$("#friendlimbobox"+objectUser).fadeOut("slow");}
			// erase the box
			break;
		case "requestsucceed":
			obj.innerHTML = button('requestsent');			
//			$('request' + objectUser).innerHTML = '<img src="" alt="" />';
			// send in update status
			break;
		case "approvesucceed":
			friendsPending--;
			if (friendsPending == 0) {
				obj.parentNode.parentNode.parentNode.innerHTML = '<p style="padding:0 16px;">You have no more pending requests. You are now being taken back to your <a href="/community/profile/">profile</a>.</p>';
				window.location = window.location;
			} else {
				obj.parentNode.parentNode.style.display = 'none';
				$('back_to_profile_requests').setAttribute('onclick', 'window.location=window.location');
			}

//			$("#friendlistbox").append($("#friendlimbobox"+objectUser).fadeIn(1000));
//			if (friendsNumber == 0) {$("#friendlistbox").fadeIn(1000);}
//			insertinprofilerequests(friendsPending,"bencallaway2");
//			insertinloginrequests1(friendsPending,"bencallaway2");
//			if (friendsPending == 0) {$("#friendspendingcontainer").fadeOut(1000);$("#friendspendingtitle").fadeOut(1000);}
//			friendsNumber++;
//			$("#friendlistbox").add("<span>Again</span>")
			// remove the box, and add it to your friends box. 
			
			break;
			
		case "deletesucceed":
			friendsNumber--;
			if (friendsPending == 0) {
				obj.parentNode.parentNode.parentNode.innerHTML = '<p style="padding:0 16px;">You currently have no users in your friends list. You are now being taken back to your <a href="/community/profile/">profile</a>.</p>';
				window.location = window.location;
			} else {
				obj.parentNode.parentNode.style.display = 'none';
				$('back_to_profile_requests').setAttribute('onclick', 'window.location=window.location');
			}
			
//			$("#friendlimbobox"+objectUser).fadeOut("slow");
//			if (friendsNumber == 1) {$("#friendlistbox").fadeOut(1000);}

			// erase the box
			break;
		case "requestfailed":
			obj.innerHTML = button('alreadyfriends');			
//			requestout(objectUser,"Already Friend!");
			// user has already sent in a request!
			break;
		case "declinefailed":
			alert("Their was an error processing your request");
//			requestout(objectUser,"Error");
			// ok, something fishy happened, here, but since the element seems to appear here anyways, let's remove it.
			break;		
		case "approvefailed":
			alert("Their was an error processing your request");
//			requestout(objectUser,"Error");
			// ok, something fishy happened, here, but since the element seems to appear here anyways, let's remove it.
			break;
		case "deletefailed":
			alert("Their was an error processing your request");
//			requestout(objectUser,"Error");
			// ok, something fishy happened, here, but since the element seems to appear here anyways, let's remove it.
			break;
	}
}

function getCommentCount(number, name){// we are looking for the number of guestbook comments
	if (number == 0) return;
	if (self.insertProfileCommentCount) insertProfileCommentCount(number);
//	if (self.insertLoginCommentCount) insertLoginCommentCount(number);
}

function getNewMessageCount(number, name){
	if (number == 0) return;
	if (self.insertProfileNewMessageCount) insertProfileNewMessageCount(number);
//	if (self.insertLoginMessageCount) insertLoginMessageCount(number);	
}

function insertProfileNewMessageCount(number){
	if (number == 0) return;
	document.getElementById('profile_new_message_count').innerHTML = ' (' + number + ')';
	document.getElementById('profile_new_message_count').parentNode.className = 'new';
}

function insertProfileCommentCount(number){
	return; // We don't need your stinking comments
	if (number == 0) return;
	document.getElementById('profile_comment_count').innerHTML = ' (' + number + ')';
}

function insertLoginMessageCount(number){
	if (number == 0) return;
	document.getElementById('login_message_count').innerHTML = " (" + number + ")";
	document.getElementById('login_message_count').parentNode.className = 'new';
}

function insertLoginCommentCount(number){
	if (number == 0) return;
	document.getElementById('login_comment_count').innerHTML = " ("+number+")";
}


/*window.onload = function communityMenu() {
	var communityMenuLinks = new Array();
	communityMenuLinks = document.getElementById('lists').getElementsByTagName("a");
	for (i=0; i<communityMenuLinks.length; i++) {
		if(document.addEventListener) {		
			communityMenuLinks[i].addEventListener('mouseover', transHover, false);
		} else {
			communityMenuLinks[i].attachEvent('onmouseover', transHover);
		}
	}
}

transHover(obj) {
	obj.	
}*/

// FORUMS
// we will execute this as soon as we got access to the iframe..
//var searchreg = /?fastRedirectURL%25/;

function forumsRedirect() {
	var upurlstr = unescape(this.location.href);
	var upurlarray = upurlstr.split("?fastRedirectURL%");
	if (upurlarray.length > 1){
		var iframeurl = upurlarray.pop();
		if (iframeurl.length > 1) {
			frames['mainframe'].location.href = iframeurl.replace(/#(.*)$/,"");
			if (/search.marieclaire|categories.page/.test(iframeurl) == false) {
				document.getElementById("breadcrumb").style.display = 'none';
				document.getElementById("forums_titlebar").style.display = 'none';
			}
		}
	}
}

function pmRedirect() {
	var upurlstr = unescape(this.location.href);
	var upurlarray = upurlstr.split("?fastRedirectURL%");
	if (upurlarray.length > 1){
		var iframeurl = upurlarray.pop();
		if (iframeurl.length > 1) {
			frames['mainframe'].location.href = iframeurl.replace(/#(.*)$/,"");
			if (/search.marieclaire|send.page/.test(iframeurl) == false) {
				document.getElementById("breadcrumb").style.display = 'none';
				document.getElementById("titlebar").style.display = 'none';
			}
		}
	}
}


function guestbookRedirect() {
	var upurlstr = unescape(this.location.href);
	var upurlarray = upurlstr.split("?guestbookID=");
	if (upurlarray.length > 1){
		var guestbookID = upurlarray.pop()
		if (guestbookID.length > 1) {			
			document.getElementById('comment_list_iframe').src = "http://forum.marieclaire.com/marieclaireguestbookrender/guestbook.jsp?articleId=" + guestbookID;
			if(document.getElementById('comment_post_iframe')) document.getElementById('comment_post_iframe').src = "http://forum.marieclaire.com/marieclaireguestbookrender/addComment.jsp?articleId=" + guestbookID;			
		}
		if (guestbookID == subjectUser) {
			$('titlebar').innerHTML = 'Your Guestbook';			
		} else {
			$('titlebar').innerHTML = guestbookID + '\'s Guestbook';
		}
	}
}

function adRefresh() {
	refreshAds();
}

function communitySearch() {
/*
	var myprefixurl = "http://www.marieclaire.com/talk/forums/?fastRedirectURL%25"
	var myarea = document.getElementById('search_area').value;
	var myterm = document.getElementById('search_term').value;
	if ( (myarea == "http://search.marieclaire.com/cgi-bin/hsearch?hits=1000&view=marieclaireforumsppublished&query=" ) || (myarea == "http://search.marieclaire.com/cgi-bin/hsearch?hits=1000&view=3Dmarieclaireblogsppublished&query=") ){
		document.location.href = myprefixurl+escape(myarea+myterm);
	} else {
		document.location.href = myarea+myterm;
	}
*/

 var myarea = document.getElementById('search_area').value;
 //var myterm = document.getElementById('search_term').value;

 var myarea = $('#search_area').val();
 var myterm = $('#search_term').val();


 //test for profile search
 if ( myarea == "/talk/profile_search/?search_term=" )
 {
   //profile search
   document.location.href = myarea+myterm;
 }
 else
 {
   //new in-house community forum search
   var formurl = "/community/forums/forumSearch?search_term=";

   document.location.href = formurl + escape(myterm);
 }
}

function fastsearchobj2forum(reply){
	var query1 = escape(reply.comm_search_term.value);
	var query2 = '/community/forums/?fastRedirectURL%http://search.countryliving.com/cgi-bin/hsearch?hits=10&query='+query1;
	document.location.href = query2;
	//	alert('http://208.68.136.125/cgi-bin/hsearch?hits=10&query='+query1+'+'+query2+'&filter=%2bmeta.collection%3ahbforum');
	//	document.getElementById('forumiframe').src='http://<server>:15100/cgi-bin/hsearch?query='+reply+'&filter=%2bmeta.collection%3ahbforum';
}


/*if (searchreg.test(upurlstr)){
	alert("search string!");
	upurlarray = upurlstr.split("?fastRedirectURL%25");
	if (upurlarray.length > 1){
		var iframeurl = upurlarray.pop()
		if ((iframeurl.length > 1) && (forumreg.test(iframeurl) != false)) { // a check is in place just in case the url is something funny. I'll have to add in a check in here so that people don't do funny stuff I suppose..
			frames['mainframe'].location.href = unescape(iframeurl).replace(/#(.*)$/,"");
		}
	}

} else {
}*/