var shop_currency = '&euro;';
var shop_currency_length = 0;
function CurrencyFormatted(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	
	s = s.replace('.',',');
	s = s+shop_currency;
	return s;
}
// end of function CurrencyFormatted()

function setPreise(){
	var gesamtpreis = 0;
	// Aufpreis für Stick eintragen
	var anzahl = jQuery('#orderAmount').attr('value');
	var aufpreis = jQuery('input#additional_charge_'+jQuery('#positionsauswahl li.active a').attr('rel')).attr('value')*anzahl;
	// ist ne zweite Zeile angelegt?
	if ( jQuery('#stick_text_2').val() ) {
		aufpreis += jQuery('input#additional_charge_zeile2_'+jQuery('#positionsauswahl li.active a').attr('rel')).attr('value')*anzahl;
	}

	jQuery('#aufpreis').html( CurrencyFormatted( aufpreis ) );
	
	
	// Gesamtpreis eintragen
	price_numb = parseFloat( jQuery('#dummy_productPrice').html().substring((shop_currency_length),(shop_currency_length)+7).replace(',','.') )*anzahl;
	
	if(price_numb){
		gesamtpreis = aufpreis+price_numb;
	}else{
		
		gesamtpreis = aufpreis+(parseFloat( jQuery('#dummy_productPrice .newPrice').html().substring((shop_currency_length),(shop_currency_length)+7).replace(',','.') )*anzahl);
	}
	
	jQuery('#gesamtpreis').html( CurrencyFormatted( gesamtpreis ) );
	
	if ( jQuery('input#additional_charge_'+jQuery('#positionsauswahl li.active a').attr('rel')).size() > 0 ) {
		jQuery('.stick_price').show();
	}
}


jQuery(document).ready(function(){
	// Stickservice - Tabs durchschalten
	jQuery('#stickservice > h3').hover(
		function(){
			if ( !jQuery(this).hasClass('active') ) {
				jQuery(this).css('cursor','pointer');
			}
		},
		function(){
			jQuery(this).css('cursor','normal');
		}
	);
	jQuery('#stickservice > h3').click(function(){
		if ( !jQuery(this).hasClass('active') ) {
			// Tabs umschalten
			jQuery(this).addClass('active').siblings('h3').removeAttr('class');
			
			// Content umschalten
			jQuery(this).siblings('.content.active').removeClass('active').siblings('div.content').addClass('active');
		}
	});
		
	// Stickservice - Login Einblenden
	jQuery('.stickservice_teaser a.login').fancybox();

	// Stickservice - Einblenden
	jQuery('.stickservice_teaser a:not(.login)').click(function(e){
		e.preventDefault();
		
		jQuery('#stickservice').slideDown();
		jQuery('.stickservice_teaser').parents('fieldset').append('<div class="stickservice_teaser_bg">');
		
		jQuery('#dummy_orderAmount').attr('value',jQuery('#orderAmount').attr('value'));
		jQuery('#dummy_productId').attr('value',jQuery('#productId').attr('value'));
		jQuery('#dummy_colorFamily').attr('value',jQuery('#colorFamily').attr('value'));
		jQuery('#dummy_availabilityStatus').html(jQuery('#availabilityStatus').html()).removeAttr('class').addClass(jQuery('#availabilityStatus').attr('class'));
		
		/*var text = jQuery('#stick_text_1').attr('value');
		if ( jQuery('#stick_text_2').attr('value') ) {
			text += '<br />'+jQuery('#stick_text_2').attr('value');
		}
		jQuery('#preview').show().html(text);*/
		setPreise();
		jQuery('#order_stickservice').attr('value','1');
		
		location.hash = 'stickservice';
		return false;
	});
	
	// Stickservice beenden 
	jQuery('.stickservice_close').click(function(e){
		//e.preventDefault();
		jQuery('#stick_login').slideUp();
		jQuery('#stick_login').remove();
		jQuery('#stickservice').slideUp();
		jQuery('#non_stick').slideDown();
		jQuery('#stickservice_close_link').hide();
		
		jQuery('#order_stickservice').attr('value','0');
		jQuery('#stickservice_nein').attr('checked','checked');
		jQuery('.stickservice_ja').removeAttr('checked');
//		location.hash = '';
	});
	
	// Stickservice - Einblenden
	jQuery('.stickservice_ja').click(function(e){
		if(jQuery('#prodStickserviceSwitch').hasClass('disabled_option')){
			return false;
		}
		jQuery('#non_stick').slideUp();
		jQuery('#stickservice').slideDown();
		if(jQuery(this).hasClass('please_login')){jQuery('#login_redirect').val(jQuery('#login_redirect').val()+'#Stickservice');
			var stick_login = document.createElement('div');
			stick_login.id = 'stick_login';
			stick_login.innerHTML = jQuery('#please_login').html();
			jQuery('#page_margins').append(stick_login);
			jQuery('#stick_login').delay('300').slideDown();
		}else{
		jQuery('#stickservice_close_link').show();
		jQuery('.stick_options').hide();
		jQuery('.' + jQuery(this).attr('id') + '_ct').show();
		
		
		jQuery('#dummy_orderAmount').attr('value',jQuery('#orderAmount').attr('value'));
		jQuery('#dummy_productId').attr('value',jQuery('#productId').attr('value'));
		jQuery('#dummy_colorFamily').attr('value',jQuery('#colorFamily').attr('value'));
		jQuery('#dummy_availabilityStatus').html(jQuery('#availabilityStatus').html()).removeAttr('class').addClass(jQuery('#availabilityStatus').attr('class'));
		
		setPreise();
		jQuery('#order_stickservice').attr('value','1');
		
		
		//if only one stickposition available slect it
		if(jQuery('#positionsauswahl li').length == 1){
			jQuery('#positionsauswahl li a').click();
		}
		}
		location.hash = 'Stickservice';
	});
	
	
	// Stickservice - Position anzeigen
	jQuery('#positionsauswahl li, #positionsauswahl_logo li').hover(
		function(){
			jQuery(this).addClass('hover');
		},
		function(){
			jQuery(this).removeClass('hover');
		}
	);
	
	// Stickservice - Position übernehmen/aktiv schalten
	jQuery('#positionsauswahl a').click(function(e){
		e.preventDefault();
		
		jQuery(this).parents('li').addClass('active').siblings('li').removeClass('active');
		//jQuery('#stick_position').val( jQuery(this).attr('rel') );
		
		// Wert ins Formular schreiben
		jQuery('#stick_position').remove();
		jQuery('#stickservice').prepend('<input type="hidden" name="stick_position" id="stick_position" value="'+jQuery(this).attr('rel')+'" />');
		
		setPreise();
		
		return false;
	});
	// Stickservice - Position übernehmen/aktiv schalten
	jQuery('#positionsauswahl_logo a').click(function(e){
		e.preventDefault();
		
		jQuery(this).parents('li').addClass('active').siblings('li').removeClass('active');
		
		// Wert ins Formular schreiben
		jQuery('#stick_position').remove();
		jQuery('#stickservice').prepend('<input type="hidden" name="stick_position" id="stick_position" value="'+jQuery(this).attr('rel')+'" />');

		return false;
	});
	
	// Stickservice - zweite Zeile anzeigen
	jQuery('#stickservice a.zeile_zwei').click(function(e){
		e.preventDefault();
		
		jQuery(this).hide();
		jQuery('#stick_text_2').show();
		return false;
	});
	
	// Stickservice - Menge/Größe/Farbe übernehmen
	jQuery('#dummy_orderAmount').change(function(){
		jQuery('#orderAmount').attr('value',jQuery(this).attr('value'));
		setPreise();
	});
	jQuery('#dummy_productId').change(function(){
		jQuery('#productId').attr('value',jQuery(this).attr('value'));
		checkAvailability(jQuery('#productId').attr('value'));
	});
	jQuery('#dummy_colorFamily').change(function(){
		jQuery('#colorFamily').attr('value',jQuery(this).attr('value'));
		setPreise();
	});
	

	// Stickservice - Preview anzeigen
	jQuery('#stick_text_1, #stick_text_2').keyup(function(){
		var text = jQuery('#stick_text_1').attr('value');
		if ( jQuery('#stick_text_2').attr('value') ) {
			text += '<br />'+jQuery('#stick_text_2').attr('value');
		}
	
		jQuery('#preview').show().html(text);
		setPreise();
	});
	
	// Stickservice - Preview anpassen
	jQuery('#schriftauswahl, #farbauswahl, #groessenauswahl').change(function(){
		// Grundgröße bestimmen
		var fontSize = 20;
		if ( jQuery('#schriftauswahl').val() == 'brush' || jQuery('#schriftauswahl').val() == 'english' ) {
			fontSize = 26;
		}

		jQuery('#preview').removeAttr('class').addClass(jQuery('#schriftauswahl').val()).addClass(jQuery('#farbauswahl').val()).css('font-size',( fontSize*jQuery('#groessenauswahl').val() )+'px');
		setPreise();
	});

	
	// Abfrage der eingegebenen Daten VOR Submit des Formulars
	jQuery('.to_wk_btn').click(function(e){
		if ( jQuery('#order_stickservice').val() == 1 ) {
			e.preventDefault()
			var error = false;
			
			// Wenn der Fokus noch nicht vom Feld genommen wurde, übernimmt der IE die neuen Werte nicht
			jQuery('#orderAmount').attr('value',jQuery('#dummy_orderAmount').attr('value'));
			jQuery('#productId').attr('value',jQuery('#dummy_productId').attr('value'));
			checkAvailability(jQuery('#productId').attr('value'));
			setPreise();

			// Fehlermeldung, wenn keine Position ausgewählt ist
			if ( !jQuery('#stick_position').val() ){
				jQuery('label[for=stick_position]').css('color','#F00');
				error = true;
			}else {
				jQuery('label[for=stick_position]').removeAttr('style');
			}
			
			// Fehlermeldung, wenn kein Text eingegeben wurde
			if ( !jQuery('#stick_text_1').val() ){
				jQuery('label[for=stick_text_1]').css('color','#F00');
				error = true;
			}else {
				jQuery('label[for=stick_text_1]').removeAttr('style');
			}
			
			
			if ( error == false ) {
				jQuery('#cart_quantity').submit();
			}
			
			return false;
		}
	});
	
	// Abfrage 'Eigenes Logo'
	jQuery('#anfrage_submit').click(function(e){
		e.preventDefault();
		
		var error = false;
		// Fehlermeldung, wenn keine Datei ausgewählt ist
		if ( !jQuery('#stick_logo').val() ){
			jQuery('label[for=stick_logo]').css('color','#F00');
			error = true;
		}else {
			jQuery('label[for=stick_logo]').removeAttr('style');
		}
		// Fehlermeldung, wenn keine Position ausgewählt ist
		if ( !jQuery('#stick_position').val() ){
			jQuery('label[for=stick_position]').css('color','#F00');
			error = true;
		}else {
			jQuery('label[for=stick_position]').removeAttr('style');
		}
		
		/*// Fehlermeldung, wenn keine Art eingegeben wurde
		if ( !jQuery('#bestickung:checked').size() && !jQuery('#bedruckung:checked').size() ){
			jQuery('div.content.active > p.error').show().children('#error_art').show();
			error = true;
		}
		else {
			jQuery('#error_art').hide();
			if ( jQuery('#error_datei:hidden').size() && jQuery('#error_position:hidden').size() ) {
				jQuery('div.content.active > p.error').hide();
			}
		}*/
		
		if ( error == false ) {
			jQuery('#cart_quantity').attr('action',jQuery('#logo_action').val());
			
			jQuery('#cart_quantity').submit();
		}
		
		return false;
	});
	
	if ( jQuery('#logo_uploaded').size() > 0 ) {
		jQuery('a.logo_uploaded').fancybox();
		setTimeout(function(){jQuery('a.logo_uploaded').click();},100);
	}
	
	jQuery('a.fancybox_close').click(function(e){
		e.preventDefault();
		$.fancybox.close();
		
		return false;
	});
});
