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

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 smplayReady() { $('a.snd.set').removeClass('set'); }

$.fn.smplay = function(){

	var url = $(this).attr('rel');
	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="elm/p/smplay.php?ie" width="1" height="1" scrolling="no" frameborder="0"></iframe>');
		else
			$('<span></span>').appendTo('body').load('elm/p/smplay.php');
	}

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



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

$(function(){

// ---------- nav ----------

$('#nav .slidefx:not(.set)').hover(function(){
	$('a',this).animate({top:"-20px"},{queue:false,duration:150}); /*,paddingLeft:"18px"*/
},function(){
	$('a',this).animate({top:"0px"},{queue:false,duration:150});
});


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

$('a.vip').click(function(){ $(this).parents('form').submit(); }); // login vip

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

$('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) {
			if ($($this).is('.pge'))
				inp = parseInt($(this).attr('rel')) * (parseInt(inp) - 1);
			
			var parts = $(this).attr('href').match(/([^#]*)(.*)/);
			window.location = parts[1] + inp + parts[2];
		}
		
		return false;
	});
});


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

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


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

$.fn.media.defaults.flvPlayer = '/elm/p/flvplay315.swf';

$('a[href$="flv"]').each(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/"]').each(function(){
	// add 2 url: &rel=0
	$(this).media({
		width: 425,
		height: 350,
		type: 'swf',
		bgColor: false,
		caption: false
	});
});

});//function()