// JavaScript Document
jQuery(document).ready(function() {
								
if ( jQuery(".entry div").hasClass("gallery") ) {
				
				var link1 = jQuery("#gallery-1 dl:first");
				var theLink = jQuery(this).find("dl a").attr('href');
				var imageLinks = jQuery(".gallery").find("dl"); 
					//var class = jQuery('img').attr('class');
					//var class1 = class.split(" ");
				
					//var classList = class1[1].split("img"); 

				//alert(jQuery(".gallery-caption").text());
				//alert(theLink);
				jQuery(".gallery").prepend('<div id="main-image"></div>');
				
				jQuery(".gallery-item a").click(function(){
					findNext(this);
				 
					
					//jQuery('#main-image div').fadeOut(300);
					//jQuery('#main-image div').eq(classList[1]).fadeIn(300);

					return false;
				});
				var imageCaptions = jQuery('a').attr('alt');
				//var iii = jQuery(".gallery-captions").find("text");
				//alert(imageLinks.length);
				if(imageLinks){
					var iCount = 0;
					jQuery(imageLinks).each(function(){
						jQuery('.gallery-item img').eq(iCount).addClass('img'+iCount);							
						jQuery('#main-image').append('<div><img src="'+jQuery(imageLinks[iCount]).find('a').attr('href')+'" /><p>'+jQuery(imageLinks[iCount]).find('a').attr('title')+'</p></div>');
						iCount++;
						});
				}
				var divH = jQuery('#main-image div').css('height');
				var imgH = jQuery('#main-image div img').css('height');
						//jQuery('.gallery-item').css({'padding-top' : 420, 'border': 'none'});
						jQuery('.gallery-item').wrapAll('<div class="gallery-items" />');
						jQuery('.gallery img').css({'border': 'none'});
						jQuery('#main-image div p').css({'width' : 560});
						jQuery('#gallery-1').removeAttr('id');

					jQuery('#main-image div').css({'position' : 'absolute', 'background' : 'none', 'padding' : '0'});
				jQuery('#main-image div').hide();
				jQuery('#main-image div:first').show();
				//jQuery('.gallery-item')
			}
		if ( jQuery("#post-91").hasClass("post") ) {
			var i = 1;
		
			jQuery("#post-91 div h4").each(function(){
						var newID = 'q'+i;			
						jQuery(this).attr('id', newID);
						jQuery(this).prepend('<span class="top-btn">Back to top</span>');
						 i+=1;
			});
			jQuery('.top-btn').css({'cursor':'pointer','float' : 'right','padding-left' : '20px'});
			jQuery('.top-btn').click(function(){
				jQuery.scrollTo( 0, 800 );
											  });
			jQuery('#post-91 div p a').click(function(){
										
				var test = trim(jQuery(this).text(), " ");
				var testCase = ":contains('"+test+"')";
				
				jQuery("#post-91 div h4").each(function(){
					 if (jQuery(this).is(testCase)){
						 var found = jQuery(this);
						 var loc = found.position();
						jQuery.scrollTo( loc.top-20, 800 );

						 
						 
						 return false;

					 }
					 else {
						 //alert('didnt find: '+test+' '+found);
					 }
				 });
			});
			
//			jQuery('#post-91 div p').css("padding","20px");

		}
	
	function findNext(thisvar) {	
			jQuery(".gallery-item a").filter(function(index) {
					var testt = jQuery(imageLinks[index]).find('a').attr('href');										  
  					//alert(imageLinks[0].find('a').attr('href'));
					if(thisvar==testt){
						jQuery('#main-image div').fadeOut(300);
						jQuery('#main-image div').eq(index).fadeIn(300);

					}
					//return index;
					});	
	}
	if( jQuery('.img-show') ) {
		var imgheight = jQuery(".img-show").find('img').css('height');
		var content1 = jQuery(".img-show").find('img');
		//var randnum = Math.floor(Math.random()*(content.length));
		//$('#sidebar ul').prepend(content[randnum]);
		jQuery(content1).hide();
		jQuery(content1[0]).show();
		var count1 = 0;
		var timer1 = setInterval(cycleImgShow,9000);
	}
	
	//jQuery('.testimonial-message div:first').show();	
	if( jQuery('.testimonial-message') ) {
		
		var content = jQuery(".testimonial-message").find('div');
		//var randnum = Math.floor(Math.random()*(content.length));
		//$('#sidebar ul').prepend(content[randnum]);
		
		jQuery(content[0]).show();
		var count = 0;
		var timer = setInterval(cycleSidebar,9000);
	}
	if( jQuery('.above-beyond') ) {
		
		var content2 = jQuery(".above-beyond");
		//var randnum = Math.floor(Math.random()*(content.length));
		//$('#sidebar ul').prepend(content[randnum]);
		
		jQuery(content2[0]).show();
		var count2 = 0;
		var timer2 = setInterval(cycleAboveBeyond,15000);
	}
function cycleSidebar() {
	if(count==content.length-1) {
		sideOut(content[count]);
			count=0;
		sideIn(content[count]);
	}
	else {	
		sideOut(content[count]);
			count++;	
		sideIn(content[count]);
	}	
}
function cycleImgShow() {
	if(count1==content1.length-1) {
		sideOut(content1[count1]);
			count1=0;
		sideIn(content1[count1]);
	}
	else {	
		sideOut(content1[count1]);
			count1++;	
		sideIn(content1[count1]);
	}	
}
function cycleAboveBeyond() {
	if(count2==content2.length-1) {
		sideOut(content2[count2]);
			count2=0;
		sideIn(content2[count2]);
	}
	else {	
		sideOut(content2[count2]);
			count2++;	
		sideIn(content2[count2]);
	}	
}

function sideOut(arg) {
	if(jQuery.browser.msie) {
		jQuery(arg).hide();
	} else {
	jQuery(arg).fadeOut(200);
	}
}
function sideIn(arg) {
	if(jQuery.browser.msie) {
		jQuery(arg).show();
	} else {	
	jQuery(arg).fadeIn(500);
	}
}
		
if( jQuery('#contact1') ) {
	var cont = jQuery('#contact1 div div').find('p:eq(1)').remove();
	jQuery('#contact2 div.alignleft-home').append(cont);
}
/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
});
