$(document).ready(function() {

	//jQuery.noConflict();
	initLightBoxImages();
	initFancyVideos();
	initFancyInline();
	initFancyIFrame();
	initFancyGalery();
	initYouTubeVideos();
	
	$('a.video_link').each(function() {
	
		var thePadding = 1;
		
		$(this).click(function(event) {
			event.preventDefault();
			initPlayer(this, thePadding, $(this).attr('href'), $(this).attr('title'), $('<div class="flvPlayer"><div/>'));
		})
	});	
	
});

function getViewport() {
	return [
		$(window).width() ,
		$(window).height() ,
		$(document).scrollLeft(),
		$(document).scrollTop()
	];
};

function initPlayer(theObject, thePadding, theClip, theTitle, container) {

	var player;
	
	$.fancybox({href: $(theObject).attr("href"), title: $(theObject).attr("title")},
	{
		'content': container,
		'padding': thePadding,
		'width': 400,
		'height': 300,
		'titleShow': true,
		'titlePosition'	: 'outside',
		'title': theTitle,
		'autoDimensions': false,
		'autoScale': true,
		'scrolling': true,
		'overlayOpacity' :	0.7,
		'overlayColor'	 :  '#000',
		'speedIn'    :	400,
		'speedOut'   :	400,
		'transitionIn'	 : 'elastic',
		'transitionOut'	 : 'elastic',
		
		'onComplete' :function(){
		
			player = flowplayer("fancybox-content", "assets/swf/flowplayer-3.2.7.swf", {
			
				play: {
					label: null,
					replayLabel: 'Wiederholen',
					opacity: 1
				},			
			
				clip: {
					url: theClip,
					autoPlay: true,
					onMetaData:function(clip){

						var clipwidth = clip.metaData.width;
						var clipheight= clip.metaData.height;
						var pos = getViewport();
						
						$("#fancybox-content").css({width:clipwidth+2*thePadding, height:clipheight});
						$("#fancybox-title").css({width:clipwidth+2*thePadding});
						$("#fancybox-wrap").css({width:clipwidth+4*thePadding, height:clipheight+2*thePadding});
						
						$("#fancybox-wrap").css('left', ((clipwidth + 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - clipwidth	- 36)	/ 2)));
						$("#fancybox-wrap").css('top',  ((clipheight + 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - clipheight - 50)	/ 2)));
						$("#fancybox-right, #fancybox-left").css({height:clipheight-60, bottom: '70px'});
						
						$("#fancybox-close").css('right', -17 - (11 - thePadding));
						$("#fancybox-close").css('top', -15 - (11 - thePadding));
					}
				}
			});	
							
			player.load();
			//Standardgroesse wieder einstellen -> player - groesse ist noch null (?) -> wird bis auf padding verkleinert
			$("#fancybox-content").css({width: 400, height:300});
			$("#fancybox-wrap").css({width:400, height:300});
			$.fancybox.resize();
		},
		
		'onClosed':function(){
			$("#fancybox-content_api").remove();
		},
		'onCleanup':function(){
			player.unload();
		}
	});
	
}; 

function initFancyGalery() {

	var theHREFs;
	var theParentId = '';
	var item;
	
	jQuery('div.box_bottom').each(function() {
		
		jQuery('a.fancyGaleryLink', this).click(function() {
		
			var theParent = jQuery(this).parent();
			
			//die passende div suchen
			while(theParent && theParent.attr("class") != 'box_bottom')
				theParent = theParent.parent();
				
			theHREFs = new Array();
			
			//alle Anker mit der Klasse 'fancyboxImg' verwenden
			theParentId = '#' + theParent.attr("id")  + ' .fancyboxImg';
			
			jQuery(theParentId).each(function() {
				item = {href: jQuery(this).attr("href"), title: '<b>' + jQuery(this).attr("title") + '</b>'};
				theHREFs.push(item);
			});	
			
			jQuery.fancybox(theHREFs,
				{
					'padding'		: 0,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'type'			: 'image',
					'hideOnContentClick': true,
					'titleShow'		: true,
					'titlePosition' : 'over',
					'easingIn': 'swing',
					'easingOut': 'easeInBack',
					'overlayShow': true,
					'overlayOpacity': 0.8,
					'overlayColor': '#000',
					'centerOnScroll': true,
					'titleFormat': formatTitle
				}
			);
		});
			
	});
}

function initYouTubeVideos() {

	$("a.youTube").click(function() {
		$.fancybox({
				'easingIn': 'swing',
				'easingOut': 'easeInBack',
				'overlayShow': true,
				'overlayOpacity': 0.8,
				'overlayColor': '#000',
				'padding'		: 0,
				'autoScale'		: true,
				'autoDimensions': true,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});

		return false;
	});
}

function initFancyIFrame() {

	jQuery("a.fancyIFrame").fancybox({
			'width'				: '50%',
			'height'			: '50%',
	        'autoScale'     	: false,
	        'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'type'				: 'iframe'
		});
}

function initFancyInline() {

	jQuery("a.fancyInline").fancybox({
			'autoDimensions'	: true,
			'overlayShow'		: true,
			'overlayOpacity'	: 0.8,
			'overlayColor'		: '#000',
			'centerOnScroll'	: true,
			'hideOnContentClick': true
		});
}

function initFancyVideos() {

	jQuery(".fancyVideo").fancybox({
		'transitionIn' : 'elastic',
		'padding' : 0,
		'autoScale' : false,
		'autoDimensions'	: false,
		'transitionOut' : 'elastic',
		'hideOnContentClick': true,
		'titleShow': true,
		'titlePosition' : 'over',
		'type' : 'swf',
		'swf'                   : {
  			'wmode'               : 'transparent',
  			'allowfullscreen'     : 'true'
		},
		'onStart': function() {
			jQuery('#fancybox-title').hide();
		},
		'onComplete' : function() {
			jQuery('#fancybox-wrap').hover(function() {
				jQuery('#fancybox-title').show();
			}, function() {
				jQuery('#fancybox-title').hide();
			});}
	});
}

function initLightBoxImages() {

	var theLink = "";
	
	jQuery(document).ready(function() {
	
		jQuery('img.lightbox').each(function() {
			theLink = jQuery(this).attr("src");
			theLink.split("&")[0].split("=")[1]; 
			theLink = theLink.replace('[(site_url)]','');
			theLink = theLink.replace('thumb_','');
			jQuery(this).wrap('<a rel="imagegroup" href="'+theLink+'" class="fancybox" title="'+this.title+'"></a>');
		});
		
				  
		jQuery('a.fancybox').fancybox({
			'padding': 0,
			'speedIn': 500,
			'speedOut': 300,
			'changeSpeed': 300,
			'transitionIn': 'elastic',
			'transitionOut': 'elastic',
			'titlePosition': 'over',
			'titleShow': true,
			'easingIn': 'swing',
			'easingOut': 'easeInBack',
			'overlayShow': true,
			'overlayOpacity': 0.7,
			'overlayColor': '#000',
			'centerOnScroll': true,
			'hideOnContentClick': true,
			'onStart': function() {
				jQuery('#fancybox-title').hide();
			},
			'onComplete' : function() {
				jQuery('#fancybox-wrap').hover(function() {
					jQuery('#fancybox-title').show();
				}, function() {
					jQuery('#fancybox-title').hide();
				});},
	        'titleFormat': formatTitle	
			});			  
		
		});
		
}

function formatTitle(title, currentArray, currentIndex, currentOpts) {
	return '<span id="fancybox-title-over">Bild ' + (currentIndex + 1) + '/' + currentArray.length + ': ' + title + '</span>';
};

function isArray(theObj) {
   if (theObj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}
