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



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

var l;
function loading(v) {
	if (!l) l = $('<div id=loading></div>').appendTo($('body'));
	l.toggle();
}


$.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('#'));
}


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

$(function(){

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

$('form[name=form]').submit(function(){ return false; });

$(':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 ----------

$('input[title]').livequery(function(){
	if (!$(this).val())
		$(this).addClass('default').val($(this).attr('title'));

	$(this).focus(function(){ //clear
		if ($(this).val() == (!$(this).attr('maxlength') ? $(this).attr('title'):
			$(this).attr('title').substr(0,$(this).attr('maxlength')))
		)
			$(this).val('').removeClass('default');
	}).blur(function(){ //restore
		if (!$(this).val())
			$(this).addClass('default').val($(this).attr('title'));
	});
});


$('#search-submit').click(function(){
	if ($('#search input').is('.default'))
		$('#search input').removeClass('default').focus();
	else
		$('#search').submit();
	return false;
});



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

// nav
$('#nav ul.sub').each(function(){
	$(this).parent().addClass('show');
});
$('#nav li.show > a').click(function(){
	return false;
});
$('#nav li.show').mouseover(function(){
	$('ul',this).show();
}).mouseout(function(){
	$('ul',this).hide();
});


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

$('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 ----------

$('a[href$="flv"], a[href*="youtube.com/v/"]').each(function(){	
	var txt = $(this).text();
	
	if ($(this).attr('href').search(/\.flv$/)>0)
		var img = $(this).attr('href').replace(/\.flv/,'_m.jpg');
	else
		var img = 'http://img.youtube.com/vi/'+$(this).attr('href').replace(/.*\/v\//,'')+'/default.jpg';
	
	$(this).
		attr('title',txt).
		html('<img src="'+img+'"/>'+txt.replace(/,/g,'<br/>')).
		wrap('<div class="mov "></div>'); // clearfix
});

$('div.mov a').livequery('click',function(){
	tb_show(
		null,
		dir_temp+'view-video.php?width=592&height=510&mov='+$(this).attr('href')+
			'&txt='+$(this).attr('title').replace(/ /g,'+'),
		false
	);
	return false;
});


// ---------- info toggle ----------

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


});//ready