$(document).ready(function() {

		// global vars
		var oldBreadcrumbContainerSize = 0;

    // create a new instance of the slider plugin
    //var slider = new $.b8_slider($('#karriere-slider'), {slideInterval:10, autoStart:true, rootObject:'#karriere-slider'});



    // shorten breadcrumb
		$(window).resize(function() {
	    var breadcrumb = $('.breadcrumb');
	    //var breadcrumbContainer = breadcrumb.closest('.box-content');
	    var breadcrumbContainer = breadcrumb.parent();
	    
	    function getBreadcrumbWidth(breadcrumb) {
		    var bcWidth = 0;
	    	$.each(breadcrumb.find('li').get(), function() {
	    		bcWidth += $(this).outerWidth(true);
	    	});
	    	return bcWidth;
	    }
	    
			function restoreBreadcrumbText(breadcrumb) {
    		// adjust breadcrumbs width
    		$.each(breadcrumb.find('li').get(), function() {
	    		var elem = $(this);
	    		var elemA = elem.find('a');
	    		
	    		if (elemA.length > 0) {
	    			// link found
		    		// restore original text from data attribute
		    		if (elemA.data('text')) {
		    			elemA.text(elemA.data('text'));
		    		}
	    		}
    		});
			}

			function adjustBreadcrumbWidth(breadcrumb, breadcrumbContainer) {
				if (getBreadcrumbWidth(breadcrumb) > breadcrumbContainer.width()) {
	    		// adjust breadcrumbs width
	    		$.each(breadcrumb.find('li').get().reverse(), function() {
	    		
		    		var elem = $(this);
		    		var elemA = elem.find('a');
		    		
		    		if (elemA.length > 0) {
		    			// link found
			    		// store original text in data attribute
			    		if (!elemA.data('text')) {
				    		elemA.attr('title', elemA.text()), 
			    			elemA.data('text', elemA.text());
			    		}
			    		
				    	if (elemA.text() == '...') {
				    		// continue with next item
				    		return true;
				    	} else {
				    		// shorten these
					    	elemA.text('...');
					    	adjustBreadcrumbWidth(breadcrumb, breadcrumbContainer);
					    	return false;
				    	}
		    		}
	    		});
	    	}
			}

	    if (oldBreadcrumbContainerSize != breadcrumbContainer.width()) {
	    
		    oldBreadcrumbContainerSize = breadcrumbContainer.width();
	    
		    if (breadcrumb.length > 0) {
		    	// initial call
					restoreBreadcrumbText(breadcrumb);
		    	adjustBreadcrumbWidth(breadcrumb, breadcrumbContainer);
		    }
		    
	    }
		}); // end breadcrumb stuff
		
		


		// init videoplayer
		$('.use-video-player').each(function(index) {
			$(this).click(function(e){
				var source = $(this).attr('rel');
				
				if (parseInt($(window).width()) <= 1024) {
					var videoWidth = 768;
					var videoHeight = 432;
				} else {
					var videoWidth = 1024;
					var videoHeight = 576;
				}
			
			  var playercode = '<!-- Begin VideoJS -->\n'+
			  '<div class="video-js-box">\n'+
			  '  <!-- Using the Video for Everybody Embed Code http://camendesign.com/code/video_for_everybody -->\n'+
			  '  <video class="video-js" width="'+videoWidth+'" height="'+videoHeight+'" controls autoplay preload="auto" poster="http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.png">\n'+
			  '    <source src="http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.mp4" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />\n'+
			  '    <source src="http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.webm" type=\'video/webm; codecs="vp8, vorbis"\' />\n'+
			  '    <source src="http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.ogv" type=\'video/ogg; codecs="theora, vorbis"\' />\n'+
			  '    <!-- Flash Fallback. Use any flash video player here. Make sure to keep the vjs-flash-fallback class. -->\n'+
			  '    <object id="flash_fallback_1" class="vjs-flash-fallback" width="'+videoWidth+'" height="'+videoHeight+'" type="application/x-shockwave-flash"\n'+
			  '      data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">\n'+
			  '      <param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />\n'+
			  '      <param name="allowfullscreen" value="false" />\n'+
			  '      <param name="flashvars" value=\'config={"playlist":["http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.png", {"url": "http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.mp4","autoPlay":true,"autoBuffering":true}]}\' />\n'+
			  '      <!-- Image Fallback. Typically the same as the poster image. -->\n'+
			  '      <img src="http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.png" width="'+videoWidth+'" height="'+videoHeight+'" alt="Poster Image"\n'+
			  '        title="No video playback capabilities." />\n'+
			  '    </object>\n'+
			  '  </video>\n'+
			  '  <!-- Download links provided for devices that cant play video in the browser. -->\n'+
			  '  <p class="vjs-no-video"><strong>Download Video:</strong>\n'+
			  '    <a href="http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.mp4">MP4</a>,\n'+
			  '    <a href="http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.webm">WebM</a>,\n'+
			  '    <a href="http://www.e-g-o.de/fileadmin/egoproducts.com/templates/video/'+source+'.ogv">Ogg</a><br>\n'+
			  '  </p>\n'+
			  '</div>\n'+
			  '<!-- End VideoJS -->';
			
        $.fancybox({
	        'padding': 7,
	        'overlayOpacity': 0.8,
	        'overlayColor' : '#000',
	        'autoDimensions': false,
	        'width': videoWidth,
	        'height': videoHeight,
	        'content': playercode,
	        'onComplete': function () {
	           $("#fancybox-inner").css({ 'overflow': 'hidden' });
	           VideoJS.setupAllWhenReady();
	        },
	        'onClosed': function () { $("#fancybox-inner").empty(); }
        });
        return false;
			});
		}); // end video stuff
		
		
		
		// trigger inital window resize
		$(window).resize();

});
