//KONFIGURACJA
var trackClicks = 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 = "Conversion";
var trackFileEventAction = "Plik";

//konfiguruj dla LINKÓW wychodzących
var useTrackEventForOutsideLinks = true;
var trackLinkEventCategory = "Conversion";
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    = ['netcamp.net.pl'];
var pageTrackerIdString = "zbiorcze"; //_gaq.push(['zbiorcze._link', e.href]);

//konfiguracja mail
var trackMailClicks = true;
var trackMailEventCategory = "Conversion";
var trackMailEventAction = "Mail";

var pathForMailID = "mail";

//konfiguracja GG
var trackGGClicks = true;
var trackGGEventCategory = "Conversion";
var trackGGEventAction = "gg";

var pathForGGID = "gg";

//konfiguracja Skype
var trackSkypeClicks = true;
var trackSkypeEventCategory = "Conversion";
var trackSkypeEventAction = "Skype";

var pathForSkypeID = "skype";


//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($) {

jQuery('a').bind(
	'click',
	function(){
		var isLinkAFile = false;
		if (trackClicks) {
			urlObject = $.urlParser.parse($(this).attr("href"));

			switch (urlObject.protocol)
			{
				case 'mailto:':
					if (trackMailClicks) {
						filePath = "/" + pathForMailID + removeLastSlashIfExist(location.pathname) + "/" + getFileName(urlObject.path);
						_gaq.push(['_trackPageview', filePath]);
						_gaq.push(['_trackEvent',trackMailEventCategory,trackMailEventAction, getFileName(urlObject.path)]);
						//alert(filePath);
					}
					break;
				case 'gg:':
					if (trackGGClicks) {
						filePath = "/" + pathForGGID + removeLastSlashIfExist(location.pathname) + "/" + getFileName(urlObject.path);
						_gaq.push(['_trackPageview', filePath]);
						_gaq.push(['_trackEvent',trackGGEventCategory,trackGGEventAction, getFileName(urlObject.path)]);
						//alert(filePath);
					}
					break;
				case 'skype:':
					if (trackSkypeClicks) {
						filePath = "/" + pathForSkypeID + removeLastSlashIfExist(location.pathname) + "/" + getFileName(urlObject.path);
						_gaq.push(['_trackPageview', filePath]);
						_gaq.push(['_trackEvent',trackSkypeEventCategory,trackSkypeEventAction, getFileName(urlObject.path)]);
						//alert(filePath);
					}
					break;
				default:
					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")));
													}
												}
											}
										}
									}
								//});
							}
						}
					}
					break;
			}
		}
	});
});

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")));
		}
	//});
	
}

//zwraca sciezke w strukturze stron
function WhereAmI()
{
	filePath = location.host.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");
			}
			return filePath;
}

//czy ta sama domena
function isTheSameDomain(hrefToCheck, domainToCheck)
{
		return hrefToCheck.replace("http://","").replace("https://", "").replace("www.","").indexOf(domainToCheck) == 0;
}

function removeLastSlashIfExist(removeFrom)
{
	if (removeFrom.lastIndexOf("/") == (removeFrom.length-1)) {
		if (removeFrom.length == 1) {
			removeFrom = "";
		} else {
			removeFrom = removeFrom.substr(0, removeFrom.length-1);
		}
	}
	return removeFrom;
}

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;
}
