
$(document).ready(function(){
	$('body').css('background-image', 'none');
	$('body').append('<div id="herma-fluss"></div>');
	fluss();
	$(window).resize(function(){
		fluss();
	});
	
	$('#logo').click(function(){
		window.location = "/";
	});
	$('#logo').css('cursor', 'pointer');
	
	$('#registerButton, #loginButton, #lostPassButton').live("mouseover", function(){
		$(this).addClass('hover');
	});
	$('#registerButton, #loginButton, #lostPassButton').live("mouseout", function(){
		$(this).removeClass('hover');
	});
	
	$('select.J_legalform_dropdown').live('change', function() {
		if ($(this).val()=='other') {
			var e = $(this).parent();
			var id = $(this).attr('id');
			var name = $(this).attr('name');
			var classname = $(this).attr('class').replace('J_legalform_dropdown','');
			$(this).remove();
			e.append('<input type="text" name="'+name+'" id="'+id+'" class="'+classname+'" />');
			$('#'+id).focus();
		}
	});
	
        if (navigator.userAgent.indexOf('MSIE') != -1) {
            $('.sendOrderButton').attr("value", '');
            $('.sendOrderButton').html('');
        }
        
   $('.but-white').mouseover(function(){
   	$(this).addClass('but-white-hover');
   })
   $('.but-white').mouseout(function(){
   	$(this).removeClass('but-white-hover');
   })
});

function fluss() {
	var pos = $('#mainmenu_left').offset();
	var wid = $('#herma-fluss').width();
	//var posLeft = pos.left - 172 - wid;
	var posLeft = pos.left - wid;
	if (posLeft < 0) {
		$('#herma-fluss').css('left', posLeft + "px");
	}
	else {
		$('#herma-fluss').css('left', "0px");
	}
}


// reset zip form and prefill with zipcode 
function reset_check_zip(input_zip, holder_zip) {
	var zip = $('input#'+holder_zip).val();
	if (zip=='') $('div#selectZipDropdown').remove();
	$('input#'+input_zip).val(zip).focus();
}

//check zip and put city
function check_zip(input_zip, holder_zip, holder_city, url) {
	
	if ($('input#'+input_zip).val().length>0) {
		var parse = isNaN(parseInt($('input#'+input_zip).val())) ? '' : $('input#'+input_zip).val();
		$('input#'+input_zip).val(parse);
	}
	
	if ($('input#'+input_zip).val().length>=4) {
		
		var zip = $('input#'+input_zip).val();
		
		$('input#'+holder_zip).val('');
		$('input#'+holder_city).val('');
		
		$('input#'+input_zip).css({'background-image':'url(/img/inputloader.gif)', 'background-repeat':'no-repeat', 'background-position':'right 2px'});
		
		$.ajax({
			type:'POST',
			url:url,
			data:'zip='+zip,
			dataType:'json',
			success:function(data) {
			
				//var pos = $('input#'+input_zip).position();
				var pos = $('input#'+input_zip).offset();
				
				if (data['stat']=='ok') {
					setMultipleCity(data['zip'], data['city'], input_zip, holder_zip, holder_city);
				
				} else if (data['stat']=='select') {
					
					var dropdown = '';
					for(i=0; data['select'].length>i; i++) {
						dropdown += '<a href="javascript:void(0);" onclick="setMultipleCity(\''+data['select'][i]['zip']+'\', \''+data['select'][i]['city']+'\', \''+input_zip+'\', \''+holder_zip+'\', \''+holder_city+'\')">'+data['select'][i]['zip']+' '+data['select'][i]['city']+'</a><br />';
					}
					buildCheckDropdown(dropdown, pos.left, (pos.top+23));
					
				} else {
					buildCheckDropdown($('div#checkZipNofoundsText').html(), pos.left, (pos.top+23));
				}
				
				setTimeout("$('input#"+input_zip+"').css({'background-image':'none'});", 250);
				
			}
		});	
	} else {
		$('div#selectZipDropdown').remove();
	}
	
	$('input#'+input_zip).live('blur', function() {
		$('input#'+input_zip).val( $('input#'+holder_zip).val()+' '+$('input#'+holder_city).val() );
		
		// jump to next field
		var idx = $('input#'+input_zip).index('input:visible[type=text]') + 1;
		$('input:visible[type=text]').eq(idx).focus();
	});
	
	$('body').live('click', function() {
		$('div#selectZipDropdown').remove();
	});
	
	$(window).resize(function() {
		$('div#selectZipDropdown').remove();
	});
	
}



function buildCheckDropdown(text, left, top) {
	if (!$('div#selectZipDropdown')[0]) $('div#selectZipDropdown').remove();
	$('body').append('<div id="selectZipDropdown" style="left:'+left+'px; top:'+top+'px;"><div class="scroll-pane">'+text+'</div></div>');
	setTimeout("$('div#selectZipDropdown').find('div.scroll-pane').jScrollPane({showArrows:false, scrollbarWidth:9, scrollbarMargin:20, dragMinHeight:30, dragMaxHeight:50});", 25);
}

//set city after multiple select
function setMultipleCity(zip, city, input_zip, holder_zip, holder_city) {
	$('input#'+input_zip).val(zip+' '+city);
	$('input#'+holder_zip).val(zip);
	$('input#'+holder_city).val(city);
	$('div#selectZipDropdown').remove();
	$('input#'+input_zip).blur();
}

function noticebox_big(headline, url, show, link, linktext, content) {
	if (show==true) {
		
		if (url!='') {
			$.ajax({
				type:'GET',
				url:url,
				dataType:'html',
				success:function(data) {
					$('div#noticebox_big').find('div.scroll-pane').html(data);
				}
			});
		} else {
			$('div#noticebox_big').find('div.scroll-pane').html(content);
		}
		
		if (linktext=='' && link!='') linktext = link;
		if (link=='') $('div#noticebox_big').find('a.J_link').hide();
		
		$('div#noticebox_big').find('a.J_link').attr('href',link).text(linktext);
		$('div#noticebox_big').find('div.headline_big').html(headline);
		$('div#noticebox_big').find('a.J_link').attr('href',link).text(linktext);
		$('div#noticebox_background').css({'opacity':0.5, 'height':$(document).height()}).show();
		$('div#noticebox_big').show();
		
		setTimeout("$('div#noticebox_big').find('div.scroll-pane').jScrollPane({showArrows:false, scrollbarWidth:9, scrollbarMargin:20, dragMinHeight:50, dragMaxHeight:50});", 500);
		setTimeout("$('div#noticebox_big').find('div.scroll-pane')[0].scrollTo(0);", 510);
		
	} else {
		$('div#noticebox_background').hide();
		$('div#noticebox_big').hide();
	}
}

