$(document).ready(function () {

   $('.mercgrid > li').click(function (e) {
		
        var deviceAgent = navigator.userAgent.toLowerCase();
        var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);
		
		// Alle Bilder zurück auf Schattenbild umstellen
		$('.mercgrid > li').each(function () {
        	//$(this).removeClass('selected');
        });
		
		
		// Bild für selektiertes Element dauerhaft anzeigen
		if (!$(this).hasClass('selected')) {
            //$(this).addClass('selected');
        }
		
		// BasisContainer anzeigen
		if ($(this).hasClass('novideo')) {
            $('#mercbasiContainer').show();
        } else {

			// Variabeln vorbereiten
			var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
			var videofile = Bal.magnoliaContextPath + '/dms/internet/mercator-be/multimedia/jobs/videos/' + $(this).children('span').text() + '.mp4';
			//var poster = Bal.magnoliaContextPath + '/dms/internet/mercator-be/multimedia/jobs/videos/' + $(this).children('span').text() + '.jpg';
			var poster = "";
			

			
			// Player zusammenbauen
			if (!agentID) {		
				// Flash vorbereiten
				flashvars = "&amp;autoplay=true&amp;sound=70&amp;buffer=2&amp;&splashscreen=" + escape(poster) + "&";
				flashvars = flashvars + "vdo=" + videofile;
				content = "<embed pluginspage=\"http://www.macromedia.com/go/getflashplayer\"";
				content = content + "src=\"" + Bal.magnoliaContextPath + "/dms-static/84dd609d-c996-42f1-8881-90ac44cfed9b/gddflvplayer.swf\"";
				content = content + "type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" wmode=\"transparent\"";
				content = content + "quality=\"best\" allowfullscreen=\"true\" flashvars=\"" + flashvars + "\" height=\"433\" width=\"720\"></embed>";
			} else {
				content = "<video height=\"433\" width=\"720\">";
				content = content + "<source type=\"video/mp4\" src=\"" + videofile + "\"></source>";
				content = content + "</video>"
			}
			
			// FancyBox mit Video Tag anzeigen		
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 762,
				'height'		: 475,
				'content'		: content
			});
		}
	});
	
	$(document).click(function () {
        //$('#mercbasiContainer').hide();
    });
	
	// Hirsch schliessen beim draufklicken
	$('#mercbasiContainer').click(function (e) {
		$(this).hide();
	});
	

	// Pfade für Author umbiegen.
 	if ((Bal.magnoliaContextPath == "/magnoliaAuthor")) {	
		if ($("#merccss1").length > 0) {
			// CSS umbiegen
			var href = $('#merccss1').attr('href'); 
			$('#merccss1').attr('href', href.replace("/dms-static","/magnoliaAuthor/dms-static"));
		
			// Bilder auf Authorenumgebung laden
			$('.mercgrid > li').each(function () {
					var style1 = $(this).attr("style");
					$(this).attr("style", style1.replace("url(/dms-static/","url(/magnoliaAuthor/dms-static/"));
			});
		
			// Hirsch umbiegen
			var style2 = $('#mercbasiContainer').attr("style");
			$('#mercbasiContainer').attr("style", style2.replace("url(/dms-static/","url(/magnoliaAuthor/dms-static/"));

		}
	}
	
	
});

