$(document).ready(function(){

	//check for hash on portfolio page
	if(window.location.hash){
		if(window.location.hash != '#logo'){
			var hash = window.location.hash.replace('#','');
			filterOverlay(hash, window.location.hash+'filter');
		}else{
			$('#folionormal').hide();
			$('#foliologo').show();
			$('#logoswitch').addClass('active');
		}
	}
	
	// Accordion.
	$(function() {
		$( "#accordion" ).accordion({active: false, collapsible: true, autoHeight: false});
	});
	
	// Accordion.
	$(function() {
		$( "#accordion2" ).accordion({active: false, collapsible: true, autoHeight: false});
	});
	
	//portfolio link hover effects
	$('.foliolink').hover(function(){
		$(this).children().next().stop(true,true).fadeIn(500);	
	}, function(){
		$(this).children().next().stop(true,true).fadeOut(500);
	});
	
	//switch from portfolio to logos
	$('#logoswitch').live('click',function(){
		$('#foliofilter .active').removeClass('active');
	
		if(!$(this).hasClass('active')){
			$('#folionormal').fadeOut(500, function(){
				$('#foliologo').fadeIn(500);
				$('#logoswitch').addClass('active');
			});
		}
		
		return false;
	});
	
	// Package opacity hover.
	if($('.package-block').length > 0) {
		$('.package-block .package a img').hover(function() {
			$(this).stop(true, true).animate({'opacity':.5}, 200);
		}, function() {
			$(this).stop(true, true).animate({'opacity':1}, 200);
		});
	}
	
	$('.latest_tweets').tweet({
	    username: "armadillotweet",
	    join_text: "auto",
	    avatar_size: 0,
	    count: 2,
	    auto_join_text_default: "we said,",
	    auto_join_text_ed: "we",
	    auto_join_text_ing: "we were",
	    auto_join_text_reply: "we replied to",
	    auto_join_text_url: "we were checking out",
	    loading_text: "loading tweets...",
	    template: '{text}'
    });
    
    // Submenu slider
    $('#header ul li').hover(function() {
    	if($(this).children('ul').length > 0) {
    		$(this).children('ul').stop(true, true).slideDown();
    	}
    }, function() {
    	if($(this).children('ul').length > 0) {
    		$(this).children('ul').stop(true, true).slideUp();
    	}
    });
    
    // Social icons fadein/out
    $('#social_icons li a').animate({'opacity':0.5}, 1);
    
    $('#social_icons li a').hover(function() {
    	$(this).stop(true, true).animate({'opacity': 1.0}, 350);
    }, function() {
    	$(this).stop(true, true).animate({'opacity': 0.5}, 350);
    });
    
    //  signup action
    $('#newsletter_signup form').submit(function(e) {
    	e.preventDefault();
    	var Email = $('input#email').val();
      	var Name = $('input#name').val();
      	var Company = $('input#company').val();
      	
      	//newsletter form validation
      	if(Email == 'Email:'){
      		$('input#email').css('color','red');
      		var fail = 1;
      	}
      	if(Name == 'Name:'){
      		$('input#name').css('color','red');
      		var fail = 1;
      	}
      	if(Company == 'Company:'){
      		$('input#company').css('color','red');
      		var fail = 1;
      	}
      	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(!emailReg.test(Email)) {
		     $('input#email').css('color','red');
      		var fail = 1;
		} else {}
      	
      	if(fail != 1){
			$.get('/ajax.php?action=adduserdata', {name: Name, company: Company, email: Email}, function(data) {
				if(data.success) {
					$('#newsletter_signup form').fadeOut(500, function(){
						$('#signup_complete').fadeIn(500);
					});
				}
			}, 'json');
		}
    });
    
    //focus color return on enwsletter form from validation
    $('input#email').live('focus',function(){
    	$(this).css('color', '#999');
    });
    $('input#name').live('focus',function(){
    	$(this).css('color', '#999');
    });
    $('input#company').live('focus',function(){
    	$(this).css('color', '#999');
    });
	
});

//filter function on portfolio page
function filterOverlay(cat, obj){
	if($('#logoswitch').hasClass('active')){
		$('#foliologo').fadeOut(500, function(){
			$('#folionormal').fadeIn(500);
		});
	}

	$('#foliofilter .active').removeClass('active');
	$(obj).addClass('active');
	
	if(cat == 'all'){
		$('.hideoverlay').fadeOut(500);
	}else{
		$('#folionormal .foliolink').each(function(){
			if($(this).hasClass(cat)){
				$(this).next().next().next().fadeOut(500);
			}else{
				$(this).next().next().next().fadeIn(500);
			}
		});
	}
	
	return false;
}

// Home newsletter signup
function homeNewsletterSignup(obj) {
	$(obj).parent().children().not('#newsletter_signup').fadeOut(function() {
		$('#newsletter_signup').fadeIn();
	});
}
