//This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, //or (at your option) any later version.
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General //Public License for more details.
//You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

 
//
//Configure LibreBox
//
//Language
	var lbox_language = 'en';
//Image Path
	var lbox_imgpath = '';
//Branding
	var lbox_branding_custom = '<a href="http://dein.gs"><img src="' + lbox_imgpath + 'branding-deings.png" alt="dein.gs"></a>';

//
//Translations
//
//Translatable Strings
var lbox_strings = new Array();
lbox_strings[0] = new Object();
lbox_strings[0]['intro'] = new Object();
lbox_strings[0]['link'] = new Object();
lbox_strings[0]['cc'] = new Object();
lbox_strings[0]['cc-by'] = new Object();
lbox_strings[0]['cc-sa'] = new Object();
lbox_strings[0]['cc-nc'] = new Object();
lbox_strings[0]['cc-nd'] = new Object();
//German
lbox_strings[0]['intro']['de'] = 'Dieses Bild wurde von %%%author unter folgender %%%licence freigegeben:';
lbox_strings[0]['link']['de'] = 'Dieses Werk wurde ursrpünglich hier veröffentlicht:<br /><a href="%%%link">%%%link</a>';
lbox_strings[0]['cc']['de'] = 'Creative-Commons-Lizenz';
lbox_strings[0]['cc-by']['de'] = 'Namensnennung';
lbox_strings[0]['cc-sa']['de'] = 'Weitergabe unter gleichen Bedingungen';
lbox_strings[0]['cc-nc']['de'] = 'Nicht-Kommerziell';
lbox_strings[0]['cc-nd']['de'] = 'Keine Bearbeitung';
//English
lbox_strings[0]['intro']['en'] = 'This picture has been put under a %%%licence by %%%author:';
lbox_strings[0]['link']['en'] = 'This work has originally been published here:<br /><a href="%%%link">%%%link</a>';
lbox_strings[0]['cc']['en'] = 'Creative Commons licence';
lbox_strings[0]['cc-by']['en'] = 'Attribution';
lbox_strings[0]['cc-sa']['en'] = 'Share Alike';
lbox_strings[0]['cc-nc']['en'] = 'Non-Commercial';
lbox_strings[0]['cc-nd']['en'] = 'No Derivates';

var lbox_count;
function lbox_init()
{
	lbox_count = 0;
	$('a[rel="licence"]').each(function(){
		lbox_count = lbox_count + 1;
		var lbox_info = $(this).attr('description').split("||");
			for (var i = 0; i < lbox_info.length; ++i) {
				
				//Determine author
				if (lbox_info[i].substring(0, 7) == 'author:')
				{
					var lbox_author = lbox_info[i].substring(7);
				}
				
				//Determine link to original file
				else if (lbox_info[i].substring(0, 5) == 'link:')
				{
					var lbox_link = lbox_info[i].substring(5);
				}
				
				//Determine licence
				else if (lbox_info[i].substring(0, 8) == 'licence:')
				{
					var lbox_licence = lbox_info[i].substring(8);
				}
			}

			//Check whether all necessary information is available
			if (lbox_author && lbox_licence && lbox_link)
			{

				//Check whether the author's name is supposed to be linked to a web page
				if (lbox_author.search(/\:\:/)!=-1)
				{
					lbox_author = '<a href="' + lbox_author.substring(lbox_author.search(/\:\:/)+2) + '">' + lbox_author.substring(0, lbox_author.search(/\:\:/)) + '</a>';
				}

				//Create intro and link text
				var lbox_intro = lbox_strings[0]['intro'][lbox_language].replace(/%%%licence/g, lbox_strings[0]['cc'][lbox_language]).replace(/%%%author/g, lbox_author);
				lbox_link = lbox_strings[0]['link'][lbox_language].replace(/%%%link/g, lbox_link);

				//Analysing the licencing information
				lbox_licence_splitted = lbox_licence.split(' ');

				//If a Creative Commons License is specified
				if (lbox_licence_splitted[0] == 'cc')
				{
					//Set branding and to Creative Commons's logo
					var lbox_branding_licence = '<a href="http://creativecommons.org"><img src="' + lbox_imgpath + 'branding-cc.png" alt="Creative Commons"></a>';

					//Creative Commons Attribution
					if (lbox_licence_splitted[1] == 'by')
					{
						var lbox_licenceimages = '<img src="' + lbox_imgpath + 'cc-by.png" alt="Attribution" />';
						var lbox_licencesummary = lbox_strings[0]['cc-by'][lbox_language]+'<br />';
					}
					
					//Creative Commons Attribution Share Alike
					else if (lbox_licence_splitted[1] == 'by-sa')
					{
						var lbox_licenceimages = '<img src="' + lbox_imgpath + 'cc-by.png" alt="Attribution" />' + '<img src="' + lbox_imgpath + 'cc-sa.png" alt="Share Alike" />';
						var lbox_licencesummary = lbox_strings[0]['cc-by'][lbox_language]+'<br />' + lbox_strings[0]['cc-sa'][lbox_language];
					}
					
					//Creative Commons Attribution No Derivates
					else if (lbox_licence_splitted[1] == 'by-nd')
					{
						var lbox_licenceimages = '<img src="' + lbox_imgpath + 'cc-by.png" alt="Attribution" />' + '<img src="' + lbox_imgpath + 'cc-nd.png" alt="No Derivates" />';
						var lbox_licencesummary = lbox_strings[0]['cc-by'][lbox_language]+'<br />'+lbox_strings[0]['cc-nd'][lbox_language];
					}

					//Creative Commons Attribution Non-Commercial
					else if (lbox_licence_splitted[1] == 'by-nc')
					{
						var lbox_licenceimages = '<img src="' + lbox_imgpath + 'cc-by.png" alt="Attribution" />' + '<img src="' + lbox_imgpath + 'cc-sa.png" alt="Non-Commercial" />';
						var lbox_licencesummary = lbox_strings[0]['cc-by'][lbox_language]+'<br />'+lbox_strings[0]['cc-nc'][lbox_language];
					}

					//Creative Commons Attribution Non-Commercial Share Alike
					else if (lbox_licence_splitted[1] == 'by-nc-sa')
					{
						var lbox_licenceimages = '<img src="' + lbox_imgpath + 'cc-by.png" alt="Attribution" />' + '<img src="' + lbox_imgpath + 'cc-nc.png" alt="Non-Commerical" />' + '<img src="' + lbox_imgpath + 'cc-sa.png" alt="Share Alike" />';
						var lbox_licencesummary = lbox_strings[0]['cc-by'][lbox_language]+'<br />'+lbox_strings[0]['cc-nc'][lbox_language]+'<br />'+lbox_strings[0]['cc-sa'][lbox_language];
					}

					//Creative Commons Attribution Non-Commercial No Derivates
					else if (lbox_licence_splitted[1] == 'by-nc-nd')
					{
						var lbox_licenceimages = '<img src="' + lbox_imgpath + 'cc-by.png" alt="Attribution" />' + '<img src="' + lbox_imgpath + 'cc-nc.png" alt="Non-Commerical" />' + '<img src="' + lbox_imgpath + 'cc-nd.png" alt="No Derivates" />';
						var lbox_licencesummary = lbox_strings[0]['cc-by'][lbox_language]+'<br />'+lbox_strings[0]['cc-nc'][lbox_language]+'<br />'+lbox_strings[0]['cc-nd'][lbox_language];
					}

					
					else
					{
						//break;
					}
				}

				//Prepare licencing images which have been selected above for use
				lbox_licence = '<div class="lbox-licenceimages">' + lbox_licenceimages + '</div>' + '<div class="lbox-licencesummary">' + lbox_licencesummary + '</div>';

				//Put everything together ...
				lbox_tmphtml = '<div class="lbox" id="lbox-' + lbox_count + '">';
					lbox_tmphtml = lbox_tmphtml + '<div class="lbox-top" />';
					lbox_tmphtml = lbox_tmphtml + '<div class="lbox-middle" />';
					lbox_tmphtml = lbox_tmphtml + '<div class="lbox-bottom" />';
					lbox_tmphtml = lbox_tmphtml + '<div class="lbox-content">';
						lbox_tmphtml = lbox_tmphtml + '<p class="lbox-intro">'  + lbox_intro + '</p>';
						lbox_tmphtml = lbox_tmphtml + '<div class="lbox-licence">' + lbox_licence + '</div>';
						lbox_tmphtml = lbox_tmphtml + '<p class="lbox-link">' + lbox_link + '</p>';
					lbox_tmphtml = lbox_tmphtml + '</div>';
					lbox_tmphtml = lbox_tmphtml + '<div class="lbox-branding">';
						lbox_tmphtml = lbox_tmphtml + '<div class="lbox-branding-licence">' + lbox_branding_licence + '</div>';
						lbox_tmphtml = lbox_tmphtml + '<div class="lbox-branding-custom">' + lbox_branding_custom + '</div>';
					lbox_tmphtml = lbox_tmphtml + '</div>';
				lbox_tmphtml = lbox_tmphtml + '</div>';
				
				//Add LibreBox to DOM
				lbox_tmpid = $(lbox_tmphtml).appendTo('body');

				//Center licencing images vertically
				lbox_tmpid.find('.lbox-licencesummary').each(function()
				{
					lbox_tmppos = 25-($(this).height()/2)-1;
					lbox_tmppos = lbox_tmppos;
					$(this).css({'top' : lbox_tmppos});
				});

				//Make links open in new window
				lbox_tmpid.find('a').click(function(e)
				{
					e.preventDefault();
					window.open($(this).attr('href'));
				});
				lbox_tmpid.hide();
				lbox_tmpid.bind('mouseleave', function(e)
				{
					$(this).fadeOut();
				});

				$(this).attr('librebox', lbox_count);

				//Create display event for LibreBox
				$(this).click(function(e)
				{
					e.preventDefault();
					var lbox_tmpx = e.pageX-10;
					var lbox_tmpy = e.pageY-10;
					if (lbox_tmpx+308 > $(document).width())
					{
						lbox_tmpx = $(document).width()-308;
					}
					if (lbox_tmpx < 0)
						{
							lbox_tmpx = 0;
						}
					if (lbox_tmpy+185 > $(document).height())
					{
						lbox_tmpy = $(document).height()-185;
					}
					if (lbox_tmpy < 0)
						{
							lbox_tmpy = 0;
						}
					$('div[id="lbox-' + $(this).attr('librebox') + '"]').css({'top': lbox_tmpy, 'left': lbox_tmpx});
					$('div[id="lbox-' + $(this).attr('librebox') + '"]').fadeIn();
				});
			}
	});
}