/*===== ui.js =====*/
/* -------------------------------------------------- *
 * ToggleVal Plugin for jQuery                        *
 * Version 1.0                                        *
 * -------------------------------------------------- *
 * Author:   Aaron Kuzemchak                          *
 * URL:      http://kuzemchak.net/                    *
 * E-mail:   afkuzemchak@gmail.com                    *
 * Date:     8/18/2007                                *
 * -------------------------------------------------- */
jQuery.fn.toggleVal = function(focusClass) {
	this.each(function() {
		$(this).focus(function() {
			// clear value if current value is the default
			if($(this).val() == this.defaultValue) { $(this).val(""); }
			
			// if focusClass is set, add the class
			if(focusClass) { $(this).addClass(focusClass); }
		}).blur(function() {
			// restore to the default value if current value is empty
			if($(this).val() == "") { $(this).val(this.defaultValue); }
			
			// if focusClass is set, remove class
			if(focusClass) { $(this).removeClass(focusClass); }
		});
	});
}
$(function(){
	if (typeof window.newsSite == 'undefined') window.newsSite = false;
	
	$('a.img-link.disable').each(function(){
		$(this).replaceWith($(this).html());
	});
	$('a.disable').remove();
	$('input.toggle').toggleVal();
	// header flash
	if (swfobject) {
		if (newsSite) {
			var flashvars = {'xml_loc':'pages/news-slide/', 'addOverlay':'no'};
		} else {
			var flashvars = {'xml_loc':'pages/slideshow/', 'addOverlay':'yes'};
		}
		var params = {'swLiveConnect':'true', 'wmode':'transparent'};
		swfobject.embedSWF("assets/swf/slideshow.swf", "slideshow", "800", "300", "8.0.0", "assets/swf/expressInstall.swf", flashvars, params, params);
	} else {
		$('#slideshow').append($('#slideshow').children('noscript').text());
	}
	
	if (!window.newsSite) {
		// spambot protection on simple contact form
		$('#cap1').val(1);
		$('#cap2').val(0);
	}
	
	$('.filethumbnail.topleft, .filethumbnail.topright').prependTo('.content:first p:first');
	
	if (typeof window.TOOLSCONTEXT_editFlags == 'undefined') {
		if (!window.newsSite) {
			// quick links
			$('#quicklinks ul').each(function(){
				$(this).children('li:gt(5)').addClass('extra').slideUp();
				$(this).append('<li class="more"><a href="#">more...</a></li>');
			});
			$('#quicklinks li.more a').click(function(){
				if ($(this).text() == 'more...') {
					$(this).parents('ul').eq(0).children('li.extra').slideDown();
					$(this).text('hide');
				} else {
					$(this).parents('ul').eq(0).children('li.extra').slideUp();
					$(this).text('more...');
				}
				$(this).blur();
				return false;
			});
		}
		
		// drop shadows on images
		$('#photos img').wrap("<div class='wrap1'><div class='wrap2'><div class='wrap3'></div></div></div>");
		// images in main content
		$('#maincol .content:first img').each(function(){
			// ignore addthis img and spacers
			if (/addthis\.com|spacer\.gif/g.test(this.src)) return true;
			if ($(this).parents('.noshadow').length > 0) return true;
			// check already wrapped
			if ($(this).parents('.wrap1').length == 0) {
				// check float and clear
				var imgfloat = $(this).css('float');
				$(this).css('float','none');
				var wrapclass = 'contentimg';
				if (imgfloat == 'left') wrapclass = wrapclass + '_left';
				if (imgfloat == 'right') wrapclass = wrapclass + '_right';
				$(this).wrap("<div class='wrap1 "+wrapclass+"'><div class='wrap2'><div class='wrap3'></div></div></div>");
				if (wrapclass == 'contentimg') {
					// no float, add clear after
					$(this).parents('.wrap1').eq(0).after('<div class="clear"></div>');
				}
			}
		});
		
		// drop shadows on videos
		// wrap objects/embeds that aren't wrapped already
		$('#maincol .content object').each(function(){
			if ($(this).parents('.wrap1').length == 0) {
				$(this).wrap("<div class='wrap1 video'><div class='wrap2'><div class='wrap3'></div></div></div>");
			}
		});
		// wrap embeds that aren't wrapped already
		$('#maincol .content embed').each(function(){
			if ($(this).parents('.wrap1').length == 0) {
				$(this).wrap("<div class='wrap1 video'><div class='wrap2'><div class='wrap3'></div></div></div>");
			}
		});
		// clear float
		$('#maincol .content .wrap1.video').after('<div class="clear"></div>');
		// center videos in centered paragraphs
		$('#maincol .content .wrap1.video').each(function(){
			if ($(this).parent().css('text-align') == 'center') {
				$(this).css('margin-left', Math.floor(($(this).parent().width() - $(this).find('object, embed').eq(0).width()) / 2) + 'px');
			} else if ($(this).parent().css('text-align') == 'right') {
				$(this).css('float', 'right');
			}
		});
	}
});