jQuery.noConflict();

var options = { path: '/', expires: 30 };
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

/********
	Single defination of domain for external links tag and Google Anlytics code
 ********/	
var domainName = 'www.pictures-of-castles.co.uk';
var filetypes = /\.(zip|txt|vsd|vxd|js|css|rar|wma|mov|avi|wmv|exe|pdf|doc*|xls*|ppt*|mp3)$/i;

jQuery(document).ready(function() {

	// Show login first popup
	jQuery(".loginFirst a").click(function(e) {

		jQuery('#LoginFirstPopUp').css({left:e.pageX,top:e.pageY}).show();

		return false;
	});
	
	/*********
		Convert span with class="email" to clickable and Google Analytics tracked link
		
		Input: <span class="email">email @ domain . com</span>
		or:    <span class="email">email(at)domain(dot)com</span>
		or combination of signs and at/dot in text.
		
		Output <a href='mailto:email@domain.com' onclick='...'>email@domain.com</a>
		
		26 Nov 2009: Updated replace strings to be case insensitive and not limited to first replacement.
		 8 Sep 2010: Fixed issue with anchor text not being replaced without use of title attribute.
		11 Oct 2010: Changed link start from email to _email
								 Removed Google Analytics Code (to avoid duplication)
	*********/
	jQuery(".email").each(function() {
		var email = jQuery(this).text().toLowerCase();
		email = email.replace(/\(/g, " ");
		email = email.replace(/\)/g, " ");
		if ((email.indexOf(" @ ") != -1) || (email.indexOf(" . ") != -1) || (email.indexOf(" at ") != -1) || (email.indexOf(" dot ") != -1)) {
			email = email.replace(/\s+dot\s+/gi, ".");
			email = email.replace(/\s+at\s+/gi, "@");
			var parts = email.split("@");
			if (parts.length == 2) {
				if (jQuery(this).attr('title').length > 0) var text = jQuery(this).attr('title');
				else var text = email.replace(/\s/g, '');
				var domain = parts[1].split(".");
				email = parts[0].trim() + "@";
				for(var i = 0; i < domain.length; i++) {
					if (i > 0) 	email += ".";
					email += domain[i].trim();
				}
				jQuery(this).replaceWith("<a href='mailto:" + email + "'>" + text + "</a>");
			}
		}
	});



	/** Tag external links **/	
	 jQuery('.typography a').filter(function() {
			return ((this.hostname && this.hostname !== location.hostname) && (this.hostname !== domainName));
		}).addClass("external");

	/**
	 Add Google Analytics tracking to document links 
	 11 Oct 2010: Updated code to include event tracking
	              Upgraded code to asynchronous code
	 **/	
	jQuery('a[href]').each(function(){
	
		var href = jQuery(this).attr('href');
		
		if ((this.hostname && this.hostname !== location.hostname) && (this.hostname !== domainName)) jQuery(this).addClass("external");

		if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
		//alert(document.domain
			jQuery(this).click(function() {
				var extLink = href.replace(/^https?\:\/\//i, '');
				_gaq.push(['_trackEvent', 'External', 'Click', extLink]);
				_gaq.push(['_trackPageview', '_outbound/' + extLink]);
				//alert(extLink);
			});
		}
		else if (href.match(/^mailto\:/i)){
			jQuery(this).click(function() {
				var mailLink = href.replace(/^mailto\:/i, '');
				_gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
				_gaq.push(['_trackPageview', '_email/' + mailLink]);
				//alert(mailLink);
			});
		}
		else if (href.match(filetypes)){
			var domainNameRegExp = domainName.replace(/\./g, "\\.");
			domainNameRegExp = domainNameRegExp.replace("www\\.", "(www.)");
			//alert(domainNameRegExp);
			
			var regExp = new RegExp('/^https?\:\/\/' + domainNameRegExp + '\//i');
			//alert(regExp);
		
			jQuery(this).click(function() {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				//var filePath = href.replace(/^https?\:\/\/(www.)breconwales\.com\//i, '');
				var filePath = href.replace("http://" + domainName + '/', '');
				_gaq.push(['_trackEvent', 'Download', 'Click - ' + extension, filePath]);
				_gaq.push(['_trackPageview', '_download/' + extension + '/' + filePath]);
				//alert(filePath);
			});
	  }
  });
}); 


