/* This needs to be window load! else if won't calucalte correctly */
$(window).load(function() { 
/* Load the kwicks for main page (needs to be general load) */
if(jQuery('.kwicks').length == 0) { } else { jQuery('.kwicks').kwicks({ max: 620, spacing: 0, event: 'click' }); }
/* Shorten main menu to not exceed the bar */
	var mainMenuWidth = 0;
	jQuery('.menu-header').each(function(){
		jQuery('ul > li', this).each(function(){
			elem = jQuery(this).parent();
	if (elem.is (".sub-menu")) {
	} else {
			jQuery(this).addClass('countMenuItem');
	}
		});
	});
/* Add the colors for the menu */
	var ActiveColor = jQuery('.menuActiveColor').css("background-color");
	var ActiveFontColor = jQuery('.menuActiveColor').css("color");
	jQuery('.menu-header').each(function(){
		jQuery('ul > li', this).each(function(){
		elem = jQuery(this);
	if (elem.is (".current-menu-item")) {
		jQuery(this).stop().animate({ backgroundColor: ActiveColor, color: ActiveFontColor }, 800);
		jQuery('a:first', this).stop().animate({ color: ActiveFontColor }, 800);
		} else if (elem.is (".current-menu-parent")) {
			jQuery(this).stop().animate({ backgroundColor: ActiveColor, color: ActiveFontColor }, 800);
			jQuery('a:first', this).stop().animate({ color: ActiveFontColor }, 800);
	} else {
		
	}
	});
/* Main menu not exceeding the width */
	jQuery('ul > li.countMenuItem', this).each(function(){
				mainMenuWidth += jQuery(this).width();
				if (mainMenuWidth >= 590) {
					jQuery(this).remove();
				}
	});
	jQuery('ul > li:last', this).addClass('lastMenuItem');
	mainMenuWidth = 0;
	});
	changeMenuColors();
	/*Menu effects */
	function changeMenuColors() {
var FontColor = jQuery('.menu-main a').css("color");
var FontHoverColor = jQuery('.menuHover').css("color");
	jQuery(".sub-menu").css({ display: "none" });
	jQuery(".menu-header .menu li").hover(function(){
	jQuery(this).find('ul:first').stop(true, true).delay(300).css({visibility: "visible",display: "none"}).fadeIn();
	elem = jQuery(this);
	if (elem.is (".current_page_item")) {
	} else if (elem.is (".current-menu-parent")) {
	} else {
	jQuery('.menuHover:first', this).stop(true, true).addClass('hoverMenu').fadeIn(800);
	jQuery('a:first', this).stop().animate({ color: FontHoverColor });
	}
	}, function(){ 
	elem = jQuery(this);
	if (elem.is (".current-menu-item")) {
	} else if (elem.is (".current-menu-parent")) {
	} else {
	jQuery('.menuHover:first', this).stop(true, true).removeClass('hoverMenu').fadeOut();
	jQuery('a:first', this).stop().animate({ color: FontColor });
	}
	jQuery(this).find('.sub-menu').stop(true, true).delay(200).fadeOut(800);
	});
	}
	// We will give Internet Explorer a little bit longer for rendering the heights
	if($.browser.msie){
	time = 400;
userAgent = $.browser.version;
userAgent = userAgent.substring(0,userAgent.indexOf('.'));
version = userAgent;
} else {
	time = 50;
}
/* Fix for the height of the sidebar and main content */
setTimeout(function(){
	var MainH = jQuery('.main').outerHeight();
	var SidebarH = jQuery('#sidebar').outerHeight();
	var HeaderH = jQuery('.header').outerHeight();
	var FooterH = jQuery('.footer').outerHeight();
	var SociableH = jQuery('.sociables').outerHeight();
	if ( MainH > SidebarH ) {
		jQuery('#sidebar').animate({'height':MainH});
  } else {
		SidebarH = SidebarH - HeaderH;
		SidebarH = SidebarH - FooterH;
		SidebarH = SidebarH - SociableH;
		jQuery('.container').animate({'height':SidebarH});
	}
	}, time);
	var t = 0;
	jQuery('.kwick').each(function(){
		if ( jQuery('.container', this).height() > t ) {
	t_elem=jQuery('.container', this);
	t=t_elem.outerHeight();
	}
	});
/* Fix height of accordion */
	jQuery('.kwick').each(function(){
		jQuery('.container', this).css({'height':t});
	});
/* Check if a blog post has a featured image as well as a slider */
	jQuery('.blog-post').each(function(index) {
	blog = jQuery('.coin-slider', this).html();
	if (jQuery(".coin-slider", this).length){
	} else {
	jQuery('.wp-post-image', this).css({'margin-top':'-40px'});
	}
	});		
	jQuery('.kwick').each(function(index) {
	blog = jQuery('.coin-slider', this).html();
	if (jQuery(".coin-slider", this).length){
	} else {
	jQuery('.wp-post-image', this).css({'margin-top':'-40px'});
	}
	});
});	
/* Background image resize script */
function backgroundResize() {
	if(jQuery('#bgimg').length > 0) {
		var oImg = new Image();
		oImg.src = jQuery('#bgimg').attr('src');
		jQuery(oImg).ready(function() {
			var orgW = oImg.width;
			var orgH = oImg.height;
			if(orgW != 0 && orgH != 0) {
				var h = jQuery(window).height();
				var w = jQuery(window).width();
				var ratioW = w / orgW;
				var ratioH = h / orgH;
				if(ratioW > ratioH) {
					jQuery('#bgimg').css({'height':(w * orgH / orgW), 'width':w});
				} else {
					jQuery('#bgimg').css({'height':h, 'width':(h * orgW / orgH)});
				}
				jQuery('#bgholder').css({'display':'block'});
			}
			setTimeout(function() {
				jQuery('#bgholder').css({'display':'block'});
			}, 1000);
		});
	}
}
jQuery(document).ready(function() {	
/* Resize background image */
  if(jQuery('#bgimg').length > 0) {
    jQuery(window).resize(backgroundResize);
    jQuery('#bgimg').ready(backgroundResize);
    setTimeout(backgroundResize, 1000);
  }
	var HoverButtonColor = jQuery('.colorPick').css("color");
	var HoverButtonBackColor = jQuery('.colorPick').css("background-color");
	var HoverButtonBorderColor = jQuery(".colorPick").css("border-left-color");
	var CommentButtonColor = jQuery('#respond .form-submit input').css("color");
	var CommentButtonBackColor = jQuery('#respond .form-submit input').css("background-color");
	var CommentButtonBorderColor = jQuery("#respond .form-submit input").css("border-left-color");
	var ContactButtonColor = jQuery('.contact-field a').css("color");
	var ContactButtonBackColor = jQuery('.contact-field a').css("background-color");
	var ContactButtonBorderColor = jQuery(".contact-field a").css("border-left-color");
	var TdButtonColor = jQuery('.td-button span').css("color");
	var TdButtonBackColor = jQuery('.td-button').css("background-color");
	var TdButtonBorderColor = jQuery(".td-button").css("border-left-color");
	var SearchButtonColor = jQuery('.searchBarDrop .searchsubmit').css("color");
	var SearchButtonBackColor = jQuery('.searchBarDrop .searchsubmit').css("background-color");
	var SearchButtonBorderColor = jQuery(".searchBarDrop .searchsubmit").css("border-left-color");
	var ReadmoreButtonColor = jQuery('.read-more-wrap ').css("color");
	var ReadmoreButtonBackColor = jQuery('.read-more-wrap ').css("background-color");
	var ReadmoreButtonBorderColor = jQuery(".td-read-more").css("border-left-color");
/* Read more button hover effect */
	jQuery('.td-read-more').hover(function() {
  jQuery(this).parent().stop().animate({ backgroundColor: HoverButtonBackColor, color: HoverButtonColor, borderTopColor: HoverButtonBorderColor, borderLeftColor: HoverButtonBorderColor, borderRightColor: HoverButtonBorderColor, borderBottomColor: HoverButtonBorderColor  }, 500);
	jQuery(this).stop().animate({ borderTopColor: HoverButtonBorderColor, borderLeftColor: HoverButtonBorderColor, borderRightColor: HoverButtonBorderColor, borderBottomColor: HoverButtonBorderColor  }, 500);
	}, function() {
  jQuery(this).parent().stop().animate({ backgroundColor: ReadmoreButtonBackColor, color: ReadmoreButtonColor, borderTopColor: ReadmoreButtonBorderColor, borderLeftColor: ReadmoreButtonBorderColor, borderRightColor: ReadmoreButtonBorderColor, borderBottomColor: ReadmoreButtonBorderColor }, 500);
	jQuery(this).stop().animate({ borderTopColor: ReadmoreButtonBorderColor, borderLeftColor: ReadmoreButtonBorderColor, borderRightColor: ReadmoreButtonBorderColor, borderBottomColor: ReadmoreButtonBorderColor }, 500);
  });
/* Add comment button hover effect */
	jQuery('#commentform > .form-submit > #submit').hover(function() {
  jQuery(this).stop().animate({ backgroundColor: HoverButtonBackColor, color: HoverButtonColor, borderTopColor: HoverButtonBorderColor, borderLeftColor: HoverButtonBorderColor, borderRightColor: HoverButtonBorderColor, borderBottomColor: HoverButtonBorderColor  }, 500);
	}, function() {
  jQuery(this).stop().animate({ backgroundColor: CommentButtonBackColor, color: CommentButtonColor, borderTopColor: CommentButtonBorderColor, borderLeftColor: CommentButtonBorderColor, borderRightColor: CommentButtonBorderColor, borderBottomColor: CommentButtonBorderColor }, 500);
  });
/* Add submit button hover effect */
	jQuery('#contact-submit').hover(function() {
  jQuery(this).stop().animate({ backgroundColor: HoverButtonBackColor, color: HoverButtonColor, borderTopColor: HoverButtonBorderColor, borderLeftColor: HoverButtonBorderColor, borderRightColor: HoverButtonBorderColor, borderBottomColor: HoverButtonBorderColor  }, 500);
	}, function() {
  jQuery(this).stop().animate({ backgroundColor: ContactButtonBackColor, color: ContactButtonColor, borderTopColor: ContactButtonBorderColor, borderLeftColor: ContactButtonBorderColor, borderRightColor: ContactButtonBorderColor, borderBottomColor: ContactButtonBorderColor }, 500);
  });
/* Add searchBar button hover effect */
	jQuery('.searchsubmit').hover(function() {
  jQuery(this).stop().animate({ backgroundColor: HoverButtonBackColor, color: HoverButtonColor, borderTopColor: HoverButtonBorderColor, borderLeftColor: HoverButtonBorderColor, borderRightColor: HoverButtonBorderColor, borderBottomColor: HoverButtonBorderColor  }, 500);
	}, function() {
  jQuery(this).stop().animate({ backgroundColor: SearchButtonBackColor, color: SearchButtonColor, borderTopColor: SearchButtonBorderColor, borderLeftColor: SearchButtonBorderColor, borderRightColor: SearchButtonBorderColor, borderBottomColor: SearchButtonBorderColor }, 500);
  });
/* Add td button hover effect */
	jQuery('.td-button').hover(function() {
  jQuery(this).stop().animate({ backgroundColor: HoverButtonBackColor, color: HoverButtonColor, borderTopColor: HoverButtonBorderColor, borderLeftColor: HoverButtonBorderColor, borderRightColor: HoverButtonBorderColor, borderBottomColor: HoverButtonBorderColor  }, 500);
	jQuery('span', this).animate({ color: HoverButtonColor });
	}, function() {
  jQuery(this).stop().animate({ backgroundColor: TdButtonBackColor, color: TdButtonColor, borderTopColor: TdButtonBorderColor, borderLeftColor: TdButtonBorderColor, borderRightColor: TdButtonBorderColor, borderBottomColor: TdButtonBorderColor }, 500);
  jQuery('span', this).animate({ color: TdButtonColor });
	});
	inputHoverColor = jQuery('.inputHoverColor').css('color');
	inputColor = jQuery('.inputColor').css('color');
	inputBgHoverColor = jQuery('.inputHoverColor').css('backgroundColor');
	inputBgColor = jQuery('.inputColor').css('backgroundColor');
	jQuery("input[type='text']").css({'color': inputColor , 'backgroundColor': inputBgColor });
	jQuery("textarea").css({'color' : inputColor , 'backgroundColor': inputBgColor });
	jQuery("input").focus(function(){
		 jQuery(this).animate({'color': inputHoverColor, 'backgroundColor': inputBgHoverColor });
	});
	jQuery("input").blur(function(){
		 jQuery(this).animate({'color' : inputColor , 'backgroundColor': inputBgColor });
	});
	jQuery("textarea").focus(function(){
		jQuery(this).animate({'color': inputHoverColor, 'backgroundColor': inputBgHoverColor });
	}); 
	jQuery("textarea").blur(function(){
		 jQuery(this).animate({'color' : inputColor , 'backgroundColor': inputBgColor });
	});
/* Shorten footer menu to not exceed the bar */
	var footerMenuWidth = 0;
	jQuery('.menu-footer').each(function(){
	jQuery('ul > li', this).each(function(){
		footerMenuWidth += jQuery(this).width();
		if (footerMenuWidth >= 590) {
			jQuery(this).remove();
		}
	});
	jQuery('ul > li:last', this).addClass('lastFooterItem');
	footerMenuWidth = 0;
	});

/* VALIDATE CONTACT FORM */
	function validateContactForm() {
  var ok = true;
  jQuery.each(jQuery('.required-field'), function() {
    if(jQuery(this).val() == '') {
      ok = false;
      jQuery(this).css({'border':'#c00 solid 1px'});
    } else {
      jQuery(this).css({'border':'0'});
    }
  });
  if(ok) {
    return true;
  } else {
    jQuery('#contact-error').fadeIn();
  }
  return false;
	}
/* Contact form validation */
  if(jQuery('#contactform').length > 0) {
    jQuery('#contactform').submit(function() { return validateContactForm(); });
    jQuery('#contact-submit').click(function() {
      jQuery('#contactform').submit();
    });
  }
/* SEARCH BAR FUNCTIONS */
	jQuery('.searchBarTop').click(function() {
		jQuery('.searchBarDrop', this).slideDown('slow', function() {
			jQuery('.searchBarClose', this).fadeIn();
		});
	});
	jQuery('.searchBarClose').click(function() {
	jQuery(this).fadeOut('slow', function() {
		jQuery(this).parent().slideUp('slow', function() {
		jQuery(this).css({ display: 'none' });
		jQuery('.searchBarClose', this).css({ display: 'none' });
	});
	});
	});
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
	{
			 jQuery("#bgholder").css("position", "static");
	};
});
