$(document).ready( function() {

  // Höhe overlay + ZoomContainer anpassen
  $('#overlay').height( $('body').height() );
  $('#zoomcontainer').height( $(window).height() );
  
  // Shop by Look PopUp - Position berechnen + CMS Popup
  calcPopUpPosition();
  calcDetailSize();
  
  $(window).resize( function() {
    calcPopUpPosition();
    calcDetailSize();
  });
  
  // Infobutton adresse
  $('.infobutton').hover( function() { $(this).next('.input_info').show();}, function() {$('.input_info').hide(); });
  
  // zweiter Link im Breadcrumb nicht klickbar
  $('#breadcrumb a').eq(1).attr('href', 'javascript:;')
  
  // Öffnet alle CMS Links im PopUp
  $('.cmslink').click( function(event) {
    $('#header .dropdown').hide();
    $('#overlay').fadeIn();
    if( $(this).attr('href').indexOf('?') > 0) {
//      var newurl = $(this).attr('href').substring(0, $(this).attr('href').indexOf('?')) + '?plain=1';
      var newurl = $(this).attr('href') + '&plain=1';
    }else{
      var newurl = $(this).attr('href') + '?plain=1';
    }
    
		setTimeout('$("#cmspopup").fadeIn()', 1000);
    
		if( document.URL.indexOf('www.') == -1 ) newurl = newurl.replace('www.', ''); // mit oder ohne www.
    $('#cmspopop_content').html('<img style="margin-left: 250px;" src="/out/bebasic/img/ajax-loader.gif" />').load( newurl );
    event.stopPropagation();
    return false;
  })

  // Popups schließen bei Klick auf X oder Hintergrund
  $('#overlay, #cmspopup_close, #nlpopup_close, .cmspopup_close, #headerbar .dropdown .head').click( function() {
		$('#cmspopop_content').html('');
    // $('#cmspopup, #overlay, .dropdown, .sbl_popup').hide();
    $('#cmspopup, #nlpopup, #overlay, #headerbar .dropdown, .sbl_popup').hide();
  }); 
  
  $(document).keyup( function(event) {
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if( keycode == 27 ) { // 27 = ESC
       $('#cmspopup_close').click();
       $('#nlpopup_close').click();
     }
  });
  
  // Opener für Dropdowns im Headerbereich
  $('.open_dropdown').click( function() {
    $(this).nextAll('.dropdown').eq(0).slideDown('slow');
    $('#overlay').fadeIn();
    return false;
  })
  
  
  // FUNKTION FÜR FORMULARE (Bezeichner (label) steht hinter dem Input Feld, und wird ausgeblendet) 
  $('#addressForm.form li input').add('#shippingAddressForm.form li input ').add('#newUser .form li input').add('input.transparent').add('#coupon input.transparent').add('#d3coupon input.transparent').add('#newsletter_header input.transparent').add('#loginBox li input.transparent').each( function() {

    // Label zwischenspeichern ...
    $(this).data('myValue', $(this).prev().html() );

    // Bei vorausgefüllten Feldern Label gleich ausblenden
    if ($(this).val()) $(this).prev().html('');
		
		if ($(this).val() == '') {
			$(this).prev('.req').append('<div style="color:#A7111C; float: right;"><b>*</b></div>');
		}else {
			$(this).prev('.req').html('<div style="color:#A7111C; float: right;"><b>*</b></div>');
		}
    // Bei Fokus Label ausblenden, bei Blur wieder einblenden, fall Textfeld leer
    $(this).focus( function() { $(this).prev().html(''); });
    $(this).blur(  function() { 
//			if ($(this).val() == '') $(this).prev().html( $(this).data('myValue') ); 
			if ($(this).val() == '') {
				$(this).prev().html( $(this).data('myValue') ); 
				$(this).prev('.req').append('<div style="color:#A7111C; float: right;"><b>*</b></div>');
			}else {
				$(this).prev('.req').html('<div style="color:#A7111C; float: right;"><b>*</b></div>');
			}
		});
  })
  
  // Lieferadresse zeigen/verstecken (im Checkout)
  $('#userChangeAddress').click( function() {
    $('#addressForm').show();$('#addressText').hide();return false;
  });
  $('#showShipAddress').change( function() {
    $('#shippingAddress').toggle();
    if( $(this).is(':checked') ){
      removeValidatorToShipping();
    }else{
      addValidatorToShipping();
    }
  });
  
  $('#userChangeShippingAddress').click( function() {
    $('#shippingAddressForm').show();
    $('#shippingAddressText').hide();
    return false;
  });

  
  
  // Slider jCarousel
  $('#manufacturerslider').jcarousel({
    'scroll': 2,
    'setupCallback': showSlider,
    'wrap': 'circular'
  });
  
  $('#topoftheshopslider').jcarousel({
    'scroll': 2,
    'setupCallback': showSlider,
    'wrap': 'circular'
  });
  
  $('#shopbylook').jcarousel({
    'scroll': 1,
    'setupCallback': showSlider,
    'wrap': 'circular'
  });
  
  $('#scrollcontainer #scroller').jcarousel({
    'scroll': 2,
    'setupCallback': showSlider,
    'wrap': 'circular'
  });
  
  // Listtemplate im LINE-Layout
  $('#listcontainer.line').width( $(window).width() );
  var pos = $('#page').offset();
  $('#listcontainer.line').css('left', -pos.left + 'px');
  $('#listcontainer.line').show();
  
  
  // List Template - Variant Pictures
  $('.variantpictures .vpic').click( function() {
    $(this).parent().prevAll('.img').find('.switchimg').eq(0).attr('src', $(this).attr('pic1'));
    $(this).parent().prevAll('.img').find('.switchimg').eq(1).attr('src', $(this).attr('pic2'));
		var pLink = $(this).parent().prevAll('.img').find('.prodListLink').eq(0).attr('rel');
    $(this).parent().prevAll('.img').find('.prodListLink').eq(0).attr('href', pLink+"?v="+$(this).attr('var'));
  })
  
  // Shop by look
  $('#shopbylook .sbl_img').click( function() {
    $(this).nextAll('.sbl_popup').eq(0).show();
  })
  
  
  //Custom Selects styled
	initializeCustomSelects(true);

	//top navigation handeling START
	
	$('#rootnavigtation li a').mouseover( function (){
		//first remove current class from all 'li' and 'a' tags
		$('#rootnavigtation li').removeClass('current');
		$('#rootnavigtation li a').removeClass('current');
		//hide the sub navigation
		$('.navigation').hide();
		//now add current class to the current mouseovered menu-item's 'a' tag
		$(this).addClass('current');
		//now add current class to the current mouseovered menu-item's 'li' tag
		$(this).prev().addClass('current');
		//now display currently mouseovered menu-item's sub menu
		$('#navigation_'+$(this).attr('rel')).show();
	});
	/*
	$('.navigation').mouseover( function (){
		$('.navigation li').removeClass('current');
		$('.navigation li a').removeClass('current');
		$(this).addClass('current');
	});
	*/
  
  // auto select first submenu on click in Mainnav
  $('#rootnavigtation li a').click( function() {
    window.location = $('#navigation_'+$(this).attr('rel')+' li').eq(0).children().eq(0).attr('href');
    $('#navigation_'+$(this).attr('rel')+' li').eq(0).children().eq(1).show();
    return false;
  })
	
	//select/activate the first menu
	$('.navigation').eq(0).show();
	
	//select/activate the current/clicked menu
	$('#rootnavigtation li a.current').mouseover();
  
  // show active Navigation after 4sec... refresh
  $('#pagetop').mouseleave( function() {
    var timer = setTimeout( function() {
      $('#rootnavigtation a.selected').mouseover();
      clearTimeout(timer);
    },  4000)
  })
	//top navigation handeling END		
  
  
    // Scrollbar fpr Category Slider
  $('#listcontainer.line').jScrollPane({
      showArrows: true
	});
  
  // Empty all fields on click "New Address" 
  $('#oxaddressid_values li').click( function() {
    if( $(this).attr('rel') == -1 )
      $('#shippingAddressForm input[type="text"]').val('');
  })
  
  // Shipping Select
  $('#shipping .dropdown_items li').click( function() {
    $('#shipping').submit();
  })
	
	$('#userLoginName').blur( function() {
		var emailVal = $(this).val()
		$.ajax({
			type: "POST",
			url: "/out/bebasic/src/userlist_emails.php?type=newsletter",
			data: "lgn_usr="+emailVal,
			cache: false,
			success: function(response){
				if(response == 'true')
					$('#newsletterSubscription').css('visibility', 'visible');
				else
					$('#newsletterSubscription').css('visibility', 'hidden');
			}

		});

	});
  
	
	
  // ENDE DOC READY ------------------------------------------------------------------------------------------------
});

//Show NEWSLETTER POPUP
function showNewsletterPopup(url){
	
	$('#header .dropdown').hide();
	$('#overlay').fadeIn();
	setTimeout('$("#nlpopup").fadeIn()', 1000);

	$('#nlpopop_content').html('');
	$('#nlpopop_content').html('<img style="margin-left: 250px;" src="/out/bebasic/img/ajax-loader.gif" />').load( url );
	
	return false;
}

// show Slider (AFTER jCarousel setup)
function showSlider() {
  $('#manufacturerslider').css('visibility', 'visible');
  $('#topoftheshopslider').css('visibility', 'visible');
  $('#shopbylook').css('visibility', 'visible');
  $('#scrollcontainer #scroller').css('visibility', 'visible');
}

function calcPopUpPosition() {
  var left = $(window).width() / 2 - 300;
  $('#shopbylook .sbl_popup').css('left',  left + 'px');
  $('#cmspopup').css('left',  left + 'px');
  $('#nlpopup').css('left',  left + 'px');
}

// Recalculate Width of Detail Page
function calcDetailSize(browser) {
  var wsize = $(window).width();
  if(wsize > 980 && wsize < 1250) {
    var dif = wsize;
    var infosize = wsize - 645;
    if(infosize>458) infosize = 458;
  }else if(wsize < 980) {
    var dif = 980;
    var infosize = 335;
  }else{
    var dif = 1250;
    var infosize = 458;
  }
  if(browser == 'ipad') {
    var dif = 980;
    var infosize = 335;
  }
  $('#details').css({width: dif+'px', left: (980-dif)/2+'px'});
  $('#details #information').css({width: infosize+'px'});
  $('#detailsMain').css('width', 'auto');
  $('.productMainInfo').css('width', 'auto');
  $('#details-tabs > div').css('width', 'auto');
  
  $('#scrollcontainer .jcarousel-clip-horizontal').css('width', dif-50+'px');
  $('#categoryproducts').css({width: dif+'px', left: (980-dif)/2+'px'});
}

function initializeCustomSelects(setStandard) {
//custom drop down menu created with ul and input type=text tag
	$('.form .dropdown_select').click( function(event) {
		$(this).next('.dropdown_items').show();
		event.stopPropagation();
	});
	
  $('.form .dropdown_items li').click( function(event) {
		$(this).parent().prev().val($(this).html());
		$(this).parent().prev().prev().val($(this).attr('rel'));
//		alert($(this).parent().prev().val());
//		alert($(this).parent().prev().prev().val());
		$(this).parent().hide();
		event.stopPropagation();
  })

  if(setStandard) {
    $('.form .dropdown_items').each(function (){
      $(this).children().eq(0).click();
      $(this).find('.active').click();
    })
  }
	
	$('body').click( function (){
		$(this).find('.dropdown_items').each(function (){
			$(this).hide();
		});
	});

//  $('.form .dropdown_items li.active').click();
	//end custom dropdown menu
}

