Elastic.configuration.refreshOnResize = false;
var first_time = false;
// jQuery DOM Ready fn
jQuery(document).bind('elastic:initialize', function (){
	$('div.jobs div.job').each(function(){
		var job = this;
		$('.details-wrapper', job).each(function(){
			var action = this;
			$(this).bind('click', function(){
				$('.job-desc', job).slideToggle(100, function(){
					if($('.show img', job).attr('src') == wp_js+'images/scroll-down-hover.png' || $('.show img', job).attr('src') == wp_js+'images/scroll-down.png'){

						$('.job-desc', job).addClass('active');
						$('.show img', job).attr('src', wp_js+'images/faq-hide.png');
						$('h2.inactive', job).css('height', '0');
						$('h2.active', job).css('height', '23px');
					}else{
						$('.job-desc', job).removeClass('active');
						$('.show img', job).attr('src', wp_js+'images/scroll-down.png');
					}
				});
				if($('h2.active', job).height() == '0'){
					$('h2.inactive', job).css('height', '0');
					$('h2.active', job).css('height', '23px');
				}else{
					$('h2.inactive', job).css('height', '23px');
					$('h2.active', job).css('height', '0');
				}
			});
			$(this).hover(function(){
				if(!$('.job-desc', job).hasClass('active')){
					$('.show img', job).attr('src', wp_js+'images/scroll-down-hover.png');
					$('h2.inactive', job).css('height', '0');
					$('h2.active', job).css('height', '23px');
				}
			});

			$(this).mouseleave(function(){
				if(!$('.job-desc', job).hasClass('active')){
					$('.show img', job).attr('src', wp_js+'images/scroll-down.png');
					$('h2.inactive', job).css('height', '23px');
					$('h2.active', job).css('height', '0');
				}
			});
		});
	});
	
	// Scrollable Index
	$(".scroller").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "div.items",
		prev       : "a#left",
		next       : "a#right",
		easing     : "linear",
		keyboard   : true
	});
	
	$('.nposts').css('height', $(".posts-wrapper").height());
	
	// Scrollable Posts
	$(".nposts").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "div.posts",
		prev       : "a#page-left",
		next       : "a#page-right",
		navi       : "div#values-nav",
		easing     : "linear",
		keyboard   : true
	});
		
	// Scrollable Favorite Links
	$(".colcontent").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "div.lists",
		prev       : "a#left",
		next       : "a#right",
		navi       : "div#links-nav",
		easing     : "linear",
		keyboard   : true
	});

	// Scrollable Tweets
	$(".twitter").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "div.tweets",
		prev       : "a#leftl",
		next       : "a#rightr",
		navi       : "div#tweets-nav",
		easing     : "linear",
		keyboard   : true
	});
		
	// Scrollable Approach
	$(".our-values").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "div.items",
		navi       : "div#values-nav",
		easing     : "linear",
		keyboard   : false
	});

	// Scrollable Service Description
	var tmp_pos = false;
	var scroll_services = $("#service-description").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "div.items",
		prev       : "a#desc-prev",
		next       : "a#desc-next",
		easing     : "linear",
		api: true,
		onSeek: function() { 
			$('.service a').removeClass('selected');
			$('.service a').parent('li').removeClass('selected');
			$("a[name='service"+this.getPageIndex()+"']").addClass('selected');
			$("a[name='service"+this.getPageIndex()+"']").parent('li').addClass('selected');
			// show the correct pointer:
			$('div.column .pointer').hide();
			$("a[name='service"+this.getPageIndex()+"']").parents('.column').find('.pointer').show();
			
			if (!first_time && tmp_pos == 0 && this.getIndex() == 0) {this.end(); };
			first_time = false;
			return false;  
		}, 
		onBeforeSeek: function() {
			tmp_pos = this.getIndex();
		},
		keyboard   : false
	});

	$(".approach-scroller").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "div.items",
		prev       : "a#approach-prev",
		next       : "a#approach-next",
		easing     : "linear",
		keyboard   : false
	});

	// Scrollable Work
	$(".work-scroller").scrollable({
		size       : 6,
		speed      : 300,
		loop       : true,
		items      : "div.items",
		prev       : "a#prev",
		next       : "a#next",
		easing     : "swing",
		keyboard   : true
	});

	// Scrollable Sketch
	$(".sketch-scroller").scrollable({
		size       : 6,
		speed      : 300,
		loop       : true,
		items      : "div.items",
		prev       : "a#prev",
		next       : "a#next",
		easing     : "swing",
		keyboard   : true
	});

	// Scrollable Team
	$("div.people div.team-scroller").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "div.items",
		prev       : "a#prev",
		next       : "a#next",
		easing     : "swing",
		keyboard   : true
	});

	// Sets the opacity at 70% for every element
	$('.70-opacity').each(function(){
		var scontainer = this;
		if($(this).not('.active')){
			$(this).fadeTo('fast', 0.70);
		}else{
			$(this).fadeTo('fast', 1);
		}

		$(this).bind('mouseenter', function(){
			$(this).fadeTo('fast', 1);
		});

		$(this).bind('mouseleave', function(){
			$(this).fadeTo('fast', 0.7);	
		});

	});

	$('.service').each(function(){
		var scontainer = this;
		var destination = $('div.services').offset().top;
		$('a', scontainer).each(function(){
			$(this).bind('click', function(){
				if ($(this).hasClass('selected')) return false;
				$('.service a, .service li').removeClass('selected');
				$(this).addClass('selected');
				$(this).parent('li').addClass('selected');
				$('.service').addClass('70-opacity');
				$(scontainer).removeClass('70-opacity');
				$(scontainer).addClass('active');
				$('.pointer').hide();
				$('.pointer', scontainer).show();
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 300 );
				scroll_services.setPage($(this).attr('name').split('service')[1]);
				//window.location.hash = $(this).attr('name');
				return false;
			});

		});
	});

	// Services URL parsing and selection
	var theUrl = document.location.toString();
	if (theUrl.match('/services/')) {
		// create a tmp id element on each link
		$('div.service-content li a').each(function() {
			$(this).attr('name', 'service'+$(this).attr('href').split('#')[1]);
			$(this).attr('href', '#'+makeSlug($(this).attr('title')));
		});
		var itmCnt = theUrl.split('#').length - 1;
		var slug;
		first_time = true;
		if (itmCnt > 0) {
			if (itmCnt > 1) {
				slug = theUrl.split('#')[2];
				$('a[href="#'+slug+'"]').click();
			} else {
				slug = theUrl.split('#')[1];
				$('a[href="#'+slug+'"]').click();
			}
		}
		
		// add events to dropdown menu
		$('.sf-menu li.services a').click(function() {
			var hash = $(this).attr('href').split('/services/')[1];
			$('a[href="'+hash+'"]').click();
			return false;
		});
		
	}
	
	if (theUrl.match('/pulse/') || theUrl.match('935')) {
		$('.control, .bullet').click(function() {
			$('html, body').animate({scrollTop:0}, 'slow');
		});
	}

	// shows and hide magnifier on projects page
	$('.project').each(function(){
		var container = this;
		$('div.container > div.unit > div.container', container).mouseenter(function(){
			$('.magnify', container).show();
		});
		$('div.container > div.unit > div.container', container).mouseleave(function(){
			$('.magnify', container).hide();
		});
	});

	// shows and hide magnifier on sketches page
	$('.sketch').each(function(){
		var container = this;
		$('div.container > div.unit > div.container', container).mouseenter(function(){
			$('.magnify', container).show();
		});
		$('div.container > div.unit > div.container', container).mouseleave(function(){
			$('.magnify', container).hide();
		});
	});

	// shows and hide magnifier on work details page
	$('.work-scroller .item').each(function(){
		$(this).mouseenter(function(){
			$('.lupa', this).show();
		});
		$(this).mouseleave(function(){
			$('.lupa', this).hide();
		});
	});

	// shows and hide magnifier on sketch details page
	$('.sketch-scroller .item').each(function(){
		$(this).mouseenter(function(){
			$('.lupa', this).show();
		});
		$(this).mouseleave(function(){
			$('.lupa', this).hide();
		});
	});

	// Hide and show faqs
	$('div.faqs div.faq').each(function(){
		var faq = this;
		$('.show', faq).each(function(){
			var action = this;
			$(this).bind('click', function(){
				$('.faq-desc', faq).slideToggle(100, function(){
					if($('.show img', faq).attr('src') == wp_js+'images/faq-show.png'){
						$('.show img', faq).attr('src', wp_js+'images/faq-hide.png');
						$('.show span', faq).html('Hide');
					}else{
						$('.show img', faq).attr('src', wp_js+'images/faq-show.png');
						$('.show span', faq).html('Show');
					}
				});
			});
		});
	});

	// Aligns the faq answer content to faq question content
	$('div.body div.faq').each(function(){
		$('.faq-desc > div.container', this).css('padding-left', $('.number', this).outerWidth());
	});

	// Shows and hides ratings for sketches
	$('.likes .fixed').each(function(){
		if( $.cookie('notebook_down_'+$('#vota_post_down').attr('rel')) || $.cookie('notebook_up_'+$('#vota_post_up').attr('rel')) == true){
			$('img', this).hide();
			$('div.unit', this).fadeIn('fast');
		}else{
			$(this).hover(function(){
				$('img', this).hide();
				$('div.unit', this).show();
			}, function(){
				$('img', this).show();
				$('div.unit', this).hide();
			});
		}
	});


	//rating sketches
	$('#vota_post_down').click(function(){
		var COOKIE_NAME = 'notebook_down_'+$(this).attr('rel');
		var options = { path: '/', expires: 1 };
		vote_user = $.cookie(COOKIE_NAME);
		if(vote_user != 'true') {
			$.ajax({
				type: "POST",
				url: wp_root+"vote.php",
				data: "vote=down_vote&post_id="+$(this).attr('rel')+"&count="+$('.n_down').attr('rel'),
				success: function(msg){
					$('.n_down').empty();
					$('.n_down').append(msg);
					$('.n_down').attr({rel:msg});

				}
			});
			$.cookie(COOKIE_NAME, 'true', options);
			$('.likes .fixed').each(function(){
				$(this).unbind('mouseenter').unbind('mouseleave');
				$('img', this).hide();
				$('div.unit', this).fadeIn('slow');
			});
		}
		return false;
	});
	$('#vota_post_up').click(function(){
		var COOKIE_NAME = 'notebook_down_'+$(this).attr('rel');
		var options = { path: '/', expires: 1 };
		vote_user = $.cookie(COOKIE_NAME);
		if(vote_user != 'true') {
			$.ajax({
				type: "POST",
				url: wp_root+"vote.php",
				data: "vote=up_vote&post_id="+$(this).attr('rel')+"&count="+$('.n_up').attr('rel'),
				success: function(msg){
					$('.n_up').empty();
					$('.n_up').append(msg);
					$('.n_up').attr({rel:msg});
				}
			});
			$.cookie(COOKIE_NAME, 'true', options);
			$('.likes .fixed').each(function(){
				$(this).unbind('mouseenter').unbind('mouseleave');
				$('img', this).hide();
				$('div.unit', this).fadeIn('slow');
			});
		}
		return false;
	});

	// show form
	$('#contact_all').click(function(){
		$('.all_thanks').hide();
		$("#all_freshout > .two :input").css( { "width": "267px"} );
		elementClick = $(this).attr("href");
		destination = $(elementClick).offset().top;

		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 200 );
		if($('.staff1 > .container > .tooltip-form1').css('opacity') == '0'){
		  $('.staff1 > .container > .tooltip-form1').animate({opacity: "1"}, 100).show();
		}else{
      $('.staff1 > .container > .tooltip-form1').fadeIn(300);
		}
		$('.staff1 > .container > .tooltip-form1 > div.container').css('margin-top', '80px');
		$('.staff1 > .container > .tooltip-form1 > div.container')
		.animate({
			marginTop : 0
		});
		return false;
	});
	
	
	// show form
	$('a.contact-b').click(function(){
		$('.all_thanks').hide();
		$("#all_freshout > .two :input").css( { "width": "267px"} );
		elementClick = $(this).attr("href");
		destination = $(elementClick).offset().top;

		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 200 );
		if($('.staff1 > .container > .tooltip-form1').css('opacity') == '0'){
		  $('.staff1 > .container > .tooltip-form1').animate({opacity: "1"}, 100).show();
		}else{
      $('.staff1 > .container > .tooltip-form1').fadeIn(300);
		}
		$('.staff1 > .container > .tooltip-form1 > div.container').css('margin-top', '80px');
		$('.staff1 > .container > .tooltip-form1 > div.container')
		.animate({
			marginTop : 0
		});
		return false;
	});
	
	// People
	$('a#leadership').bind('click', function(){
		$(this).addClass('active');
		$('a#team').removeClass('active');
		$('div.leaderships .person').css({'height': '302px', 'width': '350px'});
		$('div.leaderships').fadeIn('slow');
		$('div.team').fadeOut('fast');
		return false;
		Elastic.refresh();
	});

	$('a#team').bind('click', function(){
		$(this).addClass('active');
		$('a#leadership').removeClass('active');
		$('div.team').fadeIn('slow');
		$('div.leaderships').hide('fast');
		return false;
		Elastic.refresh();
	});

	$('a#general').bind('click', function(){
		$('.cities li a').removeClass('selected');
		$(this).addClass('selected');
		$('div.general').show();
		$('div.guadalajara').hide();
		$('div.philadelphia').hide();
		$('div.san-francisco').hide();
		if ($(document).height() > $('div.layout').height()){
			$('div.content-heading > div.container').css('min-height', $(document).height() - 382);
		}
		return false;
		Elastic.refresh();
	});
	
	$('a#guadalajara').bind('click', function(){
		$('.cities li a').removeClass('selected');
		$(this).addClass('selected');
		$('div.guadalajara').show();
		$('div.philadelphia').hide();
		$('div.san-francisco').hide();
		$('div.general').hide();
		if ($(document).height() > $('div.layout').height()){
			$('div.content-heading > div.container').css('min-height', $(document).height() - 382);
		}
		return false;
		Elastic.refresh();
	});
	
	$('a#philadelphia').bind('click', function(){
		$('.cities li a').removeClass('selected');
		$(this).addClass('selected');
		$('div.philadelphia').show();
		$('div.guadalajara').hide();
		$('div.san-francisco').hide();
		$('div.general').hide();
		if ($(document).height() > $('div.layout').height()){
			$('div.content-heading > div.container').css('min-height', $(document).height() - 382);
		}
		return false;
		Elastic.refresh();
	});
	
	$('a#san-francisco').bind('click', function(){
		$('.cities li a').removeClass('selected');
		$(this).addClass('selected');
		$('div.san-francisco').show();
		$('div.guadalajara').hide();
		$('div.philadelphia').hide();
		$('div.general').hide();
		if ($(document).height() > $('div.layout').height()){
			$('div.content-heading > div.container').css('min-height', $(document).height() - 382);
		}
		return false;
		Elastic.refresh();
	});

	$('div.guadalajara').hide('fast');
	$('div.philadelphia').hide('fast');
	$('div.san-francisco').hide('fast');

	// Recipe items
	$('div.recipe-item > div.container').each(function(){
		var item = this;

		$('a.arrow', item).bind('click', function(){
			var img = $('img', this);
			if($(img).attr('src') == wp_js+'images/people/arrow_down.png'){
				$(img).attr('src', wp_js+'images/people/arrow_up.png');
				$('p.large-content', item).slideDown('fast');
				$('p.small-content', item).hide();
			}else{
				$(img).attr('src', wp_js+'images/people/arrow_down.png');
				$('p.small-content', item).show();
				$('p.large-content', item).slideUp('fast');
			}
			Elastic.refresh($('div.layout').get(0));
			return false;
		});
		$('a.more', item).bind('click', function(){
			if($('a.arrow img', item).attr('src') == wp_js+'images/people/arrow_down.png'){
				$('a.arrow img', item).attr('src', wp_js+'images/people/arrow_up.png');
				$('p.small-content', item).hide();
				$('p.large-content', item).show();
			}else{
				$('a.arrow img', item).attr('src', wp_js+'images/people/arrow_down.png');
				$('p.small-content', item).show();
				$('p.large-content', item).hide();
			}
			return false;
		});
	});

	// People Team
	$('div.people div.grid').each(function(){
		var grid = this;
		$('div.column', this).each(function(){
			var container = this;
			$(container).hover(function(){
				$('img.thumb', container).show();
			}, function(){
				$('img.thumb', container).hide();
			});
			$('a', container).bind('click', function(){
				$(grid).css('background', 'url(' + $(this).attr('href') + ')');
				return false;
			})
		});
	});
	// Footer Fx
	var imgloader = [];
	var staffOpen = false;
	for (var i=1; i<5; i++) {
		imgloader[i] = new Image();
		imgloader[i].src = wp_js+'images/footer/pic' + i + '.png';
	}
	$('div.footer div.staff').each(function(){
		var container = this;
		var image = $('img.staff', container).attr('src').match(/\d+/g);
		$('img.staff', container).click(function() {
			if (staffOpen) {
				$('div.close-button').click();
				staffOpen = false;
				var i = 1;
				$('img.staff').each(function(){
					$(this).attr('src', wp_js+'images/footer/pic'+i+'_transparent.png');
					i++;
				});
			}
		});
		$(container).mouseenter(function(){
			if (!staffOpen) {
				$('img.staff', container).attr('src', imgloader[image].src);
				//$('div.contact-tooltip').fadeOut('fast')
				$('div.contact-tooltip').hide()
				$('div.tooltip-hi, div.tooltip-form, div.tooltip-thanks, div.all_thanks').hide();

				$('div.tooltip-hi > div.container', container).css('margin-top', '80px');
	//			if($('div.tooltip-hi', container).css('opacity') == '0'){
				  $('div.tooltip-hi', container).animate({opacity: "1"}, 100).show();
	//			}else{
	//				$('div.tooltip-hi', container).fadeIn(300);
	//			}
				$('div.tooltip-hi > div.container', container).animate({
					marginTop        : "0"
				}, 300);
			
				// Add click event to the Give a shout link
				$('a.show-contact', container).bind('click', function(){
					staffOpen = true;
					$('div.tooltip-hi', container).hide();
					$('div.tooltip-form', container).show();
					Elastic.refresh($('div.footer').get(0));
					$('div.close-button', container).click(function(){
						$('div.tooltip-hi', container).hide();
						$('div.tooltip-form', container).hide();
						staffOpen = false;
						return false;
					});
					return false;
				});
			}
		});

		$(container).mouseleave(function(){
			if (!staffOpen){
				$('img.staff', container).attr('src', wp_js+'images/footer/pic' + image + '_transparent.png');				
				//$('div.tooltip-hi, div.tooltip-form, div.tooltip-thanks, div.all_thanks').hide();
				//$('div.contact-tooltip').hide()
			}
		});

		$('a.show-contact', container).bind('click', function(){
			$('div.tooltip-hi', container).hide();
			$('div.tooltip-form', container).show();
			$('div.close-button').click(function(){
				$('div.tooltip-hi').hide();
				$('div.tooltip-form').hide();
				staffOpen = false;
				return false;
			});
			Elastic.refresh($('div.footer').get(0));
			return false;
		});
		

		// hides the tooltip
		$('div.layout').bind('click', function(ev){
			if (!staffOpen) {
				if (!$(ev.target).is('input, label, form, div.tooltip-form, textarea')) {
					$('.staff1 > .container> .tooltip-form1 > .container').animate({marginTop: "80px"})
					$('.staff1 > .container > .tooltip-form1').animate({opacity: "0"});
					$('div.tooltip-hi:visible, div.tooltip-form:visible, div.tooltip-thanks:visible').hide();
				}
			}
		});
		$(document).keyup(function(event){
			if(event.keyCode == 27){
				$('div.tooltip-hi').each(function(){
					$('div.container', this).animate({marginTop: "80px"})
  				$(this).animate({opacity: "0"});
				});
				$('div.tooltip-form').each(function(){
					$('div.container', this).animate({marginTop: "80px"})
  				$(this).animate({opacity: "0"});
				});
				$('div.tooltip-thanks').each(function(){
					$('div.container', this).animate({marginTop: "80px"})
  				$(this).animate({opacity: "0"});
				});
			};
		});
		
		// Sends and validates the contact form
		var loading_img = new Image(); loading_img.src = wp_js+'images/loading.gif';
		$('form', container).validate({
			errorLabelContainer: $("div.hidden-form-errors"),
			submitHandler: function(form) {
			  $(form).ajaxSubmit({
					beforeSend: function() {
						$('input.submit', container).hide().after('<img src="'+loading_img.src+'" width="24" height="24" border="0" class="loading-img" />');
					},
					success: function(){
						$('div.tooltip-form', container).hide();
						$('input.submit', container).show();
						$('img.loading-img', container).remove();
						staffOpen = false;
						$('div.tooltip-thanks', container).fadeIn('slow');	
					}
				});
			}
		});
	});
	
	$('#all_freshout').validate({
		errorLabelContainer: $("div.hidden-form-errors"),
		submitHandler: function(form) {
		  $(form).ajaxSubmit({
				success: function(){
					$('div.tooltip-form1').hide();
					$('div.all_thanks').fadeIn('slow');	
				}
		});
		}
	});
	
	var default_name = "Your name";
	var default_email = "Your email";
	var default_phone = "Your phone #";
	
	$('input[name=by]').change(function(){
		if ($(this).val() == 'phone') { 
			$('label[for=your_email]').text(default_phone);
			$('input[name=your_email]').removeClass('email');
		} else {
			$('label[for=your_email]').text(default_email);
			$('input[name=your_email]').addClass('email');
		}
		$('input[name=your_email]').focus();
	});
	
	$('textarea.required').focus(function(){
			$(this).attr('value', '');
	});
});

// Preload CSS Background Images
jQuery(window).bind('load', function(){
  //$.preloadCssImages();
	if ($(document).height() > $('div.layout').height()){
		$('div.content-heading > div.container').css('min-height', $(document).height() - 382);
	}
});

jQuery(window).bind('resize', function(){
	if ($(document).height() > $('div.layout').height()){
		$('div.content-heading > div.container').css('min-height', $(document).height() - 382);
	}
});


$('span.mc_error_msg').live('click', function(){
	$(this).remove();
});

$('strong.mc_success_msg').live('click', function(){
	$(this).remove();
});

makeSlug = function(str) {
	var slugcontent_hyphens = str.replace(/\s/g,'');
	var finishedslug = slugcontent_hyphens.replace(/[^a-zA-Z0-9\-]/g,'');
	return finishedslug.toLowerCase();
}