/*
   Uniform Share Function for Lufthansa and Miles&More

   Copyright (c) 2011 DRID Kommunikation und Design (www.drid.de)

   Depends on:
   - jQuery Library v1.5.1 (www.jquery.com)

   Requires:
   - share.css
   - share-overlay.png
   - share-close.png
   - share-button-arrow.gif
   - network icons in dir networks: every network must have an 16x16 gif icon in this folder (icon filename = network id)
   - facebook.jpg (Preview image for Facebook)

   Required META-TAGS in HTML-Code:
   <meta name="title" content="WEBSITE TITLE" />
   <meta name="description" content="WEBSITE DESCRIPTION (MAX 400 CHARS)" />
   <meta name="share-url" content="WEBSITE URL (WITH LANGUAGE/COUNTRY PARAMETERS" />
   <meta name="share-prov" content="'Lufthansa' OR 'Miles&More'" />
   <meta name="share-window-title" content="SHARE WINDOW TITLE" />
   <meta name="share-window-close-type" content="'image' OR 'text'" />
   <meta name="share-window-close-text" content="TEXT FOR CLOSE BUTTON" /> (optional, if share-window-close-type = 'image')
   <meta name="share-window-vertical-align" content="center" /> (optional)
   <link rel="image_src" href="THUMBNAIL URL FOR FACEBOOK" />
   
   IMPORTANT: Flash parameter 'wmode' must be set to 'opaque' or 'transparent'!
*/

var share_networks = {
	delicious:  {title: 'Delicious', url: 'http://delicious.com/post?url=[URL]&title=[TITLE]'},
	facebook:   {title: 'Facebook',  url: 'http://www.facebook.com/sharer.php?u=[URL]'},
	google:     {title: 'Google',    url: 'http://www.google.com/bookmarks/mark?op=add&bkmk=[URL]&title=[TITLE]&annotation=[DESCRIPTION]&service=bookmarks'},
	linkedin:   {title: 'LinkedIn',  url: 'http://www.linkedin.com/shareArticle?mini=true&url=[URL]&title=[TITLE]&source=[PROV]'},
	myspace:    {title: 'MySpace',   url: 'http://www.myspace.com/index.cfm?fuseaction=postto&u=[URL]&t=[TITLE]'},
	studivz:    {title: 'StudiVZ',   url: 'http://www.studivz.net/Suggest/Selection/?u=[URL]&desc=[TITLE]&prov=[PROV]'},
	twitter:    {title: 'Twitter',   url: 'http://twitter.com/home?status=[URL]'},
	misterwong: {title: 'Mr. Wong',  url: 'http://www.mister-wong.de/index.php?action=addurl&bm_url=[URL]&bm_description=[TITLE]&bm_notice=[DESCRIPTION]'}
};

document.write('<link rel="stylesheet" type="text/css" href="'+project_base+'share/share.css" />');

$(document).ready(function() {
	var nocache                  = Math.round(Math.random() * 100000000);
	var share_window_title       = $('meta[name=share-window-title]').attr('content');
	var share_window_vert_align  = $('meta[name=share-window-vertical-align]').attr('content');
	var share_window_close_type  = $('meta[name=share-window-close-type]').attr('content');
	var share_window_close_text  = $('meta[name=share-window-close-text]').attr('content');
	var share_prov               = $('meta[name=share-prov]').attr('content');

	var share_top_class          = (share_prov == 'Miles&More') ? 'mm' : 'lh';
	var close_image              = (share_window_close_type == 'image') ? '   <div id="share-close"></div>' : '';
	var close_button             = (share_window_close_type != 'image') ? '     <div id="bottom"><span id="button-share-close">'+share_window_close_text+'</span></div>' : '';
	
	var share_html = '<div id="share-overlay"></div>'
	               + '<div id="share">'
	               + close_image
	               + '   <div id="share-title">'+share_window_title+'</div>'
	               + '   <div id="share-top" class="'+share_top_class+'"></div>'
	               + '   <div id="share-networks"></div>'
	               + close_button
	               + '</div>';
	$('body').append(share_html);

	openShareWindow = function() {
		if ($.browser.msie && $.browser.version < 7) {
			// Fix overlay position, size and transparency for IE 6
			$('#share-overlay').css({
				position: 'absolute',
				width: $(window).width(),
				height: $(window).height(),
				backgroundImage: 'none',
				filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="share/share-overlay-ie.png"'
			});
			
			// Fix close button transparency
			$('#share-close').css({
				backgroundImage: 'none',
				filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="share/share-close.png"'
			});
		}
		if (share_window_vert_align == 'center') {
			padding = 20;
			$('#share').css({top: ($(window).height()-$('#share').height()-padding) / 2});
		}

		// Get Metadata
		var share_url          = $('meta[name=share-url]').attr('content');
		    share_url         += ((share_url.indexOf('?') == -1) ? '?' : '&') + 'nocache='+nocache;
		var share_title        = $('meta[name=title]').attr('content');
		var share_description  = $('meta[name=description]').attr('content');

		var network_html = '';
		for (var id in share_networks) {
			var title = share_networks[id].title;
			var url   = share_networks[id].url;
				url   = url.split('[URL]').join(escape(share_url));
				url   = url.split('[TITLE]').join(escape(share_title));
				url   = url.split('[DESCRIPTION]').join(escape(share_description));
				url   = url.split('[PROV]').join(escape(share_prov));
			var onclick = '';
			if (typeof(track_share) == 'function') {
				onclick = 'onclick="track_share(\''+id+'\');"';
			}
			network_html += '<a href="'+url+'" target="_blank" class="share-button" style="background-image:url('+project_base+'share/networks/'+id+'.gif)" '+onclick+'>'+title+'</a>';
		}
		network_html += '<br style="clear:both;" />'
		$('#share-networks').html(network_html);

		// Display overlay and Share dialog
		$('#share-overlay').fadeIn(100, function() {
			$('#share').delay(100).fadeIn(200);
		});
	}

	$('#button-share-close, #share-close').click(function() {
		$('#share').fadeOut(200, function() {
			$('#share-overlay').hide();
		});
	})

	$('.footer .links .share').click(openShareWindow);
});

