/**
 * jQuery work-around making suckerfish-like CSS dropdown menues
 * work in older browsers like IE 6
 */
$.fn.suckerfish = function() {
	if ($.browser.msie && $.browser.version*1 < 7){
		this.find('li').hover(function(){
			$(this).addClass('jshover');
		},
		function(){
			$(this).removeClass('jshover');
		});
	}
}

/**
 *  adjust image sizes
 */
$.fn.adjustImgSizes = function(){
	var offerlist	= $(this);
	if(offerlist.length == 0) return;
	
	var maxwidth = $('a.offer-preview', this).width();
	if(window.console) window.console.debug(maxwidth);
	
	offerlist.each(function(){
		var offerimgs = $('a.offer-preview', this);
		if(offerimgs.length == 0) return;

		offerimgs.each(function(){
			var link	= $(this);
			var img		= $('img', link);
			var w		= img.attr('width');
			var h		= img.attr('height');
			if(window.console) window.console.debug(img, w, h);
			if(w > maxwidth && h > 0){
				img.css({
					width	: maxwidth,
					height	: Math.round(maxwidth * h / w)
				});
				if(window.console){
					window.console.log('imgsize changed to:')
					window.console.debug(w, h);
				}
			}
		});
	});
}

/**
 * execute the following when the DOM is ready:
 */
$(document).ready(function() {
	$('#mainmenu').suckerfish();

	$('.offerlist').adjustImgSizes(); 
	
	// one-click language selection:
	if($('#locale-selection select option').size() <= 1){
		$('#lcs').css('display', 'none');
	} else {
		$('#locale-selection select').change(function(){ this.form.submit(); });
		$('#locale-selection input').css('visibility', 'hidden');
	}
	
	// one-click paging:
	$('.paging form select').change(function(){
		this.form.submit();
	});
	$('.paging form input:submit').hide();
});