var zoomOptions;
var $slider_height = 460;
var ie7 = false;
var zoomelement = '';


function resizeBg() {

		if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
			$bg
				.removeClass()
				.addClass('bgheight');
		} else {
			$bg
				.removeClass()
				.addClass('bgwidth');
		}

}

var theWindow, $bg, aspeczRatio;

var biography_options = {
         content: {
            text: '<img class="throbber" src="/projects/qtip/images/throbber.gif" alt="Loading..." />',
            url: baseUrl+'get_biography.php?artist='+$(this).attr('rel')
         },
         position: {
            corner: {
				target: 'topRight',
				tooltip: 'bottomLeft'
            },
			adjust: {
				screen: true
			}
         },
         hide: { when: 'mouseout', fixed: true, delay: 140 },
		 show: { solo: true},
         style: {
            tip: true,
            padding: '7px 13px',
            name: 'light',
			width: {min: 100, max: 500}
         },
		 api: {
			 beforeShow: function(){
				$('.qtip-wrapper',this.elements.tooltip).css('width','500px');
				$('.qtip-tip',this.elements.tooltip).parent().css('width','500px');
			 }
		}
 }

$(document).ready(function(){
	// IE 7 Detection
	if($.browser.msie && ($.browser.version.substring(0,1)=='7')) {
		ie7 = true;
	}
	
	// ****************
	// Formulare START
	// ****************
	$('input[type="text"]').toggleVal();
	// ****************
	// Formulare END
	// ****************
	
	// ****************
	// Bilderansicht START
	// ****************
	if($('#slides').length) {
		
		$('div.small-navi').show();
		$('div[rel="slided_content"]').each(function(){
			var c = $(this).children().first();
			$(this).data('fullHeight',$(this).height());
			$(this).data('smallHeight',c.height());
			
			$(this).css('height',c.height()).css('overflow','hidden');
		});
		$('.slide_link').live('click',function(e){
			if($(this).data('visible')) {
				var $tmp_smallHeight = $(this).prev('div[rel="slided_content"]').data('smallHeight');
				$(this).prev('div[rel="slided_content"]').animate({height: $tmp_smallHeight});
				$(this).removeData('visible');
			} else {
				var $tmp_fullheight = $(this).prev('div[rel="slided_content"]').data('fullHeight');
				$(this).prev('div[rel="slided_content"]').animate({height: $tmp_fullheight});
				
				if(!$(this).data('resized')) {
					var $tmp_height = parseInt($('.anythingSlider').height())+parseInt($(this).prev('div[rel="slided_content"]').data('fullHeight'));
					$('.anythingSlider').css('height',$tmp_height+'px');
					$(this).data('resized',1);
				}
				
				$(this).data('visible',1);
			}
			e.stopPropagation();
			e.stopImmediatePropagation();
			e.preventDefault();
		});
		$('a.change_image').live('click',function() { 
			$(this).addClass('active').siblings().removeClass('active').parent().parent().find('img').attr('src',$(this).attr('rel'));
			
		});
		
		$('#footer_lightbox').before('<div id="header"><div class="wrapper"><a href="start.php"><img src="../images/header/logo.jpg" /></a></div></div><div class="zoombox wrapper zoom1"><a href="" class="zoom image_big" style="display: block;"><img class="image_small" /></a><a href="Javascript:void(0);" class="button " rel="close">Ansicht schließen</a><br class="clear"/><a href="Javascript:void(0);" class="button zoombutton1 active">x1</a><br class="clear"/><a href="Javascript:void(0);" class="button zoombutton2">x2</a><br class="clear"/><a href="Javascript:void(0);" class="button zoombutton3">x3</a></div>');	
		
	}
	// ****************
	// Bilderansicht ENDE
	// ****************
	
	// *******************
	// LIGHTBOX / ZOOMBOX START
	// *******************
	$('.image_small').mouseover(function(event){
		event.stopPropagation();
	});
	$('a[rel="zoombox"]').live('click',function(){
		var $tmp_height = 0;
		var $tmp_width = 0;
		if($(this).hasClass('button')) {
			$tmp_height = $(this).parent('.contentbox').parent().find('span[rel="zoom1_height"]').text();
			$tmp_width = $(this).parent('.contentbox').parent().find('span[rel="zoom1_width"]').text();
		} else {
			$tmp_height = $(this).parent('div.image').parent('.contentbox').parent().find('span[rel="zoom1_height"]').text();
			$tmp_width = $(this).parent('div.image').parent('.contentbox').parent().find('span[rel="zoom1_width"]').text();
		}
		$('#lightbox_wrapper .image_big').attr('href',$(this).attr('href'));
		
		if($('#lightbox_wrapper img.image_small').height()>$tmp_height) {
			$tmp_height = $('#lightbox_wrapper img.image_small').height();
			$tmp_height = parseInt($tmp_height) +  $('#lightbox_wrapper a.button').length*30;
		}
		
		$('#lightbox_wrapper .zoombox').css('height',(parseInt($tmp_height)+30)+'px');
		$('#lightbox_wrapper').animate({height: $(document).height()},1800,function(){
			zoomOptions = {
				zoomWidth: $tmp_width,
				zoomHeight: $tmp_height,
				position: 'left',
				title: false,
				alwaysOn: true,
				showPreload: true,
				yOffset: 0
			};
			zoomelement = $(".zoombox .zoom").jqzoom(zoomOptions);
		});
		
	
		$('#lightbox_wrapper .zoombox').removeClass('zoom2').removeClass('zoom3').addClass('zoom1');
		$('#lightbox_wrapper .zoombutton1').addClass('active').siblings('a.button').removeClass('active');
		
		var $ele = $(this).siblings('div.text').children('div[rel="slided_content"]');
		$ele.css('height',$ele.data('smallHeight'));
		
		var $l = $(this).siblings('div.text').children('a.slide_link');
		$l.removeData('visible');
		$l.removeData('resized');
		
		return false;
	});

	$('#lightbox_wrapper a.zoombutton1').click(function(event){
		if(!$(this).hasClass('active')) {
			var $slides = $('#slides').data('AnythingSlider');
			var $src = $('span[rel="zoom1"]',$slides.$currentPage).text();
			if($.browser.safari) {
				$src = $('.slider_element:eq('+$slides.currentPage+') span[rel="zoom1"]').text();
			}
			$('#lightbox_wrapper a.image_big').attr('href',$src);
			$('.jqZoomWindow').remove();
			$('div.jqZoomPup').remove();
			zoomelement = $(".zoombox .zoom").jqzoom(zoomOptions);
			
			$(this).parent().removeClass('zoom2').removeClass('zoom3').addClass('zoom1');
			$(this).addClass('active').siblings('a.button').removeClass('active');
		}
		event.stopPropagation();
	});
	$('#lightbox_wrapper a.zoombutton2').click(function(event){
		if(!$(this).hasClass('active')) {
			var $slides = $('#slides').data('AnythingSlider');
			var $src = $('span[rel="zoom2"]',$slides.$currentPage).text();
			if($.browser.safari) {
				$src = $('.slider_element:eq('+$slides.currentPage+') span[rel="zoom2"]').text();
			}
			$('#lightbox_wrapper a.image_big').attr('href',$src);
			$('.jqZoomWindow').remove();
			$('div.jqZoomPup').remove();
			zoomelement = $(".zoombox .zoom").jqzoom(zoomOptions);
			
			$(this).parent().removeClass('zoom1').removeClass('zoom3').addClass('zoom2');
			$(this).addClass('active').siblings('a.button').removeClass('active');
		}
		
		event.stopPropagation();
	});
	$('#lightbox_wrapper a.zoombutton3').click(function(event){
		if(!$(this).hasClass('active')) {
			var $slides = $('#slides').data('AnythingSlider');
			var $src = $('span[rel="zoom3"]',$slides.$currentPage).text();
			if($.browser.safari) {
				$src = $('.slider_element:eq('+$slides.currentPage+') span[rel="zoom3"]').text();
			}
			$('#lightbox_wrapper a.image_big').attr('href',$src);
			$('.jqZoomWindow').remove();
			$('div.jqZoomPup').remove();
			zoomelement = $(".zoombox .zoom").jqzoom(zoomOptions);
			
			$(this).parent().removeClass('zoom1').removeClass('zoom2').addClass('zoom3');
			$(this).addClass('active').siblings('a.button').removeClass('active');
		}
		
		event.stopPropagation();
	});
	
	
	$('.image_small').mousemove(function(event){
		event.stopPropagation();
	});
	
	$('#lightbox_wrapper').click(function(){
		$('#lightbox_wrapper').animate({height: '0%'},800);
		$('.jqZoomWindow').remove();
		$('div.jqZoomPup').remove();
		$(".zoombox .zoom").jqzoom().deactivate();
	});
	$('a[rel="close"]').live('click',function(){
		$('#lightbox_wrapper').animate({height: '0%'},800);
		$('.jqZoomWindow').remove();
		$('div.jqZoomPup').remove();
		$(".zoombox .zoom").jqzoom().deactivate();
	});
	
	// *******************
	// LIGHTBOX ENDE
	// *******************

	// ****************
	// SLIDER START
	// ****************
	if($('#slides').length) {
		// Initialisation fuer direkt angezeigtes Bild
		$('.image-navigation a.back,.image-navigation a.next').show();
		$slider_height = parseInt($('#slides .slider_element:eq(1) div.image img').attr('rel'))+123;
		if($slider_height<460) {
			$slider_height = 460;
		}
		var $slider_content_height = parseInt($('#slides .slider_element:eq(1) div.contentbox',$(this)).height())+123;
		if($slider_content_height>$slider_height) {
			$slider_height = $slider_content_height;
		}
		
		document.title = '* Kunsthandel Draheim - '+$('#slides .slider_element:eq(1) .headline h1').text()+':'+$('#slides .slider_element:eq(1) h2[rel="title"]').text()+' *';
		//$('#slides .slider_element:eq(1) .headline h1').text($tmp_height);
		
		// Bilder in die Lightbox laden
		$('#lightbox_wrapper a.image_big').attr('href',$('#slides .slider_element:eq(1) a[rel="zoombox"]').attr('href'));
		$('#lightbox_wrapper img.image_small').attr('src',$('#slides .slider_element:eq(1)').attr('rel'));
			
		// Navigation in der Lightbox anpassen
		$('#lightbox_wrapper a.zoombutton2').show();
		$('#lightbox_wrapper a.zoombutton3').show();
		if($('#slides .slider_element:eq(1) span[rel="zoom2"]').text()=='') {
			$('#lightbox_wrapper a.zoombutton2').hide();
		}
		if($('#slides .slider_element:eq(1) span[rel="zoom3"]').text()=='') {
			$('#lightbox_wrapper a.zoombutton3').hide();
		}
		
		$('#slides').anythingSlider({
			width: 790+84,
			height: $slider_height,
			resizeContents: false,
			buildNavigation: false,
			autoPlay: false,
			startPanel: 2,
			onInitComplete: function() {
				$('.anythingSlider').css('height',$slider_height+'px');
			},
			onSlideBegin: function(){
				//Reset Text Toggle
				$('div[rel="slided_content"]').each(function(){
					var c = $(this).children().first();
					if(!$(this).data('fullHeight')){
						$(this).data('fullHeight',$(this).height());
					}
					if(!$(this).data('smallHeight')){
						$(this).data('smallHeight',c.height());
					}
					
					$(this).css('height',$(this).data('smallHeight')).css('overflow','hidden');
				});
				$('.slide_link').each(function(){
					$(this).removeData('visible');
					$(this).removeData('resized');
				})
				// End Reset
			},
			onSlideComplete: function($slides){
				
				var $active_element = $slides.$currentPage;
				var $all_elements = $slides.$el;
				var $position = $slides.currentPage;
				var $max_position = $slides.pages;
				
				// Anpassen des Titels
				document.title = '* Kunsthandel Draheim - '+$('.headline h1',$active_element).text()+':'+$('h2[rel="title"]',$active_element).text()+' *';
				
				// Anpassen der Hoehe v2
				$slider_height = parseInt($('a[rel="zoombox"] img',$active_element).attr('rel'))+123;
				if($slider_height<460) {
					$slider_height = 460
				}
				var $slider_content_height = parseInt($('div.contentbox',$active_element).height())+123;
				if($slider_content_height>$slider_height) {
					$slider_height = $slider_content_height;
				}
				//$('.anythingWindow').css('height',$tmp_height+'px');
				
				/*$('.anythingWindow',$slides.$wrapper).css('height',$tmp_height+'px');
				$('.anythingWindow',$slides.$wrapper).closest('.anythingSlider').css('height',$tmp_height+'px');*/
				
				// Bilder in die Lightbox laden
				$('#lightbox_wrapper a.image_big').attr('href',$('a[rel="zoombox"]',$active_element).attr('href'));

				$('#lightbox_wrapper img.image_small').attr('src',$('.slider_element',$active_element).attr('rel'));
				
				// Navigation in der Lightbox anpassen
				$('#lightbox_wrapper a.zoombutton2').show();
				$('#lightbox_wrapper a.zoombutton3').show();
				if($('span[rel="zoom2"]',$active_element).text()=='') {
					$('#lightbox_wrapper a.zoombutton2').hide();
				}
				if($('span[rel="zoom3"]',$active_element).text()=='') {
					$('#lightbox_wrapper a.zoombutton3').hide();
				}
				
				// Wurde etwas geaendert
				if(($position==1) || ($position==$max_position)) {
					$new_position = $position;
					// Zurueck
					if($position==1) {
						var $picture_id = prevImages.shift();
						$.ajax({
							url: baseUrl+'get_image.php',
							data: 'picture='+$picture_id,
							html: 'html',
							context: $all_elements,
							success: function(data){
								// Element erstellen
								var $tmp_element = data;
								$('li:first',$(this)).after($tmp_element);
								$new_position += 1;
								

								// Biographie
								 $('a.biographie_slider').each(function() {
								  $(this).qtip(
									  {
										 content: {
											text: '<img class="throbber" src="/projects/qtip/images/throbber.gif" alt="Loading..." />',
											url: baseUrl+'get_biography.php?artist='+$(this).attr('rel')
										 },
										 position: {
											corner: {
												target: 'topRight',
												tooltip: 'bottomLeft'
											},
											adjust: {
												screen: true
											}
										 },
										 hide: { when: 'mouseout', fixed: true, delay: 140 },
										 show: { solo: true},
										 style: {
											tip: true,
											padding: '7px 13px',
											name: 'light',
											width: {min: 100, max: 500}
										 },
										 api: {
											 beforeShow: function(){
												$('.qtip-wrapper',this.elements.tooltip).css('width','500px');
												$('.qtip-tip',this.elements.tooltip).parent().css('width','500px');
											 }
										}
									  })
								  });
								
								// Slider neu initieren
								$('li:first,li:last',$all_elements).remove();
								$slides.init({startPanel: $new_position});
								$('div.small-navi').show();
							}
						});
					}
					// Vor
					if($position==$max_position) {
						var $picture_id = nextImages.shift();
						$.ajax({
							url: baseUrl+'get_image.php',
							data: 'picture='+$picture_id,
							html: 'html',
							context: $all_elements,
							success: function(data){
								// Element erstellen
								var $tmp_element = data;
								$('li:last',$all_elements).before($tmp_element);
								
								// Biographie
								 $('a.biographie_slider').each(function() {
								  $(this).qtip(
									  {
										 content: {
											text: '<img class="throbber" src="/projects/qtip/images/throbber.gif" alt="Loading..." />',
											url: baseUrl+'get_biography.php?artist='+$(this).attr('rel')
										 },
										 position: {
											corner: {
												target: 'topRight',
												tooltip: 'bottomLeft'
											},
											adjust: {
												screen: true
											}
										 },
										 hide: { when: 'mouseout', fixed: true, delay: 140 },
										 show: { solo: true},
										 style: {
											tip: true,
											padding: '7px 13px',
											name: 'light',
											width: {min: 100, max: 500}
										 },
										 api: {
											 beforeShow: function(){
												$('.qtip-wrapper',this.elements.tooltip).css('width','500px');
												$('.qtip-tip',this.elements.tooltip).parent().css('width','500px');
											 }
										}
									  })
								  });
								  
								  
								
								// Slider neu initieren
								$('li:first,li:last',$all_elements).remove();
								$slides.init({startPanel: $new_position});
							}
						});
					}
				}
				//$('.anythingSlider').css('height',$slider_height+'px');
			}
		});
		$('.image-navigation a.next').click(function(){
			$('#slides').data('AnythingSlider').goForward();
		});
		$('.image-navigation a.back').click(function(){
			$('#slides').data('AnythingSlider').goBack();
		});
	}
	// ****************
	// SLIDER ENDE
	// ****************
	
	
	// *******************
	// Kuenstlerliste START
	// *******************
   $('table.artist_char a.tooltip').each(function(i){
      $(this).qtip(
      {
         content: {
            text: '<img class="throbber" src="/projects/qtip/images/throbber.gif" alt="Loading..." />',
            url: baseUrl+'get_images.php?artist='+$(this).attr('rel')
         },
         position: {
			//target: 'mouse',
            corner: {
				target: 'topMiddle',
				tooltip: 'bottomLeft'
				//tooltip: 'leftMiddle'
				//target: 'rightMiddle'
            },
			adjust: {
				screen: true
			}
         },
         hide: { when: 'mouseout', fixed: true, delay: 240 },
		 show: { solo: true, delay: 250},
         style: {
            tip: true,
            padding: '7px 13px',
            name: 'light',
			swidth: {min: 180, max: 500}
         },
		 api: {
			 beforeShow: function(){
				if(ie7) {
					/*var $tmp_qtip_width = 0;
					var $tmp_img_count = $('table td',this.elements.tooltip).length;
					if($tmp_img_count>3) {
						$tmp_qtip_width = 30;
						$tmp_img_count = $tmp_img_count - 1;
					}
					$tmp_qtip_width = $tmp_qtip_width +(180*$tmp_img_count);
					$('.qtip-wrapper',this.elements.tooltip).css('width',$tmp_qtip_width+'px');
					$('.qtip-tip',this.elements.tooltip).parent().css('width',$tmp_qtip_width+'px');*/
					
					// v2
					var $tmp_qtip_width = $('table',this.elements.tooltip).attr('rel');
					$('.qtip-wrapper',this.elements.tooltip).css('width',$tmp_qtip_width+'px');
					$('.qtip-tip',this.elements.tooltip).parent().css('width',$tmp_qtip_width+'px');
				} else {
					$('.qtip-tip',this.elements.tooltip).parent().css('width','auto');
					$('.qtip-wrapper',this.elements.tooltip).css('width','auto');
				}
			 },
			 onShow: function(){
				if(ie7) {
					var $tmp_qtip_width = $('table',this.elements.tooltip).attr('rel');
					$('.qtip-wrapper',this.elements.tooltip).css('width',$tmp_qtip_width+'px');
					$('.qtip-tip',this.elements.tooltip).parent().css('width',$tmp_qtip_width+'px');
				} else {
					$('.qtip-tip',this.elements.tooltip).parent().css('width','auto');
					$('.qtip-wrapper',this.elements.tooltip).css('width','auto');
				}
			 },
			 onContentUpdate: function(){
				if(ie7) {
					var $tmp_qtip_width = $('table',this.elements.tooltip).attr('rel');
					$('.qtip-wrapper',this.elements.tooltip).css('width',$tmp_qtip_width+'px');
					$('.qtip-tip',this.elements.tooltip).parent().css('width',$tmp_qtip_width+'px');
				} else {
					$('.qtip-tip',this.elements.tooltip).parent().css('width','auto');
					$('.qtip-wrapper',this.elements.tooltip).css('width','auto');
				}
			 },
			 onContentLoad: function(){
				if(ie7) {
					var $tmp_qtip_width = $('table',this.elements.tooltip).attr('rel');
					$('.qtip-wrapper',this.elements.tooltip).css('width',$tmp_qtip_width+'px');
					$('.qtip-tip',this.elements.tooltip).parent().css('width',$tmp_qtip_width+'px');
				} else {
					$('.qtip-tip',this.elements.tooltip).parent().css('width','auto');
					$('.qtip-wrapper',this.elements.tooltip).css('width','auto');
				}
			 }
		}
      })
   });
	// *******************
	// Kuenstlerliste ENDE
	// *******************
	
	// *******************
	// Biographie START
	// *******************
	$('a.biographie_slider').each(function(i){
      $(this).qtip(
      {
         content: {
            text: '<img class="throbber" src="/projects/qtip/images/throbber.gif" alt="Loading..." />',
            url: baseUrl+'get_biography.php?artist='+$(this).attr('rel')
         },
         position: {
            corner: {
				target: 'topRight',
				tooltip: 'bottomLeft'
            },
			adjust: {
				screen: true
			}
         },
         hide: { when: 'mouseout', fixed: true, delay: 140 },
		 show: { solo: true},
         style: {
            tip: true,
            padding: '7px 13px',
            name: 'light',
			width: {min: 100, max: 500}
         },
		 api: {
			 beforeShow: function(){
				$('.qtip-wrapper',this.elements.tooltip).css('width','500px');
				$('.qtip-tip',this.elements.tooltip).parent().css('width','500px');
			 }
		}
      })
	});
	// *******************
	// Biographie ENDE
	// *******************
	// *******************
	// Lightbox START
	// *******************
	$('div.event_images').each(function(){
		$('a',this).fancybox({});
	});
	$('a.event_lightbox').click(function(){
		var $classname = $(this).attr('rel');
		$('div.'+$classname+' a.lightbox:first').trigger('click');

	});
	// *******************
	// Lightbox ENDE
	// *******************

	// *******************
	// Animation START
	// *******************
	$('#startlogo').hide();
	$('#startimage').hide().fadeIn(2600,function(){
		$('#startlogo').fadeIn(2400,function(){
			$('#startlogo').animate({left: '40'},3500, function(){
				window.location.href = $(this).attr('href');
			});
		});
		
		/*$('#startlogo').css('left','-'+$('body').width()+'px').animate({left: '50'},window.location.href = $(this).attr('href');,function(){
			$('#startlogo').animate({left: '50'},1200, function(){
				//window.location.href = $(this).attr('href');
			});
		});*/
	});
	// *******************
	// Animation ENDE
	// *******************
	

	// *******************
	// Veranstaltungen START
	// *******************
	if($('.events .event').length>2) {
		$('.events .event').not(':first').addClass('inactive');
		$('.events .event').mouseover(function(){
			$(this).removeClass('inactive').siblings('.event').addClass('inactive');
		});
	}
	// *******************
	// Veranstaltungen ENDE
	// *******************
	
	// *******************
	// Nivo Slider START
	// *******************
	/*
	var e = $('.event');
	if(e.children('img').length > 1){
		e.nivoSlider({
			effect: 'fold',
			keyboardNav: false
		});
	}
	e = null;
	*/
	var e = $('.event').find('.image');
	for(var j=0;j<e.length;j++){
		var se = $(e[j]) 
		if(se.attr('data-files')){
			var f_string = se.attr('data-files');
			var f_array = f_string.split(";");
			var i_string = "";
			for(var i=0;i<f_array.length;i++){
				i_string += "<img src='"+f_array[i]+"' width=375 height=250 />";
			}
			e[j].insertAdjacentHTML('beforeEnd',i_string);
			
			se.nivoSlider({
				effect: 'fade',
				keyboardNav: false,
				controlNav: false,
				directionNav: false,
				pauseOnHover: false
			});
		}else if(se.children('img').length > 1){
			se.nivoSlider({
				effect: 'fold',
				keyboardNav: false
			});
		}
		se = null;
	}
	e = null;
	// *******************
	// Nivo Slider ENDE
	// *******************

});

/*

			
*/
