/*@cc_on'abbr article aside audio canvas details figcaption figure footer header hgroup mark menu meter nav output progress section summary time video'.replace(/\w+/g,function(n){document.createElement(n)})@*/

DialogPerfect = function() { };

$(window).load(function() {
	$('body').supersleight();
});

$(document).ready(function() {
	$('.mediumBeschriftung').each(function() {
		$(this).parent().prev('a').attr('title', $(this).text());
	});
	$('ul.menu > li:has(.submenu)').hover(function() {
        x1 = $(this).children('a').position();
        $(this).children('ul.submenu').show();
        x2 = $(this).children('ul.submenu').position();
		if(x2.left > x1.left){
            $(this).children('ul.submenu').css({left: (x1.left-10)+'px',right:'auto'});
        }
		$(this).children('a.aktiv').addClass('hover');
		if($(this).children('ul.submenu')){
    		$(this).children('a').addClass('hover');
		}
	}, function() {
		$(this).children('ul.submenu').hide();
		$(this).children('a.aktiv').removeClass('hover');
		if($(this).children('ul.submenu')){
    		$(this).children('a').removeClass('hover');
		}
	});

    $('[data-preview-image]').hover(function(ev) {
		var path = encodeURI(baseUrl + 'medien/' + $(this).attr('data-preview-image') + '/200/' + $(this).attr('data-preview-imagetext') + '.jpg');

		$('<div class="previewImage"><img title="' + $(this).attr('data-preview-imagetext') + '" src="' + path + '" /></div>').appendTo('body');
	}, function(ev) {
		$('.previewImage').remove();
	});
	/*$('[data-preview-image]').click(function() {

    });*/
	$('[data-preview-image]').mousemove(function(ev) {
		$('.previewImage').offset({ top: ev.pageY - 80, left: ev.pageX + 5 });
	});

    $('a.fancybox').fancybox({ titlePosition: 'inside' });
	var optvalue = $('#opt').val();
	if(optvalue){
	    $('label.optional').next('input[type=text][value=""]').attr('value', optvalue).css('color', '#AAAAAA').focus(function() {
			if(this.value === optvalue) {
				this.value = '';
				$(this).css('color', '#017E7D');
			}
		}).blur(function() {
			if(this.value === '') {
				this.value = optvalue;
				$(this).css('color', '#AAAAAA');
			}
		});
	}
	$('#katalogpfeil').click(function() { $(this).parent().toggleClass("offBox"); })
	$('.katalogpfeiltext').click(function() { $(this).parent().toggleClass("offBox"); })
	$('#newspfeil').click(function() { $(this).parent().toggleClass("offBox"); })
	$('.newspfeiltext').click(function() { $(this).parent().toggleClass("offBox"); })
	$('.katUeberschrift').click(function() { 
		$(this).next().click(); 
	});
	$('.kategoriepfeil').click(function() {
		$(this).toggleClass("kategoriepfeilOffBox");
		$(this).next().toggleClass("offBox"); 
	});

	jQuery().slideshow(5000);
});

$.fn.slideshow = function(speed){
    var position= 0;
	var id = '.slideItem';
	var nChildren = $(id).length;

	if(nChildren > 1)
	{
		$(document).everyTime(speed, function() {
			if(position >= nChildren - 1) position = nChildren - 1;
			$(id + ':eq(' + position + ')').fadeOut(1000, function() {

			});

			position++;

			if(position > nChildren - 1) position = 0;
			$(id + ':eq(' + (position) + ')').fadeIn(1000);
		});
	}
}

jQuery.fn.inputBlurText = function(settings) {
	return this.each(function() {
		$(this).focus(function() {
			if($(this).attr('value') === settings.text) {
				$(this).attr('value', '');
			}
		}).blur(function() {
			if($(this).attr('value') === '') {
				$(this).attr('value', settings.text);
			}
		});
	});
};

DialogPerfect.initMap = function(options) {
	if (GBrowserIsCompatible())
	{
		var defaults = {
			zoom: 13,
			center: new GLatLng(51.726284, 8.797302),
			divContainer: 'googleMap'
		};

		var options = $.extend(defaults, options);

		this.searchAddress = options.searchAddress;

		this.map = new GMap2(document.getElementById(options.divContainer));
		this.map.setCenter(options.center, options.zoom);
		this.map.enableScrollWheelZoom();
		this.map.addControl(new GSmallMapControl());

		marker = new GMarker(options.center);
		this.map.addOverlay(marker);

		GEvent.addListener(marker, "mouseover", function() {
			marker.openInfoWindowHtml(options.markerText);
		});

		GEvent.addListener(marker, "mouseout", function() {
			marker.closeInfoWindow();
		});
	}
};

DialogPerfect.findAddress = function(from, to) {
	if(this.gdir === undefined)
	{
		this.gdir = new GDirections(this.map, document.getElementById("maps_ergebnis"));

		GEvent.addListener(this.gdir, "load", function() { });
		GEvent.addListener(this.gdir, "error", function() {
			var statusCode = DialogPerfect.gdir.getStatus().code;

			if (statusCode == G_GEO_UNKNOWN_ADDRESS || statusCode == G_GEO_MISSING_QUERY)
			{
				alert("Die gesuchte Adresse wurde nicht gefunden.");
			}
			else if (statusCode == G_GEO_SERVER_ERROR || statusCode == G_GEO_BAD_KEY || statusCode == G_GEO_BAD_REQUEST)
			{
				alert("Der Geo-Server wurde nicht erreicht.");
			}
			else alert("Es ist ein fehler aufgetreten.");
		});
	}

	if(to === undefined) to = this.searchAddress;

	this.gdir.load("from: " + from + " to: " + to, { "locale": "de_DE" });
};

jQuery.fn.ticker = function(options) {
	var defaults = {
		speed: 50
	};

	var options = $.extend(defaults, options);

	var leftPosition = $(this).width();
	var startPosition = leftPosition;
	var tickerDiv = $(this).children().eq(0);
	var isMouseOver = false;

	$(tickerDiv).css('left', leftPosition + 'px').hover(function() {
		isMouseOver = true;
	}, function() {
		isMouseOver = false;
	});

	jQuery.fn.ticker.tickerTimer = function() {
		if(!isMouseOver) leftPosition -= 3;

		if(leftPosition < -(tickerDiv.width()))
		{
			leftPosition = startPosition;
		}

		$(tickerDiv).css('left', leftPosition + 'px');

		setTimeout("jQuery.fn.ticker.tickerTimer()", options.speed);
	};

	jQuery.fn.ticker.tickerTimer();
};

jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: baseUrl + 'images/blank.gif',
		apply_positioning: false
	}, settings);

	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				/* apply position to 'active' elements */
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};
