// do these when the page loads
function pageInit() {
  hideEmptyAds();
}

// use: <img onerror="hide_broken_img(this)" src="http://broken.com/image.gif" />
function hide_broken_avatar(img) {
  img.src='http://image.com.com/tv/images/avatar.jpg';
}

// hide image if not there
function hide_broken_image(img) {
  img.style.display='none';
}

// does the same thing as window.open... but apparently it's in a lot of old stories... :(
function openWin(url,name,options) {
	var newwin = window.open(url,name,options);
	if (newwin) newwin.focus();
}

// opens link in opener window if it's still there
// use <a href="http://www.gamespot.com" rel="opener">woo!</a>
function openerWin(url) {
	if (top.window.opener) {
		top.window.opener.focus();
		top.window.opener.location.href = url;
	} else {
		newWin = window.open(url);
	}
}

// check to see if you're using safari
function isSafari() {
  var agt=navigator.userAgent.toLowerCase();
  if ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1)) {
    return true;
  } else {
    return false;
  }
}


// msoft activex controls no longer start up 'activated' after april 11/06 (require focus to activate)
// workaround requires object/embed tags to be included in external js
// hence this func (document.write call must be made from externally included .js file)... sb
// note: if 'allow script debugging' is *not* checked in ie settings, you will still get the activiation prompt!
function embed(tag){
    document.write(tag);
}


// Custom function to track clicks on links to offsite domains.
function mboxTrackClickOffsite(dummyMboxName, clickedMboxName, offsiteUrl){
	var dummyRequestUrl = mboxs[dummyMboxName].getURL();
	var clickRequestUrl = dummyRequestUrl.replace(dummyMboxName, clickedMboxName);
	var proxyImage = new Image();
	proxyImage.src = clickRequestUrl;
}


/** FOR SWITCHING BETWEEN TABS IN MODDULES
 *
 * @param str pod - id of element (module) you're in
 * @param int tab - number of the tab to turn on
 * @param srt tagName - element type of sections (ie. div or table), defaults to div
 */
function toggleTab(pod, tab, tagName) {
	tagName = tagName ? tagName : 'div';
	var module = document.getElementById(pod);
	var divs = module.getElementsByTagName('div');
	for (i=0;i<divs.length;i++) {
		if (divs[i].className == 'head') {
			var tabs = divs[i].getElementsByTagName('li');
		}
	}

	// find all the sections, can pass in tagname if different than div
	var sectionCount = 0;
	var sections = new Array();
	var elements = module.getElementsByTagName(tagName);
	for (i=0;i<elements.length;i++) {
		if (elements[i].className == 'section') {
			sections[sectionCount] = elements[i];
			sectionCount++;
		}
	}

	// turn on your tab
	for (i=0;i<tabs.length;i++) {
		tabs[i].className = tabs[i].className.replace(/on/i,'');
		tabs[i].className = i == tab-1 ? tabs[i].className+' on': tabs[i].className ;
	}

	// turn on your section
	for (i=0;i<sections.length;i++) {
		sections[i].style.display = i == tab-1 ? 'block' : 'none';
	}
}

function uberpop(event,mae_bypass) {
    mae_bypass = mae_bypass ? '&mb=' + mae_bypass : '';
    url = 'index.php?type=48&action=uberpop&event_id=' + event + mae_bypass
    popupWin = window.open(url, 'uberpop_'+event, 'width=710,height=730,resizable=0,scrollbars=0,menubar=0,toolbar=0,location=0,status=0');
    popupWin.focus();
}

// Video Viewer
function videoPopup(popupurl,parenturl) {
	var player = window.open(popupurl, 'videoPlayer','width=726,height=678,resizable=no,scrollbars=no,location=0,menubar=0,statusbar=0,toolbar=0');
	if (window.focus) {player.focus();}
	return false;
}

function videoPopupFilmspot(url) {
	var player = window.open(url, 'FilmSpotVideoPlayer', 'width=960,height=560,resizable=0,scrollbars=0');
	if (window.focus) {player.focus();}
	return false;
}

// Photo Viewer
function photoViewer(url) {
	var viewer = window.open(url,'photoViewer','width=750,location=no,resizable=yes,toolbar=no,scrollbars=no,top=0,left=0');
	if (window.focus) {viewer.focus();}
	return false;
}

// Initialize All Links
function linksInit() {
	var links = $$('a');
	for (i=links.length;i>0;i--) {
		var a = links[i-1];
		if (!a.href || !a.rel) continue;
		switch (a.rel) {
			case 'photo':
				a.addEvent('click', function (evt) {
					(new Event(evt)).stop();
					photoViewer(this.href);
				});
				break;
			case 'video':
				a.addEvent('click', function (evt) {
					(new Event(evt)).stop();
					videoPopup(this.href);
				});
				break;
			case 'opener':
				a.addEvent('click', function (evt) {
					(new Event(evt)).stop();
					openerWin(this.href);
				});
				break;
			case 'parent':
				a.addEvent('click', function (evt) {
					(new Event(evt)).stop();
					parent.location = this.href;
				});
				break;
  		}
	}
}

// Initialize TV.com
function tvInit() {
	linksInit();
}

if (self.addEvent) {
	window.addEvent('domready', tvInit);
}