/*
$(document).ready(function(){
	
	$("div.product-item, div.eyecatcher").mouseenter(function(){
	      $(this).addClass("mouseover");
	    }).mouseleave(function(){
	      $(this).removeClass("mouseover");
	});

	
});
*/

$(document).ready(function(){
	/**** anpassungen für iframe einbindung *****************************************************/
	/*
	if(top!=self){
		$("html").css('margin','0px');
		$("##page_margins").css({'width':'960px'});
	}
	*/

	/**** jQuery.getUrlParam() *****************************************************/
	$.getUrlParam = function(name) {
		var results = new RegExp('[?&]' + name + '=([^&#]*)').exec(window.location.href);
		if (!results) {
		return 0;
		}
		return results[1] || 0;
	};	
	
	
	/**** ajax search.result *****************************************************/
	/*
	$(".ui-tabs a").live('click',function(){
		var url = $(this).attr('href');
		var data = {layout:'ajax',listonly: 'true'};
		var page = $(this).attr('rel');
		$.ajax({
			type: "POST",
			url: url,
			data: data,
			success: function(result){
				window.scrollTo( 0, $("#main").offset().top );
				prevResult = $('div.result>div.product-list');
				$('div.result').append(result);
				newResult = $('#product-list-'+page);
				newResult.hide().css('position','absolute');
				prevResult.fadeOut(500);
				newResult.fadeIn(500,function(){
					newResult.css('position','relative');
					prevResult.remove();
				});

			}
		});
		return false;
	});
	*/

	/*** jquery.datepicker default ****************************************************************/
	$.datepicker.setDefaults({showAnim: 'fadeIn',duration: 0});
	/*
	$.datepicker.setDefaults({
		showOn: 'both',
		buttonImageOnly: true,
		buttonImage: '#envBean.getImage()#content/ico_calendar.gif',
		buttonText: 'Kalender',
		showAnim: 'fadeIn',
		duration: 0,
		changeYear: true,
		changeMonth: true
	});
	*/
	/*** jquery.validate default ****************************************************************/
	$.validator.setDefaults({ wrapper: 'span'	});


	/**** ThickBox Init *****************************************************************/
	$(".thickbox").thickbox({loadimage: "/media/image/content/loading-big.gif", width: 800, height: 400});

	/**** Dialog *****************************************************************/
	$(".dialog:not(.dialog-processed)").addClass('dialog-processed')
	.bind('click',function(e){
		var $this = $(this);
		var options = $.extend(
			{
				title: $this.attr("title"),
				url: $this.attr("href"),
				data: {},
				success: function(){},
				method: 'post',
				target: $(this).attr("target") || 'dialog',
				width: 'auto',
				height: 'auto',
				position: ['center','center'],
				cache: true,
				iframe: false
			},
				$(this).data('dialog')
		);

		// percent to pixel
		if(options.height.indexOf('%') > 0){
			options.height = $(window).height() / 100 * parseInt(options.height);
		}

		if($('input,select,textarea',$(options.data)).length){
			var data = $('input,select,textarea',$(options.data)).serialize();
		}else{
			var data = $(options.data).serialize();
		}

		if($("#"+options.target).length == 0){
			$("body").append('<div id="'+options.target+'" class="dialog-window" style="display:none;"></div>')
		}
		
		var $target = $("#"+options.target);
		

		
		//if($target.dialog("isOpen") == true){
			
		//}else{
			$target.dialog({
				modal: true,
				zIndex: 99999,
				width: options.width,
				height: options.height,
				position: options.position,
				buttons: {}
			});
		//}
		
		$target.dialog( "option", "title", options.title );
		
		if(options.iframe){
			var $form = $(options.data);
			var iframename = options.target+'iframe';			
			if(! $form.attr('method')) $form.attr('method','POST')
			$form.attr('action',options.url).attr('target',iframename);
			$target.addClass('loading');
			$target.dialog( "option" , {
				width: options.width,
				height: options.height
			}).dialog('option','position',options.position);

			if($('iframe[name="'+iframename+'"]',$target).length) $('iframe[name="'+iframename+'"]',$target).remove()
			$target.append('<iframe name="'+iframename+'" src="about:blank" style="width:99%; height:95%; margin: auto; display: block; border: 1px solid #ccc" ></iframe>');
			$('iframe[name="'+iframename+'"]',$target).load(function(){
				$target.removeClass('loading');
				options.success.call();
			});
			//console.log($form.get(0).tagName)
			if($form.get(0).tagName == 'FORM'){
				//console.log($target,$form)
				$form.submit();
			}else{
				$('iframe[name="'+iframename+'"]',$target).attr('src',options.url)
			}
			$('*:not(iframe[name="'+iframename+'"])',$target).remove();
		
			
		}else if( true || !options.cache || options.url != $target.data('url') ){
			$target.data('url',options.url);
			$target.addClass('loading');
			$target.empty().dialog('option','position',options.position);
			$.ajax({
				cache: false,
				url: options.url,
				type: options.method,
				data: data + '&layout=ajax',
				error: function(jqXHR, textStatus, errorThrown){
					$target.html(jqXHR.responseText)
					.removeClass('loading')
					.dialog('option','position',options.position);
				},
				success: function(response, textStatus, jqXHR){
					$target.removeClass('loading');
					$target.html(response);
					
					options.success.call();
					
					// maximale höhe und breite für den fenster optimieren
					$target.css({ 
						maxWidth: $(window).width()-180,
						maxHeight : $(window).height()-180
					});
					
					// append buttonset
					var $buttonset = $('.buttonset', $target);
					if($buttonset.length){
						$target.dialog( "option", "buttons", { "Ok": function() { $(this).dialog("close"); } } );
						$('.ui-dialog-buttonset').empty();
						$($buttonset).hide()
						.children()
						.clone(true)
						.prependTo('.ui-dialog-buttonset');
					}else{
						$target.dialog( "option", "buttons", {} );
					}
					
					// reset dialog options
					$target.dialog( "option" ,{
						position: options.position,
						title: options.title //title neu setzen
					});
					
					// höhe und breite festsetzen
					$target.css({ 
						//height: $target.innerHeight(), 
						width: $target.innerWidth()+20 //scrollbar reservation
					});

				}
				
			});
			
		}	
		e.preventDefault();
		return false;
	});
	/**** equal elements height *****************************************************/
	
	$(".equal").each(function(){
		equalHeight($(".equal-item",this));	
	});
	//equalHeight("#main > div");
	
	function equalHeight(e){
		var equalheight = 0;
		var $equalitems = $(e);
		$equalitems.each(function(){
			equalheight = Math.max(equalheight,$(this).height());
		}); 
		//alert(equalheight)
		$equalitems.height(equalheight);
	}
	
	/**** tooltip-ajax **************************************************************/
	$('a.tooltip-ajax').tooltip({ 
		tip:'#tooltip',
		position: 'center left',
		effect: 'fade',
		offset: [0,-15],
		predelay: 100,
		delay: 100,
		onBeforeShow: function(event){
			$('#tooltip').width(600);
			var url = $(event.currentTarget.getTrigger()).attr('rel');
			var cache = $("div[rel='"+url+"']");
			if(cache.length){
				$('#tooltip').html(cache.html());
				//$('#tooltip>div').slideDown();
			}else{
				$('#tooltip').html('<div style="margin:20px;text-align:center"><img src="../image/content/loading-3.gif" /></div>');
				$('body').append('<div rel="' + url + '" style="display:none"></div>');
				$("div[rel='"+url+"']").load(url, function(){
					$('#tooltip').html( $("div[rel='"+url+"']").html() );
					//$('#tooltip>div').slideDown();
				});
			}
			
		},
		onShow: function(event){
			
		},
		events: {  def: "mouseenter,mouseleave"}
	}); 

	
	/**** FactFinder Suggest *****************************************************************/
	$textsearch = $("input[name='textsearch']");
	$textsearch.autocomplete("/factfinder/suggest.ff", {
		selectFirst: false,
		extraParams: {m: 'ek_de'},
		formatItem: function(item) {
			var record = item[0].split('###');
			return record[0];
		},
		formatResult: function(item) {
			var record = item[0].split('###');
			return record[0];
		}
	});
	


			
});

