
/* Click Tracking - Google Analytics*/
$(function () {
    var click = function (action, label) {
        return function () {
            _gaq.push(['_trackEvent', 'Links', action, label]);
        };
    }, files = /\.(?:do(c|t)(x)?|pdf|ppt(x)?|xls(x)?|zip?)$/;
    $('a').each(function () {
        a = $(this);
        var href = a.attr('href'),
            host = a.attr('hostname');
        if (/^mailto:/.test(href)) {
            a.bind('click', click('MailTo', href.split(':')[1]));
        } else if (a.attr('title') === '' && host.length > 0 && host.search(location.hostname) === -1 && /^http(s)?:/.test(href)) {
            a.bind('click', click('External', href.split('//')[1]));
            a.attr('target', '_blank').attr('title', 'Link opens in new window');
        } else if (files.test(href)) {
            a.bind('click', click('Files', href.split(location.host)[1]));
            if (a.attr('rel') === 'external') {
                a.attr('target', '_blank').attr('title', 'Link opens in new window');
            }
        }
    });
});

/* Blockquote Links*/
$(function () {
    if ($('#about').length > 0) {
		$('#content-sidebar blockquote:first').click(function () {
		    window.location = '/contact/';
		}).css('cursor', 'pointer');
	}
    if ($('#service').length > 0 && $('blockquote.showroom').length !== 0 ) {
		$('blockquote.showroom:first').click(function () {
		    window.location = '/showroom/';
		}).css('cursor', 'pointer');
	}
});

/* Tabbed Frames*/
$(function () {
	if ($('#tab-frame').length > 0) {
		var active = $('#overview'),
			activeLink = $('#tab-menu li a.active'),
			onclick = function (event) {
				var a = $(this),
					href = a.attr('href');
				active.removeClass('active');
				activeLink.removeClass('active');
				active = $(href);
				active.addClass('active');
				if (href === '#replacement') {
					activeLink = $('#replacement-link');
					activeLink.addClass('active');
				} else {
					a.addClass('active');
					activeLink = a;
				}
				event.preventDefault();
			};
		$('#tab-menu li a').click(onclick);
		if ($('#animation').length > 0) {
		    $('animation-open').click(onclick);
		    $('animation-close').click(onclick);
		}
	}
});

/* Contact Page Functionality*/
$(function () {
    if ($('#contact').length > 0) {
		var button = $('<button>Print Page</button>'),
			form = $('#inquiry'),
			map = new GMap2($('#gmap')[0]),
			marker,
			submitted = false,
			text = '<span class="gmap-title">Best Plumbing</span><br /><span class="gmap-address">4129 Stone Way N<br />Seattle, WA 98103</span><br /><span class="gmap-directions">Get driving directions</span><form action="http://maps.google.com/maps" target="_blank"><label for="saddr">Enter your zip code:</label><br /><input id="saddr" name="saddr" size="20" type="text" /><input name="daddr" type="hidden" value="4129 Stone Way North Seattle, WA 98103-8013 @47.657468,-122.342741" /><input type="submit" value="GO" class="gmap-submit" /></form>';
	   if (GBrowserIsCompatible()) {
            map.setCenter(new GLatLng(47.657468, -122.342741), 14);
            map.setUIToDefault();
			map.addControl(new GOverviewMapControl());
            marker = new GMarker(new GLatLng(47.657468, -122.342741));
            map.addOverlay(marker);
            marker.openInfoWindowHtml(text);
            GEvent.addListener(marker, 'click', function () {
                marker.openInfoWindowHtml(text);
            });
            $(window).unload(GUnload);
        }
		button.bind('click', function (event) {
		    window.print();
			event.preventDefault();
	    }).addClass('print').insertBefore($('p.script'));
		$('a[rel="form"]').click(function (event) {
		    form.removeClass('hide');
		    if ($('#name').length > 0) {
			    $('#name').focus();
			}
			if (window._gaq) {
				window._gaq.push(['_trackEvent', 'Contact Form', 'Launched', document.location.pathname]);
			}
			event.preventDefault();
		});
		form.submit(function (event) {
			var department = $('.radios input:checked'),
			    errorField = null,
				errorList = [],
				errorMessage = '',
				email = $('#email'),
				i,
				ii,
				isEmpty = function (value) {
					return value === null || value.length === 0 || /^\s+$/.test(value);
				},
			 	message = $('#message'),
				name = $('#name'),
			    reEmail = /^([\w\-]+(?:\.[\w\-]+)*)@((?:[\w\-]+\.)*\w[\w\-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
			if (isEmpty(name.val())) {
				errorList.push('The Name field is requried.');
				errorField = name;
			}
			if (isEmpty(email.val())) {
				errorList.push('The Email field is requried.');
				errorField = errorField === null ? email : errorField;
			} else if (!reEmail.test(email.val())) {
				errorList.push(email.val() + ' does not appear to be a valid email address.');
				errorField = errorField === null ? email : errorField;
			}
			if (department.length === 0) {
				errorList.push('Please select an option in the Department group.');			 
			}
			if (isEmpty(message.val())) {
				errorList.push('The Message field is requried.');
				errorField = errorField === null ? message : errorField;
			}
			if (errorList.length > 0) {
				errorMessage += 'The following form ' + (errorList.length > 1 ? 'fields were' : 'field is') + ' incomplete or incorrect:\n\n';
				jQuery.each(errorList, function(i, val) {
				    errorMessage += (i + 1) + '. ' + val + '\n';
				});
				errorField.focus();
				window.alert(errorMessage + '\nYour form has not been submitted.\nPlease check the information that you provided and submit the form again.');
				event.preventDefault();
				return;
			}
			$.ajax({
			    data: form.serialize(),
				type: form.attr('method'),
				url: form.attr('action') + '?ajax=true',
				success: function (data) {
					$('#inquiry fieldset:first').html('<fieldset class="submit success">' + data + '</fieldset> <a href="#" rel="close">[X] Close</a>');
				}
			});
			submitted = true;
			if (window._gaq) {
				window._gaq.push(['_trackEvent', 'Contact Form', 'Submitted', document.location.pathname]);
			}
			event.preventDefault();
		}).delegate('a[rel="close"]', 'click' , function (event) {
			form.addClass('hide');
			if (!submitted && window._gaq) {
				window._gaq.push(['_trackEvent', 'Contact Form', 'Canceled', document.location.pathname]);
			}
			event.preventDefault();
		});
    }
});

/*
* vertical news ticker
* Tadas Juozapaitis ( kasp3rito@gmail.com )
* http://www.jugbit.com/jquery-vticker-vertical-news-ticker/
*/
(function($){
	$.fn.vTicker = function(options) {
		var defaults = {
				speed: 700,
				pause: 4000,
				showItems: 3,
				animation: '',
				mousePause: true,
				isPaused: false,
				direction: 'up',
				height: 0
			},
			options = $.extend(defaults, options),
			moveUp = function(obj2, height, options){
				if (options.isPaused) {
					return;
				}
				var obj = obj2.children('ul');
				var clone = obj.children('li:first').clone(true);
				if(options.height > 0)
				{
					height = obj.children('li:first').height();
				}		
				obj.animate({top: '-=' + height + 'px'}, options.speed, function() {
					$(this).children('li:first').remove();
					$(this).css('top', '0px');
				});
				if(options.animation == 'fade')
				{
					obj.children('li:first').fadeOut(options.speed);
					if(options.height == 0)
					{
					obj.children('li:eq(' + options.showItems + ')').hide().fadeIn(options.speed).show();
					}
				}
				clone.appendTo(obj);
			},
			moveDown = function(obj2, height, options){
				if(options.isPaused) {
					return;
				}
				var obj = obj2.children('ul');
				var clone = obj.children('li:last').clone(true);
				if(options.height > 0)
				{
					height = obj.children('li:first').height();
				}
				obj.css('top', '-' + height + 'px').prepend(clone);
				obj.animate({top: 0}, options.speed, function() {
					$(this).children('li:last').remove();
				});
				if(options.animation == 'fade')
				{
					if(options.height == 0)
					{
						obj.children('li:eq(' + options.showItems + ')').fadeOut(options.speed);
					}
					obj.children('li:first').hide().fadeIn(options.speed).show();
				}
			};
		return this.each(function() {
			var obj = $(this);
			var maxHeight = 0;
			obj.css({overflow: 'hidden'})
				.children('ul').css({position: 'absolute', margin: 0, padding: 0})
				.children('li').css({margin: 0, padding: 0});
			if(options.height == 0)
			{
				obj.children('ul').children('li').each(function(){
					if($(this).height() > maxHeight)
					{
						maxHeight = $(this).height();
					}
				});
				obj.children('ul').children('li').each(function(){
					$(this).height(maxHeight);
				});
				obj.height(maxHeight * options.showItems);
			}
			else
			{
				obj.height(options.height);
			}
			var interval = setInterval(function(){ 
				if(options.direction == 'up')
				{ 
					moveUp(obj, maxHeight, options); 
				}
				else
				{ 
					moveDown(obj, maxHeight, options); 
				} 
			}, options.pause);
			if(options.mousePause)
			{
				obj.bind("mouseenter",function(){
					options.isPaused = true;
				}).bind("mouseleave",function(){
					options.isPaused = false;
				});
			}
		});
	};
})(jQuery);

$(function(){
	$('#taglines').vTicker({ 
		speed: 500,
		pause: 5000,
		animation: 'fade',
		mousePause: true,
		showItems: 1
	});
});

/*Galleries*/
$(function () {
	if ($('div.gallery').length > 0) {
	    $('div.gallery a').prettyPhoto({theme:'facebook'});
	}
});

