//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_themepath = 'themes/default/';
//Branding
	var lbox_branding_custom = '<a href="http://dein.gs"><img src="' + lbox_themepath + 'branding-librebox.png" alt="LibreBox"></a>';

//
//Translations
//
//Translatable Strings
var lbox_strings = new Array();
lbox_strings[0] = new Object();
lbox_strings[0]['intro'] = new Object();
lbox_strings[0]['by'] = new Object();
lbox_strings[0]['source'] = 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();
lbox_strings[0]['text'] = new Object();
lbox_strings[0]['video'] = new Object();
lbox_strings[0]['audio'] = new Object();
lbox_strings[0]['image'] = new Object();
lbox_strings[0]['work'] = new Object();
//German
lbox_strings[0]['intro']['de'] = '%%%work wurde%%%author unter %%%licence freigegeben:';
lbox_strings[0]['by']['de'] = ' von %%%author';
lbox_strings[0]['source']['de'] = 'Dieses Werk wurde ursrpünglich hier veröffentlicht:<br /><a href="%%%link">%%%link</a>';
lbox_strings[0]['cc']['de'] = 'folgender 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';
lbox_strings[0]['text']['de'] = 'Dieser Text';
lbox_strings[0]['video']['de'] = 'Dieses Video';
lbox_strings[0]['audio']['de'] = 'Dieses Tonmaterial';
lbox_strings[0]['image']['de'] = 'Dieses Bild';
lbox_strings[0]['work']['de'] = 'Dieses Werk';
//English
lbox_strings[0]['intro']['en'] = '%%%work has been put under %%%licence%%%author:';
lbox_strings[0]['by']['en'] = ' by %%%author';
lbox_strings[0]['source']['en'] = 'This work has originally been published here:<br /><a href="%%%link">%%%link</a>';
lbox_strings[0]['cc']['en'] = 'a 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';
lbox_strings[0]['text']['en'] = 'This text';
lbox_strings[0]['video']['en'] = 'This video';
lbox_strings[0]['audio']['en'] = 'This audio';
lbox_strings[0]['image']['en'] = 'This image';
lbox_strings[0]['work']['en'] = 'This work';

var lbox_count;
var lbox_loaded;

function lbox_create(licence_link, author, author_link, source_link, dctype, triggerobject, params)
{
	lbox_count = lbox_count + 1;
	var final_intro;
	var final_licence;
	var final_licenceimages;
	var final_source;
	var licence_match;
	var branding_licence;
	var tmphtml;
	var tmpid;
	var newtrigger;
	var subject = $(triggerobject).find('.librebox-subject');
	var licenceclass;

	//Check type of content
	if (dctype == 'http://purl.org/dc/dcmitype/Text')
	{
		dctype = 'text';
	}
	else if (dctype == 'http://purl.org/dc/dcmitype/MovingImage')
	{
		dctype = 'video';
	}
	else if (dctype == 'http://purl.org/dc/dcmitype/Sound')
	{
		dctype = 'audio';
	}
	else if (dctype == 'http://purl.org/dc/dcmitype/StillImage')
	{
		dctype = 'image';
	}
	else
	{
		dctype = 'work';
	}

	final_intro = lbox_strings[0]['intro'][lbox_language]
		.replace(/%%%work/, lbox_strings[0][dctype][lbox_language]);

	//Check for Creative Commons licence
	licence_match = licence_link.match(/http\:\/\/creativecommons\.org\/licenses\/(by|by-sa|by-nc|by-nd|bc-nc-sa|by-nc-sa|by-nc-nd)\/(1\.0|2\.0|2\.1|2\.5|3\.0)\/([a-z_]*)?\/?/i);
	if (licence_match)
	{
		licenceclass = licence_match[1];
		licence_link = licence_link + 'deed.' + lbox_language;
		branding_licence = '<a href="http://creativecommons.org"><img src="' + lbox_themepath + 'branding-cc.png" alt="Creative Commons"></a>';
		final_intro = final_intro.replace(/%%%licence/, lbox_strings[0]['cc'][lbox_language]);
		final_licence = '<ul>' + licence_match[1]
			.replace(/by/, '<li>' + lbox_strings[0]['cc-by'][lbox_language] + '</li>')
			.replace(/-sa/, '<li>' + lbox_strings[0]['cc-sa'][lbox_language] + '</li>')
			.replace(/-nc/, '<li>' + lbox_strings[0]['cc-nc'][lbox_language] + '</li>')
			.replace(/-nd/, '<li>' + lbox_strings[0]['cc-nd'][lbox_language] + '</li>')
			+ '</ul>';

		final_licenceimages = '<ul>' + licence_match[1]
			.replace(/by/, '<li>' + '<img src="' + lbox_themepath + 'cc-by.png' + '" alt="' + lbox_strings[0]['cc-by'][lbox_language] + '" />' + '</li>')
			.replace(/-sa/, '<li>' + '<img src="' + lbox_themepath + 'cc-sa.png' + '" alt="' + lbox_strings[0]['cc-sa'][lbox_language] + '" />' + '</li>')
			.replace(/-nc/, '<li>' + '<img src="' + lbox_themepath + 'cc-nc.png' + '" alt="' + lbox_strings[0]['cc-nc'][lbox_language] + '" />' + '</li>')
			.replace(/-nd/, '<li>' + '<img src="' + lbox_themepath + 'cc-nd.png' + '" alt="' + lbox_strings[0]['cc-nd'][lbox_language] + '" />' + '</li>')
			+ '</ul>';
	}
	else
	{
	alert('licence appears to be not supported');
	}	

	if (author)
	{
		if (author_link)
		{
			final_intro = final_intro
				.replace(/%%%author/, lbox_strings[0]['by'][lbox_language])
				.replace(/%%%author/, '<a href="' + author_link + '">' + author + '</a>');
		}
		else
		{
			final_intro = final_intro
				.replace(/%%%author/, lbox_strings[0]['by'][lbox_language])
				.replace(/%%%author/, author_link);
		}
	}

	if (source_link)
	{
		final_source = lbox_strings[0]['source'][lbox_language]
			.replace(/%%%link/g, source_link);
	}

	//Put everything together ...
	tmphtml = '<div class="lbox" id="lbox-' + lbox_count + '">';
		tmphtml = tmphtml + '<div class="lbox-top" />';
		tmphtml = tmphtml + '<div class="lbox-middle" />';
		tmphtml = tmphtml + '<div class="lbox-bottom" />';
		tmphtml = tmphtml + '<div class="lbox-content">';
			tmphtml = tmphtml + '<p class="lbox-intro">'  + final_intro + '</p>';
			tmphtml = tmphtml + '<div class="lbox-licence">';
				tmphtml = tmphtml + '<a rel="licencelink" href="' + licence_link + '"></a>';
				tmphtml = tmphtml + '<div class="lbox-licenceimages ' + licenceclass + '">' + final_licenceimages + '</div>';
				tmphtml = tmphtml + '<div class="lbox-licencedeed ' + licenceclass + '">' + final_licence + '</div>';
			tmphtml = tmphtml + '</div>';
			tmphtml = tmphtml + '<p class="lbox-link">' + final_source + '</p>';
			tmphtml = tmphtml + '<div class="lbox-branding">';
				tmphtml = tmphtml + '<div class="lbox-branding-licence">' + branding_licence + '</div>';
				tmphtml = tmphtml + '<div class="lbox-branding-custom">' + lbox_branding_custom + '</div>';
			tmphtml = tmphtml + '</div>';
		tmphtml = tmphtml + '</div>';
	tmphtml = tmphtml + '</div>';
	
	//Add LibreBox to DOM
	tmpid = $(tmphtml).appendTo('body');

	//Make licensing information clickable
	tmpid.find('div.lbox-licence').bind('mouseup', function()
	{
		window.open($(this).find('a[rel="licencelink"]').attr('href'));
	});
	
	//Make LibreBox fade out when the cursor leaves it
	tmpid.bind('mouseleave', function(){
		$(this).fadeOut();
	});

	//Make links open in new window
	tmpid.find('a').click(function(e)
	{
		e.preventDefault();
		window.open($(this).attr('href'));
	});

	//
	//Check what is to be done with the trigger object
	//
	//Replace it with a link
	if (params.split(":")[0] == 'link')
	{
		$(triggerobject).html('');
		$(subject).appendTo($(triggerobject));
		$('<a class="librebox-trigger" href="' + licence_link +  '">' + params.replace(/link\:/, '')+ '</a>').appendTo($(triggerobject));
		triggerobject = $(triggerobject).find('.librebox-trigger');
	}
	//Replace it with an image
	else if (params.split(":")[0] == 'image')
	{
		$(triggerobject).html('');
		$(subject).appendTo($(triggerobject));
		$('<a class="librebox-trigger" href="' + licence_link +  '">' + '<img src="' + params.replace(/image\:/, '') + '" alt="" />' + '</a>').appendTo($(triggerobject));
		triggerobject = $(triggerobject).find('.librebox-trigger');
	};
	
	//Connect the trigger object with its LibreBox
	$(triggerobject).attr('rel', 'librebox[' + lbox_count + ']');

	$(triggerobject).bind('mousedown', function(e) {
		var tmpx;
		var tmpy;

		if (e.button != 2)
		{
			e.preventDefault();
		
			tmpx = e.pageX - 10 - $(document).scrollLeft();
			tmpy = e.pageY - 10 - $(document).scrollTop();
			
			if (tmpx+308 > $(window).width())
			{
				tmpx = $(window).width()-312 + $(document).scrollLeft();
			}
			else
			{
				tmpx = tmpx + $(document).scrollLeft();
			}
			if (tmpx < 0)
			{
				tmpx = 0;
			}

			if (tmpy+185 > $(window).height())
			{
				tmpy = $(window).height()-185 + $(document).scrollTop();
			}
			else
			{
				tmpy = tmpy + $(document).scrollTop();
			}
			if (tmpy < 0)
			{
				tmpy = 0;
			}
			$('div[id="lbox-' + $(this).attr('rel').match(/\[(.*)\]/)[1] + '"]').css({'top': tmpy, 'left': tmpx});
			$('div[id="lbox-' + $(this).attr('rel').match(/\[(.*)\]/)[1] + '"]').fadeIn();
		}

	});

	tmpid.hide();
	
}


function lbox_init()
{
	if (!lbox_loaded)
	{
		lbox_loaded = true;
		$('<link type="text/css" href="' + lbox_themepath + 'librebox.css' + '" rel="stylesheet">').appendTo( $('head'));
		//$(document.createElement('link')).attr({type: 'text/css', href: lbox_themepath + 'librebox.css', rel: 'stylesheet'}).appendTo($(document.getElementsByTagName('head')[0])); 


		lbox_count = 0;

		//Scan for Librebox Tags
		$('a[rel^="librebox["]').each(function(){
			lbox_create(
				$(this).attr('rel').match(/\[(.*)\]/)[1].split('||')[0],
				$(this).attr('rel').match(/\[(.*)\]/)[1].split('||')[1],
				$(this).attr('rel').match(/\[(.*)\]/)[1].split('||')[2],
				$(this).attr('rel').match(/\[(.*)\]/)[1].split('||')[3],
				$(this).attr('rel').match(/\[(.*)\]/)[1].split('||')[4],
				$(this),
				$(this).attr('rel').match(/\[(.*)\]/)[1].split('||')[5]
			);
		$(this).attr('title', '');
		});
		
		//Scan for Librebox Tags
		$('span[class="librebox"]').each(function(){
			lbox_create(
				$(this).find('a[rel="license"]:first').attr('href'),
				$(this).find('a[property="cc:attributionName"]').html(),
				$(this).find('a[property="cc:attributionName"]').attr('href'),
				$(this).find('a[rel="dc:source"]').attr('href'),
				$(this).find('span[rel="dc:type"]').attr('href'),
				$(this),
				$(this).attr('title')
			);
		$(this).attr('title', '');
		});
	}
}