﻿//KONFIGURACJA
var trackFilesClicks = true;
var fileTypes  = [".doc",".docx",".xls",".xlsx",".exe",".zip",".pdf"];

//konfiguruj TrackPage dla PLIKÓW
var useTrackPageViewForFiles = true;
var pathForFilesID = "plik"; //ustawia jaki tekst dodawany bedzie za każdym razem do identyfikacji klikniec w plik: '/plik/<ścieżka pliku>'
var pathForOutsideLinksID = "wychodzacy"; //analogicznie do pathForFilesID

//konfiguruj useTrackEvent dla PLIKÓW
var useTrackEventForFiles = true;
var trackFileEventCategory = "Sygma kariera";
var trackFileEventAction = "Plik";

//konfiguruj dla LINKÓW wychodzących
var useTrackEventForOutsideLinks = true;
var trackLinkEventCategory = "Sygma kariera";
var trackLinkEventAction = "Wychodzacy";

var useTrackPageViewForOutsideLinks = true;
var pathForLinksID = "wychodzacy";

//konfiguruj śledzenie domen
var usePageTrackerForDomains = true;
var listenAllPossibleDomains = false; //ignoruje tablice domainsToListen i wywołuje kod dla wszystkich na stronie
var domainsToListen    = ['sygmabank.pl', 'sygmabonus.pl','sygma-bank-kariera.pl'];
var pageTrackerIdString = ""; //_gaq.push(['zbiorcze._link', e.href]);

//OD TEGO MIEJSCA NIE EDYTOWAC ;)
var localPath = location.pathname;
//usuń od ostatniego "/"
localPath = localPath.substr(0, localPath.lastIndexOf("/"));

if (pageTrackerIdString) {
	pageTrackerIdString = pageTrackerIdString + ".";
}

jQuery(document).ready(function($) {
//alert('jestem2');
	var isLinkAFile = false;
	if (trackFilesClicks) {
		$('a').each(function(index) {
			//sprawdz czy prowadzi do pliku
			isLinkAFile = false;
			for (k = 0; k < fileTypes.length; k++){ 
				if ($(this).attr("href")) {
					////alert($(this).attr("href"));
					if (isSubstring($(this).attr("href"), fileTypes[k])) {
						isLinkAFile = true;
						setOnClickEventForFile(this);
						break;
					}
				}
			}
			
			//jesli nie jest plikiem
			if (!isLinkAFile && $(this).attr("href")) {
				//sprawdz czy jest wychodzacym linkiem
				if (isSubstring($(this).attr("href"), "http://") || isSubstring($(this).attr("href"), "https://")) {
					//jest
					//czy dotyczy innej domeny niz ta na ktorej sie znajdujemy
					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]);
								//alert("_gaq.push(['_trackPageview'," + filePath + "]);");
							}
							
							if (useTrackEventForOutsideLinks) {
								_gaq.push(['_trackEvent',trackLinkEventCategory,trackLinkEventAction,$(this).attr("href").replace("http://", "").replace("https://", "").replace("www.", "")]);
								//alert("_gaq.push(['_trackEvent'," + trackLinkEventCategory + "," +trackLinkEventAction+","+$(this).attr("href").replace("http://", "").replace("https://", "").replace("www.", "")+"]);");
							}
							
							if (usePageTrackerForDomains) {
								if (listenAllPossibleDomains) {
											//alert("Wywołałem: _gaq.push([" + pageTrackerIdString + "._link" + ", " + $(this).attr("href") + "])");
											_gaq.push([pageTrackerIdString + "_link", $(this).attr("href")]);
											var pageTracker = _gat._getTrackerByName(pageTrackerIdString);
											if ($(this).attr("target") == "_blank") {
												window.open(pageTracker._getLinkerUrl($(this).attr("href")));
												e.preventDefault();
												//e.returnValue = false;
											} else {
												$(this).attr("href", pageTracker._getLinkerUrl($(this).attr("href")));
											}
								} else {
									for (k = 0; k < domainsToListen.length; k++){ 
										if (isTheSameDomain($(this).attr("href"), domainsToListen[k])) {
											//alert("Wywołałem: _gaq.push([" + pageTrackerIdString + "._link" + ", " + $(this).attr("href") + "])");
											_gaq.push([pageTrackerIdString + "_link", $(this).attr("href")]);
											var pageTracker = _gat._getTrackerByName(pageTrackerIdString);
											if ($(this).attr("target") == "_blank") {
												window.open(pageTracker._getLinkerUrl($(this).attr("href")));
												e.preventDefault();
												//e.returnValue = false;
											} else {
												$(this).attr("href", pageTracker._getLinkerUrl($(this).attr("href")));
											}
										}
									}
								}
							}
						});
					}
				}
			}
		});
	}
});

function setOnClickEventForFile(object) {
	
	
	jQuery(object).click(function() {
		////alert(typeof(this) + ", " + this.nodeName + ", jquery" + jQuery(this));
		filePath = jQuery(this).attr("href");
		//czy wlasny czy wychodzacy
		if ((isSubstring(jQuery(this).attr("href"), "http://") || isSubstring(jQuery(this).attr("href"), "https://")) && !isSubstring(jQuery(this).attr("href"), location.host.replace("www.",""))) {
			//wychodzacy
			pathID = pathForLinksID;
			filePath = "/" + filePath.replace("http://", "").replace("https://", "").replace("www.", "");
		} else {
			filePath = filePath.replace(location.host.replace("www.",""), "").replace("http://", "").replace("https://", "").replace("www.","");
			//czy sciezka wzgledna czy bezwzgledna?
			if (filePath.indexOf("/") != 0) {
				//wzgledna - stworz sciezke bezwzgledna
				filePath = localPath + "/" + jQuery(this).attr("href");
			}
			
		}
	
		trackPreviewPath = "/" + pathForFilesID + filePath;
		if (useTrackPageViewForFiles) {
			_gaq.push(['_trackPageview',trackPreviewPath]);
			//alert("trackPageview: " + trackPreviewPath); 
		}
		if (useTrackEventForFiles) {
			_gaq.push(['_trackEvent',trackFileEventCategory,trackFileEventAction,getFileName(jQuery(this).attr("href"))]);
			//alert("trackEvent: " + getFileName(jQuery(this).attr("href")));
		}
	});
	
}

//czy ta sama domena
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;
}
