/**
 * Tracking
 */
var GAID = 'UA-21658841-1';
var _gaq = _gaq || [];
_gaq.push(['_setAccount', GAID]);
_gaq.push(['_trackPageview']);
var gaInitialized = false;
function initGA() {
	var ga = document.createElement('script');ga.type = 'text/javascript';ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga, s);
	gaInitialized = true;
}

function trackPageView(url) {
	_gaq.push(['_trackPageview', url]);
}

function trackEvent(category, action) {
	_gaq.push(['_trackEvent', category, action]);
}

function trackExternalLink(link) {
	trackEvent('Outbound Links', link.href);
	return true;
}


/**
 * Ready
 */
$(document).ready(function() {
	$('header ul.navigation > li').mouseenter(function() {
		$('header ul.navigation li ul').hide();
		$(this).children('ul').show();
	});

	$('a.up').click(function() {$('body').scrollTo(300);return false;});

	$('a[rel=external]').click(function() { return trackExternalLink(this); }).attr('target', '_blank');
	$('a[rel=download]').click(function() { trackPageView(this.href); }).attr('target', '_blank');
});


/**
 * Print
 */
function printPage() {
	trackEvent('Print button clicked', document.location.pathname);
	window.print();
}


/**
 * Transform links into iframes
 * @author André Zahn
 */
(function ($) {
	if (!$) return;
	$.fn.extend({
		toIframe: function() {
			this.each(function() {
				var link = $(this).children("a:first").attr("href");
				if (link) {
					var iframe;
					if ($.browser.msie) iframe = '<iframe src="'+ link + '" frameBorder="0" allowTransparency="true" scrolling="no"></iframe>';
					else iframe = '<iframe src="'+ link + '"></iframe>';
					$(this).html(iframe);
					$(this).children("iframe").hide().load(function() {$(this).show();});
				}
			});
			return this;
		}
	});
})(jQuery);


/**
 * Image Replacement
 * @author André Zahn
 */
(function ($) {
	if (!$) return;
	$.fn.extend({
		replaceWithImage: function() {
			this.each(function() {
				var src = $(this).attr("data-image");
				if (src) {
					var parent = this;
					var image = document.createElement('img');
					image.onload = function() {$(parent).empty().append(this);};
					image.src = src;
					image.alt = $(this).text();
				}
			});
			return this;
		}
	});
})(jQuery);


/* IE PNG fix multiple filters */
(function ($) {
	if (!$) return;
	$.fn.extend({
		fixPNG: function(sizingMethod, forceBG) {
			if (!($.browser.msie)) return this;
			var emptyimg = "/style/img/blank.gif";
			sizingMethod = sizingMethod || "scale";
			this.each(function() {
				var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
					imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
					src = (isImg) ? imgname : imgname.substring(5, imgname.length-2);
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
				if (isImg) this.src = emptyimg;
				else this.style.backgroundImage = "url(" + emptyimg + ")";
			});
			return this;
		}
	});
})(jQuery);


/* Smooth scrolling with AZ's cool new offset feature */
jQuery.fn.extend({
	scrollTo: function(speed, offset, easing) {
		if (!offset) offset = 0;
		return this.each(function() {
			var targetOffset = $(this).offset().top + offset;
			if (targetOffset < 0) targetOffset = 0;
			$('html,body').animate({scrollTop: targetOffset}, speed, easing);
		});
	}
});


/**
 * Brings HTML5 placeholder attribute to all modern browsers
 * @see http://github.com/NV/placeholder.js
 */
jQuery.fn.textPlaceholder = function () {
	return this.each(function(){
		var that = this;

		if (that.placeholder && 'placeholder' in document.createElement(that.tagName)) return;

		var placeholder = that.getAttribute('placeholder');
		var input = $(that);

		if (that.value === '' || that.value == placeholder) {
			input.addClass('text-placeholder');
			that.value = placeholder;
		}

		input.focus(function(){
			if (input.hasClass('text-placeholder')) {
				this.value = '';
				input.removeClass('text-placeholder')
			}
		});

		input.blur(function(){
			this.value = $.trim(this.value); // AZ
			if (this.value === '') {
				input.addClass('text-placeholder');
				this.value = placeholder;
			} else {
				input.removeClass('text-placeholder');
			}
		});

		that.form && $(that.form).submit(function(){
			if (input.hasClass('text-placeholder')) {
				that.value = '';
			}
		});

	});
};
