if(typeof GenericWindowPopup == "undefined")
	throw("Core Popup Library Required");

/*
LinksToSocialNetworks

Assumptions:
* Prototype is included
* Core popup library is included

*/
var gSocialNetworkItemClassName = "sn_link";

var LinksToSocialNetworks = Class.create({
	initialize: function(containerElementId, locale) {
	
		var self = this;
		
		// Get all links within the container
		$$('#' + containerElementId + ' .' + gSocialNetworkItemClassName).each(function(link) {
			new GenericWindowPopup(link.identify(), null, true);
		});
		
		// Monitor popup events.
		document.observe("genericWindowPopup:popup", function(event) {
			
			if(event.element().descendantOf(containerElementId)) {
				
				var socialNetworkName = self.getSocialNetworkNameFromElementId(event.element().identify());

				var socialNetwork = self.getSocialNetwork(socialNetworkName);
				if(socialNetwork == null)
					throw("Social network not currently supported: '" + socialNetworkName + "'");

				// Track the activity
				self.trackUrl(socialNetwork.getTrackingName(), document.location.href);
				
				// Dynamically build a popup url and get the tracking
				event.memo.sender.windowContentUrl = socialNetwork.getUrl(locale);
			}
		});
	},
	getSocialNetworkNameFromElementId: function(elementId) {
		var suffixPosition = elementId.indexOf('_');
		if(suffixPosition != -1)
			return elementId.substr(0, suffixPosition);
		else
			return elementId;
	},
	getSocialNetwork: function(socialNetworkName) {
		var socialNetwork = null;
		switch(socialNetworkName) {
			case "facebook":
				socialNetwork = new FacebookSocialNetwork();
				break;
			case "twitter":
				socialNetwork = new TwitterSocialNetwork();
				break;
			case "you-tube":
				socialNetwork = new YouTubeSocialNetwork();
				break;
			case "hyves":
				socialNetwork = new HyvesSocialNetwork();
				break;
		}
		return socialNetwork;
	},
	trackUrl: function(socialNetworkName, currentUrl) {
if (typeof pageTracker === 'undefined')
			return;
		
		pageTracker._trackEvent('socialBookmark', socialNetworkName, currentUrl);
	}
});

var FacebookSocialNetwork = Class.create({
	getTrackingName: function() {	return "facebook";	},
	getUrl: function(locale) {
		var url = "http://www.facebook.com/Specsavers"; 
		if(locale != null)
			switch(locale.getCountryCode()) {
				case "NL":
					url = "http://www.facebook.com/specsaversnederland";
					break;
			}
		return url; 
	}
});

var TwitterSocialNetwork = Class.create({
	getTrackingName: function() {	return "twitter";	},
	getUrl: function(locale) {
		var url = "http://twitter.com/Specsavers"; 
		if(locale != null)
			switch(locale.getCountryCode()) {
				case "NL":
					url = "http://twitter.com/specsaversNL";
					break;
			}
		return url;
	}
});

var YouTubeSocialNetwork = Class.create({
	getTrackingName: function() {	return "you-tube";	},
	getUrl: function(locale) {	return "http://www.youtube.com/user/SpecsaversOfficial"; }
});

var HyvesSocialNetwork = Class.create({
	getTrackingName: function() {	return "hyves";	},
	getUrl: function(locale) {	return "http://specsaversnederland.hyves.nl/"; }
});

