﻿$(document).ready(function () {
	//LOGOTYPE
	$('#Logotype').click(function () {
		var url = $('#Logotype a').attr("href");
		if (url == null)
			url = "";
		url = url.replace("http://", "");
		url = url.replace("https://", "");
		if (url.charAt(url.length - 1) == '/') {
			url = url.substring(0, url.length - 1)
		}
		var currentUrl = window.location;
		_gaq.push(['_trackEvent', 'banner-klick', currentUrl, url]);
	});
	//EXTERNAL LINKS, NOT TEASERS
	$('a[href^="http://"], a[href^="https://"]').not("#Logotype a").not(".teaser").not(".expandable").click(function () {
		var url = $(this).attr('href');
		url = url.replace("http://", "");
		url = url.replace("https://", "");
		_gaq.push(['_trackPageview', '/virtuell/external/' + url]);
	});

	//RSS FEEDS
	$('a:[href^="/rss"][href!="/rss/"], a:[href$="/rss"]').click(function () {
		_gaq.push(['_trackPageview', '/virtuell/prenumeration/RSS']['_trackEvent', 'prenumeration', 'RSS']);
	});

	//MAIL LINKS
	$('a:[href^="mailto:"]').click(function () {
		var mail = $(this).attr('href');
		mail = mail.replace("mailto:", "");
		mail = mail.substring(0, mail.indexOf("@"));
		_gaq.push(['_trackPageview', '/virtuell/epost/mottagare/' + mail]);
	});

	//DOWNLOAD FILES AND PUBLICATIONS/FILES
	$('a:not([href^="http://"][href^="https://"][href^="mailto:"])').not(".teaser").click(function () {
		var url = $(this).attr('href');
		if (typeof url != "undefined") {
			var positionOfExtension = url.lastIndexOf(".");
			if (positionOfExtension > 0) {
				var extension = url.substring(positionOfExtension + 1);

				var url = window.location.protocol + '//' + window.location.hostname + url;

				var pushUrl = '/virtuell/download/';
				if (window.location.pathname.toLowerCase().startsWith("/publikationer")) {
					pushUrl = pushUrl + 'publikationer/';
				}

				pushUrl = pushUrl + extension + '/' + url;

				_gaq.push(['_trackPageview', pushUrl]);
			}
		}
	});

	//TEASER LINKS
	$("a.teaser").click(function (e) {
		var teaserUrl = $(this).attr("href");
		if (teaserUrl.indexOf('http://') == -1 && teaserUrl.indexOf('https://') == -1 && teaserUrl.indexOf(window.location.hostname) == -1) {
			teaserUrl = window.location.hostname + teaserUrl;
		}
		teaserUrl = teaserUrl.replace("http://", "");
		teaserUrl = teaserUrl.replace("https://", "");
		if (teaserUrl.charAt(teaserUrl.length - 1) == '/') {
			teaserUrl = teaserUrl.substring(0, teaserUrl.length - 1)
		}

		var currentUrl = window.location.href;
		var teaserName = $(this).children(':first').attr("alt");

		_gaq.push(['_trackEvent', teaserName, currentUrl, teaserUrl]);
	});

	//TWITTER
	$('#custom-tweet-button a').click(function (e) {
		var href = $(this).attr("href");
		var width = 500;
		var height = 300;
		var left = parseInt((screen.availWidth / 2) - (width / 2));
		var top = parseInt((screen.availHeight / 2) - (height / 2));
		var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
		window.open(href, 'retweet', windowFeatures);
		_gaq.push(['_trackEvent', 'dela-raden', 'retweet', href]);
		e.preventDefault();
	});

	//FACEBOOK
	if ($(".facebook").length) {
		FB.Event.subscribe('edge.create', function (href, widget) {
			_gaq.push(['_trackEvent', 'dela-raden', 'facebook-rekommendera', href]);
			//_gaq.push(['_trackEvent', 'dela-raden', 'facebook-rekommendera', href]);
		});
	}

	/*
	* Slide toggles elements specified by the class with the expId prefix in the element with the expandable class
	* Example of usage:
	* <a class="expandable expIdtest12345">expand by clicking here</a>
	* <p class="test12345">
	*/
	$('.expandable').click(function (event) {
		event.preventDefault();
		var className = $(this).attr('class');
		var expId = className.substr(className.indexOf('expId'));
		if (expId.length > 5) {
			expId = expId.substr(5);
		}
		else {
			return;
		}
		if (expId.indexOf(' ') > 0) {
			expId = expId.substr(0, expId.indexOf('expId'));
		}		
		$('.' + expId).slideToggle('fast');
	});

});

// track clicks from flash banner
function trackBannerClick(url) {    
    url = url.replace("http://", "");
    url = url.replace("https://", "");
    if (url.charAt(url.length - 1) == '/') {
        url = url.substring(0, url.length - 1)
    }
    var currentUrl = window.location;
//    if (window.location.search.indexOf('debugtest') != -1) {
//        alert('FLASH BANNER - URL: ' + url);
//    }
    _gaq.push(['_trackEvent', 'banner-klick', currentUrl, url]);
}

