
// var pageTrackerIdString = "";

var useTrackPageViewForOutsideLinks = true;
var pathForLinksID = "link";

var useTrackEventForOutsideLinks = true;
var trackLinkEventCategory = "link";
var trackLinkEventAction = "wychodzacy";

var trackFilesClicks = true;
var fileTypes  = [".doc",".docx",".xls",".xlsx",".exe",".zip",".rar",".pdf"];

var useTrackPageViewForFiles = true;
var pathForFilesID = "plik";

var useTrackEventForFiles = true;
var trackFileEventCategory = "plik";
var trackFileEventAction = "pobranie";

var usePageTrackerForDomains = false;
var listenAllPossibleDomains = false;
var domainsToListen = [];

var localPath = location.pathname;

localPath = localPath.substr(0, localPath.lastIndexOf("/"));

// if (pageTrackerIdString) {
//	pageTrackerIdString = pageTrackerIdString + ".";
// }

jQuery(document).ready(function($) {

	var isLinkAFile = false;
	if (trackFilesClicks) {
		$('a').each(function(index) {
		
			isLinkAFile = false;
			for (k = 0; k < fileTypes.length; k++){ 
				if ($(this).attr("href")) {
					
					if (isSubstring($(this).attr("href"), fileTypes[k])) {
						isLinkAFile = true;
						setOnClickEventForFile(this);
						break;
					}
				}
			}
			
			
			if (!isLinkAFile && $(this).attr("href")) {
			
				if (isSubstring($(this).attr("href"), "http://") || isSubstring($(this).attr("href"), "https://")) {
					
					if (!isTheSameDomain($(this).attr("href"),location.host.replace("www.",""))) {
						$(this).click(function(e) {
							if (useTrackPageViewForOutsideLinks) {
								filePath = "/" + pathForLinksID + "/" + $(this).attr("href").replace("http://", "").replace("https://", "").replace("www.", "");
								_gaq.push(['_trackPageview',filePath]);

							}
							
							if (useTrackEventForOutsideLinks) {
								_gaq.push(['_trackEvent',trackLinkEventCategory,trackLinkEventAction,$(this).attr("href").replace("http://", "").replace("https://", "").replace("www.", "")]);

							}
							
							if (usePageTrackerForDomains) {
								if (listenAllPossibleDomains) {
								
										// do nothing
										
								} else {
								
                                                                   // do nothing
                                                                   
								}
							}
						});
					}
				}
			}
		});
	}
});

function setOnClickEventForFile(object) {
	
	
	jQuery(object).click(function() {
		filePath = jQuery(this).attr("href");
		if ((isSubstring(jQuery(this).attr("href"), "http://") || isSubstring(jQuery(this).attr("href"), "https://")) && !isSubstring(jQuery(this).attr("href"), location.host.replace("www.",""))) {
			pathID = pathForLinksID;
			filePath = "/" + filePath.replace("http://", "").replace("https://", "").replace("www.", "");
		} else {
			filePath = filePath.replace(location.host.replace("www.",""), "").replace("http://", "").replace("https://", "").replace("www.","");
			if (filePath.indexOf("/") != 0) {
				filePath = localPath + "/" + jQuery(this).attr("href");
			}
			
		}
	
		trackPreviewPath = "/" + pathForFilesID + filePath;
		if (useTrackPageViewForFiles) {
			_gaq.push(['_trackPageview',trackPreviewPath]);
		}
		if (useTrackEventForFiles) {
			_gaq.push(['_trackEvent',trackFileEventCategory,trackFileEventAction,getFileName(jQuery(this).attr("href"))]);
		}
	});
	
}

function isTheSameDomain(hrefToCheck, domainToCheck)
{
		return hrefToCheck.replace("http://","").replace("https://", "").replace("www.","").indexOf(domainToCheck) == 0;
}

function getFileName(pathToSearch)
{
	if (pathToSearch.lastIndexOf("/") >= 0) {
		pathToSearch = pathToSearch.substr(pathToSearch.lastIndexOf("/")+1, pathToSearch.length);
	}
	return pathToSearch;
}

function isSubstring(haystack, needle) {
    return haystack.indexOf(needle) != -1;
}

