//$=jQuery.noConflict();
(function($) {
	
	//same as $(document).ready();
	$(function() {
	
	
//$(document).ready(function(){
			
 	$('#top-sites-header ul li').not('.current-site').not('.music, .info').hover(function() {
		
		$('#tooltip').stop(true, true);

		$('#tooltip .content').empty().append($(this).find('img').clone().show());
		$('#tooltip .head').css({'background-position' : 'top center'});
		$('#tooltip').css({'width' : '200px', 'height' : '130px', 'position' : 'absolute', 'top' : $(this).position().top + 30, 'left' : $(this).position().left - 70});
			
		$('#tooltip').fadeIn('slow');
	
	}, function() {
		$('#tooltip').stop(true, true).fadeOut('slow', function() {
			$('#tooltip .content').empty();
		});
	});
	
    	var shopscroller = $('#shop .content-scroller').jScrollPane({ autoReinitialise: true}).data('jsp');
        var shopcontent = shopscroller.getContentPane();
	
		$('#shop').dialog({
			title: 'Shop',
			bgiframe: false,
			height: 500,
			width: 	600,
			modal: false,
			resizable: false,
			autoOpen: false,
			//open: reinitScrollbarShop,
			close: function() {
						shopcontent.empty();
						shopcontent.load('/shop/shopping_cart.php', function() {
							if($('#shop #basket').length) {
								$('.basket-sidebar').fadeIn();
							} else {
								$('.basket-sidebar').fadeOut();
							}
						});
					}
		});
		
		shopcontent.load('/shop/shopping_cart.php',
						function() { 
							if($('#shop #basket').length) {
								$('.basket-sidebar').fadeIn();
							}});
		

		$('.show-basket').click(function() {
			shopcontent.load('/shop/shopping_cart.php');
			$('#shop').dialog('open');
		});
    
		
		
	/*	$('.shop-login').click(function() {
			$('#shop').load('/shop/account.php').dialog('open');
		});
*/

		$('<a href="#" title="Anmelden" class="ui-dialog-titlebar-home ui-corner-all" role="button" unselectable="on" style="-moz-user-select: none;"></a>') 
		    // add icon and text 
		    .append('<span class="ui-icon ui-icon-home" unselectable="on" style="-moz-user-select: none;">login</span></a>') 
		    // add the anchor to the titlebar 
		    .appendTo('.ui-dialog-titlebar') 
		    // bind callback 
		    .click(function() { 
		        shopcontent.load('/shop/account.php');
			})
			.hover(function() {
					$(this).addClass('ui-state-hover');
				}, 
				function() {
					$(this).removeClass('ui-state-hover');				
				}); 

		$('<a href="#" title="Abmelden" class="ui-dialog-titlebar-logoff ui-corner-all" role="button" unselectable="on" style="-moz-user-select: none;"></a>') 
		    // add icon and text 
		    .append('<span class="ui-icon ui-icon-unlocked" unselectable="on" style="-moz-user-select: none;">login</span></a>') 
		    // add the anchor to the titlebar 
		    .appendTo('.ui-dialog-titlebar') 
		    // bind callback 
		    .click(function() { 
		        shopcontent.load('/shop/logoff.php');
			})
			.hover(function() {
					$(this).addClass('ui-state-hover');
				}, 
				function() {
					$(this).removeClass('ui-state-hover');				
				}); 		
		/*

					wif ($(this).parent().is('a'))
				$('#shop').load($(this).parent().attr('href'), '', reinitScrollbarShop);
			else 
				$('#shop').load($(this).attr('href'), '', reinitScrollbarShop);
*/

		$('#shop a').live('click', function(event) {
			event.preventDefault();
			//console.log("#shop a link");
			shopcontent.load($(this).attr('href'));
		});		


        $('#shop a').live('click', function(event) {
			event.preventDefault();
			shopcontent.load($(this).attr('href'));
		});	
		

		$('#shop form').live('submit', function(event) {
			if (!$('#checkout_confirmation').length) {
				event.preventDefault();
				$.post($(this).attr('action'), $(this).serialize(), 
						function(data) { 
							shopcontent.html(data);
				 		});
			} else {
				if ($(this).attr('action').match(/paypal/) == null) {
					event.preventDefault();
					if (acceptOrder()) {
						$.post($(this).attr('action'), $(this).serialize(), 
							function(data) { 
								shopcontent.html(data);
					 		});
					}
				} else {
					if (acceptOrder()) {
						return true;
					}
					return false;
				}
			}
						
		});



		/*
		
		
		$('.show-basket').hover(function() {
			$('#tooltip').stop(true, true);
			
			
			$('#shop').load('/shop/shopping_cart.php');
			
			$('#tooltip .content').empty().append('<table class="basket-data"><tbody><tr><th>Stk.</th><th>Artikel</th></tr></tbody></table>');
			
			$('#basket .quantity').each(function(index) {
				var string = $('#basket .product')[index].innerHTML;
				var str_exp = string.split('_');
				$('#tooltip .basket-data').append('<tr><td class="quantity">' + $(this).val() + '</td><td>' + str_exp[0] + ' ' + str_exp[1] + ' ' + str_exp[2] + '</td></tr>');
			});
			
			$('#tooltip .basket-data').append('<tr><td colspan="2" class="subtotal">' + $('#sub_total').text() + '</td></tr>');
			
			$('#tooltip .head').css({'background-position' : '90%'});
			$('#tooltip').css({'position' : 'absolute', 'top' : ($(this).position().top + ($.browser.safari ? 65 : 50)), 'left' : $(this).position().left - 15, 'width' : '250px', 'height' : 'auto'});
			
			$('#tooltip').fadeIn('slow');
			
		}, function() {
			$('#tooltip').stop(true, true).fadeOut('slow', function() {
				$('#tooltip .content').empty();
			});
		});

		
		
		*/
		

		$('.close-shop').live('click', function(event) {
			event.preventDefault();
			$('#shop').dialog('close');			
		});


		$('.getSeminar').click(function(event) {
			event.preventDefault();
			var elm = $(this);
			$.post(
					$(this).attr('href'), 
					{ products_id: $(this).attr('id') },
					function() {
							$(elm).children('.img-buchen').toggle();
							$('.basket-text').html('Artikel im Warenkorb');
							$('.basket-sidebar').fadeIn();
							shopcontent.load('/shop/shopping_cart.php');
					});			
		});
		

		acceptOrder = function() {
			if (!$('#accept').attr('checked')) {
					$('#acceptText').css('color', '#f26100');
					return false;
			} else return true;
		}
		
		
		if($('#shop #basket').length) {
			$('.basket-sidebar').fadeIn();
		}
		
		
		$('#nextevent-carousel').jcarousel({
        	scroll: 1,
        	visible: 1,
	       	buttonNextHTML: null,
    	   	buttonPrevHTML: null
       	});
       	
       	$('.seminar-row td').not('.buchen').bind('click', function() {
       		
       		var trClick = $(this).parent('tr');
       		
       		$('.clicked').removeClass('clicked');
       		
       		trClick.addClass('clicked');
       	
       		var newElement = trClick.next('tr').children('td').children('div').clone();
       		
       		var childs = newElement.children();
       		
       		//remove .topText
       		
       		var topText = childs.filter('.topText').children();
       		var description, insidePackage;
       		topText.each(function() {
       			if ($(this).hasClass('introDescription')) {
       				description = $(this);
       			} else if ($(this).hasClass('insidePackage')) {
       				insidePackage = $(this);
       			} else {
       				$(this).remove();
       			}
       		});
       		childs.remove('.topText');
       	      			
       		//remove .img
       		
       		childs.remove('.img');
       		
       		newElement.find('.borderBox p').children().each(function() {
       				     				
       				if (!$(this).children().size() > 0) {
       					if($.trim($(this).text()) == "") {
  							$(this).remove();
						} else {
							$(this).replaceWith('<p>' + $(this).text() + '</p>');
						}	
       				} else {
       					$(this).removeClass('alignLeft');
       					$(this).removeClass('alignRight');
	       				$(this).wrapAll('<p />');
       				}
        	});
        	
        	if (description || insidePackage)        	
	        	newElement.children().filter('.borderBox').prepend('<br />').prepend(description).prepend(insidePackage);
        	
        	
       			
       		newElement.children().filter('.borderBox, .kleingedrucktes').wrapAll('<div class="panel" />').parent('.panel').prepend('<h2 class="title" style="display:none;">Daten</h2>');
       		
       		newElement.children().filter('.description').addClass('panel').prepend('<h2 class="title" style="display:none;">Beschreibung</h2>');
       		newElement.children().filter('.quoteBox').addClass('panel').prepend('<h2 class="title" style="display:none;">Meinungen</h2>');;
       		//newElement.children().filter('.borderBox').before('<h2 class="title">Daten</h2>');
			
			if ($('.coda-nav').length)
				$('.coda-nav').remove();
			
       		$('.coda-slider-wrapper').fadeOut("slow").children('.seminar-description').empty().append(newElement);
       		
       		$('.coda-slider').codaSlider({
       			dynamicArrows: false,
       			dynamicTabs: true,
				dynamicTabsAlign: "left",
				crossLinking: false
			}).css('height', ($('.coda-slider').height() + 40));

       		
       		$('.seminar-description .panel').wrapInner('<div class="panel-wrapper" />');
       		$('.seminar-description').find('img').each(function() {
       			$(this).attr('src', "http://www.seros.de" + $(this).attr('src'));
       		});
       		
       		$('.seminar-description').find('a [href]').each(function() {
       			if ($(this).attr('href').match(/http/) == null) {
       				$(this).attr('href', 'http://www.seros.de' + $(this).attr('href'));
       			}
       			
       		});
       		
       		$('.coda-nav li a').filter('.current').parent('li').addClass('current');
       		bindTabClick();
       		$('.coda-slider-wrapper').fadeIn('slow');
       		
       		$.scrollTo('#seminar');       		
       	  });
        
      
        
      bindTabClick = function() {  $('.coda-nav li a').bind('click', function() {
        	$(this).filter('.current').parent('li').addClass('current');
        });
        
        }

       
        
		/*$('#shop').change(function() {
			console.log('load');
			if ($('#shop #basket').length) {
				$('.basket-sidebar').fadeIn();
			} else {
				$('.basket-sidebar').fadeOut();
			}
		});*/

	});


	$(window).bind("load", function() {
		
		
	
	});
})(jQuery);
