// initialize all codes
$(document).ready(function(){
	$('#nav_list').nav();
	$('#contact-info').contact();
	$('#portfolio_items').portfolio();
	$('#team_list, #team-char').ajaxedSection('#team-char');
	$('#services_list').ajaxedSection('#services-item');
	$('#contact').contactForm();
});


// nav animation effects
$.fn.nav = function(){
	// get nav links except .active
	this.find('a:not(.active)').each(function(){
		
		var $this = $(this);
		$this.css('opacity','0.4'); // add opacity
		
		$this.hover(
			function(){ // on mouse over
				var $this = $(this);
				if(isIE6()){ $this.css({ opacity: 1 }); } // w/o animation
				else { $this.stop().animate({ opacity: 1 }, 250); } // w/ animation
			},
			
			function(){ // on mouse out
				var $this = $(this);
				if(isIE6()){ $this.css({ opacity: 0.4 }); } // w/o animation
				else { $this.stop().animate({ opacity: 0.4 }, 250); } // w/ animation
			}
		);
		
	});
	
	// remove :active outline dots
	$('a').click(function(){ $(this).blur(); });



// contact info effects
$.fn.contact = function(){
	var self = this;
	
	// get contact dd
	self.find('dd').each(function(){
		// prepend dt text
		$(this).prepend('<strong>' + $(this).prev().text() + '</strong> <br />');
	});
	
	// get contact dt
	self.find('dt').each(function(){
		var $this = $(this);
		
		// wrap a contact link
		$this.find('span').wrap('<a href="'+url()+'/contact/">','</a>');
		
		$this.hover(
			function(){ // on mouse over
				var $dd = $this.next();
				if(isIE6()){ $dd.show(); }  // w/o animation
				else { $dd.stop().animate({ opacity: 1 }, 250); } // w/ animation
			},
			
			function(){ // on mouse out
				var $dd = $this.next();
				if(isIE6()){ $dd.hide(); } // w/o animation
				else { $dd.stop().animate({ opacity: 0 }, 250); } // w/ animation
			}
		);
		
	});
};


// validate all forms
$.fn.contactForm = function(){
	$('form', this).each(function(){
		//$('<input type="hidden" name="aqt_val" value="contact" />').appendTo(this);
		var theform = this;
		
		$.get( url()+'/contact/token', function(txt){
			$('<input type="hidden" name="aqt_val" value="'+txt+'" />').appendTo(theform);
		});
		
		$(this).validate({
			submitHandler: function(form) {
				$(form).ajaxSubmit({ target: '#contact-response' });
			}
		});
	});
};


// add a focus class to all form fields
$(':input')
		.focus(function(){ $(this).addClass('focus'); })
		.blur(function(){ $(this).removeClass('focus'); });
};


// portfolio effects
$.fn.portfolio = function(){
	var self = this;
	
	// get all .item
	self.find('.item').each(function(){
		var $this = $(this);
		
		$this.hover(
			function(){ $this.addClass('hover'); }, // on mouse over
			function(){ $this.removeClass('hover'); } // on mouse out
		);
		
		var $href = $this.find('a').attr('href');
		$this.wrap('<a href="'+ $href +'" style="cursor:pointer;">','</a>');
	});
};


// ajaxed section function
$.fn.ajaxedSection = function(x){
	this.find('a').each(function(){

		// vars
		var $x = $(x);
		var $this = $(this);
		var $href = $this.attr('href');
		var fullhref = this.href;
		
		$this.click(function(){
			$this.ajaxStop(function(){ });
			
			// Loading effect
			$x.html('<p id="loading" style="color:#888; line-height:16px;"> <img src="'+ url() +'/themes/aqt_green/img/loading.gif" style="vertical-align:middle; margin-right:5px;" alt="" /> Loading.. </p>');
			
			// ajax
			$.ajax({
				type: 'GET',
				dataType: 'html',
				url: $href,
				error: error,
				success: success
			});
			
			// error message
			function error(error){
				var msg;
				msg  = '<p class="error"><strong>Error:</strong> There was an error loading this page.<br />';
				msg += 'Please <a href="'+url()+'/contact/">contact us</a> and let us know about this bug.<br />';
				msg += '<em>'+fullhref+'</em></p>';
				$x.html(msg);
			}
			
			// success process
			function success(data){
				if (isIE6()) { $x.html(data); } // w/o animation
				else { // w/ animation
					$x.stop().fadeOut(550, function(){
						$x.hide().html(data).animate({ opacity: 1 }, 750);
					});
				}
			}
			
			return false;
		});
	});
};


// if IE6 returns a boolean true
function isIE6(){
	var ie6 = !(typeof document.body.style.maxHeight != "undefined");
	if(ie6){ return true; }
};