

jQuery(document).ready(function() {


/* ACCORDION */


	// use video-js Lib
	
	// VideoJS.setupAllWhenReady();

	//mit flexibler hoehe
	jQuery('.accordion_typ1, .accordion_typ2').accordion({
		active: false,
		header: '.head',
		navigation: true,
		active: '.selected',
		event: 'click',
		//animated: 'easeslide',
		alwaysOpen: false,
		autoheight: false
	});



	function formatText(index, panel) {
	  return index + "";
	}

	jQuery(function () {
	
		jQuery('.anythingSlider_typ1').anythingSlider({
			easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
			autoPlay: false,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
			delay: 5000,                    // How long between slide transitions in AutoPlay mode
			startStopped: false,            // If autoPlay is on, this can force it to start stopped
			animationTime: 500,             // How long the slide transition takes
			hashTags: false,                // Should links change the hashtag in the URL?
			buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
			pauseOnHover: false,            // If true, and autoPlay is enabled, the show will pause on hover
			startText: "Go",             	// Start text
			stopText: "Stop",               // Stop text
			navigationFormatter: formatText // Details at the top of the file on this use (advanced use)
		});

		
	});
	




	function setWidth() {
		/*carousels - breite fuer ul setzen*/
		jQuery('.anythingSlider ul').each(function() {
			var li_count 	= jQuery(this).find('li').length;
			var li_width 	= parseInt( jQuery(this).find('li').width() );
	
			var ul_sum		= li_count*li_width;
			jQuery(this).css('width', ul_sum);
		});
	}
	setWidth();
	

	/* prev-link im carouselpager flexibel machen */
	/* gezählt wird die pager-link-anzahl, nicht die li's, wegen der cloned li's */
	jQuery('.workspace .anythingSlider').each(function() {
		var pL_count 	= jQuery(this).find('.thumbNav a').length;
		var pL_width 	= parseInt( jQuery(this).find('.thumbNav a').css('width') );
		var pL_margin	= parseInt( jQuery(this).find('.thumbNav a').css('margin-right') );
		var thNav_right	= parseInt( jQuery(this).find('.thumbNav').css('right') );

		var pl_sum		= pL_count*(pL_width+pL_margin)+thNav_right+pL_margin;
		jQuery(this).find('.back').css('right', (pl_sum+'px'));
	});
	

	jQuery('#carouselDiv').addClass("accordCarouselLoad");
	jQuery('a#carousel').click();

	jQuery('a#carousel').click(function() {
		jQuery('.thumbNav').children().click();
	});								




	// detailansicht slider

	jQuery('a.close, .detail').click(function() {
	    jQuery('.detail').fadeOut('fast', function() {
        	// Animation complete
      	});
		jQuery('#carousel .box h3').css('color','#000');
		return false;
	});
	jQuery('#carousel').click(function() {
		jQuery('#carousel .box h3').css('color','#000');
	});

	jQuery(".inhalt .pic img").click(function() {
	    jQuery(this).parent().parent().parent().find('.detail').fadeIn('fast', function() {
        	// Animation complete
      	});
		jQuery('#carousel .box h3').css('color','#FFF');
	});


	var ct_slides = jQuery('.thumbNav').children().size();
	var slider_pos = (280+((5-ct_slides)*15));

	if(ct_slides < 5) {
		jQuery('.thumbNav').css('left',slider_pos+'px')
	}
	
	
	
	var teaser = jQuery("#teaserimage");
	var content = jQuery("#all");
	
	content.append(teaser);
});


var video = (function() {
	var myVideo,
	mp4Video,
	oggVideo,
	webmVideo,
	currentVideo,
	container,
	videoEnded = function(){},
	videoPaused = function(){},
	firstCall = false,
	videoIsRunning = false;
	return {
		create: function(el, file, width, height){ 
			container = $(el).parent();
			
			var uniqueId = 'video_' + String((new Date()).getTime()).replace(/\D/gi,'');
			
			width = width || "100%";
			height = height || "auto";
			
			var poster = container.children('img');
			var controls = '';
			
			if(poster == undefined){
				poster = "";
			}
			
			if(poster.data('dimension')){
				width = poster.width();
				height = poster.height();
			}
			
			myvideo = document.createElement('video');
			myvideo.setAttribute('width', width);
			myvideo.setAttribute('height', height);
			myvideo.id = uniqueId;
			
			if(poster.data('controls')){
				myvideo.controls = 'controls';
			} else {
				myvideo.controls = false;
			}
			
			mp4Video = document.createElement('source');
			mp4Video.setAttribute('src', file + '.mp4');
			mp4Video.setAttribute('type',"video/mp4");
			
			oggVideo = document.createElement('source');
			oggVideo.setAttribute('src', file + '.ogv');
			oggVideo.setAttribute('type',"video/ogg");
			
			webmVideo = document.createElement('source');
			webmVideo.setAttribute('src', file + '.webm');
			webmVideo.setAttribute('type',"video/webm");
			
			myvideo.appendChild(mp4Video);
			myvideo.appendChild(webmVideo);
			myvideo.appendChild(oggVideo);
			
			container.append(myvideo);
			
			container.css('z-index', 1000000000);
			
			$(el).css('display','none');
			
			var videos = document.getElementsByTagName('video');
			for (var i = 0; i < videos.length; i++) {
					videos[i].pause();
			}
					
			$('#' + uniqueId)[0].play();
			
			currentVideo = $('#' + uniqueId)[0];
			this.setToggle();
			videoIsRunning = true;
			firstCall = true;
			
			currentVideo.onended = function(e) {
			    //alert('ok');
			    //videoEnded.call();
			}
			
			currentVideo.onpause = function(e) {
				if(currentVideo.paused == true){
					videoPaused.call();
				}
			}
			
		},
		
		setToggle: function(){
			
			$('video').each(function(){
				var containment = $(this).parent();
				var thisVideo = $(containment).children('video')[0];
				
				$(containment).unbind('click');
				$(containment).click(function(){
					
					//console.log(thisVideo);
					
					if(currentVideo == thisVideo){
						if(videoIsRunning){
							thisVideo.pause();
							videoIsRunning = false;
						} else {
							thisVideo.play();
							videoIsRunning = true;
						}
					} else {
						var videos = document.getElementsByTagName('video');
						for (var i = 0; i < videos.length; i++) {
								videos[i].pause();
						}
						
						thisVideo.play();
						
					}
					
					currentVideo = thisVideo;
					
					if(firstCall){
						thisVideo.play();
						firstCall = false;
					}
					
					return false;
				});

			});
			
		},
		
		addEndedCallback: function(fn){
			videoEnded = fn;
		},
		
		addPausedCallback: function(fn){
			videoPaused = fn;
		},
		
		resetWrapper: function(el){
			el.unbind('click');
			el.children('video').remove();
			el.children('img').css('display',' block');
		}
	}
})();

