$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            

        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        
        // 5. Bind to the forward and back buttons
        $('#services .arrow-back').click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('#services .arrow-forward').click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};
$.fn.infiniteCarousel2 = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            return false;
        }
        
        // 5. Bind to the forward and back buttons
        $('#logos .arrow-back').click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('#logos .arrow-forward').click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};


function slideFaqs() {
	var anchorLinks = $("a.anchorLink");
	var anchorTargets = $("h4[name=\"anchorTarget\"]");
	var show = false;

	anchorLinks.bind("click", function(){
		var self = $(this);
		var linkStr = self.attr("href");
		var linkStr = linkStr.substr(1, 15);
		anchorTargets.each(function(){
			if($(this).attr("id") === linkStr){
				slideFaqDown($(this));
			}
		});
	});
	
	anchorTargets.each(function() {
		$(this).bind("click", function(){
			if (show == false) {
				slideFaqDown($(this));
			} else if (show == true) {
				slideFaqUp($(this));
			} 
		});
	});
	
	function slideFaqDown(heading, array) {
		heading.next("div").slideDown("slow");
		show = true;
	}
	
	function slideFaqUp(heading) {
		heading.next("div").slideUp("slow");
		show = false;
	}
}


function slideMenu() {
	var lastItem = $("#menu-item-68 a");
	var lastItem2 = $("#menu-item-546 a");
	var lastItem3 = $("#menu-item-238 a");
	var lastItem4 = $("#menu-item-310 a");
	var lastItem5 = $("#menu-item-548 a");
	var show = false;

	lastItem.parent().css({"border":"none"});
	lastItem2.parent().css({"border":"none"});
	lastItem3.parent().css({"border":"none"});
	lastItem4.parent().css({"border":"none"});
	lastItem5.parent().css({"border":"none"});
	

	menuYloc = parseInt($("#sub-nav").css("top").substring(0,$("#sub-nav").css("top").indexOf("px")))
	$(window).scroll(function() {
		var offset = menuYloc+$(document).scrollTop()+"px";
			$("#sub-nav").animate({marginTop:offset},{duration:500,queue:false});
	});

	$("#menu-item-173 a, #menu-item-548 a").bind("click", function(){
		if (show == false) {
			$(this).parent().addClass("menu-down");
			$(this).next(".sub-menu").slideDown();
			show = true;
		} else if (show == true) {
			$(this).parent().removeClass("menu-down");
			$(this).next(".sub-menu").slideUp();
			show = false;
		} 
	});

}

function blogSlider() {
	var rssContainer = $("#kb-advanced-rss-2"),
			rssLink = rssContainer.find(".kbrsswidget"),
			rssDescription = rssLink.next();
	
	rssDescription.css({"display":"none"});
	
	rssLink.bind("click", function(event){
		event.preventDefault();
		$(this).next().slideToggle("fast");
	});
	
}


function checkPlaceholder() {
// Check if browser supports HTML5 input placeholder
	function supports_input_placeholder() {
		var i = document.createElement('input');
		return 'placeholder' in i;
	}
	
	// Change input text on focus
	if (!supports_input_placeholder()) {
		
		$(':text,textarea').each(function(){
			$(this).val($(this).attr('placeholder'));
		});
		
		$(':text,textarea').focus(function(){
			if ($(this).val() == $(this).attr('placeholder')) $(this).val('');
		}).blur(function(){
			if ($(this).val() == '') {
				$(this).val($(this).attr('placeholder'));
			}
		});
		
		
	} else {
		$(':text').focus(function(){
			$(this).css('color', '#000');
		});
	}

}

$(document).ready(function(){


	checkPlaceholder();
	
	$('#access a').hover(function(){
		$(this).addClass("access-hover").fadeIn("slow");
	}, function(){
		$(this).removeClass("access-hover");
	});


	$('#sliders').tabs({fx:{opacity: 'toggle'}}).tabs('rotate', 8000, true);
	$('#services .infiniteCarousel').infiniteCarousel();
	$('#logos .infiniteCarousel').infiniteCarousel2();
	
	/*
	$('.infiniteCarousel .wrapper li').hover(function(){
		$(this).find(".services-arrow").fadeIn();
	}, function(){
		$(this).find(".services-arrow").fadeOut();
	});*/
	
	var slideTabs = $(".ui-tabs-nav li");
	
	$('#menu-item-27 a').click(function() {
		$('#contactArea').slideDown();
	});
	
	$('#close-contact-form').click(function() {
		$('#contactArea').slideUp();
	});
	
	//Changes background color of even rows on homepage
	//$('#updates p:even').css({'background':'#fafafa'});
	
	//Modify latest blog post height to match testimonial height
	var firstTestimonial = $('.grid_6 p').eq(0);
	var secondTestimonial = $('.grid_6 p').eq(1);
	var thirdTestimonial = $('.grid_6 p').eq(2);
	var firstBlog = $('.advanced-recent-posts li').eq(0);
	var secondBlog = $('.advanced-recent-posts li').eq(1);
	var thirdBlog = $('.advanced-recent-posts li').eq(2);
	
	if(firstTestimonial.css('height') >= firstBlog.css('height')) {
		firstBlog.css('height', firstTestimonial.css('height')) 
	} else {
		firstTestimonial.css('height', firstBlog.css('height'))
	}
	if(secondTestimonial.css('height') >= secondBlog.css('height')) {
		secondBlog.css('height', secondTestimonial.css('height')) 
	} else {
		secondTestimonial.css('height', secondBlog.css('height'))
	}
	if(thirdTestimonial.css('height') >= thirdBlog.css('height')) {
		thirdBlog.css('height', thirdTestimonial.css('height')) 
	} else {
		thirdTestimonial.css('height', thirdBlog.css('height'))
	}
	
	if($("body").hasClass("home")){
		var container = $(".container");
		container.eq(1).css({"box-shadow":"0px 0px 0px #fff"});
		blogSlider();
		var viewMoreLink = $(".view-more").clone();
		$("#kb-advanced-rss-2").find(".widget-title").after(viewMoreLink);
		$(".footer").css({"margin":"-13px 0 50px 10px"});
		
	}
	
	
		
	if($("body").hasClass("page-template-page-inner-nav-php")){
		slideMenu();
		slideFaqs();
	} else if($("body").hasClass("page-id-424")) {
		slideMenu();
	}
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed: 'slow', /* fast/slow/normal */
		slideshow: false, /* false OR interval time in ms */
		autoplay_slideshow: false, /* true/false */
		opacity: 0.80, /* Value between 0 and 1 */
		show_title: true, /* true/false */
		allow_resize: false, /* Resize the photos bigger than viewport. true/false */
		default_width: 500,
		default_height: 344,
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook / pp_default */
		horizontal_padding: 20, /* The padding on each side of the picture */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		wmode: 'opaque', /* Set the flash wmode attribute */
		autoplay: true, /* Automatically start videos: True/False */
		modal: false, /* If set to true, only the close button will close the window */
		deeplinking: true, /* Allow prettyPhoto to update the url to enable deeplinking. */
		overlay_gallery: false, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
		keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){}, /* Called when prettyPhoto is closed */
		ie6_fallback: true,
		markup: '<div class="pp_pic_holder"> \
					<div class="ppt">&nbsp;</div> \
					<div class="pp_top"> \
						<div class="pp_left"></div> \
						<div class="pp_middle"></div> \
						<div class="pp_right"></div> \
					</div> \
					<div class="pp_content_container"> \
						<div class="pp_left"> \
						<div class="pp_right"> \
							<div class="pp_content"> \
								<div class="pp_loaderIcon"></div> \
								<div class="pp_fade"> \
									<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
									<div class="pp_hoverContainer"> \
										<a class="pp_next" href="#">next</a> \
										<a class="pp_previous" href="#">previous</a> \
									</div> \
									<div id="pp_full_res"></div> \
									<div class="pp_details"> \
										<div class="pp_nav"> \
											<a href="#" class="pp_arrow_previous">Previous</a> \
											<p class="currentTextHolder">0/0</p> \
											<a href="#" class="pp_arrow_next">Next</a> \
										</div> \
										<p class="pp_description"></p> \
										{pp_social} \
										<a class="pp_close" href="#">Close</a> \
									</div> \
								</div> \
							</div> \
						</div> \
						</div> \
					</div> \
					<div class="pp_bottom"> \
						<div class="pp_left"></div> \
						<div class="pp_middle"></div> \
						<div class="pp_right"></div> \
					</div> \
				</div> \
				<div class="pp_overlay"></div>',
		gallery_markup: '<div class="pp_gallery"> \
							<a href="#" class="pp_arrow_previous">Previous</a> \
							<div> \
								<ul> \
									{gallery} \
								</ul> \
							</div> \
							<a href="#" class="pp_arrow_next">Next</a> \
						</div>',
		image_markup: '<img id="fullResImage" src="{path}" />',
		iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
		inline_markup: '<div class="pp_inline">{content}</div>',
		custom_markup: '',
		social_tools: '<div class="pp_social"><div class="twitter"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="facebook"><iframe src="http://www.facebook.com/plugins/like.php?locale=en_US&href='+location.href+'&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:500px; height:23px;" allowTransparency="true"></iframe></div></div>' /* html or false to disable */

	});
	
	$("#services .infiniteCarousel ul li a").bind("click", function(){
		var imagesLi = $(".pp_gallery").find("li");
		for(i=0; i<imagesLi.length; i++){
			if(i<5 || i>14){
				imagesLi.eq(i).remove();
			}
		}
	});
});




