/*------------------------------------------------------------------
Title:		TMS News & Features JavaScript Functions
Author:		John Reed, john@studiobonito.com (unless otherwise noted)
Updated:	June 20 2007
----------------------------------------------------------------- */





// add load event function
// source: http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


// get elements by class name function
// source: http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


// funtion to toggle "advanced" and "simple" search
function searchType() {
	if (document.getElementById) {
		var showAdvanced = document.getElementById("showAdvanced");
		var showSimple = document.getElementById("showSimple");
		var advancedSearch = document.getElementById("advancedSearch");
		var simpleSearch = document.getElementById("simpleSearch");
		
		showAdvanced.onclick = function() {
			simpleSearch.style.display = "none";
			advancedSearch.style.display = "block";
			return false;
		}

		showSimple.onclick = function() {
			simpleSearch.style.display = "block";
			advancedSearch.style.display = "none";
			return false;
		}
	}
}


// prepares links based on "rel" attribute
function prepareLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		
		// prepare external links
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}

		// prepare close window links
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "close") {
			anchor.onclick = function() {
				window.close();
			}
		}
		
		// prepare print links
		if (anchors[i].getAttribute("rel") == "print") {
			anchors[i].onclick = function() {
				window.print();
			}
		}
	}
}


// *** protect our loyal users from spam!
// specify only the tribune.com username
function noSpam(u,t) {
	if(u) {
		if(t) {
			document.write('<a href="mai' + 'lto:' + u + '@' + 'tribu' + 'ne.com">' + t + '</a>');
		} else {
			document.write('<a href="mai' + 'lto:' + u + '@' + 'tribu' + 'ne.com">' + u + '@tr' + 'ibune.com</a>');
		}
	}
}

// function to find Y position of a specific element
// source: http://blog.firetree.net/2005/07/04/javascript-find-position/
function findPosY(obj) {
	var curtop = 0;

	if(obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent) {
				break;
			}
			obj = obj.offsetParent;
		}
	} else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
}


// set heights of content columns
function setHeights() {
	if(document.getElementById) {
		
		// set variables for content columns and footer
		var left = document.getElementById('sources');
		var center = document.getElementById('content');
		if( document.getElementById('marketing') != null ) var right = document.getElementById('marketing');
		var footer = document.getElementById('footer');
		
		// find the Y origin of #left (thus #center & #right) and #footer
		var leftTop = findPosY(left);
		var centerTop = findPosY(center);
		if( document.getElementById('marketing') != null ) var rightTop = findPosY(right);
		var footerTop = findPosY(footer);
		
		// calculate the height in pixels
		var leftHeight = footerTop - leftTop;
		var centerHeight = footerTop - centerTop;
		if( document.getElementById('marketing') != null ) var rightHeight = footerTop - rightTop;
		
		// find largest height
		
		var height = leftHeight;
		if ( centerHeight > height ) height = centerHeight;
		if( document.getElementById('marketing') != null && rightHeight > height ) height = rightHeight;
		
		// set the heights
		left.style.height = height + 'px';
		center.style.height = height + 'px';
		if( document.getElementById('marketing') != null ) right.style.height = height + 'px';
	}
}

// load events
addLoadEvent(prepareLinks);


                                                                                             
function popup(url) {
        var popwin = window.open(url, 'pop', 'location=0,status=0,directories=0,menubar=0,scrollbars=1,resizable=1,toolbar=0,width=450,height=600');
        popwin.moveTo(50,50);
        popwin.focus();
}


// ***** MARKETING IN RIGHT RAIL ******


// set these variables
var numCreators = 16; // how many creators are below?  (remember, starts from 0)
var numShown = 3;
var featuresDomain = 'http://us.tmsfeatures.com';
var creatorImagesDomain = 'http://www.featureserv.com/images/';
var marketingImagesDomain = 'http://us.tmsfeatures.com/images/marketing/';


// see http://richardbowles.tripod.com/javascript/section5/lesson5.htm

var creator = new Array();
var count;
for (count = 0; count < numCreators; count++)
 creator[count] = new Array();  // This declares each column in turn

// column[n][0] is CREATOR NAME
// column[n][1] is MUG JPEG
// column[n][2] is BIO TEXT
// column[n][3] is BLENDER CONTENT ARCHIVE FOR CREATOR
// column[n][4] is MARKETING JPEG


creator[0][0] = 'Cal Thomas';
creator[0][1] = creatorImagesDomain + 'thomas-cal.jpg';
creator[0][2] = 'Thomas advocates a return to traditional values, which he believes must come from the people-not their government. He takes on issues pertinent to readers&#39; lives, leaving them energized and optimistic about the future. Trusted by millions for his conservative views, Thomas is the host of &#39;After Hours with Cal Thomas&#39; on the FOX News Channel.';
creator[0][3] = featuresDomain + '/tms-bin/tms.cgi?page=20&wire=KD&fwire=KD';
creator[0][4] = marketingImagesDomain + 'thomas.jpg';

creator[1][0] = 'Amy Dickinson';
creator[1][1] = creatorImagesDomain + 'dickinson-amy.jpg';
creator[1][2] = 'In the tradition of the great personal advice columnists, Chicago Tribune&#39;s Dickinson is a plainspoken straight shooter who relates to readers of all ages. She answers personal questions by addressing issues from both her head and her heart. A solid reporter, Dickinson researches her topics to provide readers with informed opinions and answers.';
creator[1][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=ZA&fwire=ZA';
creator[1][4] = marketingImagesDomain + 'dickinson.jpg';

creator[2][0] = 'Brewster Rockit: Space Guy by Tim Rickard';
creator[2][1] = creatorImagesDomain + 'rickard_tim.jpg';
creator[2][2] = 'Brewster Rockit is an unlikely captain. He&#39;s woefully ill-prepared to lead ... and that&#39;s part of his charm. Orbiting the stars in the space station R.U. Sirius, Brewster and his crew of misfits encounter a host of interplanetary anomalies. Brewster Rockit: Space Guy! pushes the limits of space, time and humor. Reach readers 18-34.';
creator[2][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=XL&fwire=XL';
creator[2][4] = marketingImagesDomain + 'brewster.jpg';

creator[3][0] = 'Chris Elliott';
creator[3][1] = creatorImagesDomain + 'headshot_elliott-chris.jpg';
creator[3][2] = 'Christopher Elliott, National Geographic Traveler&#39;s ombudsman, journalist, consumer advocate and travel industry critic, writes \"The Travel Troubleshooter,\" straight-talking, reader-friendly advice column for harried travelers. His weekly Q\&A offers practical, real-world tips and common-sense advice on how to get the most out of your complaint and help you navigate the ins and outs of questionable hotel deals, airline come-ons and mystery cruise-ship surcharges.';
creator[3][3] = featuresDomain + '/tms-bin/tms.cgi?page=21&wire=WA&fwire=WA';
creator[3][4] = marketingImagesDomain + 'elliott.jpg';

creator[4][0] = 'Bliss by Harry Bliss';
creator[4][1] = creatorImagesDomain + 'Harry-Bliss-headshot.gif';
creator[4][2] = 'Bliss is for people who take their humor very seriously. Harry tinkers with an idea and gets it right the first time by finding fresh angles and twisting them into intelligent jokes that sneak up on you so that only perfection will ever grace your comics page. Each panel is a sharp and smart look at the paradoxes of life ranging from lust and love to misfortune and bitterness. Bliss cartoons expose truths so human ... so real ... they&#39;re timeless.';
creator[4][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=XB&fwire=XB';
creator[4][4] = marketingImagesDomain + 'bliss.jpg';

creator[5][0] = 'Jonah Goldberg';
creator[5][1] = creatorImagesDomain + 'goldberg-jonah.jpg';
creator[5][2] = 'Goldberg delivers biting commentary and fresh analysis. Popular among young conservatives, Goldberg is a new voice for your op-ed page. His witty pop culture references put a new spin on heavily covered stories. Goldberg brings pressing issues to his readers with a youthful conservative twist. He is a contributor to The Wall Street Journal, an editor-at-large for the National Review Online and a commentator for CNN. Goldberg is a winner of the Lowell Thomas Award. Reach readers 18-34.';
creator[5][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=KA&fwire=KA';
creator[5][4] = marketingImagesDomain + 'goldberg.jpg';

creator[6][0] = 'Kakuro';
creator[6][1] = creatorImagesDomain + 'kakuro.jpg';
creator[6][2] = 'Challenge your logic and test your math skills with the newest Japanese puzzle creating an international stir! Considered a cross between Sudoku, Cross Sums and a crossword puzzle, Kakuro requires elementary addition and subtraction to build the answers. Using a 9x9 grid, players solve Kakuro using the numbers 1-9 in blocks of two to nine squares, running horizontally and vertically in a grid. The sum of each line must match its associated \"clue\"; these numbers already appear in white circles. But there is a catch -- numbers cannot be repeated in any consecutive boxes. Kakuro may be more challenging than Sudoku, but it&#39;s just as addictive and exciting!';
creator[6][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=MJ&fwire=MJ';
creator[6][4] = marketingImagesDomain + 'kakuro.jpg';

creator[7][0] = 'Kiplinger&#39;s Money Power';
creator[7][1] = creatorImagesDomain + '';
creator[7][2] = 'From planning a backyard wedding to figuring out the new Medicare prescription rules, daily life is full of financial decisions and dilemmas. Kiplinger&#39;s daily Money Power feature gives readers succinct, readable consumer information on an array of topics.</p><p>Kiplinger&#39;s experts, including Janet Bodnar and her popular \"Money-Smart Kids\" column, help readers navigate the basics of personal finance -- investing, saving, retirement -- as well as travel, real estate, autos, entertainment, food, careers, health and other topics in the news.</p><p>Five days a week, in 350-word installments, Money Power makes the respected Kiplinger brand an important part of your business or lifestyle pages.';
creator[7][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=BH&fwire=BH';
creator[7][4] = marketingImagesDomain + 'kiplinger.jpg';

creator[8][0] = 'Mary Sanchez';
creator[8][1] = creatorImagesDomain + 'sanchez-mary.jpg';
creator[8][2] = 'Mary Sanchez doesn&#39;t believe in preaching to the choir. A reporter foremost, Sanchez tells it like it is. Sanchez, the daughter of a Mexican immigrant, believes that \"true culture is so much deeper\" than language or location. And that&#39;s what Mary Sanchez will deliver to your paper -- the Latina voice you&#39;re searching for.';
creator[8][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=LC&fwire=LC';
creator[8][4] = marketingImagesDomain + 'sanchez.jpg';

creator[9][0] = 'Premium Health News Service';
creator[9][1] = creatorImagesDomain + '';
creator[9][2] = 'Outstanding health-related content from top national and international publications, including U.S. News \& World Report, Psychology Today, New Scientist and Fitness magazines, Mayo Clinic and the Harvard Health Letters. The package offers solid advice on everything from the latest advances in medical care to staying fit; diet tips; helping kids to grow up healthy; and navigating changes in health insurance. Rounding out the package are Q\&As by experts at the Mayo Clinic and Harvard University, as well as commentary by Dr. Bernadine Healy of U.S. News \& World Report.';
creator[9][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=JI&fwire=JI';
creator[9][4] = marketingImagesDomain + 'phns.jpg';

creator[10][0] = 'Rick Steves';
creator[10][1] = creatorImagesDomain + 'headshot_steves-rick.jpg';
creator[10][2] = 'Rick Steves is the celebrated host, writer and producer of the public television show\"Rick Steves&#39; Europe.\" He has authored 30 European travel books, including \"Rick Steves&#39; Italy 2006,\" which is the best-selling international travel guidebook in the United States. Steves&#39; weekly column offers a much slower, more thoughtful and intimate approach to destination travel, unveiling the pleasures of Europe off the beaten path. Introduce your readers to the wonders of European travel ... the Rick Steves way.';
creator[10][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=WD&fwire=WD';
creator[10][4] = marketingImagesDomain + 'steves.jpg';

creator[11][0] = 'Sudoku';
creator[11][1] = creatorImagesDomain + 'sudoku.jpg';
creator[11][2] = 'What started on the features page in London&#39;s Daily Telegraph is now conquering the world one fan at a time. Sudoku is an innovative puzzle that gives new meaning to the phrase \"number-crunching.\" Using a 9x9 grid subdivided into 9 smaller grids of 3x3 squares, players solve the puzzle by placing the numbers 1-9 in each row, column and box. It&#39;s a cross-number crossword that&#39;s fun, exciting and addicting. It will definitely challenge your brain waves, as well as your patience!';
creator[11][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=MN&fwire=MN';
creator[11][4] = marketingImagesDomain + 'sudoku.jpg';

creator[12][0] = 'Variety Entertainment News Service';
creator[12][1] = creatorImagesDomain + 'varietybug3.gif';
creator[12][2] = 'Variety Entertainment News Service provides your entertainment and feature sections with worldwide entertainment coverage. Our 24-hour service offers breaking news and an array of articles, profiles and photos on the hottest developments in TV, film, music, theater and the celebrity world that your readers are anxious to read about. You get it from the people who are more than insiders-they&#39;re entertainment experts';
creator[12][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=UA&fwire=UA';
creator[12][4] = marketingImagesDomain + 'variety.jpg';

creator[13][0] = 'Walt Handelsman';
creator[13][1] = creatorImagesDomain + 'handelsman-walt.jpg';
creator[13][2] = 'A liberal cartoonist for Newsday, Handelsman&#39;s wry sense of humor and clear-cut commentary help readers grasp the issues before they fall to the ground laughing.';
creator[13][3] = featuresDomain + '/tms-bin/source_view.cgi?wire=OH&fwire=OH';
creator[13][4] = marketingImagesDomain + 'handelsman.jpg';


// *** ignore from here below unless you want to modify the core display


function popBio(creator,img,biotext) {

	var bio = window.open('', 'bioWin', 'height=375,width=500');
	var createbio = bio.document;
	createbio.write('<html><head><title>' + creator + '</title>');
	createbio.write('<link rel="stylesheet" href="/tms/styles/marketing.css" />');
	createbio.write('</head><body>');
	createbio.write('<h1>' + creator + '</h1>');
	createbio.write('<p>');
	if ( img && (img !== creatorImagesDomain) ) {
		createbio.write('<img src="' + img + '" alt="' + creator + '" class="mug" />');
	}
	createbio.write(biotext + '</p>');
	createbio.write('<p class="close">');
	createbio.write(' [<a href="[<a href="#" onclick="self.close()">Close this window</a>]</p>');
	createbio.write('</body></html>');
	createbio.close();
	if (window.focus && bio) {
		bio.focus();
	}
}

function accessBio(num) {
	popBio(creator[num][0], creator[num][1], creator[num][2], creator[num][3]);
}



// credit: http://www.learndzine.com/JavaScriptfiles/RandomNum.html
// this function creators 3 non-repeating random numbers
// if more than 3 creators will be featured on a page, this needs to be increased

var randCreator = new Array(3);

function getRandomNumber(max)
{

  var n = Math.floor(Math.random()*max);
//	alert(n);
	return n;
}



function selectCreators() {
	for (i = 0; i < numShown; i++) {
		var n = getRandomNumber(numCreators);

		switch(i){

		   case 0:
			while (n==randCreator[0]) {
				n = getRandomNumber(numCreators);
		 	}
			break;

	           case 1:
			while (n==randCreator[0] || n==randCreator[1]) {
				n = getRandomNumber(numCreators);
		 	}
			break;

  		   case 2:
			while (n==randCreator[0] || n==randCreator[1] || n==randCreator[2]) {
				n = getRandomNumber(numCreators);
		 	}
			break;

		default:
			break;

		}

//		alert("slot=" + i + " pic= " + n);
		 randCreator[i] = n;

	}
}


function outputMarketing() {
	selectCreators();

	if(creator[randCreator[0]][4]) document.write('<p class="small-text"><a href="#" onclick="accessBio(' + randCreator[0] + ')" title="View more about this creator"><img src="' + creator[randCreator[0]][4] + '" /></a><br />' + creator[randCreator[0]][0] + '</p>');
	if(creator[randCreator[1]][4]) document.write('<p class="small-text"><a href="#" onclick="accessBio(' + randCreator[1] + ')" title="View more about this creator"><img src="' + creator[randCreator[1]][4] + '" /></a><br />' + creator[randCreator[1]][0] + '</p>');
	if(creator[randCreator[2]][4]) document.write('<p class="small-text"><a href="#" onclick="accessBio(' + randCreator[2] + ')" title="View more about this creator"><img src="' + creator[randCreator[2]][4] + '" /></a><br />' + creator[randCreator[2]][0] + '</p>');
	document.write('<h2>Creator Headshots</h2>');
	document.write('<p><a href="http://www.tmsfeatures.com/offerings/creators" target="_creators">Download headshot images</a></p>');
	
	
}

