jQuery(document).ready(function($) {

	tagshover($);

	footerAnimations($);

	ajaxsearch($);

	commentInputs($);

	contactPop($);

});



//Display the tags for each post onhover

function tagshover($){

	var preld = new Image();

	preld.src = "/wp-content/themes/trr/images/post-tags-border.png";

	$('.post-tags-wrap').hover(function() {

		$(this).find('.post-tags').stop(true, true).fadeIn(300);

	}, function() {

		$(this).find('.post-tags').stop(true, true).fadeOut(300);

	});

}



//animate the footer area up and down

function footerAnimations($){

	var button = $('.footer-top a.footer-login-logo');

	var footer = $('.footer');

	

	button.click(function(){

		if (footer.hasClass('state-up')){

			footer.stop(true, false).animate({

				bottom: '-50px'

			}, 300, 'swing').removeClass('state-up');

		} else {

			footer.stop(true, false).animate({

				bottom: '0'

			}, 300, 'swing').addClass('state-up');

		}

		return false;

	});

	button.click();

}

//Live search

function ajaxsearch($){

	var _url = "http://www.trrdigital.com/wp-content/themes/trr/search_results.php";

	var _input = $('.ajax-search input#s');

	var _wrap = $('#ajax-dropdown').hide().css('visibility','visible');

	var _ajaxbody = $('.ajax-body', _wrap);

	var _ajaxbodyinner = $('.ajax-body-inner', _ajaxbody);

	var _ajax = $.ajax();

	var _loading = $("<div class='ajax-loading'><p>Loading...</p></div>");	

	var _allresholder = $('.ajax-more', _wrap);

	var _allres = $('a.allres', _allresholder).hide();

	var active = false;

	var _timeout = -1;

	

	_loading.css('display','none').appendTo(_allresholder);

	

	_input.keyup(function(){

		var _val = _input.val();

		if(_val.length > 3) {

			_allres.stop(true,true).fadeOut(250);

			if(_timeout>-1)

				clearTimeout(_timeout);

			_ajax.abort();			

			_timeout = setTimeout(function(){				

				_ajax = $.ajax({

					url: _url,

					data: "s="+_val,

					beforeSend: function(){

						if(!active) {

							active = true;		

							_wrap.fadeIn(250, function(){

								_allresholder.animate({

									height: '36px'

								},250,function(){

									_allres.stop(true,true).fadeOut(250,function(){

										_loading.fadeIn(250);

									});

								});

							});							

						}

						else {

							_allres.stop(true,true).fadeOut(250,function(){

								_loading.fadeIn(250);

							});							

						}

					},

					success: function(msg){

						_loading.fadeOut(250, function(){							

							_allresholder.animate({

								height: '0px'

							}, 250);

						});						

						_ajaxbodyinner.fadeOut(250, function(){

							_ajaxbodyinner.html(msg);

							var _height = _ajaxbodyinner.height() > 0 ? _ajaxbodyinner.height() + 'px' : '20px';

							_ajaxbody.animate({

								height: _height

							}, 250, function(){

								_ajaxbodyinner.fadeIn(250, function(){

									_allresholder.animate({

										height: '36px'

									}, 250, function(){

										_allres.attr('href', 'http://www.trrdigital.com?s='+_val).appendTo(_allresholder).fadeIn(250);

									});

								});

							});

						});

					}

				});

			}, 800);//setTimeout

		}//if

	});	

	_input.blur(function(){

		_ajax.abort();		

		_ajaxbodyinner.fadeOut(250, function(){

			_ajaxbodyinner.empty();

			_ajaxbody.animate({

				height: '0px'

			}, 250, function(){

				_wrap.fadeOut(250, function(){

					active=false;

				})

			});

		});

	});

}

function commentInputs($){

	_form = $('#commentform');

	if(_form.index()==0){

		$('.form-left input',_form).each(function(){

			$(this).focus(function () {

				$(this).prev("label").addClass("focus");

			});

				  

			$(this).keypress(function () {

				$(this).prev("label").addClass("has-text").removeClass("focus");

			});

				  

			$(this).blur(function () {

				if($(this).val() == "") {

					$(this).prev("label").removeClass("has-text").removeClass("focus");

				}

			});

		});

	}

}

function contactPop($){

	var pretnks = new Image();

	pretnks.src = 'http://www.trrdigital.com/wp-content/themes/trr/images/contact-thanks.png';

	$("a.contact-popup").fancybox({

		'centerOnScroll': false,

		'padding': 0, 

		'hideOnOverlayClick':false,

		'frameWidth' : 776,

		'frameHeight' : 469,

		'overlayColor' : '#000000',

		'overlayOpacity' : '0.6',

		'hideOnContentClick':false,

		'callbackOnShow': 

			function(){

				var _form = $('.contact-overlay form');				

				var _inputs = $('.form-item-inner input', _form);

				

				_inputs.focus(function(){

					$(this).parent().addClass('has-focus');

				});

				_inputs.blur(function(){

					$(this).parent().removeClass('has-focus');

				});

				

				_form.submit(function(){

					var _p = "<p class='thanks-portfolio'><strong>Don't forget you can check out a selection of our work on</strong><br/><a href='http://selection.trrdigital.com/'>selection.trrdigital.com</a></p>";

					var data = {

						name : $('#contact-name', _form).val(),

						email : $('#contact-email', _form).val(),

						inquiry : $('#contact-inquiry', _form).val(),

						ajax : '1',

						sub: 'true'

					};

					

					$.ajax({

						url: 'http://www.trrdigital.com/wp-content/themes/trr/contact.php',

						data: data,

						type: "POST",

						beforeSend: function(req){

							$('.contact-top p, .contact-top h3, .contact-overlay').fadeOut(300);

							return;

						},

						success: function(msg){

							$('.contact-top').append(msg);

							$('.contact-top img').fadeIn(300);

							$('.contact-bottom').append(_p);

							return;

						},

						error: function(){							

							return;

						}

					});						

					return false;

				});

			}

	});	

	  

}




















