$(document).ready(function(){
						   
	/* News Ticker */
	
	/* Initially hide all news items */
	
	$('#ticker1').hide();
	$('#ticker2').hide();
	$('#ticker3').hide();
	
	var randomNum = Math.floor(Math.random()*3); /* Pick random number */
	
	newsTicker();
	
	function newsTicker() {
		
		if (!$("#ticker").hasClass("noTicker")) {
	
			$("#ticker").oneTime(2000,function(i) { /* Do the first pull out once */
											   
				$('div#ticker div:eq(' + randomNum + ')').show(); /* Select div with random number */
								   
				$("#ticker").animate({right: "0"}, {duration: 800 }); /* Pull out ticker with random div */
				
			});
		
			$("#ticker").oneTime(15000,function(i) { /* Do the first retract once */
								   
				$("#ticker").animate({right: "-450"}, {duration: 800}); /* Retract ticker */
				
				$("#ticker").oneTime(1000,function(i) { /* Afterwards */
								   
					$('div#ticker div:eq(' + (randomNum) + ')').hide(); /* Hide that div */
				
				});
			
			});
		
			$("#ticker").everyTime(16500,function(i) { /* Everytime timer gets to certain point */
		
				/* Show next div */
				
				randomNum = (randomNum+1)%3;
				
				$('div#ticker div:eq(' + (randomNum) + ')').show();
		
				$("#ticker").animate({right: "0"}, {duration: 800}); /* Pull out right away */
		
		
				$("#ticker").oneTime(15000,function(i) { /* Afterwards */
		
					$("#ticker").animate({right: "-450"}, {duration: 800});/* Retract ticker */
		
				});
		
				$("#ticker").oneTime(16000,function(i) { /* Afterwards */
		
					/* Hide all divs */
			
					$('#ticker1').hide();
					$('#ticker2').hide();
					$('#ticker3').hide();
		
				});
				
			});
		
		} else {
			
			$("#ticker").animate({right: "-450"}, {duration: 800}); /* Retract ticker */
		
			$("#ticker").oneTime(1000,function(i) { /* Afterwards */
						   
				$('div#ticker div:eq(' + (randomNum) + ')').hide(); /* Hide that div */
				
			});
			
			$("#ticker").stopTime();
			
		}
	
	}
	
	/* when nav item is clicked re-run news ticker function but give it new class to prevent activity */
	
	/* $("#nav li").click(function() {
		
		$("#ticker").addClass("noTicker");
		
		newsTicker();
														 
	}); */
	
	/* when close button is clicked re-run news ticker function but take away new class so activity can start again */
	
	/* $("#cboxClose").click(function() {
		
		$("#ticker").removeClass("noTicker");
		
		newsTicker();
		
		alert("Callback was activated");
														 
	}); */
						   
	/* Greycale Ticker Icons */
	
	$("ul.icons li").hover(function() { /* On hover */
		
	var thumbOver = $(this).find("img").attr("src"); /* Find image source */
		
	/* Swap background */
	
	$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') center bottom no-repeat'}); 
	$(this).find("span").stop().fadeTo('fast', 0 , function() {
		$(this).hide()
	}); 
	} , function() {
		$(this).find("span").stop().fadeTo('fast', 1).show();
	});
	
	/* Navigation */
	
	$("#nav li#services-link").hover(function() { /* On hover */
        $("#nav li#services-link a").stop()
            .animate({
                bottom: '180px' /* Animate nav item */
            }, 300);
        } , function() {
        $("#nav li#services-link a").stop()
            .animate({
                bottom: '200px'
            }, 220);
    });
	
	$("#nav li#portfolio-link").hover(function() {
        $("#nav li#portfolio-link a").stop()
            .animate({
                bottom: '340px'
            }, 300);
        } , function() {
        $("#nav li#portfolio-link a").stop()
            .animate({
                bottom: '360px'
            }, 220);
    });


	$("#nav li#contact-link").hover(function() {
        $("#nav li#contact-link a").stop()
            .animate({
                bottom: '310px'
            }, 300);
        } , function() {
        $("#nav li#contact-link a").stop()
            .animate({
                bottom: '330px'
            }, 220);
    });
	
	/* Overlay */
	
	$('#overlay').hide();
	
	$("#nav li").click(function() {
		$("#overlay").fadeIn(300)
	});
	
	$("#close").click(function() {
		$("#overlay").fadeOut(300)
	});
	
	/* Links in new windows */
	
	$('a.blank').click(function(){
	this.target = "_blank";
	});
	
	/* Services */
	
	/* Hide all pages except for web */
	
	$("#services #web-block").show();
	$("#services #print-block").hide();
	$("#services #branding-block").hide();
	activeButton();
	
	/* When a button is clicked, show that page and hide others */
	
	$("#services #web-button").click(function() {
												 
		$("#services #web-block").show();
		$("#services #print-block").hide();
		$("#services #branding-block").hide();
		activeButton();
														 
	});
	
	$("#services #print-button").click(function() {
												 
		$("#services #print-block").show();
		$("#services #web-block").hide();
		$("#services #branding-block").hide();
		activeButton();
														 
	});
	
	$("#services #branding-button").click(function() {
												 
		$("#services #branding-block").show();
		$("#services #web-block").hide();
		$("#services #print-block").hide();
		activeButton();
														 
	});	
	
	/* If buttons are active, disable hovering */
	
	function activeButton() {
	
		if ($('#services #web-block').is(":visible")) { 
			
			$("#services #web-button").css({background: "#444444", color: "#999999"});
			$('#services #web-button').hover( function(){$(this).css({"background":"#444444", "color":"#999999"});},
   			function(){$(this).css({"background":"#444444", "color":"#999999"});});
			
		} else { 
			
			$("#services #web-button").css({background: "#000000", color: "#999999"});
			$('#services #web-button').hover( function(){$(this).css({"background":"#666666", "color":"#ffffff"});},
   			function(){$(this).css({"background":"#000000", "color":"#999999"});});
			
		}
		
		if ($('#services #print-block').is(":visible")) { 
			
			$("#services #print-button").css({background: "#444444", color: "#999999"});
			$('#services #print-button').hover( function(){$(this).css({"background":"#444444", "color":"#999999"});},
   			function(){$(this).css({"background":"#444444", "color":"#999999"});});
			
		} else { 
			
			$("#services #print-button").css({background: "#000000", color: "#999999"});
			$('#services #print-button').hover( function(){$(this).css({"background":"#666666", "color":"#ffffff"});},
   			function(){$(this).css({"background":"#000000", "color":"#999999"});});
			
		}
		
		if ($('#services #branding-block').is(":visible")) { 
			
			$("#services #branding-button").css({background: "#444444", color: "#999999"});
			$('#services #branding-button').hover( function(){$(this).css({"background":"#444444", "color":"#999999"});},
   			function(){$(this).css({"background":"#444444", "color":"#999999"});});
			
		} else { 
			
			$("#services #branding-button").css({background: "#000000", color: "#999999"});
			$('#services #branding-button').hover( function(){$(this).css({"background":"#666666", "color":"#ffffff"});},
   			function(){$(this).css({"background":"#000000", "color":"#999999"});});
			
		}
	
	}
	
	
	/* Portfolio */
						   
	$("#portfolio-main div.column").hover(function() { /* On hover of portfolio item */
								 
		$(this).find(".desc").animate({bottom: "0"}, {duration: 500}); /* Bring in description */
		
	}, function() {
		
		$(this).find(".desc").animate({bottom: "-190"}, {duration: 500});

		
	});
	
	/* Portfolio Item */
		
		$("#info-button").toggle(function() { /* On click of info button */
								 
		$("#portfolio-info").animate({bottom: "0"}, {duration: 750}); /* Bring in description */
		
	}, function() {
		
		$("#portfolio-info").animate({bottom: "-410px"}, {duration: 750});

	});
	
	/* Contact */
	
	$('form#contactForm').submit(function() {
		$('form#contactForm .error').remove();
		var hasError = false;
		
		$('.requiredField').each(function() {
			if(jQuery.trim($(this).val()) == '') {
				
				var labelText = $(this).prev('label').text();
				$append('<p class="error">Please fill out all the fields.</span>');
				hasError = true;
				
			} else if($(this).hasClass('email')) {
				
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				
				if(!emailReg.test(jQuery.trim($(this).val()))) {
					
					var labelText = $(this).prev('label').text();
					$(this).parent().append('<span class="error">You entered an invalid '+labelText+'.</span>');
					hasError = true;
					
				}
			}
		});
		
		
		if(!hasError) {
			$('form#contactForm li.buttons button').fadeOut('normal', function() {
				$(this).parent().append('<img src="/wp-content/themes/td-v3/images/template/loading.gif" alt="Loading…" height="31" width="31" />');
			});
			
			var formInput = $(this).serialize();
			$.post($(this).attr('action'),formInput, function(data){
				$('form#contactForm').slideUp("fast", function() {
					$(this).before('<p class="thanks">Thanks for contacting us! You can be sure we will reply within the next 48 hours.</p>');
				});
			});
		}

		return false;

	});

	
	/* Links In New Window */

	$('a.blank').click(function(){
		this.target = "_blank";
	});	

});

/* Portfolio Item Image Swap */

function swapBanner(image, elemId, index) {
	
	$(".banner-button.banner-button_active").removeClass("banner-button_active");
	$("#banner_button_" + index).addClass("banner-button_active");

	$("#" + elemId).css("background", "url( " + image + " ) center");
	
	return false;
	
}