// 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($) {

//-----lazy load images -- http://www.appelsiini.net/projects/lazyload
//$("#Content img").lazyload({
//effect : "fadeIn",
//threshold : 10,
//failurelimit : 4
//});


//-----LOAD PLUGINS

	//-----History Plugin (fixes back button and allows deep linking) --by Taku Sano http://www.mikage.to/jquery/jquery_history.html
	// jQuery.extend({historyCurrentHash:undefined,historyCallback:undefined,historyIframeSrc:undefined,historyInit:function(callback,src){jQuery.historyCallback=callback;if(src)jQuery.historyIframeSrc=src;var current_hash=location.hash.replace(/\?.*$/,'');jQuery.historyCurrentHash=current_hash;if(jQuery.browser.msie){if(jQuery.historyCurrentHash==''){jQuery.historyCurrentHash='#';} jQuery("body").prepend('<iframe id="jQuery_history" style="display: none;"'+ (jQuery.historyIframeSrc?' src="'+jQuery.historyIframeSrc+'"':'') +'></iframe>');var ihistory=jQuery("#jQuery_history")[0];var iframe=ihistory.contentWindow.document;iframe.open();iframe.close();iframe.location.hash=current_hash;} else if(jQuery.browser.safari){jQuery.historyBackStack=[];jQuery.historyBackStack.length=history.length;jQuery.historyForwardStack=[];jQuery.lastHistoryLength=history.length;jQuery.isFirst=true;} if(current_hash) jQuery.historyCallback(current_hash.replace(/^#/,''));setInterval(jQuery.historyCheck,100);},historyAddHistory:function(hash){jQuery.historyBackStack.push(hash);jQuery.historyForwardStack.length=0;this.isFirst=true;},historyCheck:function(){if(jQuery.browser.msie){var ihistory=jQuery("#jQuery_history")[0];var iframe=ihistory.contentDocument||ihistory.contentWindow.document;var current_hash=iframe.location.hash.replace(/\?.*$/,'');if(current_hash!=jQuery.historyCurrentHash){location.hash=current_hash;jQuery.historyCurrentHash=current_hash;jQuery.historyCallback(current_hash.replace(/^#/,''));}}else if(jQuery.browser.safari){if(jQuery.lastHistoryLength==history.length&&jQuery.historyBackStack.length>jQuery.lastHistoryLength){jQuery.historyBackStack.shift();} if(!jQuery.dontCheck){var historyDelta=history.length-jQuery.historyBackStack.length;jQuery.lastHistoryLength=history.length;if(historyDelta){jQuery.isFirst=false;if(historyDelta<0){for(var i=0;i<Math.abs(historyDelta);i++)jQuery.historyForwardStack.unshift(jQuery.historyBackStack.pop());}else{for(var i=0;i<historyDelta;i++)jQuery.historyBackStack.push(jQuery.historyForwardStack.shift());} var cachedHash=jQuery.historyBackStack[jQuery.historyBackStack.length-1];if(cachedHash!=undefined){jQuery.historyCurrentHash=location.hash.replace(/\?.*$/,'');jQuery.historyCallback(cachedHash);}}else if(jQuery.historyBackStack[jQuery.historyBackStack.length-1]==undefined&&!jQuery.isFirst){if(location.hash){var current_hash=location.hash;jQuery.historyCallback(location.hash.replace(/^#/,''));}else{var current_hash='';jQuery.historyCallback('');} jQuery.isFirst=true;}}}else{var current_hash=location.hash.replace(/\?.*$/,'');if(current_hash!=jQuery.historyCurrentHash){jQuery.historyCurrentHash=current_hash;jQuery.historyCallback(current_hash.replace(/^#/,''));}}},historyLoad:function(hash){var newhash;hash=decodeURIComponent(hash.replace(/\?.*$/,''));if(jQuery.browser.safari){newhash=hash;} else{newhash='#'+hash;location.hash=newhash;} jQuery.historyCurrentHash=newhash;if(jQuery.browser.msie){var ihistory=jQuery("#jQuery_history")[0];var iframe=ihistory.contentWindow.document;iframe.open();iframe.close();iframe.location.hash=newhash;jQuery.lastHistoryLength=history.length;jQuery.historyCallback(hash);} else if(jQuery.browser.safari){jQuery.dontCheck=true;this.historyAddHistory(hash);var fn=function(){jQuery.dontCheck=false;};window.setTimeout(fn,200);jQuery.historyCallback(hash);location.hash=newhash;} else{jQuery.historyCallback(hash);}}});

	//-----preload CSS Images Plugin -- 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();
	
	
	
//-----Cascade Comment Opacity for Wordpress by Brent Lagerman and Jack Hoge
	
	function cascadeBkg(x)
	{
		var bkgColor = $('.commentlist .thread-'+x+' .vcard').css("background-color");
	
		$('.commentlist .thread-'+x).each(function(){
			$(this).find('.vcard').each(function(intIndex){
			  	$(this).css('background', 'none');
			  	$(this).parent()
			  		.before('<span class="thespan" style="background: '+bkgColor+'; position: absolute; top: 0; left: 0; width: 100%; height: 63px;"></span>')
			  		.parent().find('.thespan')
					.fadeTo("0", 1 - intIndex * 0.10);
			});
		});
	}
	//call the cascadeBkg function for even and odd threads
	cascadeBkg('odd');
	cascadeBkg('even');
	

//-----Show and Hide Stuff
		$(".Toggle")
			.addClass('MakeLink') // make headings look like links
			.addClass('headerHidden')
			.click(function(){
		        var $this = $(this);
		        if( $this.is('.headerShown') ) {
		                $this.next().slideToggle('normal');
		                $this.removeClass('headerShown');
		                $this.addClass('headerHidden');
		        }
		        else {
		                $this.next().slideToggle('normal');
		                $this.removeClass('headerHidden');
		                $this.addClass('headerShown');
		        }
		        return false;
		});


//-----Make a link with the class of popup open in a new window
$('.Popup').attr('target', '_blank');


//-----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)
	$("#name").toggleVal({ populateFrom: "label", removeLabels: true });
	$("#ulthm-ulthm").toggleVal({ populateFrom: "label", removeLabels: true });
	$("#cf_field_1").toggleVal({ populateFrom: "label", removeLabels: true }); 
	$("#cf_field_2").toggleVal({ populateFrom: "label", removeLabels: true }); 
	$("#cf_field_3").toggleVal({ populateFrom: "label", removeLabels: true }); 
	
});//<--- this is the end of the document ready function don't delete it
