//keep this line
function login( email, password, callback )
{
	$.post( $env.root_url + "/ajax.php?command=login", { email: email, password: password }, function( response )
	{
		if( response == true )
		{
			window.parent.$( window.parent.document ).trigger( 'user.login' );
		}
		
		if( callback ) callback( response );
	}, 'json' );
}

function logout( callback )
{
	$.post( $env.root_url + "/ajax.php?command=logout", null, function( response )
	{
		window.parent.$( window.parent.document ).trigger( 'user.logout' );
		if( callback ) callback();
	}, 'json' );	
}

function userdata( fields, callback )
{
	$.post( $env.root_url + "/ajax.php?command=userdata", { fields: fields }, function( data )
	{
		window.parent.$( window.parent.document ).trigger( 'user.data' );
		if( callback ) callback( data );
	}, 'json' );		
};
//keep this line
var prevLink, menusToClose = [], menuCloseTimeout, loginForm, loader, lastScrollPosition, /*closeCartTimeout, */nextPostloadStart = 15, isPostLoading = false;

function confirm( question, title, callback )
{
	$( '<div title="' + title + '">' + question + '</div>' ).dialog({
		modal		: true,
		resizable	: false,
		buttons		: 
		{
			OK: function()
			{
				if( callback )
					callback( true );
					
				$(this).dialog( 'destroy' );
			},
			
			Abbrechen: function()
			{
				if( callback )
					callback( false );
					
				$(this).dialog( 'destroy' );
			}
		}
	});
}

function showDetails( href )
{
	var link = $( '<a href="' + href + '?ajax" class="fancybox-details"></a>' );
	$( 'body' ).append( link );
	updateFancyBox();
	link.click();
	link.remove();
	
}
 
function postLoad()
{
	var bookList = $( 'div.book-container' );
	isPostLoading = true;
	//loader.show();
	$.post( $env.root_url + 'ajax.php?command=postload&category=' + $books.category , { start: nextPostloadStart }, function( data )
	{
		//loader.hide();
		
		var data = $( data ); 
		bookList.append( data );
		bookList.append( bookList.find( 'div.clear' ) );
		updateDraggables();
		updateFancyBox();
		updateTooltips();
		isPostLoading = false;
	} );
	nextPostloadStart += 15;
}

function addURLTrenner(href)
{
	if(href.match(/\?/))
	{
		href += '&'
	}
	else
	{
		href += '?'
	}
	return href;
}

function updateFancyBox( include )
{

	var targets = [ $( document ), $( include ) ];
	
	for( var i in targets )
	{
		targets[ i ].find( 'a.fancybox-details' ).each( function( index, link )
		{
			link = $( link );
			var href = link.attr('href');
			if(!href.match(/\?ajax/))
			{
				href = addURLTrenner(href);
				href += 'ajax';
			}
			if(link.hasClass('nocache'))
			{
				href = addURLTrenner(href);
				href += new Date().getTime();
			}
			link.fancybox(
			{
				href : href					,
				titleShow		: false		,
				showCloseButton	: false		,
				autoDimensions	: false		,
				width			: 740		,
				height			: 600		,
				padding			: 20		,
				
				onComplete: function()
				{
					if( $( '#fancybox-outer' ).find( 'div.tool-bar' ).length == 0  )
						$( '#fancybox-outer' ).append( $( '#fancybox-inner' ).find( 'div.tool-bar' ) );


					
					$( '#fancybox-inner' ).find( '.cart-link' ).click( function()
					{
						var self = $( this );
						$.post($env.root_url + 'ajax.php?command=add_to_cart&id=' + self.attr( 'id' ).split( '-' )[ 1 ], null, function(data)
						{
							self.show( 'pulsate' );
							updateCart();
						});	
					});
					
					$( '#fancybox-outer' ).find( 'img.close' ).one( 'click', function(){ $.fancybox.close(); } );
					$( '#fancybox-inner' ).find( 'a.fancybox' ).fancybox(
					{
						titleShow		: true		,
						showCloseButton	: false		,
						showNavArrows	: false		,
						onComplete  	: function()
						{
							$( '#fancybox-img' ).wrap( '<a href="' + link.attr( 'href' ) + '" class="fancybox-details"/>' );
							$( '#fancybox-img' ).attr( 'title', 'Klicken Sie auf das Bild um zurück zur Hauptansicht zu gelangen.' );
							
							updateFancyBox();
						}
					});
					//updateFancyBox(); // das war einer zuviel, deswegen kam man von großansichten nicht mehr zurück!
				}
			});
		});
	
		targets[ i ].find( 'a' ).each( function( index, link )
		{
			link = $( link );
			var href = link.attr('href');
			if(!href) return;
		
			if(!href.match(/\?ajax/))
			{
				href = addURLTrenner(href);
				href += 'ajax';
			}
			if(link.hasClass('nocache'))
			{
				href = addURLTrenner(href);
				href += new Date().getTime();
			}
			
			var classes = link.attr( 'class' ).split( ' ' );
			
			for( var j = 0; j < classes.length; j++ )
			{
				var cl = classes[ j ].split( '-' );
				
				if( cl[ 0 ] == 'fancybox' && cl[ 1 ] !== 'details' )
				{
					link.fancybox(
					{
						href : href,
						titleShow		: false			,
						showCloseButton	: false			,
						autoDimensions	: false			,
						width			: parseInt( cl[ 1 ] ),
						height			: parseInt( cl[ 2 ] ),
						padding			: 20			,
						onComplete		: function()
						{
							if( $( '#fancybox-outer' ).find( 'div.tool-bar' ).length == 0 )
								$( '#fancybox-outer' ).append( $( '#fancybox-inner' ).find( 'div.tool-bar' ) );
	
							$( '#fancybox-outer' ).find( 'img.print' ).unbind( 'click' ).click( function()
							{
								$( '#fancybox-inner div:first' ).printElement(
										{
											overrideElementCSS:
												[
												 	$env.skin_url + '/css/print.css'
												]
										});
							} );
								
							$( '#fancybox-outer' ).find( 'img.close' ).one( 'click', function(){ $.fancybox.close(); } );
							
							updateFancyBox();
						}
					});
					
					break;
				}
			}
		});
	}
}

function updateDraggables()
{
	var books = $( 'div.book div.book-wrap a img, .main-image' );
	
	for( var i = 0; i < books.length; i++ )
	{
		var book = $( books[ i ] );
		
		if( !book.parent().parent().find( 'div.price' ).hasClass( 'dl-price' ) )
		{
			book.draggable( 
			{
				opacity	: 0.7,
				helper	: 'clone',
				cursor	: 'move',
				distance: 10,
				start	: function( event, ui )
				{
					ui.helper.css( 'z-index', '999' );
					//openCart();
				},
				
				stop: function( event, ui )
				{
					//closeCartAfterTimeout(3000);
				}
			} );
		}
	}
	
	var bubbles = $( 'div.book div.book-wrap div.price' ).unbind( 'click' ).click( function()
	{
		var bubble = $( this );
			bubble.disableSelection();
				
		if( bubble.hasClass( 'dl-price' ) )
		{
			var id = bubble.attr('id').split('-')[2];
			// window.open( $env.root_url + 'index.php?context=redirect&id=' + id );
			alert('Der Download ist zur Zeit leider nicht möglich.');
		}
		else
		{
			var id = bubble.attr('id').split('-')[1];
			
			//showLoaderOn( $( 'div.cart' ), 'cart' );
			
			$.post($env.root_url + 'ajax.php?command=add_to_cart&id=' + id, null, function(data)
			{
				bubble.find( '.check-icon' ).show( 'pulsate' );
				bubble.find( 'div.icon' ).show( 'pulsate' );
				
				//openCart();
				//closeCartAfterTimeout(3000);
				updateCart();
			});	
		}
	});
	
	/*
	 * Add to Cart from "Bestseller" and "Neu"
	 */
	var bubbles = $('div.right-col div.right-box div.content div.item table.add-to-cart-link').unbind( 'click' ).click( function()
	{
		var bubble = $( this );
			bubble.disableSelection();
				
		if( bubble.hasClass( 'dl-price' ) )
		{
			var id = bubble.attr('id').split('-')[2];
			window.open( 'http://www.keytechnology.ch/sybex/Basket.asp?Action=INSERT&Product=' + id + '&Delivery=1&BCD=0&INV=0' );
		}
		else
		{
			var id = bubble.attr('id').split('-')[1];
			$.post($env.root_url + 'ajax.php?command=add_to_cart&id=' + id, null, function(data)
			{
				bubble.find( '.add-to-cart' ).show( 'pulsate' );
				updateCart();
			});	
		}
	});
	
	bubbles.each( function( index, bubble )
	{
		bubble = $( bubble );
		
		if( bubble.hasClass( 'liveshopping' ) && bubble.find( 'div.ls-box' ).length == 0 )
		{
			var lsBox = $( '<div class="ls-box"><div class="timer"></div><div class="value">Alter Preis: ' + bubble.find( 'input[name=price]' ).val() + ' €</div><div class="stock"></div></div>' );
			bubble.append( lsBox );
			bubble.bind( 'mouseenter', function()
			{
				if($.browser.msie){
					lsBox.show();
				} else {
					lsBox.fadeIn( 'fast' );
				}
				lsBox.position(
				{
					of: bubble,
					my: 'right bottom',
					at: 'left top',
					offset: '100 44'
				});
				lsBox.css( 'z-index', '9999999' );
			});
			
			bubble.bind( 'mouseleave', function()
			{
				if($.browser.msie){
					lsBox.hide();
				} else {
					lsBox.fadeOut( 2000 );
				}
			});
			
			lsBox.bind( 'mouseenter', function()
			{
				//lsBox.fadeOut( 'fast' );
			});
			
			lsBox.hide();
			
		}
	});
}

/*function showLoaderOn( target, type )
{
	var loader = $(target).find('.loader.'+type);
	if (!loader.length)
	{
		var loader = $( '<img/>' );
		target.append( loader );
		
		loader.attr( 'src', $env.skin_url + '/images/loader_48x48_' + type + '.gif' );
		loader.addClass( type );
		loader.addClass( 'loader' );
		loader.css( 'z-index', 1 );
	}
	loader.position( { of: target, my: 'center center', at: 'center center' } );
	loader.show();		
}

function hideLoaderOn( target )
{
	target.find( '.loader' ).hide();
}*/

function updateMainCart(){
	//showLoaderOn( $( 'div.cart' ), 'cart' );
	//var right = $( 'div.cart' ).css( 'right' );
	$.post($env.root_url + 'ajax.php?command=get_maincart_contents', null, function(data){
		//hideLoaderOn( $( 'div.cart' ) );
		
		
		var cart = $('div.maincart').replaceWith(data);
		cart = $('div.maincart');
		
		cart.find('a.checkout').attr('href', $env.root_url + "?context=checkout&random=" + new Date().getTime());
		
		var cartItems = cart.find('.cart-item');
		
		cartItems.find('td.quantity').disableSelection();
		cartItems.find('td.quantity span.ui-icon-right').css('cursor', 'pointer');
		cartItems.find('td.quantity span.ui-icon-right').click(function(){
			var qty = $(this).parent();
			//showLoaderOn( $( 'div.cart' ), 'cart' );
			$.post($env.root_url + 'ajax.php?command=set_quantity&id=' + qty.parent().attr('id').split('-')[1] + '&qty=' + (parseInt(qty.text().replace(/x|«|»/, '')) + 1), null, function(data){
				//hideLoaderOn( $( 'div.cart' ) );
				updateCart();
				updateMainCart();
			});
		});
		
		cartItems.find('td.quantity span.ui-icon-left').css('cursor', 'pointer');
		cartItems.find('td.quantity span.ui-icon-left').click(function(){
			var qty = $(this).parent();
			//showLoaderOn( $( 'div.cart' ), 'cart' );
			$.post($env.root_url + 'ajax.php?command=set_quantity&id=' + qty.parent().attr('id').split('-')[1] + '&qty=' + (parseInt(qty.text().replace(/x|«|»/, '')) - 1), null, function(data){
				//hideLoaderOn( $( 'div.cart' ) );
				updateCart();
				updateMainCart();
			});
		});
		
		$('.contents').droppable({
			accept: '.cart-item',
			drop: function(event, ui){
				var id = ui.draggable.attr('id').split('-')[1];
				
				//showLoaderOn( $( 'div.cart' ), 'cart' );
				
				$.post($env.root_url + 'ajax.php?command=remove_from_cart&id=' + id, null, function(data){
					//hideLoaderOn( $( 'div.cart' ) );
					updateCart();
					updateMainCart();
				});
			}
		});
		
		updateFancyBox();
		
		cart.find('a.checkout').fancybox({
			type: 'iframe',
			titleShow: false,
			showCloseButton: false,
			autoDimensions: false,
			width: 740,
			height: 400,
			padding: 0,
			scrolling: 'no',
			onComplete: function(){
				$('#fancybox-outer').append($('#fancybox-inner').find('div.tool-bar'));
				$('#fancybox-outer').find('img.close').one('click', function(){
					$.fancybox.close();
				});
			},
			onClosed: function(){
				cart.find('a.checkout').attr('href', $env.root_url + "?context=checkout&random=" + new Date().getTime());
			}
		});
		updateTooltips();
	});
}

function updateCart()	
{
	$.post( $env.root_url + 'ajax.php?command=get_cart_contents', null, function( data )
	{
		var cart = $('div.cart').replaceWith(data);
		cart = $('div.cart');

		cart.find('a.checkout').attr('href', $env.root_url + "?context=checkout&step=2&random=" + new Date().getTime());
		
		var cartItems = cart.find('.cart-item');
		
		updateFancyBox();
		
		cart.find('a.checkout').fancybox({
			type: 'iframe',
			titleShow: false,
			showCloseButton: false,
			autoDimensions: false,
			width: 740,
			height: 400,
			padding: 0,
			scrolling: 'no',
			onComplete: function(){
				$('#fancybox-outer').append($('#fancybox-inner').find('div.tool-bar'));
				$('#fancybox-outer').find('img.close').one('click', function(){
					$.fancybox.close();
				});
			},
			onClosed: function(){
				cart.find('a.checkout').attr('href', $env.root_url + "?context=checkout&step=2&random=" + new Date().getTime());
			}
		});
		
		updateTooltips();
	} );

}


function openCart( animate )
{
	/*if( animate == true )
	{
		 $( 'div.cart' ).animate( { right: 0 }, 500 );
	}
	else
	{
		 $( 'div.cart' ).css( 'right', '0' );
	}
	
	$( 'div.cart div.header span' ).text( '»' );*/
}

function closeCart( animate )
{
	/*if( animate !== true )
		animate = true;
		
	if( animate == true )
	{
		 $( 'div.cart' ).animate( { right: -316 }, 500 );
	}
	else 
	{
		$( 'div.cart' ).css( 'right', '-316px' );
	}
	
	$( 'div.cart div.header span' ).text( '«' );*/
}

function closeCartAfterTimeout(timeoutLength)
{
	if (closeCartTimeout) window.clearTimeout(closeCartTimeout);
	closeCartTimeout = window.setTimeout(closeCart, timeoutLength);
}


function updateCategorieMenu( element, depth )
{	
	$.each( element.children(), function( index, child )
	{
		 
		if( child.tagName.toLowerCase() !== 'li' )
			return;
			
		var li = $( child );
		var ul = li.find('ul:first');
			ul.css( 'z-index', '100' + depth );
			ul.hide();
			
		updateCategorieMenu( ul, depth + 1 );
		
		li.unbind('mouseenter').mouseenter( function( event )
		{
			try {
				if ( $( event.originalTarget ).is( 'a' ) || $( event.srcElement ).is( 'a' ) ) 
					return;
			}
			catch( error ){};
		
			if( ul.css( 'display' ) !== 'none' )
				return;
						
			ul.show();
			ul.position({
				of: li,
				my: 'left top',
				at: 'right top',
				collision: 'none none',
				offset: '0 0'
			});
				
			ul.hide();
			//ul.show( 'slide' );
			ul.show();
		});
		 
		li.unbind('mouseleave').mouseleave(function()
		{
			menusToClose.push( ul[ 0 ] );
			for( var index in menusToClose )
			{
				if( $( menusToClose[ index ] ).css( 'display' ) !== 'none' )
				{
					//$( menusToClose[ index ] ).stop(true, true).hide( 'slide' );
					$( menusToClose[ index ] ).stop(true, true).hide();
				}
			}
			
			menusToClose = [];
		});
		
		ul.unbind( 'mouseenter' ).mouseenter( function()
		{
			menusToClose.splice( menusToClose.indexOf( ul[ 0 ] ), 1 );
		} );
		 
		//li.click();
		li.mouseenter();
		ul.hide();
	});
}

function updateTimers()
{
	$( 'div.liveshopping' ).each( function( index, item )
	{
		item = $( item );
		
		var end 	= new Date( item.children( 'input[name="end"]' ).val() ).getTime() / 1000;
		var now		= new Date().getTime() / 1000;
		var sec		= end - now;
		var min		= sec / 60;
		var hours	= min / 60;
		var days	= hours / 24;
		
		sec    		= Math.floor( sec - ( min * 60 ) + ( ( min % 1 ) * 60 ) );
		min	   		= Math.floor( min - ( hours * 60 ) + ( ( hours % 1 ) * 60 ) );
		hours  		= Math.floor( hours - ( days * 24 ) + ( ( days % 1 ) * 24 ) );
		days		= Math.floor( days );
		
		var message  = "Noch ";
			message  += days > 0 ? days + 'Tage ' : '';
			message += hours > 0 ? hours + 'h:' : '';
			message += min > 0 ? min + 'm:' : '';
			message += sec >= 0 ? sec + 's' : 'Abgelaufen!';
			
		item.find( 'div.ls-box div.timer' ).text( message );
	});

	if( $( 'div.liveshopping' ).length > 0 )
		setTimeout( updateTimers, 1000 );
}

function updateLiveshoppingStock()
{
	$.post( $env.root_url + 'ajax.php?command=liveshoppingstock', null, function( data )
	{
		var available = [];
		var others = $( 'div.liveshopping input[name=product]' );

		for (var i=0,il=data.length; i < il; i++)
		{
			var item = $( 'input[name=product][value=' + data[ i ].Produkte_ID + ']' ).parent();

			item.find( 'div.ls-box div.stock' ).empty();
			item.find( 'div.ls-box div.stock' ).append( "Noch <b>" + data[ i ].Bestand + "</b> Exemplare" );
			
			available.push( data[ i ].Produkte_ID );
		}
		
		for( var i = 0; i < available.length; i++ )
			others = others.not( '[value=' + available[ i ] + ']' );
		
		others.parent().parent().parent().remove();
	}, 'json');

	if( $( 'div.liveshopping' ).length > 0 )
		setTimeout( updateLiveshoppingStock, 5000 );
}

function updateTooltips()
{
	$('[title]').tooltip({showURL: false, delay: 200, fade:false,  track: true, showBody: " - "});
}

function addToMerkliste(produkte_ID) {
	$.post( $env.root_url + 'ajax.php?command=ADDTOMERKLISTE', {produkte_ID : produkte_ID});
	$('#merkliste-link').show();
}

/*------------------------------------------------------------------------------------------------------------------------------------------ docuement ready ----*/
$( document ).ready( function()
{	
	//pulsate on click by "an die Merkliste heften"
	var bubbles = $('div.contents div.book-details div.book-details-wrap a.add-to-watchlist').unbind( 'click' ).click( function()
	{
		var bubble = $( this );
		bubble.disableSelection();
		bubble.find( '.add-to-watchlist' ).show( 'pulsate' );
	});

	if ( jQuery('#last-book-id').val() == "" ) {
		jQuery('td.thumb-img').html('&nbsp;');
		jQuery('td.titel').html('&nbsp;');
	}
	
	setInterval(function(){ $('.blink').toggleClass( 'visibleOrNot' ); }, 700); // Liveshopping-Preis soll blinken (sic!)
	updateFancyBox();
	updateDraggables();
	updateCart();
	updateMainCart()
	updateTimers();
	updateLiveshoppingStock();
	updateCategorieMenu( $( 'div.category-menu ul:first' ), 1 );
	
	$( '.category-menu ul:first' ).show();
	
	$( '.live-ticker ul' ).marquee(
	{
		show: function( m, li )
		{
			//li.effect( 'pulsate', null, 200 );
		}
	});
	
	/*$('div.cart').live('mouseenter',function(){
		if(closeCartTimeout) window.clearTimeout(closeCartTimeout);
	});*/	
	
	//loader = $( '.loader' );
	//loader.hide();
	
	var menu = $( 'div.category-menu' );
	menu.hide();
	
	$( document ).bind( 'click', function( event )
	{
		try {
			if (!$(event.target).is('div.cart, div.cart *, div.category-menu, div.category-menu *, div.ui-dialog, div.ui-dialog *, .category-menu-hotspot') &&
			!$(event.srcElement).is('div.cart, div.cart *, div.category-menu, div.category-menu *, div.ui-dialog, div.ui-dialog *, .category-menu-hotspot') ) 
			{
				//closeCart( true );
				//menu.slideUp();
			};
		}
		catch( error ){};
	} );

	var hotspot = $( '.category-menu-hotspot' );
	var menuOpen = false;
	hotspot.show();
	
	$( '.jcarousel-skin-tango' ).css( 'margin-left', '0px' );
	
	hotspot.bind( 'click', function()
	{
		//if(menuCloseTimeout) window.clearTimeout(menuCloseTimeout);
		
		if( !menuOpen )
		{
			//menu.slideDown();
			menu.show();
			menuOpen = true;
			$( '.jcarousel-skin-tango' ).css( 'margin-left', '221px' );
		}
		else
		{
			//menuClose();
			menu.hide();
			menuOpen = false;
			$( '.jcarousel-skin-tango' ).css( 'margin-left', '0px' );
		}
		
	});
	
	if( location.pathname=='/' )hotspot.click();
	
	menu.mouseenter(function(){
		//if(menuCloseTimeout) window.clearTimeout(menuCloseTimeout);
	})
	.mouseleave(function()
	{ 
		//menuClose();
	});

	if ($page.type == 'list' || $page.type == 'home' )
	{
		$( window ).scroll( function()
		{
			var scrollPostion = $( this ).scrollTop();
			if ( scrollPostion == lastScrollPosition) return; //IE7 triggert 2x bei Mausradscroll
			lastScrollPosition = scrollPostion;

			if( ( ( scrollPostion >= $( 'div.book-container' ).height() - $( 'div.book-container' ).position().top ) || ( $( document ).height() - $( this ).height() == scrollPostion ) ) && !isPostLoading )
			{
				postLoad();
			}
		} );
		
	}		
	
	/*
	$( window ).scroll( function()
	{
		if ($(this).scrollTop() !== 0 )
		{
			$( '.page-header' ).css( 'border-bottom', '1px dotted #CD0000' );
		}
		else
		{
			$( '.page-header' ).css( 'border-bottom', 'none' );
		}
	} );
	*/

	$( '.search-box .form input:text' ).toggleValue();
		
	$( '.search-box .form input:text' ).autocomplete(
	{
		source: function( request, response )
		{
			$.post( $env.root_url + 'ajax.php?command=search&term=' + request.term, null, function( data )
			{
				response( data );
			}, 'json' );
		},
		
		select: function( event, ui )
		{
			window.location = ui.item.Link;
			//$( '.search-box .form input' ).val( ui.item.Titel );
			//$( '.search-box .form input' ).focus();
			/*var link = $( '<a style="display:none" id="detail-link-' + ui.item.ID + '" href="'+ui.item.Link+'" class="fancybox-details"></a>' );
			$( 'body' ).append( link );
			updateFancyBox();
			link.click();
			link.remove();*/
		},
		
		open: function( event, ui )
		{
			$( '.search-box .form input:text' ).autocomplete( 'widget' ).css( 'z-index', '1000' );
			$( ".ui-autocomplete li.ui-menu-item:odd" ).addClass( "alternate" );
			$( '.search-box .form input' ).focus();
		},
		
		minLength: 3 
		
	});

	$( '.search-box .form input:text' ).data( 'autocomplete' )._renderItem = function( ul, item ) 
	{
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a style=\"font-size: 10px;\">" + item.Titel + "</a>" )
			.appendTo( ul );
	};
		
	$( '.search-box .hotspot' ).click ( function() 
	{
		$( '.search-box .form form' ).submit();
	} );
	
	$("#search-form input").keyup(function(event){
		if(event.keyCode == 13){
			$( '.search-box .form form' ).submit();
		}
	});

	
	/*$( '.search-box .form input' ).bind('focus', function()
	{
		$(this).stop().animate({
		'font-size'	: 20, 
		'left'		: -160,
		'top'		: -3,
		'width'	: 340
		}, 550, 'swing');
	});
	
	$( '.search-box .form input' ).bind('blur', function()
	{
		$( this ).stop().animate({
		'font-size'	: 20,
		'left'		: -2,
		'top'		: -3,
		'width'	: 207
		}, 450, 'swing');
	});*/
	$( '.iphone-apps' ).cycle(
	{
		fx: 'fade'
	});
	
	$( 'div.header-logo' ).click( function()
	{
		window.location.href = $env.root_url;
	} );
	
	$( '.page-wrap-1,.page-wrap-2' ).height( $( document ).height() );
	$( '.login-panel form input' ).toggleValue();
	$( '.login-panel form input' ).bind( 'keydown', function( event )
	{
		if( event.keyCode == 13 || event.which == 13 )
			$( '#panel-login-button' ).click();
	});
	
	if ($page.type == 'details')
	{
		$( 'div.tool-bar img.close' ).hide();
	}

	$( '#coverflow' ).flash(
	{
		src: $env.media_url + "/flash/coverflow.swf",
		width: "100%",
		height: '350',
		salign: 'tl',
		menu	:false,
		flashvars: { path: $env.root_url + "ajax.php?command=coverflow&random=" + new Date().getTime()  },
		wmode: 'transparent',
		scale: 'noscale'
	},
	{
		expressInstall: true,
		version: '10.0.0'
	});
			
	$( document ).bind( 'user.login', function()
	{
		userdata( [ 'Sex', 'Name' ], function( data )
		{
			// Wenn kein Name vorhanden auch keinen anzeigen!
			if( data.Name === '' )
			{ 
				var sentence = '<span style="color: #CD0000; cursor: pointer;" class="editUser">Meine Daten</span> | <span style="color: #CD0000; cursor: pointer;" class="logout">Ausloggen</span>';
			} 
			else 
			{
				$( 'div.ssl-icon' ).css({
					'top': '22px'
				})
				var sentence = 'Hallo ' + ( data.Sex == 'male' ? 'Herr' : 'Frau' ) + ' ' + data.Name + '! <span style="color: #CD0000; cursor: pointer;" class="editUser">Meine Daten</span> | <span style="color: #CD0000; cursor: pointer;" class="logout">Ausloggen</span>';
			}
			
			loginForm = $( 'div.login-panel' ).clone( true );
			$( 'div.login-panel *' ).remove();
			$( 'div.login-panel' ).append( '<div>' + sentence + '</div>' );
			$( 'div.login-panel span.logout' ).one( 'click', function()
			{
				
				$( 'div.ssl-icon' ).css({
					'top': '3px'
				})
				logout();
			});
			$( 'div.login-panel span.editUser' ).click( function()
			{
				$( '<a href="' + $env.root_url + '?context=edituser&rand=' + new Date().getTime() + '">edit</a>' ).fancybox(
						{
							type			: 'iframe'	,
							titleShow		: false		,
							showCloseButton	: false		,
							autoDimensions	: false		,
							width			: 740		,
							height			: 400		,
							padding			: 0			,
							scrolling		: 'no'		,
							onComplete		: null		,
							onClosed		: function()
							{
								//$( 'a.checkout' ).attr( 'href', $env.root_url + "?context=checkout&random=" + new Date().getTime() );
							}
				}).click();
			});
			$( 'div.cart' ).find( 'a.checkout' ).attr( 'href', $env.root_url + "?context=checkout&random=" + new Date().getTime() );
		});
	} );
	
	$( document ).bind( 'user.logout', function()
	{
		$( 'div.login-panel *' ).remove();
		$( 'div.login-panel' ).replaceWith( loginForm );
		$( 'div.cart' ).find( 'a.checkout' ).attr( 'href', $env.root_url + "?context=checkout&random=" + new Date().getTime() );
	});
	
	$( '#panel-login-button' ).unbind( 'click' ).click( function()
	{
		var data = $( 'div.login-panel' ).jserialize();
		login( data.username, data.password, function( response )
		{
			if( !response )
			{
				$( '<div title="Loginfehler">Login fehlgeschlagen. Überprüfen Sie Ihre Email-Adresse und Kennwort und versuchen Sie es erneut.</div>' ).dialog(
						{
							modal	: true,
							buttons	:
							{
								OK: function()
								{
									$( this ).dialog( 'destroy' );
								}
							}
						}).dialog( 'widget' ).find( 'button' ).bind( 'mouseenter click', function(){ $(this).removeClass( 'ui-state-hover ui-state-focus') } ).removeClass( 'ui-state-hover ui-state-focus');
			}
		});
	});
	
	if ($env.user.logged_in)
	{
		$( document ).trigger( 'user.login' );
	}
		
	$( '.search-box .form input' ).focus();
	
	$( 'a.register' ).fancybox(
	{
		type			: 'iframe'	,
		titleShow		: false		,
		showCloseButton	: false		,
		autoDimensions	: false		,
		width			: 740		,
		height			: 400		,
		padding			: 0			,
		scrolling		: 'no'		,
		onComplete		: null		,
		onClosed		: function()
		{
			//$( 'a.checkout' ).attr( 'href', $env.root_url + "?context=checkout&random=" + new Date().getTime() );
		}
	});
	
	$( 'div.register' ).click( function()
	{
		$( 'a.register' ).click();
	});

	$( 'a.extendedsearch' ).fancybox(
	{
		type			: 'iframe'	,
		titleShow		: false		,
		showCloseButton	: false		,
		autoDimensions	: false		,
		width			: 300		,
		height			: 250		,
		padding			: 0			,
		scrolling		: 'no'		,
		onComplete		: null		,
		onClosed		: function()
		{
			//$( 'a.checkout' ).attr( 'href', $env.root_url + "?context=checkout&random=" + new Date().getTime() );
		}
	});
	// Liveshopping-Prompt zu Anfang einblenden
	if($.browser.msie){ // drecks-MSIE kann keine transparenten PNGs faden
					setTimeout( function(){
				$('.ls-box').each(function(){
				
					$(this).position(
					{
						of: $(this).parent(),
						my: 'right bottom',
						at: 'left top',
						offset: '100 44'
					})
					.show(); 
					} )
					}, 4000 );
		setTimeout( function(){ $('.ls-box').hide(); } , 10000 );
	} else {	
		setTimeout( function(){
			$('.ls-box').each(function(){
			
				$(this).position(
				{
					of: $(this).parent(),
					my: 'right bottom',
					at: 'left top',
					offset: '100 44'
				})
				.fadeIn(2000); 
				} )
				}, 4000 );
				
		setTimeout( function(){ $('.ls-box').fadeOut(2000); } , 10000 );
	}
});

$(window).load(function(){
	
	if ( jQuery('#last-book-id').val() == "" ) {
		jQuery('td.thumb-img').html('&nbsp;');
		jQuery('td.titel').html('&nbsp;');
	}
	
	
	
	// Liveshopping-Prompt zu Anfang einblenden (es funtioniert original nur wenn sowohl in document ready als auch in window load)
	if($.browser.msie){ // drecks-MSIE kann keine transparenten PNGs faden
		setTimeout( function(){
				$('.ls-box').each(function(){
				
					$(this).position(
					{
						of: $(this).parent(),
						my: 'right bottom',
						at: 'left top',
						offset: '100 44'
					})
					.show(); 
					} )
					}, 4000 );
	} else {		
		setTimeout( function(){
				$('.ls-box').each(function(){
				
					$(this).position(
					{
						of: $(this).parent(),
						my: 'right bottom',
						at: 'left top',
						offset: '100 44'
					})
					.fadeIn(4000); 
					} )
					}, 2000 );
	}
	//schon mal ne runde Bücher nachladen, damits unten nicht so leer ist
	if ($page.type == 'list')
	{
		postLoad();
	}		
	updateTooltips();
	
});;

