/* #################################################
# Projekt	: Base JavaScripts
# Stand		: 10.02.09
# Autor		: Daniel Zander, Source-Media.com
#################################################### */



// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// FUNCS
// ----------------------------------------------------------------------

$.fn.clearDef = function(){
	$(':input', this).each(function(){
		if ($(this).val() == $(this).attr('title'))
			$(this).val('');
	});	
};


function email(v) {
	return /^[\w-_\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(v);
}

function viewAlert(f,v) {
	$('.l',f).removeClass('alert'); // reset
	
	var a = v.split('#');
	for (i in a) {
		if (a[i]) {
			$(':input[name='+a[i]+']',f).parents().prev('.l').addClass('alert');
			var fail=1;
		}
	}
	return (fail ? false : true);
}

function checkAlert(f,v) {
	var n = new Array();
	var a = v.split('#');
	for (i in a) {
		var inp = ':input[name='+a[i]+']';
		
		if (a[i].match(/mail/i) && !email($(inp).val()) // email
		|| !$(inp,f).val() // empty
		) {
			n[i] = a[i];
		}
	}
	return viewAlert(f,n.join('#'));
}

jQuery.fn.typeWatch = function(event,wait,fire){
	this.each(function(){
		var input = this;
		var time = null;
		$(input).bind(event, function(){
			if (time)
				clearTimeout(time);
			time = setTimeout(function(){
				fire(input);
			}, wait);
		});
	});
};

// ---------- smplay ----------

function smplayFinished() { $('a.snd.set').removeClass('set'); }

function smplayReady() { $('a.snd.set.load').removeClass('load'); }

$.fn.smplay = function(){

	var url = dir_pub + ($(this).attr('rel') ? $(this).attr('rel')+'.mp3' : $(this).attr('href'));
	var smplayCall = function(url){
		try {
			var fla = $.browser.msie ? frames['smplay'].window["smplay"] : document["smplay"];
			fla.smplayStart(url);
		} catch(e) {
			window.setTimeout(function(){ smplayCall(url); }, 500);
		}
	};

	if (!$('#smplay').attr('id')) {
		if ($.browser.msie)
			$('body').append('<iframe id="smplay" name="smplay" src="'+dir_elm+'p/smplay.php?ie" width="1" height="1" scrolling="no" frameborder="0"></iframe>');
		else
			$('<span></span>').appendTo('body').load(dir_elm+'p/smplay.php');
	}

	if ($(this).is('.set')) {
		var fla = $.browser.msie ? frames['smplay'].window["smplay"] : document["smplay"];
		$(this).removeClass('set');
		fla.smplayStop();
	} else {
		$('a.snd.set').removeClass('set');
		$(this).addClass('set');
		$(this).addClass('load');
		smplayCall(url);
	}
};



// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// READY
// ----------------------------------------------------------------------

$(function(){//ready


// ---------- form ----------

$(':radio, :checkbox').livequery(function(){ $(this).addClass('auto'); });

$(':input[charset]').livequery('keypress',function(e){
	var c = String.fromCharCode(e.charCode == undefined ? e.keyCode : e.charCode);
	if (!e.ctrlKey && c >= ' ' && !c.match(new RegExp('['+$(this).attr('charset')+']')))
		return false;
	return true;
}).livequery('blur',function(){
	if ($(this).val())
		$(this).val($(this).val().replace(new RegExp('[^'+$(this).attr('charset')+']','g'),''));
});

// set default value
$(':input').livequery(function(){
	if (!$(this).val())
		$(this).val($(this).attr('title'));

	// onfocus clear default
	$(this).focus(function(){
		if ($(this).val() == $(this).attr('title'))
			$(this).val('');
	});
});


// clear default
$('form').livequery('submit',function(){
	$(this).clearDef();
	if ($(this).attr('name')=='form')
		return false;
});


// preview
$('#Form :input').livequery('click',function(){
	$('#Preview').slideDown();
});
$('.btn.preview').livequery('click',function(){
	$('#Preview').animate({opacity: 0.1}, function(){
		$(this).animate({opacity: 1});
	});
});
$('.preview:input').livequery(function(){
	$(this).typeWatch('keyup',500,function(obj){
		var opt = $(obj).metadata();
		var val = opt.textile ? liveTextile($(obj).val()) : $(obj).val();
		
		if (!opt.attr)
			$(opt.target).html(val);
		else
			$(opt.target).attr(opt.attr, val);
	});
});


// ---------- diverse ----------

if ($.browser.msie)
	$('a').attr('hidefocus','hidefocus');

// ticker
$('div#tick marquee').hover(function(){
	$(this).attr('scrollamount',0);
},function(){
	$(this).attr('scrollamount',4);

});

// zoom icon
$('a.thickbox img').livequery(function(){ $(this).after('<p />'); });

$('a[href$="mp3"]').livequery(function(){
	$(this).addClass('snd');
});

$('a.snd').livequery('click',function(){
	$(this).smplay(); return false;
});

$('a.show').livequery('click',function(){
	if ($($(this).attr('rel')).css('display') == 'none') {
		$('.navForm').slideUp('fast');
		$($(this).attr('rel')).slideDown('fast');
	} else {
		$($(this).attr('rel')).slideUp('fast');
	}
	return false;
});

$('div.navForm').livequery(function(){
	var $this = this;
	
	$('a',$this).click(function(){
		
		var inp = $(':input',$this).val();
		if (inp) {
			// console.log('page: ' + $(this).attr('href') + inp);
			if ($($this).is('.pge'))
				inp = parseInt($(this).attr('rel')) * (parseInt(inp) - 1);
			window.location = $(this).attr('href') + inp;
		}
		return false;
	});
});

// info toggle
$('.info a').livequery('click',function(){
	var p = $(this).parent().next('p');
	$(this).html( (p.is(':hidden') ? '(&minus;)':'(+)') );
	p.slideToggle('fast');
	return false;
});


// ---------- media ----------

$.fn.media.defaults.flvPlayer = dir_elm+'p/flvplay316.swf';

$('a[href$="flv"]').livequery(function() {
	if ($(this).is('.ontop')) return; // filter adblock
		
	$(this).media({
		src: dir_pub+$(this).attr('href'),
		width: 360,
		height: 296,
		attrs: {allowfullscreen:'true', wmode:'transparent'},
		params: {allowfullscreen:'true'},
		flashvars: {image:dir_pub+$(this).attr('href').replace(/flv/,'jpg'), 
			backcolor:'0x000000', frontcolor:'0xcccccc', lightcolor:'0xD00077'},
		bgColor: false,
		caption: false
	});
});

$('a[href*="youtube.com/v/"]').livequery(function(){
	if ($(this).is('.ontop')) return; // filter adblock
	
	$(this).media({ // add 2 url: &rel=0
		width: 360,
		height: 302,
		attrs: {allowfullscreen:'true', wmode:'transparent'},
		params: {allowfullscreen:'true'},
		type: 'swf',
		bgColor: false,
		caption: false
	});
});


// ---------- navi/ajax ----------

var navpic = $('div#nav img');
var navdir = dir_elm+'i/nav/';
var blank = navpic.attr('src');
var aktiv = blank;


function ie6alpha(elm,img){
	if ($.browser.msie && $.browser.version.substr(0,1)==6)
		elm.css('filter',
			"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='scale')");
}

function setRubric(id){
	var lnk = $('div#nav area[rel='+id+']');
	var hdr = lnk.attr('alt') ? lnk.attr('alt') : $('.nav a[rel='+id+']').text();
	
	// hdr
	if ($('div#head span:hidden') || $('#rub').text() != lnk.attr('rel'))
		$('div#head span').animate({opacity:0},function(){
			$(this).show().html('<a href="?r='+id+'">'+hdr+'</a>').animate({opacity:1},function(){
				if ($.browser.msie)
					this.style.removeAttribute('filter');
			});
		});
	
	// set
	$('div#nav area').removeClass('set');
	lnk.addClass('set');
	aktiv = lnk.attr('alt') ? navdir+lnk.attr('alt')+'.png' : blank;
	navpic.attr('src',aktiv);
	ie6alpha(navpic, aktiv);
	
	window.scrollTo(0,0);
	$('#load').hide();
}

function setContent(lnk){
	$.get(url_cnt+lnk.attr('href'),function(v){
		if (v){
			setRubric(lnk.attr('rel'));
			$('div#cnt').animate({opacity:0},function(){
				$(this).show().html(v).animate({opacity:1});
			});
		} else
			window.location = lnk.attr('href');
	});
}


// start
if (!$('div#face').is('.go') || ($.browser.msie && $.browser.version.substr(0,1)==6)){ //depplink || ie6
	$('body').addClass('init');
	setRubric($('div#nav').attr('data'));
	
} else 
	$('div#face').fadeIn(1700,function(){
		if ($.browser.msie){ //ie7
			$('a#logo, #jahre').show();
			navpic.show();
		
		} else {
			$('a#logo, #jahre').fadeIn(900);
			navpic.fadeIn(900);
		}
	});


// nav hover
$('div#nav area').mouseover(function(){
	navpic.attr('src',navdir+$(this).attr('alt')+'.png');
	ie6alpha(navpic, navdir+$(this).attr('alt')+'.png');
}).mouseout(function(){
	navpic.attr('src',aktiv);
	ie6alpha(navpic, aktiv);
});

// nav click
$('div.nav a[rel!=ext], div#nav area[rel!=ext]').click(function(){
	$('#load').show();
	setContent($(this));
	
	// init
	if (!$('body').is('.init')){
		$('body').addClass('init');
		$('#face').hide();
		$('div.show').show();
	}
	return false;
});


// cnt click
$('div#cnt a[target!=_blank]').livequery('click',function(){
	var a = $(this);
	if (!a.attr('href').match(/^java|#|\.[a-z]{2,4}$/gi)){ //#|.file/domain
		
		$('#load').show();
		
		$('div#cnt').load(url_cnt + a.attr('href').replace(/(.*)\?/g,'?'),function(v){ // for IE
			if (v){
				if ($('#rub').text() != $('div#nav area.set').attr('rel')){
					setRubric($('#rub').text());
				} else {
					window.scrollTo(0,0);
					$('#load').hide();
				}
			} else
				window.location = a.attr('href');
		});
		return false;
	}
});

});//ready