/* #################################################
# Projekt	: Base JavaScripts
# Stand		: 04.06.09
# 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,a) {
	$('.l',f).removeClass('alert'); // reset
	var a = a.split('#');
	
	for (i in a) {
		if (a[i]) {
			$('#f_'+a[i],f).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);
		});
	});
};

function rndm(){
	return String(Math.floor((Math.random()*1E16)));
}

// ---------- 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(){

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

$('a.thickbox').colorbox({
	transition:'elastic',
	opacity:0.8,
	slideshow:true,
	slideshowAuto:false,
	current: '{current} von {total}'
});

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


// ---------- preview ----------

$('#Form :input').live('click',function(){
	$('#Preview').slideDown();
});
$('.btn.preview').live('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').click(function(){
	$(this).smplay();
	return false;
});

$('#mid a.thickbox').each(function(){ $(this).append('<p />'); }); // zoom icon

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

$('a.show').live('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 ----------

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


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

if ($('div#mid').is('.multimov')){ // video section

	$('a[href$="flv"], a[href*="youtube.com/v/"]').each(function(){	
		var txt = $(this).text();
		var txt_lnk = txt.length>50 ? txt.substr(0,50)+'&hellip;':txt;
		
		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_lnk.replace(/,/g,'<br/>')).
			wrap('<div class="mov"></div>'); // clearfix
	});
	
	$('div.mov a').live('click',function(){
		tb_show(
			null,
			dir_temp+'view-video.php?width=592&height=520&mov='+$(this).attr('href')+
				'&txt='+$(this).attr('title').replace(/ /g,'+'),
			false
		);
		return false;
	});
	
} else { // rest sections

	 $.fn.media.defaults.flvPlayer = dir_publ+'elm/p/nonverblaster.swf';
	
	$('a[href$="flv"], a[href$="mov"], a[href$="mp4"], a[href*="youtube.com/v/"]').each(function(){
		var mov = $(this).attr('href');
		var add = 'qkt';
		
		var options = {
			preferMeta: false,
			width: 555, // 480x270, 640x360
			height: 304,
			autoplay: false,
			bgColor: '#000000',
			caption: false,
			attrs: {allowfullscreen:'true', wmode:'transparent'},
			params: {allowfullscreen:'true'}
		};
		if (mov.search(/youtube/)>0){
			options = $.extend(options,{
				src: mov+'&fs=1&fmt=6', // fmt: 18=mp4, 6=flv
				type: 'swf'
			});
			add = 'you';
		}
		if (mov.search(/\.flv/)>0){
			options = $.extend(options,{
				flashvars: {
					videoURL: dir_publ+mov,
					teaserURL: dir_publ+mov.replace(/\.(flv)/,'.jpg')
				}
			});
			add = 'flv';
		}
		$(this).addClass('mov '+add).media(options);
	});
}


});//ready