$(function() {
	
	var pageId = $(document.body).attr('id');
	
	CGSITE.updateCopyright(2007);
	
	// initialize modal window only if not on an Apple iphone/ipod touch/ipad
	if (!CGSITE.isAppleDevice()) {
		var maxHeight = $(window).height();
		var maxWidth = $(window).width();
		var winWidth = 800;
		var winHeight = maxHeight - 80;
		winHeight = winHeight < 400 ? 400 : winHeight;
		var gbOptions = {
			gbWidth: winWidth,
			gbHeight: winHeight
		};
		$('#downloadsLink').greybox(gbOptions);
	}
	
	// get all h2 tags and convert to Flash + add printable HTML headline after
	$('h2').each(function(index){
		if (!$(this).hasClass('noConvert')) {
			$(this).attr('id', 'headline' + index);
			var headlineText = $(this).html();
			var flashText = CGSITE.textToFlash(headlineText);
			// if it's not inside a .featurePods li, it's an overall page headline:
			if (!$(this).parent().hasClass('featurePods')) {
				var swfUrl = "headline_page.swf?hlText=" + flashText;
				var so = new SWFObject(swfUrl, flashText, 545, 28, 9, "#FFFFFF");
			// else, if it's a headline inside a .featurePods li:
			} else {
				var swfUrl = "headline_featurepod.swf?hlText=" + flashText;
				var so = new SWFObject(swfUrl, flashText, 265, 28, 9, "#FFFFFF");
			}
			so.addParam("wmode", "transparent");
			so.write($(this).attr('id'));
			/* did the Flash get added? if so, add a printing version of the headline
			 * since objects and embeds won't print, per style sheet
			 */
			var comparisonString = $(this).html();
			if (comparisonString.match(/<embed/gi) || comparisonString.match(/<object/gi) || comparisonString.match(/\.swf/gi)) {
				var printOnlyHead = $('<h2 class="printOnly">' + headlineText + '<\/h2>');
				$(this).after(printOnlyHead);
			} // end if
		} // end if
	});
	
	// get all featurePods divs and reformat to 2-column layout
	var featurePods = $('ul.featurePods li');
	var clearingEl = pageId == 'downloadsPage' || pageId == 'gamesHome' ? "" : "<li class=\"clear\"><\/li>";
	var floatValue = "right";
	featurePods.each(function(index){
		if ($(this).parent().hasClass('featurePods')) {
			// pad the top-right pod a bit at right, to compensate for banana tree leaf
			if (index == 1) {
				var innerPad = $('<div><\/div>');
				innerPad.addClass('treePad');
				innerPad.html($(this).html());
				$(this).html(innerPad);
			}
			floatValue = (floatValue == "right" ? "left" : "right");
			$(this).css( { 'width' : '48%', 'float' : floatValue } );
			if (floatValue == "right" || index == featurePods.length - 1) {
				$(this).after(clearingEl);
			} // end inner if
		} // end outer if
	});
	
	// write Flash games into their spots on game pages
	if ($('#gamePod').hasClass('games_drawing') || $('#gamePod').hasClass('games_counting')) {
		switch($('#gamePod').attr('class')) {
			case "games_drawing":
				var hl = "Learn to Draw Curious George!";
				break;
			case "games_counting":
				var hl = "Curious About Counting?";
				break;
		}
		var fileName = $('#gamePod').attr('class') + ".swf";
		var so = new SWFObject(fileName, hl, 550, 366, 9, "#FFFFFF");
		so.addParam("wmode", "transparent");
		so.write("gamePod");
	}
	
});


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20880597-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
