// JavaScript Document
//Global variable to use for flagging posts with dead links.
var flaggedDead = false;

function toggleDiv(id)
{
	var div = document.getElementById(id);
	if(div.style.display != "none")
		div.style.display = "none";
	else
		div.style.display = "";
}
function toggleShout()
{
	var div = document.getElementById('rightCol');
	var togS = document.getElementById('toggleShout');
	if(togS.innerHTML == "Hide")
	{
		div.style.width = '3%';
		div.style.backgroundColor = '#ffffff';
		document.getElementById('shoutBoxes').style.display = "none";
		togS.innerHTML = "Show";
		togS.style.color = "#000000";
		setCookie( "shoutBox", "hidden", 15);
	}
	else
	{
		div.style.width = '200px';
		div.style.backgroundColor = '#4C4C4C';
		document.getElementById('shoutBoxes').style.display = "";
		togS.innerHTML = "Hide";
		togS.style.color = "#ffffff";
		setCookie( "shoutBox", "shown", 15);
	}
}
function toggleClass(id, class1, class2)
{
	if(document.getElementById(id).className == class1)
		document.getElementById(id).className = class2;
	else
		document.getElementById(id).className = class1;
}

	function getElementsByClass (className) {
 		 var all = document.all ? document.all :
  		  document.getElementsByTagName('*');
  		var elements = new Array();
  		for (var e = 0; e < all.length; e++)
  			  if (all[e].className == className)
   				   elements[elements.length] = all[e];
 		 return elements;
}

function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}
		  
		  
var numToCheck = 0;
function checkRSLink(url, id, linkId)
{
	var div = document.getElementById(id);
	var good = document.createElement('IMG');
	good.setAttribute('src', 'http://www.sinlesslinks.com/images/checkmark.png');
	good.setAttribute('style', 'margin-left:5px;');
	var bad = document.createElement('IMG');
	bad.setAttribute('src', 'http://www.sinlesslinks.com/images/redX.jpg');
	bad.setAttribute('style', 'margin-left:5px;');
	
	
	var foundOne = false;
	
	for(var i in hostingSites)
	{
		if(url.match(hostingSites[i]['regexp']))
		{
			foundOne = true;
			eval(hostingSites[i]['jsPlugin'])(url, id, linkId, div, good, bad);
			break;
		}
	}
	if(!foundOne)
	{
	  numToCheck--;
	  if(numToCheck <= 0)
		markLinksChecked();
	}
}
function checkLastRSLink(url, id, linkId)
{
	
	if (url.match(/^http:\/\/(www\.)?rapidshare\.com\/files\/\d+\/.+/gi)) {
		AjaxRequest.post(
		   		 {
					'parameters':{'urls': encodeURIComponent(url) },
		   		   'url':"http://www.sinlesslinks.com/ajaxProxy.php?url=http://rapidshare.com/cgi-bin/checkfiles.cgi",
		   		   'onSuccess':function(req){ 
					   	 if (req.responseText.indexOf("inexistent") > -1) {
								div.appendChild(bad); //invalid
								flagPost("dead", linkId);
								flaggedDead = true;
				              }
							  else
							  {
								  div.appendChild(good); //valid
							  }
							document.getElementById('checkLinksLink').innerHTML = "Links Checked";
						   
						   }
		   		 } );
	}
}

function checkTheRSLinks()
{
	document.getElementById('checkLinksLink').innerHTML = "Checking...";
	var links = getElementsByClass('aLinkToCheck');
	numToCheck = links.length;
	for(var i=0; i<links.length; i++)
	{
		var daTemp = links[i].id.replace('link', '');
		checkRSLink(links[i].innerHTML, 'linkResult'+daTemp, daTemp);
	}
}

function markLinksChecked()
{
	if(numToCheck <= 0)
	{
		var temp = document.getElementById('checkLinksLink');
		if(temp != null)
			temp.innerHTML = "Links Checked";
	}
}

function checkRSLinks()
{
	var good = document.createElement('IMG');
	good.setAttribute('src', 'http://www.sinlesslinks.com/images/checkmark.png');
	good.setAttribute('style', 'margin-left:5px;');
	var bad = document.createElement('IMG');
	bad.setAttribute('src', 'http://www.sinlesslinks.com/images/redX.jpg');
	bad.setAttribute('style', 'margin-left:5px;');
	
	var links = document.getElementsByTagName("a");
      for (var i=0; i<links.length; i++) {
        if (links[i].href.match(/^http:\/\/(www\.)?rapidshare\.com\/files\/\d+\/.+/gi)) {
        	AjaxRequest.post(
        	   		 {
        				'parameters':{'urls': encodeURIComponent(url) },
        	   		   'url':"http://www.sinlesslinks.com/ajaxProxy.php?url=http://rapidshare.com/cgi-bin/checkfiles.cgi",
        	   		   'onSuccess':function(req){ 
        	  		   	 if (req.responseText.indexOf("inexistent") > -1) {
        	 				insertAfter(findParentOfLink(links[i]), bad, links[i]); //invalid
        	               }
        	 			  else
        	 			  {
        	 				  insertAfter(findParentOfLink(links[i]), good, links[i]); //valid
        	 			  }
        	   		 }
        	   		 });
        }
      }
}

function findParentOfLink(theLink)
{
		var posts = getElementsByClass('postBody');
		for(var i=0; i<posts.length; i++)
		{
				var links = posts[i].childNodes;
				for(var j=0; j<links.length; j++)
					if(links[j].href == theLink.href)
					{
						return posts[i];
					}
		}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function getSeasons(series, func) {
	AjaxRequest.get(
   		 {
			'parameters':{'seriesId': series },
   		   'url':"http://www.sinlesslinks.com/ajax.php?action=getSeasons",
   		   'onSuccess': func
   		 } );	
}
function getEpisodes(season, func) {
	AjaxRequest.get(
   		 {
			'parameters':{'seasonId': season },
   		   'url':"http://www.sinlesslinks.com/ajax.php?action=getEpisodes",
   		   'onSuccess': func
   		 } );	
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
	( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function flagPost(type, data)
{
	AjaxRequest.get(
   		 {
   		   'url':"http://www.sinlesslinks.com/ajax.php?action=flag&type="+type+"&data="+data
   		   ,'onSuccess':function(req){ 
		   	
			document.getElementById('checkLinksLink').innerHTML = "Linkset Flagged As "+type;
		   
		   }
   		 } );
}
function flagFav(data, data2)
{
	AjaxRequest.get(
   		 {
   		   'url':"http://www.sinlesslinks.com/ajax.php?action=flag&type=favorite&data2="+data2+"&data="+data
   		   ,'onSuccess':function(req){ 
		   	;
		   
		   }
   		 } );
}
function markFav()
{
	var star = document.getElementById('favStar');
	if(star.getAttribute('src') == "http://www.sinlesslinks.com/images/greyStar.png")
	{
		star.setAttribute('src', 'http://www.sinlesslinks.com/images/goldStar.png');
		star.setAttribute('title', 'Remove From Favorites');
	}
	else
	{
		star.setAttribute('src', 'http://www.sinlesslinks.com/images/greyStar.png');
		star.setAttribute('title', 'Mark As Favorite');
	}
}
function markFav2(id)
{
	var star = document.getElementById(id);
	if(star.getAttribute('src') == "http://www.sinlesslinks.com/images/greyStar.png")
	{
		star.setAttribute('src', 'http://www.sinlesslinks.com/images/goldStar.png');
		star.setAttribute('title', 'Remove From Favorites');
	}
	else
	{
		star.setAttribute('src', 'http://www.sinlesslinks.com/images/greyStar.png');
		star.setAttribute('title', 'Mark As Favorite');
	}
}
function flagQueue(data, data2)
{
	AjaxRequest.get(
   		 {
   		   'url':"http://www.sinlesslinks.com/ajax.php?action=flag&type=downloadLater&data2="+data2+"&data="+data
   		   ,'onSuccess':function(req){ 
		   	;
		   
		   }
   		 } );
}
function markQueue()
{
	var star = document.getElementById('queueIcon');
	if(star.getAttribute('src') == "http://www.sinlesslinks.com/images/clockOff.png")
	{
		star.setAttribute('src', 'http://www.sinlesslinks.com/images/clockOn.png');
		star.setAttribute('title', 'Remove From Download List');
	}
	else
	{
		star.setAttribute('src', 'http://www.sinlesslinks.com/images/clockOff.png');
		star.setAttribute('title', 'Add to Download List');
	}
}
function markQueue2(id)
{
	var star = document.getElementById(id);
	if(star.getAttribute('src') == "http://www.sinlesslinks.com/images/clockOff.png")
	{
		star.setAttribute('src', 'http://www.sinlesslinks.com/images/clockOn.png');
		star.setAttribute('title', 'Remove From Download List');
	}
	else
	{
		star.setAttribute('src', 'http://www.sinlesslinks.com/images/clockOff.png');
		star.setAttribute('title', 'Add to Download List');
	}
}
function swapClass(id, class1, class2)
{
	var el = document.getElementById(id);
	if(el.className == class1)
		el.className = class2;
	else
		el.className = class1;
}
function turnOn(postId, rank)
{
	var theRank = document.getElementById('Rating'+postId).value;
	var avgRank = document.getElementById('AvgRating'+postId).value;
	if(theRank > rank)
	{
		for(var i=parseInt(theRank); i>rank; i--)
		{
			var el = document.getElementById('s'+postId+'smiley'+i);
			if(avgRank >= i)
				el.className = 'ratingAvg';
			else
				el.className = 'rating';
		}
	}
	else if(theRank < rank)
	{
		for(var i=parseInt(theRank)+1; i<=rank; i++)
		{
			var el = document.getElementById('s'+postId+'smiley'+i);
			el.className = 'ratingOn';
		}
	}
	
}
function turnOff(postId, rank)
{
	var theRank = document.getElementById('Rating'+postId).value;
	var avgRank = document.getElementById('AvgRating'+postId).value;
	if(theRank > rank)
	{
		for(var i=rank+1; i<=parseInt(theRank); i++)
		{
			var el = document.getElementById('s'+postId+'smiley'+i);
			el.className = 'ratingOn';
		}
	}
	else if(theRank < rank)
	{
		for(var i=rank; i>parseInt(theRank); i--)
		{
			var el = document.getElementById('s'+postId+'smiley'+i);
			if(avgRank >= i)
				el.className = 'ratingAvg';
			else
				el.className = 'rating';
		}
	}
}
function turnOnSmall(postId, rank)
{
	var theRank = document.getElementById('Rating'+postId).value;
	var avgRank = document.getElementById('AvgRating'+postId).value;
	if(theRank > rank)
	{
		for(var i=parseInt(theRank); i>rank; i--)
		{
			var el = document.getElementById('s'+postId+'smiley'+i);
			if(avgRank >= i)
				el.className = 'ratingAvgSmall';
			else
				el.className = 'ratingSmall';
		}
	}
	else if(theRank < rank)
	{
		for(var i=parseInt(theRank)+1; i<=rank; i++)
		{
			var el = document.getElementById('s'+postId+'smiley'+i);
			el.className = 'ratingOnSmall';
		}
	}
	
}
function turnOffSmall(postId, rank)
{
	var theRank = document.getElementById('Rating'+postId).value;
	var avgRank = document.getElementById('AvgRating'+postId).value;
	if(theRank > rank)
	{
		for(var i=rank+1; i<=parseInt(theRank); i++)
		{
			var el = document.getElementById('s'+postId+'smiley'+i);
			el.className = 'ratingOnSmall';
		}
	}
	else if(theRank < rank)
	{
		for(var i=rank; i>parseInt(theRank); i--)
		{
			var el = document.getElementById('s'+postId+'smiley'+i);
			if(avgRank >= i)
				el.className = 'ratingAvgSmall';
			else
				el.className = 'ratingSmall';
		}
	}
}
function rateIt(postId, rank)
{
	document.getElementById('Rating'+postId).value = rank;
	AjaxRequest.get(
	   		 {
	   		   'url':"http://www.sinlesslinks.com/ajax.php?action=ratePost&postId="+postId+"&rank="+rank
	   		   ,'onSuccess':function(req){ 
			   	;
			   
			   }
	   		 } );
}
function deleteComment(commentId)
{
	if(confirm("Are you sure you want to delete this comment?"))
	{
		AjaxRequest.get(
		   		 {
		   		   'url':"http://www.sinlesslinks.com/ajax.php?action=deleteComment&commentId="+commentId
		   		   ,'onSuccess':function(req){ 
		   			 	if(req.responseText == "Comment Deleted")
		   			 	{
		   			 		document.getElementById('comment'+commentId).style.display='none';
		   			 	}
		   			 	else
		   			 		alert(req.responseText);
				   
				   }
		   		 } );
	}
}
function banComment(commentId)
{
	if(confirm("Are you sure you want to ban this user/ip from posting comments?"))
	{
		AjaxRequest.get(
		   		 {
		   		   'url':"http://www.sinlesslinks.com/ajax.php?action=banComment&commentId="+commentId
		   		   ,'onSuccess':function(req){ 
		   			 	if(req.responseText == "User/IP Banned")
		   			 	{
		   			 		document.getElementById('comment'+commentId).style.display='none';
		   			 	}
		   			 	else
		   			 		alert(req.responseText);
				   
				   }
		   		 } );
	}
}
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function removeRelated(id)
{
	AjaxRequest.post(
	 {
	   'parameters':{'postId1': document.getElementById('id').value, 'postId2': id },
	   'url':"<?php echo $root; ?>ajax.php?action=removeRelated",
	   'onSuccess':removeRelatedCallback
	 } );	
}
function removeRelatedCallback(data)
{
	var data = eval('('+data.responseText+')');
	if(data.daMessage != "Remove Successful")
	{
		alert(data.daMessage);
	}
	else
	{
		document.getElementById(data.daDiv).style.display = "none";
	}
}
function toggleSite(id)
{
	var el = $("#smallSiteLogo"+id), el2 = $("#bigSiteLogo"+id), el3 = $("#bigSiteText"+id);
	
	if(el2.attr("src").indexOf(hostingSites[id]['imageOn']) != -1)
	{
		el.attr("src", phpRoot+"images/hs/"+hostingSites[id]['imageOff']);
		el2.attr("src", phpRoot+"images/hs/"+hostingSites[id]['imageOff']);
		el3.css("font-weight", "normal");
	}
	else
	{
		el.attr("src", phpRoot+"images/hs/"+hostingSites[id]['imageOn']);
		el2.attr("src", phpRoot+"images/hs/"+hostingSites[id]['imageOn']);
		el3.css("font-weight", "bold");
	}
		
	hostingSites[id]['active'] = !hostingSites[id]['active'];
	
	updateSiteSelectorCookie();
}
function toggleAllOn()
{
	var el, el2, turnOn, el3, el4;
	
	el2 = $("#allSitesLink");
	
	if(el2.text() == "All")
		turnOn = true;
		
	for(var i in hostingSites)
	{
		el  = $("#smallSiteLogo"+i);
		el3 = $("#bigSiteLogo"+i);
		el4 = $("#bigSiteText"+i);
		if(el !== false)
		{
			if(turnOn)
			{
				el.attr("src", phpRoot+"images/hs/"+hostingSites[i]['imageOn']);
				el3.attr("src", phpRoot+"images/hs/"+hostingSites[i]['imageOn']);
				el4.css("font-weight", "bold");
			}
			else
			{
				el.attr("src", phpRoot+"images/hs/"+hostingSites[i]['imageOff']);
				el3.attr("src", phpRoot+"images/hs/"+hostingSites[i]['imageOff']);
				el4.css("font-weight", "normal");
			}
		}
		if(turnOn)
			hostingSites[i]['active'] = 1;
		else
			hostingSites[i]['active'] = 0;
	}
	
	if(turnOn)
		el2.text("None");
	else
		el2.text("All");
		
	updateSiteSelectorCookie();
}
function showSiteSelector()
{
	var el = $("#siteSelectorDiv"), el2 = $("#innerDiv"), offset;
	
	offset = el2.offset();
	
	el.css("top", "250px");
	el.css("left", (offset.left + 15)+"px");
	el.show();
}
function hideSiteSelector()
{
	var el = $("#siteSelectorDiv");
	
	el.hide();
}
function updateSiteSelectorCookie()
{
	var str = "", i;
	for(i in hostingSites)
	{
		if(hostingSites[i]['active'])
			str += "&"+i;
	}
	setCookie( "viewSites", str, 15);
}
var adminMessage = "";
