/*

jQuery bxSlider Settings
by David Baudisch (c) 2011

*/

$(document).ready(function(){

//SEARCH FORM
$('#search').focusin(function() {
	$(this).select();
});

$('#search').focusout(function() {
if($(this).attr('value') == ''){
	$(this).attr('value', 'Suche');
}
});

//TOGGLE FUNCTION

(function($){
	$.fn.toggler = function() {
		return this.each(function(){
			var option = {'title' : $(this).attr("title")};
			var settings = {'title' : "Ansehen!"};
			var title = $.extend(settings, option);
			$(this).before('<a class="toggler" title="Klicken, um weiteren Inhalt anzuzeigen">'+ title.title +'</a>');
			$(this).data("realHeight", 20 + parseInt($(this).css('height').substr(0, ($(this).css('height').length)-2))).css({ overflow: "hidden", height: "12px" });
		});
		
	}
})(jQuery);

$('.toggle').toggler();

$('.toggler').toggle(function() {
		$(this).next('.toggle').animate({
		'height': $(this).next('.toggle').data("realHeight")+"px"
		}, 1000, 'swing');
		},
		function() {
		$(this).next('.toggle').animate({
		'height': '12px'
		},1000, 'swing' );
		
	});

//TOGGLE END

//SHUFFLE FUNCTION

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);

//SHUFFLE END

var menuheight = 200;
var shrinkheight = $('#primaryContent').css('height');
var shrinked = 0;
  
//Correct window height
var len = ($('#container').css('height').length)-2;
var maxheight = parseInt($('#container').css('height').substr(0, len));
maxheight = 250 + maxheight;
$('#move-box').css('height', maxheight+'px'); 
  
//DRAG & DROP SETTINGS

$('#container').draggable({
	containment: '#move-box',
	scroll: false,
	handle: '#menu-logo',
	drag: function(){$('#menu-controls').delay(500).stop(false, true);},
	stop: function(){$('#menu-controls').delay(500).stop(true, true);}
});

//DRAG & DROP SETTINGS END

//MENU SHRINKER

function shrink_menu() {
  
$('#mainmenu').animate({
	'height': '0px',
	'margin-top': menuheight+'px'
}, 600, 'swing', function(){

$('.shrinker').css('background-position', 'right center');

$('#container').delay(200).css('top', parseInt($('#container').css('top').substr(0, ($('#container').css('top').length)-2))+menuheight);
$('#mainmenu').delay(200).css('margin-top', '0px');

$('#menu-logo').delay(200).css('-webkit-transition', 'all ease-in-out 1.5s');
$('#menu-logo').delay(200).css('-moz-transition', 'all ease-in-out 1.5s');
$('#menu-logo').delay(200).css('-o-transition', 'all ease-in-out 1.5s');
$('#menu-logo').delay(200).css('background-color', 'rgba(255, 255, 255, 0)');

$('#container').delay(1000).css('width', '120px');

});

shrinked = 1;

}

function expand_check() {

if(parseInt($('#container').css('top').substr(0, ($('#container').css('top').length)-2)) < menuheight) {

var xdiff = menuheight - parseInt($('#container').css('top').substr(0, ($('#container').css('top').length)-2));

$('#container').animate({
	'top': '+='+ xdiff
}, 600, 'swing', function(){expand_menu()});

} else {

expand_menu();

}

}

function expand_menu() {

$('.shrinker').css('background-position', 'left center');

$('#container').delay(1000).css('width', 'auto');
$('#container').css('top', parseInt($('#container').css('top').substr(0, ($('#container').css('top').length)-2))-menuheight);
$('#mainmenu').css('margin-top', menuheight+'px');

$('#mainmenu').animate({
	'height': menuheight+'px',
	'margin-top': '0px'
}, 600, 'swing');

shrinked = 0;

}

//FADER NEW

$('.shrinker').toggle(function() {
	$('#primaryContent').fadeOut(800);
	shrink_menu();
}, function() {
	$('#primaryContent').fadeIn(800);
	expand_check();
	$('#menu-logo').delay(200).css('-webkit-transition', '');
	$('#menu-logo').delay(200).css('-moz-transition', '');
	$('#menu-logo').delay(200).css('-o-transition', '');
	$('#menu-logo').delay(200).css('background-color', 'rgba(236, 236, 236, 1)');
});

//HOVER SETTINGS

$('#menu-logo').hover(function(){
	$('#menu-controls').slideDown(400, 'swing');
	$('#menu-logo').delay(200).css('background-color', 'rgba(236, 236, 236, 1)');
},function(){
	$('#menu-controls').delay(1000).slideUp(400, 'swing');
if(shrinked==1){
	$('#menu-logo').delay(600).css('background-color', 'rgba(236, 236, 236, 0)');
}
});
$('#menu-controls').hover(function(){
	$('#menu-controls').stop(true, true);
	$('#menu-logo').delay(200).css('background-color', 'rgba(236, 236, 236, 1)');
},function(){
	$('#menu-controls').delay(1000).slideUp(400, 'swing');
if(shrinked==1){
	$('#menu-logo').delay(600).css('background-color', 'rgba(236, 236, 236, 0)');
}
});

//HOVER SETTINGS END

//LOAD IMAGES

$('.random').shuffle();

/*var imagesrc = $('.scroll-item').map(function() {
  return $(this).attr("style").substr(16, $(this).attr("style").length-17);
}).get();*/

//var imagesrc = new Array('images/image1.jpg', 'images/image2.jpg');

//$.loadImages(imagesrc, function(){slideshow_init()});

slideshow_init();

//SLIDESHOW START
function slideshow_init() {
  
$('#slides').delay(2000).fadeIn(2000, 'swing');

//if cookie, click on button
if($.cookie('slideshow') == 'true') {
setTimeout(function(){$('.showtoggle')[0].click();},2500);
}

var showspeed = 3000;
var easing = 'swing';
var qty = 10;
var mqty = 1;

	var slider1 = $('#scroll-wrapbox1').bxSlider({
		infiniteLoop: true,
		easing: easing,
		auto: true,
		autoDirection: 'next',
		tickerHover: true,
		autoDelay: 1000,
		speed: showspeed,   
		pause: 5000,   
		pager: false,
		displaySlideQty: qty,
		moveSlideQty: mqty               
	});
	
	var slider2 = $('#scroll-wrapbox2').bxSlider({
		infiniteLoop: true,
		easing: easing,
		auto: true,
		autoDirection: 'next',
		tickerHover: true,
		autoDelay: 500,
		speed: showspeed/2,   
		pause: 8000,   
		pager: false,
		displaySlideQty: qty,
		moveSlideQty: mqty*3                 
	});
	
	var slider3 = $('#scroll-wrapbox3').bxSlider({
		infiniteLoop: true,
		easing: easing,
		auto: true,
		autoDirection: 'next',
		tickerHover: true,
		autoDelay: 2000,
		speed: showspeed,   
		pause: 3000,   
		pager: false,
		displaySlideQty: qty,
		moveSlideQty: mqty                
	});
	
	var slider4 = $('#scroll-wrapbox4').bxSlider({
		infiniteLoop: true,
		easing: easing,
		auto: true,
		autoDirection: 'next',
		tickerHover: true,
		autoDelay: 2000,
		speed: showspeed,   
		pause: 4000,   
		pager: false,
		displaySlideQty: qty,
		moveSlideQty: mqty*2                 
	});
	
	var slider5 = $('#scroll-wrapbox5').bxSlider({
		infiniteLoop: true,
		easing: easing,
		auto: true,
		autoDirection: 'next',
		tickerHover: true,
		autoDelay: 500,
		speed: showspeed,   
		pause: 5000,   
		pager: false,
		displaySlideQty: qty,
		moveSlideQty: mqty                
	});
	
$('.showtoggle').toggle(function(){

	slider1.stopShow();
	slider2.stopShow();
	slider3.stopShow();
	slider4.stopShow();
	slider5.stopShow();
	$(this).css('background-position', 'right center');
	//Set cookie for slideshow
	$.cookie('slideshow','true', { expires: 15 });

}, function() {

	slider1.startShow();
	slider2.startShow();
	slider3.startShow();
	slider4.startShow();
	slider5.startShow();
	$(this).css('background-position', 'left center');
	//Set cookie for slideshow
	$.cookie('slideshow', null);

});

$('.bx-window').css('overflow', 'visible');
$('.bx-prev').css('display', 'none');
$('.bx-next').css('dispaly', 'none');

}
//SLIDESHOW END

});
