$(document).ready(function(){


/* ------------------------------------------ Home Grid Isotope ------------------------------------------ */

	$('#main_search').val("Search...");

	var $mainGrid = $('#main_grid');

	$mainGrid.isotope({
	  itemSelector : '.isotope_wrapper',
	  layoutMode : 'fitRows'
	});
	
	$('#main_search').focus(function() {
		if($(this).val() == this.defaultValue)
		$(this).val("");
		$(this).removeClass("inactive_search");
	});

	$('#main_search').blur(function() {
		if($(this).val() == "")
		$(this).val(this.defaultValue);
		$(this).addClass("inactive_search");
	});

	var $search_val = '';
	var $filter_val = [];
	var $filter_val_nice = [];
	var final_filter = '';
	var search_filter = '';
	var $filter_valTemp = '';
	var $filter_valTemp_nice = '';
	var $filterActive = false;
	var $filterScore = 0;
	var filter_string = '';
	var val = '';
	var origVal = '';

	$("#main_search").bind("keyup change", function(){

		origVal = $(this).val()
		val = origVal.toLowerCase();
		$(".omit-me").removeClass("omit-me");
		
		if (val) {
			$mainGrid.children(":not(.blank)").filter(function(){
			return $(this).find('.search_title').text().toLowerCase().indexOf(val) === -1;
			}).addClass("omit-me");
			$search_val = 'full';
			filter_now();
			$('#main_search').addClass('searchActive');
			//$mainGrid.isotope({ filter: ':not(.omit-me)' });
			//$('#no_search_results').show();
		} else {
			$search_val = 'empty';
			filter_now();
			$('#main_search').removeClass('searchActive');
			//$mainGrid.isotope({ filter: '*' });
			//$('#no_search_results').hide();
		}
		
		//$('#no_artists .search_artist').text(origVal);
	
	});
	
	
	$('.filter_cats').click(function() {
		
		if ( $(this).hasClass('activeFilter') ) {
			$filterActive = true;
		} else {
			$filterActive = false;
		}
		
		$filter_valTemp = $(this).attr('data-filter');
		$filter_valTemp_nice = $(this).attr('data-nicename');
		
		if ( $filterActive == false ) {
			$(this).addClass('activeFilter');
			$filterScore = $filterScore + 1;
			$filter_val.push($filter_valTemp);
			$filter_val_nice.push($filter_valTemp_nice);
			
		} else {
			$(this).removeClass('activeFilter');
			var filter_index = $filter_val.indexOf($filter_valTemp);
			$filter_val.splice(filter_index,1);
			
			var filter_index_nice = $filter_val_nice.indexOf($filter_valTemp_nice);
			$filter_val_nice.splice(filter_index_nice,1);
			
			$filterScore = $filterScore - 1;
		}
		
		if ($filterScore > 0) {
			$('#nav_filter').addClass('filtersActive');
						
		} else {
			$('#nav_filter').removeClass('filtersActive');
		}
		
		filter_now();
		
		return false;
	});
	
	
	function filter_now() {
		if ( $search_val == 'full' ) {
			search_filter = ':not(.omit-me)';
		} else if ( $search_val == 'empty' ) {
			search_filter = '*';
		}

		if ($filter_val) {
			var $filter_string = $filter_val.join('');
			final_filter = search_filter + $filter_string;
		} else {
			final_filter = search_filter;
		}
		
		$mainGrid.isotope({ filter: final_filter });
		
		if (val) {
			$('#no_results_search_term').html('for <em>'+origVal+'</em>');
		} else {
			$('#no_results_search_term').html('');
		}
		
		if ($filterScore > 0) {
			if (val) {
			filter_string = ' within the categories of <span class="green">';
			} else {
			filter_string = ' the categories of <span class="green">';	
			}
			
			filter_string = filter_string + $filter_val_nice.join('<em>,</em> ');
								
			filter_string = filter_string + '</span>';
			
			$('#no_results_filter_terms').html(filter_string);
			
		} else {
			$('#no_results_filter_terms').html('.');
		}
		
				// display message box if no filtered items
		if ( !$mainGrid.data('isotope').$filteredAtoms.length ) {
		  $('#no_search_results').fadeIn();
		} else {
		  $('#no_search_results').hide();
		}
		
	}

	$('#nosearch_name,#nosearch_email').focus(function() {
		if($(this).val() == this.defaultValue)
		$(this).val("");
		$(this).removeClass("inactive_search");
	});

	$('#nosearch_name,#nosearch_email').blur(function() {
		if($(this).val() == "")
		$(this).val(this.defaultValue);
		$(this).addClass("inactive_search");
	});


	$('#nosearch_submit').click(function() {
		var nosearch_string = $('#no_results_full_term').text();
		$('#nosearch_string').val(nosearch_string);
		
		if ( $('#nosearch_name').val() == "Your Name" ) {
			$('#nosearch_name').val('');
		}
	});

	$("#no-search_form").validate({
		rules: {
			nosearch_email: {
				required:true,
				email:true
			},
			nosearch_name: "required"
		},
		messages: {
			nosearch_email: {
				required:"&uarr; Please enter your email address",
				email:"&uarr; Please enter a valid email address"
			},
			nosearch_name: {
				required:"&uarr; Please enter your name"
			}
		}
	});

	$('#no-search_form').ajaxForm(function() {
		$('#no_search_success').fadeIn().delay(5000).fadeOut();
	});



/* ------------------------------------------ Home Block ------------------------------------------ */

	$('#home_block_close, .clear_home_block').click(function(){
		
		$items = $('#home_block_isotope');
		
		$('#main_grid').isotope('remove',$items).isotope('reLayout');
		
		_gaq.push(['_trackEvent','Clicks','Close Home block']);
		
		return false;	
	});

/* ------------------------------------------ Top Bar signup ------------------------------------------ */


	$('#top_signup_email').focus(function() {
		if($(this).val() == this.defaultValue)
		$(this).val("");
	});

	$('#top_signup_email').blur(function() {
		if($(this).val() == "")
		$(this).val(this.defaultValue);
	});
	
	$("#top_bar_signup").validate({
		rules: {
			top_signup_email: {
				required:true,
				email:true
			}
		},
		messages: {
			top_signup_email: {
				required:"",
				email:"&uarr; Please enter a valid email address"
			}
		}
	});


	$('#top_bar_signup').ajaxForm({
		beforeSubmit: top_bar_before,
		success: top_bar_success
	});
	
	
	function top_bar_before() {
		$('#top_bar_loading').fadeIn();
	}
	
	function top_bar_success() { 
    	$('#signup_success_excess').animate({'left':'-400px'});
		$('#top_bar_success').animate({'left':'0px'});
		_gaq.push(['_trackPageview','/newsletter_success']);
	}



/* ------------------------------------------ AJAX Post Load ------------------------------------------ */


	$.ajaxSetup({cache:false});
	
	$(".ajax_post_link").click(function(){
		
		if (lightboxActive == false) { 

			var post_id = $(this).attr("rel");
			$(this).parent('.grid_item_bg').children('.ajax_loading_icon').addClass('activeLoadingIcon');
			$(this).parent('.grid_item_bg').children('.ajax_loading_icon').fadeIn();
			$("#ajax_post_wrapper").load("http://www.shotgunfront.com/post-ajax/",{id:post_id}, function() {
				sf_lightbox();
				$('.hentry .activeLoadingIcon').hide();
				$('.hentry .activeLoadingIcon').removeClass('activeLoadingIcon');
			});
			return false;
		
		}
		
	});


	$(".ajax_blog_link").click(function(){
		
		if (lightboxActive == false) { 

			var post_id = $(this).attr("rel");
			$(this).parent('.grid_item_bg').children('.ajax_loading_icon').addClass('activeLoadingIcon');
			$(this).parent('.grid_item_bg').children('.ajax_loading_icon').fadeIn();
			$("#ajax_blog_post_wrapper").load("http://www.shotgunfront.com/post-ajax-blog/",{id:post_id}, function() {
				sf_lightbox_blog();
				$('.hentry .activeLoadingIcon').hide();
				$('.hentry .activeLoadingIcon').removeClass('activeLoadingIcon');
			});
			return false;
		
		}
		
	});



/* ------------------------------------------ Blog Lightbox ------------------------------------------ */

	var lightboxActive = false;


	function sf_lightbox_blog() {
	
		if (lightboxActive == false) { 
			lightboxActive = true;
			$('.backdrop').animate({'opacity':'.90'}, 300, 'linear');
			$('#ajax_blog_post_wrapper').animate({'opacity':'1'}, 300, 'linear');
			$('.backdrop, #ajax_blog_post_wrapper').css('display', 'block');
			$('html, body').animate({ scrollTop: 0 }, 'fast');
			$('#header_shield').show();
		}
	
	};
 
/* ------------------------------------------ Case Study Lightbox ------------------------------------------ */

	var lightboxActive = false;


	function sf_lightbox() {
	
		if (lightboxActive == false) { 
			lightboxActive = true;
			$('.backdrop').animate({'opacity':'.90'}, 300, 'linear');
			$('#ajax_post_wrapper').animate({'opacity':'1'}, 300, 'linear');
			$('.backdrop, #ajax_post_wrapper').css('display', 'block');
			$('html, body').animate({ scrollTop: 0 }, 'fast');
			$('#header_shield').show();
		}
	
	};
 
	$('#post_popup_close').live('click', function(){
		close_box();
	});

	$('.backdrop,#header_shield').click(function(){
		close_box();
	});
 
	function close_box() {
		$('.backdrop, #ajax_post_wrapper, #ajax_blog_post_wrapper').fadeOut(300, function(){
			$('.backdrop, #ajax_post_wrapper, #ajax_blog_post_wrapper').css('display', 'none');
			$('#header_shield').hide();
			lightboxActive = false;
		});
		
	};



/* ------------------------------------------ About Us Lightbox ------------------------------------------ */


	function sf_about() {
		
		if (lightboxActive == false) { 
			lightboxActive = true;
			$('.backdrop').animate({'opacity':'.90'}, 300, 'linear');
			$('#about_us_wrapper').animate({'opacity':'1'}, 300, 'linear', function() {
				$('#about_us_shine').addClass('activateShine');
			});
			$('.backdrop, #about_us_wrapper').css('display', 'block');
			$('html, body').animate({ scrollTop: 0 }, 'fast');
			$('#header_shield').show();
			
			_gaq.push(['_trackPageview','/services/']);	
		}
		
	};
 
	$('#about_popup_close').live('click', function(){
		about_close_box();
	});

	$('.backdrop,#header_shield').click(function(){
		about_close_box();
	});
 
	function about_close_box() {
		$('.backdrop, #about_us_wrapper').animate({'opacity':'0'}, 300, 'linear', function(){
			$('.backdrop, #about_us_wrapper').css('display', 'none');
			$('#about_us_shine').removeClass('activateShine');
			$('#header_shield').hide();
			lightboxActive = false;
		});
	};


/* ------------------------------------------ Contact Us Lightbox ------------------------------------------ */

	var contactSent = false;

	function sf_contact() {
		if (lightboxActive == false) { 
			lightboxActive = true;
			$('.backdrop').animate({'opacity':'.90'}, 300, 'linear');
			$('#contact_us_wrapper').animate({'opacity':'1'}, 300, 'linear');
			$('.backdrop, #contact_us_wrapper').css('display', 'block');
			$('html, body').animate({ scrollTop: 0 }, 'fast');
			$('#header_shield').show();
			_gaq.push(['_trackPageview','/contact/']);	
		}
	};
 
	$('#contact_popup_close').live('click', function(){
		contact_close_box();
	});

	$('.backdrop,#header_shield').click(function(){
		contact_close_box();
	});
 
	function contact_close_box() {
		$('.backdrop, #contact_us_wrapper').animate({'opacity':'0'}, 300, 'linear', function(){
			$('.backdrop, #contact_us_wrapper').css('display', 'none');
			$('#header_shield').hide();
			lightboxActive = false;
			
			if (contactSent == true) {
				$('#contact_success').css('left','-700px');
				$('#contact_form').css('left','16px');
				$('#contact_loading').hide();
				contactSent = false
			}
		});
	};



/* ------------------------------------------ Main Nav Controls ------------------------------------------ */

	$('#main_nav_about').click(function(){
		sf_about();
		return false;
	});

	$('#main_nav_contact, #topnav_enquire').click(function(){
		sf_contact();
		return false;
	});
	
	

/* ------------------------------------------ Contact Form ------------------------------------------ */


	$("#contact_form").validate({
		rules: {
			cform_name: "required",
			cform_email: {
				required:true,
				email:true
			},
			cform_msg: {
				required: true,
				minlength: 5
			}
		}
	});


	$('#contact_form').ajaxForm({
		beforeSubmit: contact_form_before,
		success: contact_form_success,
		clearForm: true
	});
	
	
	function contact_form_before() {
		$('#contact_loading').fadeIn();
	}
	
	function contact_form_success() { 

		$('#contact_form').animate({'left':'700px'});
		$('#contact_success').animate({'left':'16px'});
		contactSent = true
	
		_gaq.push(['_trackPageview','/contact_success']);
		
    }
	
	var mapBuilt = false;
	
	$('#contact_map_launch').click(function(){
		gmapLaunch();
		$('#contact_gmap_wrapper').fadeIn(300);
		
		_gaq.push(['_trackEvent','Clicks','View google map']);
		
	});


	$('#contact_gmap_close').click(function(){
		$('#contact_gmap_wrapper').fadeOut(300);
	});


	  function gmapLaunch() {
			if (mapBuilt != true) {
				var latlng = new google.maps.LatLng(50.740099,-1.586311);
				var myOptions = {
				  zoom: 9,
				  center: latlng,
				  mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				var map = new google.maps.Map(document.getElementById("gmap_wrapper"),
					myOptions);
				var image = 'http://www.shotgunfront.com/wp/wp-content/themes/sf/images/shared/google-pointer.png';
				var marker = new google.maps.Marker({
				position: latlng, 
				map: map, 
				title:"Shotgun Front",
				icon: image
				})
				mapBuilt = true
			}
	  }



/* ------------------------------------------ FB Portfolio AJAX Post Load ------------------------------------------ */


	$(".fb_ajax_post_link").click(function(){
		
			var post_id = $(this).attr("rel");
			$(this).parent('.hentry').children('.fb_ajax_loading_icon').addClass('activeLoadingIcon');
			$(this).parent('.hentry').children('.fb_ajax_loading_icon').fadeIn();
			$("#fb_ajax_post_wrapper").load("http://www.shotgunfront.com/fb-post-ajax/",{id:post_id}, function() {
				fb_lightbox();
				$('.hentry .activeLoadingIcon').hide();
				$('.hentry .activeLoadingIcon').removeClass('activeLoadingIcon');
			});
			return false;
		
	});


/* ------------------------------------------ FB Portfolio Case Study Lightbox ------------------------------------------ */

	var fb_lightboxActive = false;


	function fb_lightbox() {
	
		if (fb_lightboxActive == false) { 
			fb_lightboxActive = true;
			$('.backdrop').animate({'opacity':'.90'}, 300, 'linear');
			$('#fb_ajax_post_wrapper').animate({'opacity':'1'}, 300, 'linear');
			$('.backdrop, #fb_ajax_post_wrapper').css('display', 'block');
			$('html, body').animate({ scrollTop: 0 }, 'fast');
		}
	
	};
 
	$('#post_popup_close').live('click', function(){
		fb_close_box();
	});

	$('.backdrop').click(function(){
		fb_close_box();
	});
 
	function fb_close_box() {
		$('.backdrop, #fb_ajax_post_wrapper').fadeOut(300, function(){
			$('.backdrop, #fb_ajax_post_wrapper').css('display', 'none');
			fb_lightboxActive = false;
		});
		
	};






}); //   /doc_ready

