// google analytics - tracking of downloads

$(document).ready(function(){ 
						   
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	
	//use jQuery to call the Google Analytics JavaScript
	$.getScript(gaJsHost + "google-analytics.com/ga.js", function(){
	
		//tell Analytics about the current page load using standard _trackPageview method
                try{
                    var pageTracker = _gat._getTracker("UA-11440126-1");
                    pageTracker._trackPageview();
                    pageTracker._setDomainName(".xilinx.com");
                    pageTracker._trackPageLoadTime();
                } catch(err) {}
				
        //get value of XilinxUser cookie	
		        if ($.cookie('XilinxUser')) {
					var XilinxUserVal = $.cookie('XilinxUser');
				} else {
					var XilinxUserVal = 'NoUserID';
				}
		//		alert('xilinxUserVal = ' + XilinxUserVal);
		//loop though each anchor element
		$('#middle a[class!=GAcampaign]').each(function(){
			
			var href = $(this).attr('href');
			var filetypes = /\.(zip|exe|gif|pdf|doc|xls|ppt|mp3|pptx|docx|xlsx)$/i;
						
			if (typeof href !== 'undefined' && href !== false) {
			    //check for links starting with http or https, making sure that links to our own domain are excluded
			    if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
				    $(this).attr('target','_blank');
    				$(this).click(function() {
	    				var extLink = href.replace(/^https?\:\/\//i, '');
		    			var xIDextLink = 'XilinxUserID = ' + XilinxUserVal + ' | External Link = ' + extLink;
			    		pageTracker._trackEvent('External-Link', 'Click', xIDextLink);
				    });
			    }
    			//check for links starting with mailto:
	    		else if (href.match(/^mailto\:/i)){
                    $(this).attr('target','_blank');
			    	$(this).click(function() {
				    	var mailLink = href.replace(/^mailto\:/i, '');
					    var xIDmailLink = 'XilinxUserID = ' + XilinxUserVal + ' | Mail Link = ' + mailLink;
    					pageTracker._trackEvent('Mailto', 'Click', xIDmailLink);
	    			});
		    	}
			    //check for links with file extension that match the filetypes regular expression:
    			else if (href.match(filetypes)){
	    			$(this).attr('target','_blank');
		    		$(this).click(function() {
  			    		var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				    	var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
				    	var xIDextLink = 'XilinxUserID = ' + XilinxUserVal + ' | Extension = ' + extension + ' | Filepath = ' + filePath;
				    	pageTracker._trackEvent('Downloads', 'Click', xIDextLink);
				    });
			    }			
			}			
		}); // end of anchor loop
		$('a[class*=GAcampaign]').each(function(){
			
			var href = $(this).attr('href');
			var filetypes = /\.(zip|exe|gif|pdf|doc|xls|ppt|mp3|pptx|docx|xlsx)$/i;
			var campaign = $(this).attr('class');
			
			if (typeof href !== 'undefined' && href !== false) {
			    //check for links starting with http or https, making sure that links to our own domain are excluded
			    if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
				    $(this).attr('target','_blank');
				    $(this).click(function() {
					    var extLink = href.replace(/^https?\:\/\//i, '');
    					pageTracker._trackEvent(campaign, 'Click', extLink);
	    			});
		    	}
			    //check for links with file extension that match the filetypes regular expression:
    			else if (href.match(filetypes)){
	    			$(this).attr('target','_blank');
		    		$(this).click(function() {
  			    		var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				    	var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
				    	pageTracker._trackEvent(campaign, 'Click', filePath);
				    });
			    }
			}
		}); // end of anchor loop
	});
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

});

