/*
Author: Joe Tan (joetan54@gmail.com)
*/
jQuery(function($) {
	$('input[type=text]:not(.noclear)').focus(function() {
		if ($(this).attr('defaultValue') == $(this).val()) $(this).val('');
	});
	$('link[href$=UserGlobalStyle.css]').remove()
});

$('link[href$=UserGlobalStyle.css]').remove()

// drop down menu handlers
jQuery(function($) {
	var nav = $('#nav');
	/*
	nav.find('ul li.level-1 a.level-1').click(function() {
		var li = $(this).parent();
		if (li.hasClass('over')) {
			li.removeClass('over').find('.level-2').removeClass('hover').hide();
		} else {
			li.addClass('over').siblings().removeClass('over').removeClass('selected');
			li.siblings().find('.level-2').removeClass('hover').hide();
			li.find('.level-2').addClass('hover').show();
			
		}
		return false;
	}).hover(function() {
	}, function() {
	});
	*/
	// mouse over
	nav.find('ul li.level-1').hover(
		function() { //over
			if(nav.data('menuTimer'))    clearTimeout(nav.data('menuTimer'))
			if(nav.data('menuTimerOut')) clearTimeout(nav.data('menuTimerOut'))
			if ($(this).find('ul.level-2').hasClass('hover')) { return;} // already opened
			
			nav.data('menuDropDown', $(this).find('.level-2'));
			nav.data('menuTimer', setTimeout(function() {
				$('#nav .level-2').hide().removeClass('hover');
				$('#nav a.level-1').removeClass('hover');

				var menu = $('#nav').data('menuDropDown')
				$(menu).addClass('hover').show();
				$(menu).prev().addClass('hover').show();
				
			}, 150));
			$(this).addClass('over');
		},
		function() { //out
			if(nav.data('menuTimer'))    clearTimeout(nav.data('menuTimer'))
			if(nav.data('menuTimerOut')) clearTimeout(nav.data('menuTimerOut'))
			nav.data('menuTimerOut', setTimeout(function() {
				$('#nav .level-2').hide().removeClass('hover');
				$('#nav a.level-1').removeClass('hover');
			}, 1500));
			$(this).removeClass('over');
		}
	);
	nav.find('.level-2 > *').mouseover(function() {
		var nav = $('#nav');
		if(nav.data('menuTimer'))    clearTimeout(nav.data('menuTimer'))
		if(nav.data('menuTimerOut')) clearTimeout(nav.data('menuTimerOut'))
	});

	$('#subnav > ul, #subnav > ul > li, , #subnav > ul > li > a').addClass('level-1');
	$('#subnav > ul').each(function() {
		$(this).find('> li > ul, > li > ul > li').addClass('level-2')
	});

	// add level-1, level-2, etc
	addNavClasses('#subnav');
	
	// expanding nav
	$('#subnav a[href$=#]').each(function() {
		$(this).click(function() {
			if ($(this).hasClass('opened')) {
				if ($(this).removeClass('opened').siblings('ul').hide().length > 0) {
					return false;
				}
			} else {
				if ($(this).addClass('opened').siblings('ul').show().length > 0) {
					return false;
				}

			}
		});
	});
	
	// add selected classes
	$('#subnav a').each(function() {
		var loc = window.location.toString();
		if (this.href == loc) {
			$(this).addClass('selected').parent().addClass('selected');
		}
	});
});

function addNavClasses(root, level) {
	if (level > 5) return;
	if (!level) level=1;
	
	$(root).find('> ul, > ul > li, > ul > li > a').addClass('level-'+level);
	$(root).find('> ul > li').each(function() {
		addNavClasses(this, level+1);
	})
}

// misc page tweaks
jQuery(function($) {
	$('ul').each(function() {
		$(this).find('> li:last').addClass('last');
	});
	$('img[align=left]').addClass('alignleft');
	$('img[align=right]').addClass('alignright');
});


// tabbed
jQuery(function($) {
	$('.tabbed .tabs li a').each(function() {
		if ($(this).find('br').length <= 0) {
			$(this).addClass('single');
		}
	}).click(function() {
		$(this).parent().addClass('selected').siblings().removeClass('selected').each(function() {
			$( $(this).find('a').attr('href') ).hide();
		});
		
		if ($(this).attr('href').indexOf('#') == 0) {
			if ($( $(this).attr('href') ).show().length <= 0) {
				console.log('Content for this tab not found');
			}
		}
		return false;
	});
	$('.tabbed .tabs li.selected a').click();
});

// replace media icons with transparent pngs
jQuery(function($) {
	var pagename = $('.storiesfrom').text();
	if( pagename == 'Videos' ) {
		$('.stories-icon').replaceWith("<div class='icon-video'>Video</div>");
	}
	else if( pagename == 'Audio' ) {
		$('.stories-icon').replaceWith("<div class='icon-audio'>Audio</div>");
	}
	else if( pagename == 'Photo Galleries' ) {
		$('.stories-icon').replaceWith("<div class='icon-photo'>Photo</div>");
	}
});

function expand(img_c, img_e, div) {
	var el = document.getElementById(div);

	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
		document.getElementById(img_c).style.display = '';
		document.getElementById(img_e).style.display = 'none';
	}
	else {
		el.style.display = '';
		document.getElementById(img_e).style.display = '';
		document.getElementById(img_c).style.display = 'none';
	}

}
function expand2(elem, div) {
	var el = document.getElementById(div);

	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
		$(elem).removeClass('close');
		$(elem).addClass('expandable');
	}
	else {
		el.style.display = '';
		$(elem).removeClass('expandable');
		$(elem).addClass('close');
	}

}