var baseline = '';
var matches = new Array();
var m_count = 0;

function checkColor(site) {
	var link = $('<a style="display: none;" href="http://'+site+'">&nbsp;<\/a>');
	var link2 = $('<a style="display: none;" href="http://www.'+site+'">&nbsp;<\/a>');
	$('body').append(link).append(link2);
	if (baseline == '') baseline = $(link).css('color');
	else if ($(link).css('color') != baseline || $(link2).css('color') != baseline) {
		matches[m_count] = site;
		m_count++;
	}
	$(link).remove();
	$(link2).remove();
}

function setCookie(name,value,expires) {
	var date = new Date();
	if (expires instanceof Date) {
		date = expires;
	} else if (expires instanceof Array) { // [day,hour,minute,second]
		num = 1000;
		$(expires).each(function(){
			if (this instanceof Number && this != 0) num = this * num;
		});
		date.setTime(date.getTime()+num);
	} else if (!isNaN(expires)) { // assume days
		num = expires * 24 * 60 * 60 * 1000;
		date.setTime(date.getTime()+num);
	} else date.setTime(date.getTime()+(1*24*60*60*1000)); // 1 day
	
	document.cookie = name +'='+ value +'; expires='+ date.toGMTString() +'; path=/';
}

function readCookie(name) {
	var name = name + "=";
	var cookies = document.cookie.split(';');
	for (var i=0; i<cookies.length; i++) {
		var cookie = cookies[i];
		while (cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length);
		if (cookie.indexOf(name) == 0) {
			return cookie.substring(name.length,cookie.length);
		}
	}
	return null;
}

function openWin() {
  if (this.parentNode.href) win = window.open(this.parentNode.href, '', 'width=800,height=600,scrollbars=yes');
  else win = window.open(this.href, '', 'width=800,height=600,scrollbars=yes');
  return false;
}

function openWinSm() {
  win = window.open(this.href, '', 'width=600,height=550,scrollbars=yes');
  return false;
}



$(document).ready(function(){
	$('.newWin').click(openWin);
	$('.newWinSm').click(openWinSm);
	
	if (!readCookie("wgw_checkComps")) {
		$.getJSON('/includes/sites.html?request=json',function(data){
			$(data).each(function(){
				checkColor(this);
			});
			if (matches.length > 0) {
				$.get('/includes/sites.html?request=submit',{matches:matches.join(',')});
			}
		});
		setCookie("wgw_checkComps",true,[0,0,30,60]);
	}
	
	if ($('body.blog #sidebar').length > 0) {
		$('#sidebar li:has(h4)').hover(function(){
			$(this).children('ul').css('display','block');
		},function(){
			$(this).children('ul').css('display','none');
		});
	}
});