/* #################################################
# Projekt	: Base JavaScripts
# Stand		: 02.05.08
# 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);
	}
};


// ---------- pict ----------

function nextPic(){
	var p = $('div#pict');
	var r = Math.floor(Math.random()*3);
	
	if (p.is('#pict')){
		r = (r!=p.attr('data') ? r : ((r+1)>2 ? r-1:r+1));
		p.attr('data',r).attr('style',$('div',p).attr('style').replace(/.\./,r+'.'));
	}
}



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

$(function(){//ready


nextPic();//init

// ---------- 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');

// 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;
	});
});


// ---------- 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 ----------

function setRubric(id){
	var a = $('.nav a[rel='+id+']');

	$('div.nav a').removeClass('set');
	a.addClass('set');
	
	$('div#pict div').fadeOut(function(){
		$(this).attr('style',$(this).parent().attr('style')).show(function(){
			nextPic();
		});
	});
	$('div#head span').animate({opacity:0},function(){
		$(this).html('<a href="?r='+id+'">'+a.text()+'</a>').animate({opacity:1},function(){
			if ($.browser.msie)
				this.style.removeAttribute('filter');
		});
	});
	
	window.scrollTo(0,0);
	$('#load').hide();
}

$('div#nav > a').click(function(){
	var e = $('div#nav ul');
	if ($.browser.msie)
		e.toggleClass('hide');
	else
		e.slideToggle('slow');
	return false;
});

$('div.nav a[rel!=ext]').click(function(){
	var a = $(this);
	
	$('#load').show();

	$('div#cnt').load(url_cnt+a.attr('href'),function(v){
	// $.get(url_cnt,{r:1},function(v){ // parseURL noch erl., siehe aperol cntbox
		if (v){
			setRubric(a.attr('rel'));
			// $('div#cnt').html(v);
		} else
			window.location = a.attr('href');
			
	});
	return false;
});

$('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){ // replace for IE
			if (v){
				if ($('#rub').text() != $('.nav a.set').attr('rel')){
					setRubric($('#rub').text());
				} else {
					window.scrollTo(0,0);
					$('#load').hide();
				}
			} else
				window.location = a.attr('href');
		});
		return false;
	}
});

// ---------- logo ----------

$('#logo').click(function(){
	$('div#pict div').fadeOut(function(){
		$(this).attr('style',$(this).parent().attr('style')).show(function(){
			nextPic();
		});
	});
	return false;
});

/*
$('#logo').click(function(){
	if (!$.browser.msie){ // Math.floor(Math.random()*11) > 7
		var tick_len = 0;
		var tick_div = '#code';
		var tick_txt = $('#cnt').html();
		
		function runTick(){
			if (!tick_len){
				$('#bug').animate({height:'0%'},function(){
					$(this).animate({height:'100%'});
				});
			}
			if (tick_len <= tick_txt.length+24) {
				$(tick_div+' div').text(tick_txt.substring(0,tick_len)+' _');
				$(tick_div).html($(tick_div).html().replace(/\r|\n/gi,"<br>"));
				tick_len+=24;
				setTimeout(function(){runTick();},0);
			}else {
				var check = confirm('System rebooting?...');
				if (check)
					window.location = $('#logo').attr('href');
				$('#bug').remove();
			}
		}
		
		$('body').append('<div id="bug" style="position:absolute;z-index:3;overflow-y:scroll;left:0;top:0;width:100%;height:0;background:#000;">'
		+'<div id="code" style="width:666px"><b style="color:#f00;font:42px verdana">error!<br><\/b>'
		+'<div style="color:#0f0;font:14px verdana;text-decoration:none;border:0"><\/div><\/div>'
		+'<\/div>');
		
		runTick();
		return false;
	}
});
*/

});//ready