// mYm Functions v 1.3 - brent@mimoymima.com
// last edited: Oct 31, 2009
// last change: adding jsActive to page

// add class of .jsActive to pages when javascript is available
document.documentElement.className = 'jsActive';

// DOCUMENT READY: uses noConflict for prototype which we use for lightview
jQuery.noConflict();
jQuery(document).ready(function($) {

//-----Simon wrote this one <--- adds class of popup to sociable
	$('.sociable li a').addClass("Popup");

//-----add a background color to extra space around the images that are aligned center
	$('.aligncenter').parent("a").addClass("Highlight");

//-----add the class of LinkList to any elements with the class of lcp_catlist (category lists are link lists)
	$('.lcp_catlist').addClass('LinkList');

//-----clear the style info on the map-- this doesn't work yet
	$('#mYmMap').attr("style", "");

//-----clear the style info on the map
	$('#mYmMap').attr("style", "");

//-----Center descriptive text vertically for listings -- not working perfectly yet
	$('.Listing .post span').each(function(){
	
	//get height of the description
	var h = $(this).height();
	
	//subtract the height from 185px (the height of the large thumb) and divide by 2
	var varMargin = (190-h)/2;
	
	//set css on everything -- fix html for home page so that works as well
	$(this).css({ "display" : "block", "margin-top" : varMargin+"px" });
	
	});
		
//-----Cascade comment opacity

	function cascadeBkg(x)
	{
		var bkgColor = $('.commentlist .thread-'+x+' .vcard').css("background-color");
		var spanHeight = $('.commentlist .thread-'+x+' .vcard').css("height");
	
		$('.commentlist .thread-'+x).each(function(){
			$(this).find('.vcard').each(function(intIndex){
			  	$(this).css('background', 'none');
			  	$(this).parent()
			  		.before('<span class="spanBkg" style="background: '+bkgColor+'; height: '+spanHeight+';"></span>')
			  		.parent().find('.spanBkg')
					.fadeTo("0", 1 - intIndex * 0.10);
			});
		});
	}
	// call the cascadeBkg function for even and odd threads
	cascadeBkg('odd');
	cascadeBkg('even');

	
//-----fade in and out nav on page transitions
	// Fade in when you enter a page
	$('#MainNav, #mYmLogo').css({'display' : 'none'}).fadeIn(1200);

	// Fade out when you click any link on a page
	$('.InternalLink').click(function(){
		$('#MainNav').fadeOut('fast');
		return true;
	});
	
	
//-----Make a link with the class of popup open in a new window
	$('.Popup').attr('target', '_blank');


//-----Show and Hide Stuff
	$('.Toggle')
		.addClass('MakeLink') // make headings look like links
		.click(function() {
    		$(this).next().slideToggle('normal');
	}).next().hide();
	
	
//-----Add a print link in the PageNav if there is a PageNav
	$('#PageNav > :last').after("<a href='' id='PrintLink'>print</a>");
	$('#PrintLink').click(function(){
		window.print();
		return false;
	});


//-----Toggle text in an input box -- ToggleVal 2.1 - http://jquery.kuzemchak.net/toggleval.php
(function($){$.fn.toggleVal=function(theOptions){if(!theOptions||typeof(theOptions)=="object"){theOptions=$.extend({focusClass:"tv-focused",changedClass:"tv-changed",populateFrom:"default",text:null,removeLabels:false},theOptions)}else if(typeof(theOptions)=="string"&&theOptions.toLowerCase()=="destroy"){var destroy=true}return this.each(function(){if(destroy){$(this).unbind("focus.toggleval").unbind("blur.toggleval").removeData("defText");return false}var defText="";switch(theOptions.populateFrom){case"alt":defText=$(this).attr("alt");$(this).val(defText);break;case"label":defText=$("label[for='"+$(this).attr("id")+"']").text();$(this).val(defText);break;case"custom":defText=theOptions.text;$(this).val(defText);break;default:defText=$(this).val()}$(this).addClass("toggleval").data("defText",defText);if(theOptions.removeLabels==true){$("label[for='"+$(this).attr("id")+"']").remove()}$(this).bind("focus.toggleval",function(){if($(this).val()==$(this).data("defText")){$(this).val("")}$(this).addClass(theOptions.focusClass).removeClass(theOptions.changedClass)}).bind("blur.toggleval",function(){if($(this).val()==""){$(this).val($(this).data("defText"))}$(this).removeClass(theOptions.focusClass);if($(this).val()!=$(this).data("defText")){$(this).addClass(theOptions.changedClass)}else{$(this).removeClass(theOptions.changedClass)}})})}})(jQuery);
	// toggle the searchbox message (only works correctly at the moment when one searchbox is on the page)
	$(".SearchForm #s").toggleVal({ populateFrom: "label", removeLabels: true });


//-----preload CSS Images -- by Scott Jehl, http://www.filamentgroup.com/lab/update_automatically_preload_images_from_css_with_jquery/
	;jQuery.preloadCssImages=function(settings){settings=jQuery.extend({statusTextEl:null,statusBarEl:null,errorDelay:999,simultaneousCacheLoading:2},settings);var allImgs=[],loaded=0,imgUrls=[],thisSheetRules,errorTimer;function onImgComplete(){clearTimeout(errorTimer);if(imgUrls&&imgUrls.length&&imgUrls[loaded]){loaded++;if(settings.statusTextEl){var nowloading=(imgUrls[loaded])?'Now Loading: <span>'+imgUrls[loaded].split('/')[imgUrls[loaded].split('/').length-1]:'Loading complete';jQuery(settings.statusTextEl).html('<span class="numLoaded">'+loaded+'</span> of <span class="numTotal">'+imgUrls.length+'</span> loaded (<span class="percentLoaded">'+(loaded/imgUrls.length*100).toFixed(0)+'%</span>) <span class="currentImg">'+nowloading+'</span></span>')}if(settings.statusBarEl){var barWidth=jQuery(settings.statusBarEl).width();jQuery(settings.statusBarEl).css('background-position',-(barWidth-(barWidth*loaded/imgUrls.length).toFixed(0))+'px 50%')}loadImgs()}}function loadImgs(){if(imgUrls&&imgUrls.length&&imgUrls[loaded]){var img=new Image();img.src=imgUrls[loaded];if(!img.complete){jQuery(img).bind('error load onreadystatechange',onImgComplete)}else{onImgComplete()}errorTimer=setTimeout(onImgComplete,settings.errorDelay)}}function parseCSS(sheets,urls){var w3cImport=false,imported=[],importedSrc=[],baseURL;var sheetIndex=sheets.length;while(sheetIndex--){var cssPile='';if(urls&&urls[sheetIndex]){baseURL=urls[sheetIndex]}else{var csshref=(sheets[sheetIndex].href)?sheets[sheetIndex].href:'window.location.href';var baseURLarr=csshref.split('/');baseURLarr.pop();baseURL=baseURLarr.join('/');if(baseURL){baseURL+='/'}}if(sheets[sheetIndex].cssRules||sheets[sheetIndex].rules){thisSheetRules=(sheets[sheetIndex].cssRules)?sheets[sheetIndex].cssRules:sheets[sheetIndex].rules;var ruleIndex=thisSheetRules.length;while(ruleIndex--){if(thisSheetRules[ruleIndex].style&&thisSheetRules[ruleIndex].style.cssText){var text=thisSheetRules[ruleIndex].style.cssText;if(text.toLowerCase().indexOf('url')!=-1){cssPile+=text}}else if(thisSheetRules[ruleIndex].styleSheet){imported.push(thisSheetRules[ruleIndex].styleSheet);w3cImport=true}}}var tmpImage=cssPile.match(/[^\("]+\.(gif|jpg|jpeg|png)/g);if(tmpImage){var i=tmpImage.length;while(i--){var imgSrc=(tmpImage[i].charAt(0)=='/'||tmpImage[i].match('://'))?tmpImage[i]:baseURL+tmpImage[i];if(jQuery.inArray(imgSrc,imgUrls)==-1){imgUrls.push(imgSrc)}}}if(!w3cImport&&sheets[sheetIndex].imports&&sheets[sheetIndex].imports.length){for(var iImport=0,importLen=sheets[sheetIndex].imports.length;iImport<importLen;iImport++){var iHref=sheets[sheetIndex].imports[iImport].href;iHref=iHref.split('/');iHref.pop();iHref=iHref.join('/');if(iHref){iHref+='/'}var iSrc=(iHref.charAt(0)=='/'||iHref.match('://'))?iHref:baseURL+iHref;importedSrc.push(iSrc);imported.push(sheets[sheetIndex].imports[iImport])}}}if(imported.length){parseCSS(imported,importedSrc);return false}var downloads=settings.simultaneousCacheLoading;while(downloads--){setTimeout(loadImgs,downloads)}}parseCSS(document.styleSheets);return imgUrls};
	$.preloadCssImages();


//END
});