/**
 * debug logging
 */
;(function($) {	
	$.log = function(){		
		if(top.window.console && console.log) top.window.console.log(arguments);
	}
}(jQuery));

$(document).ready(function() {
	var ie = $.browser.msie;
	var ie6 = $.browser.msie && parseInt($.browser.version)==6;
	var ff2 = $.browser.mozilla && parseFloat($.browser.version)<=1.8;

	$(document).bind("contextmenu",function(e){   return false;  });  

	var hasFlash = $.fn.flash.hasFlash('9');
	if(!hasFlash){		
		var altContent = $("<span>You need the <a href='http://get.adobe.com/flashplayer/' >Adobe Flash Player</a><br/>to view this content.</span>");
		$("object[data$=swf]").each(function(n,val){
			var w = $(val).attr('width');
			var h = $(val).attr('height');
			if(w.match(/[0-9]+/)) w = w + 'px';
			if(h.match(/[0-9]+/)) h = h + 'px';			
			var newDiv = $("<div></div>");
			$(newDiv).height(h)
					.width(w)					
					.addClass('noflash')
					.html(altContent);
			$(val).replaceWith(newDiv);
		});
	}

	// blockUI defaults (change default message)
	$.extend($.blockUI.defaults,{
		message: "<img hspace='5' src='/_template/images/throbber.gif' width='16' height='16' style='vertical-align:middle'/> <span class='glow-throbber'>processing...</span>"
		,
		css: { 
			padding:        0,
			margin:         0,
			width:          '30%', 
			top:            '40%', 
			left:           '35%', 
			textAlign:      'center', 
			color:          '#000', 
			border:         '',
			backgroundColor:'',
			cursor:         'wait'
		}
		,
		 // styles for the overlay
	    overlayCSS:  { 
	        backgroundColor:'#fff', 
	        opacity:        '0.8' 
	    }
	});	
	
	/////////////////////////////////////////////
	// Homepage - curved corners on tiles
	if (!ie6) {
		$("DIV.home-tile-img").each(function(){
			var overlay = $("<div class='home-tile-overlay'></div>");
			var linkUrl = $("img", this).parents("a").attr("href");
			if (linkUrl && linkUrl.length) {
				$(overlay).click(function(){
					document.location = linkUrl;
				})
				var pos = $(this).position();
				$(overlay).css("cursor","pointer");
				// firefox doesn't like the marginTop - overlay appears underneath
				if (!ie) {
					$(overlay).css({
						margin:0,				
						position: "absolute",
						top: pos.top + 'px',
						left: pos.left + 'px'
					});
				}	
			}
			$(this).after(overlay);
		});
	}
	
	/////////////////////////////////////////////
	// remove margin on last main-nav item
	$("li:last","#main-nav").css("margin",0);

	/////////////////////////////////////////////
	// add decoration to footer links
	$("li:not(:last)", "#footer").append("<span class='footer-link-decoration'>//</span>");
	
	//////////////////////////////////////////////////////////////
	// SEARCH FIELD clear on focus
	$(":input","#search-box").focus(function () {
		if($(this).attr('value').toLowerCase()=='search') $(this).attr('value','');
	});
	$(":input","#search-box").blur(function () {
		if($(this).attr('value')=='') $(this).attr('value','Search');
	})
	
	//////////////////////////////////////////////////////////////
	// add small dragonfly to 'current' items in searchbar nav
	$("LI.current","#search-box").append("<img src='_template/images/nav_dragonfly_small.gif'/>");
	
	//////////////////////////////////////////////////////////////
	// adjust bg placement on vertical list landing page (right column)	(really only for IE6)
	$("td:last", "table.landing-vertical-list tr").css("backgroundPosition","top right");		
	
	////////////////////////////////////////////////////////////////////////////////////////
	// SITEMAP COLUMNS	
	if ($("#sitemap").size() > 0) {
		// create columns
		var sitemapColumn = $("<div class='sitemapcolumn'></div>");
		var nCols = 3;
		for (var i = 0; i < nCols; i++) {			
			var col = sitemapColumn.clone();
			col.attr('id', 'sitemapcolumn' + i);
			$("#sitemap").append(col);
		}
		$("#sitemap").append("<div class='clear'></div>");
		// add elements to columns
		var currentCol = $(".sitemapcolumn:first","#sitemap");
		$("#sitemap > UL > LI").each(function(n, el){			
			// get entry data
			var h2 = $("<h2></h2>").append( $("a:first",el).clone() );
			var nav = $("ul:first", el).clone();
			var entry = $("<div></div>").append(h2).append(nav);
			// determine which column needs more data to balance
			//$('#sitemapcolumn'+(n%nCols)).append(entry);
			
			currentCol.append(entry);
			
			var smallestCol = currentCol;
			$("#sitemap .sitemapcolumn").each(function(n2,el2){
				if($(el2).innerHeight() <= $(smallestCol).innerHeight()) smallestCol = $(el2);
			});
			currentCol = smallestCol;
		});
		$("#sitemap>ul").remove();
	}
	
	glow_initStyles();	
	
	//////////////////////////////////////////////////////////////
	// MODULE SPECIFIC INITS
	//////////////////////////////////////////////////////////////
	
	//////////////////////////////////////////////////////////////
	// Init forms
	glow_initForms();
	glow_initImageGalleries();
	glow_initCompetition();
});

function glow_initStyles() {
	/////////////////////////////////////////////
	// adjust padding on horizontal list landing page	
	$("li:nth-child(4n)", "ul.landing-horizontal-list").css("marginRight","0");	
	$("li:not(.processed)","ul.landing-horizontal-list").each(function(){
		$(this).addClass('processed');	
		// store items
		var txt = $(this).text();	
		var img = $("img",this).remove();
		var a = $("a",this).html("").remove();		
		if(a.size()==0) a = $("<a href='#'></a>");
			
		// write back in the proper sequence		
		$(this).html("");	 		// blank it
		a.append(img);		 		// image goes inside link
		$(this).append(a[0]);	 	// link goes in base
		$(this).append("<span class='caption'>" + txt + "</span>");	// caption goes after link
	});
	
	/////////////////////////////////////////////
	// adjust padding on horizontal list2 landing page	
	$("li:nth-child(4n)", "ul.landing-horizontal-list2").css("marginRight","0");		
	$("li:not(.processed)","ul.landing-horizontal-list2").each(function(){
		$(this).addClass('processed');
		// store items				
		var hdr = $("h2",this).remove();		
		var img = $("img",this).remove();
		var txt = $(this).text();
		var a = $("a:first",this).text("").remove();		
		// write back in the proper sequence		
		$(this).html("");		// blank it	
		$(this).append(hdr);	// header comes first
		a.append(img);		 	// image goes inside link
		$(this).append(a)	 	// link goes in base		
		$(this).append("<span class='caption'>" + txt + "</span>");	// caption goes after link
	});
	
	/////////////////////////////////////////////
	// adjust padding on right column promos		
	$(".promo:not(.processed)","#content-main .content-main-col2").each(function(){
		$(this).addClass('processed');
		// store items				
		var hdr = $("h2",this).remove();		
		var img = $("img",this).remove();
		var a = $("a:first",this).text("").remove();		
		var txt = $(this).html();		
		// write back in the proper sequence		
		$(this).html("");		// blank it	
		$(this).append(hdr);	// header comes first
		a.append(img);		 	// image goes inside link
		$(this).append(a)	 	// link goes in base		
		$(this).append("<span class='caption'>" + txt + "</span>");	// caption goes after link		
	});
	
	//////////////////////////////////////////////////////////////
	// Captioned images (convert to table with caption)
	$("img:not(.processed)").each(function(){
		$(this).addClass('processed');	
		var caption = $(this).attr('title');
		if(caption=="") caption = $(this).attr('alt');
		if(caption.indexOf('caption:')==0){
			caption = caption.substr(8,caption.length);
			var tbl = $("<table border='0' cellpadding='0' cellspacing='0' class='img-caption'><tr><td></td></tr><tr><td></td></tr></table>");
			
			// manipulate display
			if($(this).hasClass('img-right')){
				tbl.addClass('img-right');
				$(this).removeClass('img-right');
			}			
			$(this).css('float','none');
			$(this).css('align','');	
			
			
			// perform replacement
			$("tr:first td", tbl).append($(this).clone());		
			$("tr:last td", tbl).append(caption).addClass('caption');
			$(this).replaceWith(tbl);
		}
	});
	
	////////////////////////////////////////////////////////////////////////////////////////
	// remove top margin/padding from first paragraph element (help hero image alignment)
	$("p:first","#content").css({paddingTop:0,marginTop:0});

	/////////////////////////////////////////////
	// banded tables
	$("tr:first",".list-table-heading").addClass('first');
	$("tr:odd",".list-table-heading").addClass('odd');
	$("tr:even",".list-table-heading").addClass('even');
	// no heading	
	$("tr:odd",".list-table-no-heading").addClass('odd');
	$("tr:even",".list-table-no-heading").addClass('even');
	// no banding
	$("tr:first",".list-table-no-banding").addClass('first');	
}

