$(document).ready(function(){
    
    // Home Page Carousel
    $("#carousel").jCarouselLite({
        btnNext: "#next",
        btnPrev: "#prev",
        circular: "true"
    });
    		
    
    // Home Page News Scroller
    $('#news-insight-wrapper').before('<div id="pager-nav">').cycle({ 
    	fx:     'scrollDown', 
    	speed:   1000, 
    	timeout: 10000,
    	delay: -3000,
    	pause: 1, 
    	pager:  '#pager-nav' 
	});
	
	
	//Accordions
	$("#accordion > div").hide();
	$('#accordion > h3').click(function () {
 		var $nextDiv = $(this).next();
 		var $visibleSiblings = $nextDiv.siblings('div:visible');
		 	if ($visibleSiblings.length ) {
   				$visibleSiblings.slideUp('slow', function() {
  					$nextDiv.slideToggle('slow');
   				});
		 	} else {
    			$nextDiv.slideToggle('slow');
 		 	}
		$(this).toggleClass('active').siblings(".active").removeClass('active');
	});
	
	$("#accordion > h3").mouseover(function() {$(this).addClass("over");});
	$("#accordion > h3").mouseout(function() {$(this).removeClass("over");});

	
	// Accordion listeners. toggles accordion from another anchor
	$('#cordCall a').click(function() {
  		var thisClass = this.className;
  		$('h3.' + thisClass).trigger('click');
  		return false;
	});
	
	
	// Spec Sheet Slide Out
	$("#subscribe-form").hide();
	$("a.toggle").click(function () {
		$("#subscribe-form").slideToggle("slow");
		return false;
	});
	
	
	// Portfolio Nav Slider :: Part 1
	$(".segmentType").hide();
	$('#segmentFilter li a').click(function() {
		var thisBtn = this.className;
		$('ul.' + thisBtn).slideToggle('slow').siblings('.segmentType').slideUp('slow');
		$(this).parent().toggleClass('current').siblings().removeClass('current');
		
		if (thisBtn == 'all') {
			$('.segmentType').slideUp('fast');
			$('ul#portfolio li.hidden').fadeIn('slow').removeClass('hidden');
			$('.segmentType .current').removeClass('current');
		}
		
		return false;
	});
	
	// Dynamic Portfolio :: Part 2
	$('.segmentType li a').click(function() {
	$(this).css('outline','none');
	$('.segmentType .current').removeClass('current');
	$(this).parent().addClass('current');
		
	var filterVal = $(this).text().toLowerCase().replace(/ /ig,'-');
				
	$('ul#portfolio li').each(function() {
		if(!$(this).hasClass(filterVal)) {
			$(this).fadeOut('normal').addClass('hidden');
		} else {
			$(this).fadeIn('slow').removeClass('hidden');
		}
	});
	
	return false;
	}); // end portfolio filter

	
	//Portfolio Tool Tip
	$(".tooltip").easyTooltip();
	
	//Portfolio Single Item
	$('#port-box').before('<div id="port-nav">').cycle({ 
	    fx:     'scrollLeft', 
	    speed:  'fast', 
	    timeout: 0, 
	    pager:  '#port-nav' 
	});
	
	//Opens new window for external sites
	$("a.external").click(function(){
		window.open(this.href);
		return false
	});

	
}); // end doc ready


// AJAX Eblitz Subscribe
$(function()
	{
		$(":submit").click(function() {	
			
			// First, disable the form from submitting
			$('#subForm').submit(function() { return false; });
			
			// Grab form action
			formAction = $("#subForm").attr("action");
			
			emailId = "tjwv";
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("#subForm").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "/proxy_curl.php",
				type: "POST",
				data: final,
				success: function(html){
					$("#theForm").hide(); // If successfully submitted hides the form
					$("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
					$("#subscribe a").addClass('sent'); // Adds 'sent' class
				}
			});
		});
	});

function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}