
/*
 * stickyfloat - jQuery plugin for verticaly floating anything in a constrained area
 * 
 * Example: jQuery('#menu').stickyfloat({duration: 400});
 * parameters:
 * 		duration 	(200)	 - the duration of the animation
 *		startOffset (number) - the amount of scroll offset after the animations kicks in
 *		offsetY		(number) - the offset from the top when the object is animated
 *		lockBottom	(true)	 - set to false if you don't want your floating box to stop at parent's bottom
 *		delay		(0)		 - delay in milliseconds  until the animnations starts
		easing		(linear) - easing function (jQuery has by default only 'swing' & 'linear')
 * $Version: 08.10.2011 r2
 * $Version: 05.16.2009 r1
 * Copyright (c) 2009 Yair Even-Or
 * vsync.design@gmail.com
 */
(function($){
	$.fn.stickyfloat = function(options, lockBottom){
		var $obj 				= this,
			doc					= $(document),
			opts, bottomPos, pastStartOffset, objFartherThanTopPos, objBiggerThanWindow, newpos, checkTimer, lastDocPos = doc.scrollTop(),
			parentPaddingTop 	= parseInt($obj.parent().css('padding-top')),
			startOffset 		= $obj.position().top;
		
		$.extend( $.fn.stickyfloat.opts, options, { startOffset:startOffset, offsetY:parentPaddingTop} );
		opts = $.fn.stickyfloat.opts;
		$obj.css({ position: 'absolute', left: $obj.position().left,top: $obj.position().top  });
		
		if(opts.lockBottom){
			bottomPos = $obj.parent().height() - $obj.outerHeight() + parentPaddingTop; //get the maximum scrollTop value
			if( bottomPos < 0 )
				bottomPos = 0;
		}
		
		function checkScroll(){
			if( opts.duration > 40 ){
				clearTimeout(checkTimer);
				checkTimer = setTimeout(function(){
					if( Math.abs(doc.scrollTop() - lastDocPos) > 0 ){
						lastDocPos = doc.scrollTop();
						initFloat();
					}
				},40);
			}
			else initFloat();
		}
		
		function initFloat(){
			$obj.stop(); // stop all calculations on scroll event
			
			pastStartOffset			= doc.scrollTop() > opts.startOffset;	// check if the window was scrolled down more than the start offset declared.
			objFartherThanTopPos	= $obj.offset().top > startOffset;	// check if the object is at it's top position (starting point)
			objBiggerThanWindow 	= $obj.outerHeight() < $(window).height();	// if the window size is smaller than the Obj size, then do not animate.
			
			// if window scrolled down more than startOffset OR obj position is greater than
			// the top position possible (+ offsetY) AND window size must be bigger than Obj size
			if( (pastStartOffset || objFartherThanTopPos) && objBiggerThanWindow ){ 
				newpos = (doc.scrollTop() -startOffset + opts.offsetY );

				if ( newpos > bottomPos )
					newpos = bottomPos;
				if ( doc.scrollTop() < opts.startOffset ) // if window scrolled < starting offset, then reset Obj position (opts.offsetY);
					newpos = parentPaddingTop;
				
				$obj.delay(opts.delay).animate({ top: newpos }, opts.duration , opts.easing );
			}
		}
		
		$(window).scroll(checkScroll);
	};
	
	$.fn.stickyfloat.opts = { duration:200, lockBottom:true , delay:0, easing:'linear' };
})(jQuery);



jQuery(document).ready(function() {


			//setup tooltips for footer links
			jQuery('#footer a[title]').qtip({
				content: false,
				position: {
				   my: 'left bottom', // Use the corner...
				   at: 'right top' // ...and opposite corner
				},
				style: {
				   classes: 'ui-tooltip-shadow ui-tooltip-dark'
				}
			});

			/*
			set up background resizer
			*/
			jQuery(function($){
				var cookieName = 'gv_back';
				var cookieInterval = 5; //time in minutes
				
				//read cookie
				var cookieSet = jQuery.cookie(cookieName);
				var expDate = new Date(); // create a new date object
				expDate.setMinutes(expDate.getMinutes()+cookieInterval); //set expiration date to 10 minutes from now
					
				
				if(cookieSet == null) { 
				//if(true) {
					// expiration date
					cookieSet = 0;
					jQuery.cookie(cookieName, 0,{path: '/',expires: expDate});
				//alert('cookie '+cookieName+' set:'+vars.current_slide);
				} else {
					//alert('cookie value:'+cookieSet);
					//use random bg
					
				}
				
				jQuery.supersized({
					// Functionality
					slideshow               :   1,			// Slideshow on/off
					autoplay				:	0,			// Slideshow starts playing automatically
					start_slide             :   parseInt(cookieSet),			// Start slide (0 is random)
					stop_loop				:	0,			// Pauses slideshow on last slide
					slide_interval          :   3000,		// Length between transitions
					transition              :   1, 			// 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
					transition_speed		:	1000,		// Speed of transition
					new_window				:	1,			// Image links open in new window/tab
					pause_hover             :   0,			// Pause slideshow on hover
					keyboard_nav            :   1,			// Keyboard navigation on/off
					performance				:	1,			// 0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
					image_protect			:	1,			// Disables image dragging and right click with Javascript
					
															   
					// Size & Position						   
					min_width		        :   0,			// Min width allowed (in pixels)
					min_height		        :   0,			// Min height allowed (in pixels)
					vertical_center         :   1,			// Vertically center background
					horizontal_center       :   1,			// Horizontally center background
					fit_always				:	0,			// Image will never exceed browser width or height (Ignores min. dimensions)
					fit_portrait         	:   1,			// Portrait images will not exceed browser height
					fit_landscape			:   0,			// Landscape images will not exceed browser width
				
					// Components							
					slide_links				:	'blank',	// Individual links for each slide (Options: false, 'number', 'name', 'blank')
					thumb_links				:	0,			// Individual thumb links for each slide
					thumbnail_navigation    :   0,			// Thumbnail navigation
					slides  :  	[ 
						{image : 'fileadmin/templates/relaunch/image/bg/linien.jpg', title : 'Linien am Sonnendeck',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/sterne.jpg', title : 'Wandmalerei',thumb : 'fileadmin/templates/relaunch/image/bg/sterne.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/schiff.jpg', title : 'Historische Tapete in der Puppenstube',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/putz.jpg', title : 'Aussenwand an der Speckstrasse ',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						//{image : 'fileadmin/templates/relaunch/image/bg/farben.jpg', title : 'Sonnendeck',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/wand.jpg', title : 'Aussenwand Butze',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/tapete_01.jpg', title : 'Historische Tapete',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/tapete02.jpg', title : 'Historische Tapete',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/tapete_03.jpg', title : 'Historische Tapete',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/tapete_04.jpg', title : 'Historische Tapete: Orange',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
						{image : 'fileadmin/templates/relaunch/image/bg/tapete_05.jpg', title : 'Historische Tapete',thumb : 'fileadmin/templates/relaunch/image/bg/lila.jpg'},	
					],
					// Theme Options			   
					progress_bar			:	0,			// Timer for each slide							
					mouse_scrub				:	0

				});
				//set cookie				
				var slideTitle = api.getField('title');
				jQuery('#infoslide').attr('title',slideTitle);
				//alert('set value:' + vars.current_slide);
				jQuery.cookie(cookieName, vars.current_slide+1,{path: '/',expires: expDate});
				jQuery('#infoslide').qtip({
					content: {
					   text: api.getField('title'), 
					   title: {
						  text: 'Tapete' ,
						  button: false
					   }
					},
					position: {
					   my: 'left bottom', // Use the corner...
					   at: 'right top' // ...and opposite corner
					},
					style: {
					   classes: 'ui-tooltip-shadow ui-tooltip-dark'
					},
					events: {
						show: function(event, api_tool) {
							// Update the content of the tooltip on each show
							//var target = $(event.originalEvent.target);
							api_tool.set('content.text', api.getField('title'));							
						}
					}
				});
				
				jQuery('#nextslide').click(function() {
					//alert('click:'+vars.current_slide);
					api.nextSlide();
					var slideTitle = api.getField('title');
					jQuery('#infoslide').attr('title',slideTitle);
					//api.playToggle();
					jQuery.cookie(cookieName, vars.current_slide+1,{path: '/',expires: expDate});
				});
				jQuery('#prevslide').click(function() {
					api.prevSlide();
					var slideTitle = api.getField('title');
					jQuery('#infoslide').attr('title',slideTitle);
					jQuery.cookie(cookieName, vars.current_slide,{path: '/',expires: expDate});
				});						    	
		    });
			
			
			/*set up colorbox for links*/
			jQuery("a.lightbox").colorbox({
				current: "{current} von {total}",
				previous: "vorher",
				next: "n&auml;chstes",
				close: "schliessen",
				slideshow:true,
				slideshowAuto: true,
				slideshowSpeed: 2500,
				slideshowStart: "start",
				slideshowStop: "stop",
			
			});
			
			/*
			set up masory
			*/
			if(jQuery('#grid')) {
				var $container = jQuery('#grid');
	  
				$container.imagesLoaded( function(){
				  $container.masonry({
					itemSelector : '.box',
					 columnWidth: 220,
					 gutterWidth: 30
				  });
				});
			
				/*setup infinite scroll for cal archiv*/
				if(jQuery('.pagebrowser').length > 0 && false) {
					$container.infinitescroll({
		
						// callback		: function () { console.log('using opts.callback'); },
						navSelector  	: "a.url:last",
						nextSelector 	: "a.url:last",
						itemSelector 	: "#grid div.box",
						dataType	 	: 'html',
						donetext  : 'No more pages to load.',
						img : 'fileadmin/templates/relaunch/image/loading.gif',
						bufferPx: 200,
						//debug: false,
						errorCallback: function() { 
							// fade out the error message after 2 seconds
							jQuery('#infscr-loading').animate({opacity: .8},2000).fadeOut('normal');   
						  }
						// behavior		: 'twitter',
						// appendCallback	: false, // USE FOR PREPENDING
						// pathParse     	: function( pathStr, nextPage ){ return pathStr.replace('2', nextPage ); }
					}, 
					function( newElements ) {
						var $newElems = jQuery( newElements );
						//alert(newElems)
						// ensure that images load before adding to masonry layout
						$newElems.imagesLoaded(function(){
						  $container.masonry( 'appended', $newElems, true ); 
						});
						//window.console && console.log('context: ',this);
						//window.console && console.log('returned: ', newElements);

					}

					);
					jQuery('.pagebrowser').hide();
				}
			}
			//set up random cal events for startpage	
			var conti = '#grid_cal';
			if(jQuery(conti)) {
				if(jQuery(conti + ' .box').length > 2) {
					
					/*
					var pause = 12000; // define the pause for each tip (in milliseconds) 
					var length = $(conti + " .box").length; 
					var temp = -1;		
					var temp2 = -1;		

					 function getRan(){
						// get the random number
						var ran = Math.floor(Math.random()*length) + 1;
						return ran;
					};
					function show(){
						var old_temp = temp;
						var old_temp2 = temp2;
						var ran = getRan();
						// to avoid repeating
						while (ran == temp2 || ran == temp){
							ran = getRan();
						}; 
						temp = ran;
						var ran2 = getRan();
						// to avoid repeating
						while (ran2 == temp2 || ran2 == temp){
							ran2 = getRan();
						}; 
						temp2 = ran2;
						//$(conti + " .box").hide();	
						$(conti + " .box:nth-child(" + old_temp + ")").fadeOut("fast");		
						$(conti + " .box:nth-child(" + old_temp2 + ")").fadeOut("fast");		
						$(conti + " .box:nth-child(" + ran + ")").fadeIn("fast");		
						$(conti + " .box:nth-child(" + ran2 + ")").fadeIn("fast");		
					};
					
					show(); setInterval(show,pause);
					*/
					$("#grid_cal").scrollable({vertical: true,circular: true, mousewheel: true}).autoscroll({
						interval: 9000		
					});
				}
				
			}
			
			
			/*
				set up pageslider
			*/
			//jQuery("#slide-left").pageSlide({ width: "350px", direction: "left" });
			
			//jQuery('#subcontent').stickyfloat({duration: 400});
			/*set up scroll totop*/
		//jQuery('#subcontent').css({ position: 'absolute', left: jQuery('#footer').position().left + jQuery('#footer').width(),bottom: jQuery('#footer').position().top  });
		jQuery(function() {
			jQuery(window).scroll(function() {
				if(jQuery(this).scrollTop() != 0) {
					jQuery('#toTop').fadeIn();	
				} else {
					jQuery('#toTop').fadeOut();
				}
			});
		 
			jQuery('#toTop').click(function() {
				jQuery('body,html').animate({scrollTop:0},800);
			});	
		});
		
		// if content container smaller than content menu set content container to same height as content menu
		if(jQuery('#content_row').height() < jQuery('#content_menu').height()) {
			jQuery('#content_row').height(jQuery('#content_menu').height());
			jQuery('#content_row').css('padding-bottom',0);
		}
		
		if(jQuery('.csc-header h2').length > 0) {
			

			jQuery('.csc-header h2').click(function(){
				jQuery(this).toggleClass("active").parent().next('.acc-container').slideToggle("slow");
				return false; //Prevent the browser jump to the link anchor
			});			
		}

});



