window.ie6 = ($.browser.msie && $.browser.version <= 6) ? true : false;


/* ############### History Manager ############### */
historyManager = {
	hashVal : '',
	interval : 100,
	init : function(){
		// historyManager.checkHash();
		setInterval('historyManager.checkHash()', this.interval);
	},
	backFunc : function(hash){
		cl(hash);
	},
	registerBackFunc : function(func){
		this.backFunc = func;
	},
	checkHash : function(){
		hash = this.getHash();
		if(hash != this.hashVal){
			// cl('setting hash: ' + hash);
			this.backFunc(hash, 0);
			this.hashVal = hash;
		}
	},
	getHash : function(){
		return document.location.hash.replace('#', '');
	},
	setHash : function(hash){
		if(hash.replace(' ', '').length <= 0){
			cle('Trying to setHash with empty string. String: "' + hash + '"');
			clt('Trying to setHash with empty string. String: "' + hash + '"');
			return false;
		}
		document.location.hash = hash;
		this.hashVal = hash;
		// Delay the logging of Clicky data so that we don't have a lag
		setTimeout("clicky.log('" + document.location.pathname + '#' + hash + "')", 850);
	}
}

$(document).ready(function(){
	
	
	/* ############### Portfolio, Team Slider ############### */
	
	if($('.portfolio-slider .slider-slides, .team-slider .slider-slides').length > 0){
		$('.portfolio-slider .slider-slides, .team-slider .slider-slides').cycle({ 
		    fx: 'scrollHorz',
		 	speed: 600,
			next: '#slider_next',
			prev: '#slider_previous',
			pager: '#slider_pager',
			height: 551,
			timeout: 0,
			// easeIn: true,
			// easeOut: true,
			containerResize: 0,
			// easing:          'easeOutQuad',
			// manualTrump: false,
			before : function(currSlideElement, nextSlideElement, options, forwardFlag){
			
				cl('cycle().before ' + currSlideElement);
			
			
				// This prevents the slide from running if the page loads with a string in the hash
				if($('.slider-slides').data('first_slide_loaded')){
				
					// If hash is empty and slide is first, don't update hash
					if($(this).prevAll().length == 0 && historyManager.getHash() == '') 
						return $('.slider-slides').data('first_slide_loaded', true);
					
					historyManager.setHash($(this).attr('class'));
				}
				$('.slider-slides').data('first_slide_loaded', true)
			
			},
			after : function(){
			},
			pagerAnchorBuilder: function(idx, slide) { 
		        return '#slider_pager li:eq(' + idx + ') a'; 
		    }
		});
	}
	
	/* ############### Homepage Slider ############### */	
	if($('.slider-slides.home-slider').length > 0){
		$('.slider-slides.home-slider').cycle({ 
		    fx: 'scrollHorz',
		 	speed: 600,
			next: '#slider_next',
			prev: '#slider_previous',
			pager: '#slider_pager',
			height: 551,
			timeout: 5000,
			// easeIn: true,
			// easeOut: true,
			containerResize: 0,
			pagerAnchorBuilder: function(idx, slide) { 
		        return '#slider_pager li:eq(' + idx + ') a'; 
		    }
		});
	}

	
	$('#slider_pager li a').click(function(){
		// $('#slider_pager li a').removeClass('active');
		// $(this).addClass('active');
		return false;
	});
	
	historyManager.registerBackFunc(function(hash){
		slider = $('.slider-slides');
		
		// Make sure the hash is found in our slider:
		if($('.slider-slides').find('.' + hash).length <= 0)
			return false;
		
		if(hash == '')
			hash = slider.find('li:first').attr('class')
		
		cl('sliding in first slide');
		// 
		// if(slider.data('first_slide_loaded') !== true){
		// 	cl('sliding in first slide');
		// 	prevSpeed = slider.data('cycle.opts').speedOut;
		// 	slider.data('cycle.opts').speedIn = slider.data('cycle.opts').speedOut = 0;
		// }
		
		var currentSlide = $('.' + hash);
		var slideIndex = currentSlide.prevAll().length;
		slider.cycle(slideIndex);
		
		// if(typeof prevSpeed != "undefined")
		// 	slider.data('cycle.opts').speedIn = slider.data('cycle.opts').speedOut = prevSpeed;
			
		slider.data('first_slide_loaded', true);
	});
	historyManager.init();
	
	/* ############### External Links ############### */
	$("a[href^='http']:not([href*='classyllama'])").attr("target","_blank");
	// $("a:not([href*='wordpress']):not([href^='#'])").attr("target","_blank");

	
	
	/* ############### Tooltips ############### */
	
	$('.icon').qtip({
		style : {
			name : 'dark',
			tip: 'bottomRight',
			background: '#000',
			border: {
				width: 3,
				radius: 5,
				color: '#1dd0db'
			}
		},
		position: {
			corner: {
				target: 'topMiddle',
				tooltip: 'bottomRight'
			}
		}
	})
	$('#contact_link').qtip({
		style : {
			name : 'dark',
			tip: 'bottomLeft',
			background: '#000',
			border: {
				width: 3,
				radius: 5,
				color: '#1dd0db'
			}
		},
		position: {
			corner: {
				target: 'topLeft',
				tooltip: 'bottomLeft'
			}
		},
		show: {
			delay: 0
		},
		api: {
			beforeShow : function(){
				if($('#bottom_bar_content').css('display') != 'none')
					return false;
			}
		}
	})
	
	/* ############### Contact Form ############### */
	$('#contact_form ol li input, #contact_form ol li textarea').focus(function(){
		$(this).addClass('active');
	}).blur(function(){
		$(this).removeClass('active');
	});
	
	// $('li.page-item-41 a, .contact-link, a[href^="/contact"]').click(function(){
	// 	$('#bottom_bar_content').slideToggle();
	// 	$(this).toggleClass('active');
	// 	historyManager.setHash('contact_form');
	// 	return false;
	// });
	
	$('#contact_link, a.contact-btn, li.page-item-41 a, .contact-link, a[href^="/contact"], #close_form, #shade').click(function(){
		
		if(ie6) // Contact form takes too much work to make work for our 1.5% of site traffic
			return true;
		
		$(this).toggleClass('active');

		$('#close_form').toggle();
		
		// Hide tooltip so that it doesn't still show once contact box has popped open
		if($('#bottom_bar_content').css('display') == 'none'){
			
			$('.qtip').hide();
			setTimeout("clicky.log('#contact_form')",1400);
			setTimeout('pageTracker._trackPageview("/contact_form");',1400);
			// cl($(this).attr('rel'));
			opt_label = $(this).attr('rel') != null ? $(this).attr('rel') : '';
			// cl(opt_label);
			setTimeout("pageTracker._trackEvent('Contact Form', 'Open Form', '" + opt_label + "');",1400);
			
			$('#shade').fadeIn();
		} else {
			$('#shade').fadeOut();
		}
		$('#bottom_bar_content').slideToggle();
		return false;
	});
	
	/* ############### Lightbox ############### */
	
	$("a.lightbox,a[href*='uploads/']").fancybox({overlayOpacity: 0.5}); 
	
	/* ############### Tracking ############### */	
	
	window.contact_form_submitted = false;
	$('.sendbutton').click(function(){
		// Only log link to Google once
		if(!window.contact_form_submitted){
			clicky.goal( 436 );
			setTimeout("clicky.log('#contact_submitted')",1000);
			pageTracker._trackPageview("/contact_submitted");
			pageTracker._trackEvent('Contact Form', 'Submit Form');
			window.contact_form_submitted = true;
		}
		cl("Clicky - Logged goal");
	});
	
	// $('li.page-item-41 a, .contact-link').click(function(){
	// 	clicky.log('#contact_form');
	// });
	
	// Set the tracking value for this customer
	$('#cf_field_7, #cf2_field_4').val(
		'http://getclicky.com/stats/visitors?site_id=73209&date=' + current_date + '&ip_address=' + ip_address
	);
	
	// If a form gets submitted without AJAX, we'll want to pop open bottom section so the user can see the success message
	if(document.location.hash == '#usermessagea' && $('#bottom_bar_content').css('display') == 'none')
		$('#bottom_bar_content').slideToggle();

	setTimeout('bindOlarkEvents();',5000);
});

function bindOlarkEvents(){
	$('#habla_wcsend_input').keypress(function (e) {
	      if (e.which == 13 ) {
			pageTracker._trackEvent('Olark', 'Submit Chat', 'http://getclicky.com/stats/visitors?site_id=73209&date=' + current_date + '&ip_address=' + ip_address);
			cl('Submitted Olark Query');
	      }
	});
	cl('bound olarkEvents');
}