$(document).ready(function() {
	if (window.location.hash!='') {
		var hash = window.location.hash.replace(/#vid_/, "");
		if ($("#v_"+hash).length!=0) {
			$("#v_"+hash).trigger('click');
			return false;
		}
	}

	afterAjaxLoad();
});

function effekt(id, wert) {
	if(wert==0 || wert=='nein' || wert=='no') $('#'+id).slideUp('slow');
	else $('#'+id).slideDown('slow');
}
function defaultText(elem, val1, val2) {
	if(elem.value.replace(/^\s+/,'').replace(/\s+$/,'')==val1) elem.value = val2;	
}

function afterAjaxLoad() {
	$('.formInput select:not(".noUI")').selectmenu({maxHeight: 200, style:'dropdown'});
	$('.numeric').forceNumeric();
	
	$(".imgStart").click( function() {
		var rel = $(this).attr('rel').replace(/_start$/,'');
		if ($(".img[rel='"+rel+"']:eq(0)").length!=0) {
			$(".img[rel='"+rel+"']:eq(0)").trigger('click');
		}
		return false;
	});
	$(".img").fancybox({
		'titlePosition'	: 'over',
		'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
		    return (currentArray.length>1 || title!='')?'<span id="fancybox-title-over"><span class="page">Bild '+(currentIndex + 1)+' / '+currentArray.length+'</span>'+title+'&nbsp;</span>':'';
		},
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'onComplete'	: function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").show();
			}, function() {
				$("#fancybox-title").hide();
			});
		}
	});
	$(".vid").each(function() {
		this.href = this.href.replace(new RegExp("watch\\?v=", "i"), 'v/');
	});
	$(".vid").fancybox({
		'padding'	: 0,
		'autoScale'	: false,
		'title'		: this.title,
		'titleShow'	: false,
		'width'		: 680,
		'height'	: 495,
		'type'		: 'swf',
		'swf'		: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
		}
	});

	$(".frameMode").click(function () {
		var obj = this;
		var target = obj.href+((obj.href.indexOf('?')!='-1')?'&':'?')+'frameMode=true';
		$.fancybox({
			'titlePosition'	: 'inside',
			'titleShow'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'showNavArrows'	: false,
			'href'			: target,
			'width'			: 908,
			'height'		: '90%',
			'orig'			: obj,
			'type'			: 'iframe'
		});
		return false;
	});
	$(".teaser").find('span.zoom').remove();
	$(".teaser").css({cursor: 'pointer'}).prepend('<span class="zoom">&nbsp;</span>').click(function () {
		var obj = this;
		var target = $(obj).css('backgroundImage').replace(/\"/g, '').slice(4, -1);
		$.fancybox({
			'titlePosition'	: 'over',
			'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
				return (currentArray.length>1 || title!='')?'<span id="fancybox-title-over"><span class="page">Bild '+(currentIndex + 1)+' / '+currentArray.length+'</span>'+title+'&nbsp;</span>':'';
			},
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'onComplete'	: function() {
				$("#fancybox-wrap").hover(function() {
					$("#fancybox-title").show();
				}, function() {
					$("#fancybox-title").hide();
				});
			},

			'href'			: target,
			'orig'			: obj,
			'type'			: 'image'
		});
		return false;
	});
}

// forceNumeric() plug-in implementation
jQuery.fn.forceNumeric = function () {
	return this.each(function () {
		$(this).keydown(function (e) {
		var key = e.which || e.keyCode;
			if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
				key >= 48 && key <= 57 ||
				key >= 96 && key <= 105 ||
				key == 190 || key == 188 || key == 109 || key == 110 ||
				key == 8 || key == 9 || key == 13 ||
				key == 35 || key == 36 ||
				key == 37 || key == 39 ||
				key == 46 || key == 45)
				return true;
			
			return false;
		});
	});
}
