/* #################################################
# Projekt	: Base JavaScripts
# Stand		: 27.11.07
# 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 ----------

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


var charset = function() {
	var exp = new RegExp('([^'+ $(this).attr('charset') +']*)','g');
	if ($(this).val()) $(this).val($(this).val().replace(exp,''));
};
$('input[charset]').livequery(function() {
	$(this).bind('blur',charset).bind('keyup',charset);
}); 


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

	// onfocus clear default
	$('input').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;
});


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

$('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) {
			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 = '/elm/p/flvplay312.swf';

$('a[href$="flv"]').livequery(function() {
	if ($(this).is('.ontop')) return; // filter adblock
		
	size = new Array(425,350);
	if ($(this).parents('#lft').length)
		size = new Array(170,128);

	$(this).media({
		width: size[0],
		height: size[1],
		attrs: {allowfullscreen:'true', wmode:'transparent'},
		params: {allowfullscreen:'true'},
		flashvars: {image:$(this).attr('href').replace(/flv/,'jpg')},
		bgColor: false,
		caption: false
	});
});

$('a[href*="youtube.com/v/"]').livequery(function(){
	// add 2 url: &rel=0
	$(this).media({
		width: 425,
		height: 350,
		type: 'swf',
		bgColor: false,
		caption: false
	});
});

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

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

});//function()