$(function(){
	// Navigation
	$('.nav li').hover(function(){
			$(this).find('ul:first').show();
		}, function(){
			$(this).find('ul:first').hide();
		}
	);
	
	// Searchbox
	$('#search input[type="text"]').focus(function() {
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	}).blur(function() {
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	
	//Headeranimation
	var image_count;
	var current_image=0;
	var fade_speed = 1500;
	var fade_interval = 8000;
	
	function feature_rotate() {
		old_image = current_image % image_count;
		new_image = ++current_image % image_count;
		$('.img-rotate:eq(' + new_image + ')').fadeIn(fade_speed);
		$('.img-rotate:eq(' + old_image + ')').fadeOut(fade_speed);
	}
	
	image_count = $('.img-rotate').hide().size();
	$('.img-rotate:eq(' + current_image + ')').show();
	setInterval(feature_rotate, fade_interval);
	
	//Tabs
	$('ul.tabs').tabs('div.panes > div').history();
	
	//Save Images
	$('img#sb-content, .thumbs.pane img, .thumb img, ').live('mousedown', function(e){
		if(e.which == 3){
			return false;
		}
	});
	
	//Rating
	var OldPos = 1;
	$('.rating.edit')
	.mouseover(function(){
		$(this).addClass('doedit');
	}).mouseout(function(){
		if($(this).hasClass('doedit'))
		{
			$(this).removeClass('doedit rating1 rating2 rating3 rating4 rating5 rating6').addClass('rating' + OldPos);
		}
	}).click(function(){
		$(this).removeClass('doedit');
		OldPos = pos;
	}).html($('<input type="hidden" name="ShopArticleReview[Rating]" value="1">')).addClass('rating1');
	
	$('.rating.edit.doedit').live('mousemove', function(e){
		pos = Math.ceil((e.clientX - $(this).offset().left) / 18);
		$(this).removeClass('rating1 rating2 rating3 rating4 rating5 rating6').addClass('rating' + pos);
		$(this).find('input').attr('value', pos);
	})
	
	//Law Popup
	$('.shop_de_order_input_law dl label, .shop_en_order_input_law dl label').click(function(){
		title = $(this).text();
		
		$.get('?Controler=Template&Function=get&Template=' + $('meta[name=Language]').attr('content') + '/' + $(this).attr('for').toLowerCase(), null, function(resp){
			Shadowbox.open({
				content: resp,
				player: 'html',
				title: title,
				height: 450,
				width: 612
			});
		});
		
		return false;
	})
	
	//ArtlistPrint
	$('.shoparticlelist .print').click(function(){
		Shadowbox.open({
			content: $(this).attr('href'),
			player: 'iframe',
			title: $(this).text(),
			height: 450,
			width: 612
		});
		
		return false;
	})
	
	//ArtlistClick
	$('.shoparticlelist li').click(function(e,d){
		href = $('a', this).attr('href');
		(e.metaKey == true) ? window.open(href) : location.href = href;
	}).css('cursor','pointer')
	
	//ArtDetail Thumb Click Gallery
	$('#metathumbwarp .thumb img').click(function(){
		$('.thumbs.pane a:eq(0)').triggerHandler('click');
	}).css('cursor','pointer')
	
	//Region Select
	$('.field-region #Region').change(function(){
		$('.field-country #Country').val((this.selectedIndex == 0) ? 'Deutschland' : '');
	})
});

function formatPrice(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).replace('.',',');
	
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(s)) {
		s = s.replace(rgx, '$1' + '.' + '$2');
	}
	
	if(s.indexOf(',') < 0) { s += ',00'; }
	if(s.indexOf(',') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
