var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18163473-1']);
_gaq.push(['_trackPageview']);

(function() {
	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);
})();

$(document).ready( function() {
	
	/* Tracking and modal windows for social sharing */
	$('a.modal.share').click( function( e ) {
		e.preventDefault();
		_gaq.push(['_trackEvent', 'Share', $(this).text(), window.location.pathname]);
		window.open( $(this).attr('href'), 'Del', 'menubar=0,resizable=0,width=600,height=380,location=0,status=0,scrollbars=0');
	});
	

	
	/* Affiliate prefixes */
	$('[href^="http://shop.telia.dk"], [href^="http://www.callme.dk"], [href^="https://www.telmore.dk"], [href^="http://www.3.dk"]').click( function(e) {
		e.preventDefault();
		var action = $(this).text(), label = window.location.pathname, affId1 = '', affId2 = '', theHref = $(this).attr('href');
		_gaq.push(['_trackEvent', 'Affiliate', action, label]);
		if( theHref.search('www.callme.dk' ) != -1 ) { affId1 = '137083'; affId2 = '18237022'; }
		if( theHref.search('shop.telia.dk' ) != -1 ) { affId1 = '81753';  affId2 = '17641356'; }
		if( theHref.search('telmore.dk' )    != -1 ) { affId1 = '121476'; affId2 = '18065518'; }
		if( theHref.search('www.3.dk' )      != -1 ) { affId1 = '48511';  affId2 = '17309464'; }
		window.open( "http://clk.tradedoubler.com/click?p(" + affId1 + ")a(1852497)g(" + affId2 + ")url(" + $(this).attr('href') + ")" );
	});
	
	/* Adjust default YouTube embeds to actual page width */
	$('object[src*="www.youtube.com"], embed[src*="www.youtube.com"], iframe[src*="www.youtube.com"').each( function() {
		$(this).attr({
			width: '480',
			height: '268'
		}).width(480).height(268);
	});
	
	/* Adjust old image widths */
	$('img').each( function() {
		if( $(this).attr('width') > 480 ) {
			$(this).attr({
				width: 480,
				height: Math.floor( $(this).attr('height')/($(this).attr('width')/480) )
			});
		}
	});
	
	/* Fix element to top of viewport - use easing animation */
	$('.fixed').each( function(){	
		var $item = $(this), $window = $(window), offset = $item.offset(), topPadding = 15;
		$window.scroll( function() {		
			if( $window.scrollTop() > offset.top ) {
				$item.stop().animate({
					marginTop: $window.scrollTop() - offset.top + topPadding
				});
			} else {
				$item.stop().animate({
					marginTop: 0
				});
			}
		});	
	});
	
});
