// Torsten Frings - Custom JavaScript
function popup(URL) {
	var win1 = window.open (URL,'win1','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,copyhistory=0,width=570,height=630');
	win1.document.close();
	var winl = (screen.width - 570) / 2;
	var wint = (screen.height - 630) / 2;
	win1.moveTo(winl,wint);  
}

// FUNCTIONS
function close_modal() {
	$('#mask').fadeOut(500);  
	$('#modwindow').fadeOut(500);
}  
function show_modal(slideid) { 
	$('#mask').css({ 'display' : 'block', opacity : 0});  
    $('#mask').fadeTo(350,0.8);
    $('#modwindow').fadeIn(350);
}
function setModwindow() {
	var window_width = $(window).width();  
	var window_height = $(window).height(); 
	
	$('.modal_window').each(function(){ 
		var modal_height = $(this).outerHeight();  
		var modal_width = $(this).outerWidth(); 
		var top = (window_height-modal_height)/2;  
		var left = (window_width-modal_width)/2;
		$(this).css({'top' : top , 'left' : left});
	}); 
}



// JQUERY
$(document).ready(function(){
	$.preloadCssImages();
	
	var impressumhref = $('.impressum a').attr('href');
	$('.impressum a').attr('href', 'javascript:popup("'+impressumhref+'")');
	
	//REBUILD SLIDER
	$('.slideswhite').prepend($('#slide-box'));
	$('#browsable').append($('.items'));
	
	if ($('.items li').length < 4) { $('.slides_container').css({'display': 'block'}); } else { $('#slidercontrols').show(); }
	
	
	/* Remove Wordpress-IMG-Title */
	$('img').attr('title', '');
	
	/* MENU */
	var activeItem;
	var currentItem;
	var duration = 200;
	$('li.current-menu-parent ul').show();

	
	$('ul.sub-menu').each(function (index) {
		var submenuWidth = 0;
		$(this).find('li').each(function(indexli) {
			submenuWidth = submenuWidth + $(this).actual('outerWidth');
		});
		$(this).width(submenuWidth);
	});
	
	var twicon = $('.twicon').offset();
	//var fblikePos = twicon.left + 28;
	var fblikePos = twicon.left + $('.twicon').outerWidth();
	var qtranschooserPos = twicon.left + $('.twicon').outerWidth() - 3;
	$('#fblike').css({left: fblikePos+'px'});
	$('#qtranslate-chooser').css({left: qtranschooserPos+'px'});
	
	if($('#qtranslate-chooser').children().length > 1) { 
		$('#qtranslate-chooser').show(); 
		if($(window).width() > 1119) {
			$('#qtranslate-chooser').css({left: '1010px', top: '-38px'});
		}
	}
	
	
	/* SLIDER *///MAX SLIDEHEIGHT
	var maxH = 0;
	var slideLength = $('.slide').length;
	$('.slide').each(function(index) {
		var newH = $('.slide').attr('id','slide'+(index+1)).eq(index).actual('outerHeight');
		if(maxH < newH){
			maxH = newH;	
		}
		if(slideLength > 1) {
			var slidecaption = $('<div class="slidecaption" />');
			var slidecontrols = $('<div class="subcontrols" />');
			$(this).find('img').after(slidecontrols);
			$(this).find('img').after(slidecaption);
			slidecontrols.append('<a href="#" id="sub-prev" class="prev subcontrol-prev">zurück</a><span id="blender-prev" class="subcontrol-prev">zurück</span>');
			slidecontrols.append('<a href="#" id="sub-next" class="next subcontrol-next">weiter</a><span id="blender-next" class="subcontrol-next">weiter</span>');
			//slidecontrols.append('<a href="#" id="sub-next" class="next subcontrol">weiter</a>');
			var captiontext = 'Bild '+(index+1)+' von '+slideLength;
			$(this).find('.slidecaption').text(captiontext);
		}
		
	});
	$('.slides_container').height(maxH);
	
	// Initialize Slides			
	$('#slides').slides({
		generatePagination: false,
		paginationClass: 'items',
		effect: 'slide, fade',
		crossfade: true,
		slideSpeed: 350,
		fadeSpeed: 0,
		animationStart: function(current){
		},
		animationComplete: function(current){
			if(current == 1) { 
				$('a.prev').hide();
				$('span#blender-prev').show().fadeTo(0,0.5);
			} else { 
				$('a.prev').show(); 
				$('span#blender-prev').hide(); 
			}
			if(current == $('div.slide').length) { 
				$('a.next').hide(); 
				$('span#blender-next').show().fadeTo(0,0.5);
			} else { 
				$('a.next').show(); 
				$('span#blender-next').hide(); 
			}
		},
		slidesLoaded: function() {
		}
	});
			
			
	
	//BEGINN SLIDERBAR			
	var thisCarousel = $('.scrollable');  // var this scroller
	var size = 1;
	var bucketCount = $(thisCarousel).find('li').length; // get number of buckets in this scroller
	if (bucketCount > size) {
		$('.navi').show();
		if (bucketCount <= 4) {
			size = bucketCount;
		} else {
			size = 4;
		}
	}
	
	var thisNext = $(thisCarousel).parent().find('a.snext'); // var this next controller
	if (bucketCount < size + 1) { // hide next controller if less than size
		thisNext.addClass('disabled');
	}
	$(thisCarousel).scrollable({next: '.snext', prev: '.sprev'});
	var scrollable = $(thisCarousel).data("scrollable");
	
	// SELFMADE-NAVIGATOR
	$(thisCarousel).find('li').each(function(index) {
		$('.navi').append('<a href="#">'+(index+1)+'</a>');
	});
	$('.navi a').click(function() {
		var index = $(this).text()-1;
		var end = bucketCount - size+1;
		var activeIndex = $('a.currentmask:first').index()+1;
		if (activeIndex == end && (index+1) >= end) {
			return false;
		} else {
			$('.active').removeClass('active');
			$(this).addClass('active');
			
			if(index < end) {
				scrollable.seekTo(index);
			} else {
				scrollable.seekTo((end-1));
			}
			
			if (index >= end) { index = end-1; }
			$('.currentmask').removeClass('currentmask');
			for(var i=index; i< index+4; i++) {
				$('.navi a').eq(i).addClass('currentmask');
			}
		}
	});
	$('.navi a').eq(0).addClass('active');
	$('.navi a').each(function (index) {
		if(index < size) {
			$(this).addClass('currentmask');
		}
	});
	
	$('.sprev').click(function() {
		var currentpager = $('a.active').index();
		var next = currentpager-1;
		
		$('.active').removeClass('active');
		$('.navi a').eq(next).addClass('active');
	});
	$('.snext').click(function() {
		//var currentpager = $('#slidercontrol span a').index($('.active'));
		var currentpager = $('a.active').index();
		var next = currentpager+1;
		
		$('.active').removeClass('active');
		$('.navi a').eq(next).addClass('active');
		
		var limit = bucketCount - size+1;
		if (next >= limit) { next = limit; }
		$('.currentmask').removeClass('currentmask');
		for(var i=next; i< next+4; i++) {
			$('.navi a').eq(i).addClass('currentmask');
		}
	});
	
	scrollable.onSeek(function () {
		if (this.getIndex() >= this.getSize() - size) {
			thisNext.addClass('disabled');
		}
	});
	scrollable.onBeforeSeek(function (event, index) {
		if (this.getIndex() >= this.getSize() - size) {
			$('a.active').removeClass('active');
			thisNext.addClass('disabled');
			if (index > this.getIndex()) {
				return false;
			}
		}
	});
	
	$('.navi a').each(function (index) {
		$(this).text(index+1);	
	});
	
	
	
	$('#sliderbar').width((size*215)-15);
	
	
	
	/* SELFMADE-TOOLTIP */
	$('.items li a').each(function(index) {
		var tipid = 'tip'+index;
		var tooltip = $('<div id="'+tipid+'" />');
		tooltip.addClass('tooltip').appendTo('body');
		
		$('#tiptext'+index).removeClass('hideit').addClass('tooltip-inner').appendTo(tooltip);
		
		$(this).attr('rel', index);
	}).mouseover(function() {
		
		var position = $(this).offset();
		//var tipid = '#tip'+$(this).parent().index();
		var tipid = $(this).attr('rel');
		tipid = '#tip'+tipid;
		var tip = $(tipid);
		
		
		
		var tooltipW = tip.outerWidth();
		var tooltipH = tip.outerHeight();
		
		var toolbtnW = $(this).width();
		
		var tooltipPox = 0;
		var tooltipPoy = 0;
		var Xoffset = 110;
		var Yoffset = 0;
		
		tooltipPoy = position.top-tooltipH+Yoffset;
		tooltipPox = (tooltipW - toolbtnW)/2;
		tooltipPox = position.left-tooltipPox+Xoffset;
		
		tip.css({left: tooltipPox, top: tooltipPoy});
		//tip.css({left: position.left, top: position.top});
		
		var oPos = parseInt(tip.css('top'));
		var sPos = oPos -25;
		tip.css({top: sPos+'px', opacity: 0});
		tip.show();
		
		tip.animate({top: oPos+'px', opacity: 1},200);
	}).mouseout(function() {
		//var tipid = '#tip'+$(this).parent().index();
		var tipid = $(this).attr('rel');
		tipid = '#tip'+tipid;
		$(tipid).fadeOut(60);
	});
	
	$('.items li a').not('.extern').click(function() {
		var index = $('.items li a').index(this);
		/*if(index < 1) { $('a.prev').hide(); } else { $('a.prev').show(); }
		if(index == ($('div.slide').length-1)) { $('a.next').hide(); } else { $('a.next').show(); }
		*/
		
		
		if(index < 1) { 
			$('a.prev').hide();
			$('span#blender-prev').show().fadeTo(0,0.5);
			//$('a#sub-prev').addClass('inactive').fadeTo("fast", .5).removeAttr("href").unbind('click'); 
		} else { 
			$('a.prev').show(); 
			$('span#blender-prev').hide(); 
			//$('a#sub-prev').removeClass('inactive').fadeTo("fast", 1).attr("href","#").bind('click');

		}
		if(index == ($('div.slide').length-1)) { 
			$('a.next').hide(); 
			$('span#blender-next').show().fadeTo(0,0.5);
			//$('a#sub-next').addClass('inactive').fadeTo("fast", .5).removeAttr("href").unbind('click'); 
		} else { 
			$('a.next').show(); 
			$('span#blender-next').hide(); 
			//$('a#sub-next').removeClass('inactive').fadeTo("fast", 1).attr("href","#").bind('click'); 
		}
		setModwindow();
		show_modal(index);
	});
	
	/*$('.subcontrol').bind('click', function() {
		if($(this).hasClass('inactive')) {
		// perform actions upon disabled... show the user he cannot click this link
		return false;
		}

	});*/
	
	
	//$('.items').css({ 'display' : 'block', opacity : 0});  
    $('.items').fadeIn(1000);
	$('.items li').css({'display' : 'block'}); 
	
	
	//CLOSE MODWINDOW
	$('.close_modal').click(function(){
		close_modal();
	});  
	//RESIZE WINDOW 
	$(window).resize(function(){
		setModwindow();
	});
});
