function unblur() {
	this.blur();
}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

function getLinksToBlur() {
	if (!document.getElementById) return
	links = document.getElementsByTagName("a");
	for(i=0; i<links.length; i++) {
		links[i].onfocus = unblur
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var resizeHasRun = false;
function checkTwitterSize() {
	resizeHasRun = true;
	try {
		var winWidth = window.innerWidth || document.body.clientWidth;
		var twitterSize = 230;
		
		var main_container = document.getElementById('main_container');
		var twitter_div = document.getElementById('twitter_div');
		var twitter = document.getElementById('twitter');
		
		var halfSize = (winWidth - main_container.offsetWidth) / 2;
		
		if(halfSize < (twitterSize - 25)) {
			var fullSize = (main_container.offsetWidth + twitterSize);
			// can it fit?
			if(fullSize <= winWidth) {
				// will fit, make it fit
				var whatsLeft = (winWidth - fullSize);
				var offsetW = Math.floor( whatsLeft / 2) - 7;
				
				main_container.style.left = "0px";
				var curPos = findPos(main_container);
				main_container.style.left = '-' + (curPos[0] - offsetW) + 'px';
			} else {
				// won't fit even if we try
				var curPos = findPos(main_container);
				main_container.style.left = '-' + (curPos[0] - 2) + 'px';
			}
		} else {
			main_container.style.left = "0px";
		}
	} catch(ex) {}
}

var announceShown = null;
var lastAnnounceShown = null;
var disableShowing = false;
var announceDelay = 200;
function bind_announcements() {
	try {
		var announce_list = $('announce_list');
		var announcements = announce_list.getElementsByTagName('li');
/*	for (var i=0; i < announcements.length; i++){
			var ann_li	= announcements[i];
			var ann		= ann_li.getElementsByTagName('a')[0];
			
			ann.onmouseover	= function () {
				announceShown = this;
				setTimeout('announcement_show("'+this.getAttribute('id').split('_')[2]+'")', announceDelay);
				return;
			}
			ann.onmouseout	= function () {
				announceShown = null;
				setTimeout('announcement_hide("'+this.getAttribute('id').split('_')[2]+'")', announceDelay);
				return;
			}
			//ann.onclick		= function () { }
			
			$('announce_desc_'+ann.getAttribute('id').split('_')[2]).style.display = 'none';
			
			// Blink the first one
			if(i == 0) {
				// Check cookie
				var last_ann = Cookie.get('latestAnnouncement');
				if(last_ann != ann.getAttribute('id')) {
					new Effect.Pulsate(ann);
					// Save
					Cookie.set('latestAnnouncement', ann.getAttribute('id'), 365);
				}
			}
		}*/
	} catch(ex) { }
}
function announcement_show(annID) {
	try {
		var oldID = announceShown.getAttribute('id').split('_')[2];
		if(disableShowing != oldID && announceShown != null && annID == oldID) {
			var ann = 'announce_desc_'+annID;
			// Add event for description
			Event.observe(ann, 'mouseover', function() { announceShown = this; });
			// Show
			new Effect.BlindDown(ann,
				{ duration: 0.3
				});
			disableShowing = annID;
		}
	} catch(ex) { console.log(ex); }
}
function announcement_hide(annID) {
	try {
		if(announceShown == null) {
			var ann = 'announce_desc_'+annID;
			// Hide
			new Effect.BlindUp(ann,
				{ duration: 0.3
				});
			disableShowing = false;
		}
	} catch(ex) {}
}
function announcement_toggle(annID) {
	try {
		var ann = 'announce_desc_'+annID;
		//if(announceShown == null || disableShowing)
			new Effect.toggle(ann, 'blind', {duration: 0.3});
	} catch(ex) {}
}

var globalPageID = null;

function changeBorderBar(navID) {
	/*
		Home: ff148a
		Blog: 9418ff
		Videos: ff1010
		Chat: 77C500
		About: ff860d
		Contact: 0d0dff
		Pics: 00A6FF
		Community: 77C500
		Contest: 21BA00
	*/
	var borderBar = document.getElementById('navbox');
	
	color = '#ff148a';
	
	switch(navID) {
		case 'blog':
			color = '#9418ff'; break;
		case 'vids':
			color = '#ff1010'; break;
		case 'chat':
		case 'forums':
			color = '#77C500'; break;
		case 'about':
			color = '#ff860d'; break;
		case 'contact':
			color = '#0d0dff'; break;
		case 'pics':
			color = '#00A6FF'; break;
		case 'community':
			color = '#77C500'; break;
		case 'contest':
			color = '#21BA00'; break;
		case 'home':
		default:
			// use default color
			break;
	}
	
	// Change the color
	borderBar.style.borderBottomColor = color;
	
	//alert(color);
}
function defaultColorBar() {
	if(globalPageID)
		changeBorderBar(globalPageID);
}

function onLoader(pageID) {
	// 0	- Unknown
	// 1	- Home
	// 2	- Blogs
	// 3	- Videos
	// 4	- About
	// 5	- Contact
	// 6	- chat
	// 7	- forums
	
	switch(pageID) {
		case 0:
		case 1:
			navID = 'home'; break;
		case 2:
			navID = 'blog'; break;
		case 3:
			navID = 'vids'; break;
		case 4:
			navID = 'about'; break;
		case 5:
			navID = 'contact'; break;
		case 6:
			navID = 'chat'; break;
		case 7:
			navID = 'forums'; break;
		case 8:
			navID = 'pics';
			Event.observe(window,'load',function(){
				try { $('conContainer').addClassName('pixPage'); } catch(ex){}
			});
			break;
		case 9:
			navID = 'community'; break;
		default:
			navID = 'home'; break;
	}
	
	changeBorderBar(navID);
	
	globalPageID = navID;
	
	getLinksToBlur();
	
	if (typeof document.body.style.maxHeight != "undefined") {
		// this don't work in IE6. poopy for them, they should upgrade anyway
		bind_announcements();
	}
	
	//checkTwitterSize();
}

//window.onresize = checkTwitterSize;

function shareVideo(postID) {
	window.open(vidBoxPath + "sharevid.php?pid=" + postID, "shareVideo", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=0,height=460,width=360");
}

var lastVid = null;
var vidBoxPath = null;
var ajaxLoader = null;

function changeVideo(postID) {
	// Put the last vid to rest
	if(lastVid != null)
		lastVid.className = '';
	// Change the active video background
	var curVid = document.getElementById('vid_'+postID);
	curVid.className = 'activeVid';
	// Remember what the last video was
	lastVid = curVid;
	
	document.getElementById('vidbox').innerHTML = ajaxLoader;
	
	// Make an AJAX call to get the video HTML
	new Ajax.Updater('vidbox', vidBoxPath + 'vidbox.php', {
		method: 'get',
		parameters: { pid: postID },
		evalScripts: false,
		onComplete: function() {
		}
	});
}