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

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

var pict;
function time_pict(sec){
	window.clearTimeout(pict);
	pict = window.setTimeout("load_pict()",sec);
}
function load_pict(){
	var f = 17; // files
	var h = $('#pict');
	var p = parseInt(h.attr('data'));
	var i = parseInt($('div',h).attr('data'));
	
	if (i<30){ //max loops
		
		var ii=0;
		while (ii<9){
			ii++;
			
			r = 1 + f*(Math.random());
			r = Math.floor(r);
			
			if (r != p){
				pic = 'elm/i/hd/'+r+'.jpg';
				break;
			}
		}
		$('<img />').attr('src',pic+($.browser.msie ? '?'+rndm():'')).load(function(){
			$('div',h).hide().css('background-image','url('+pic+')').attr('data',i+1).fadeIn(1500,function(){
				
				h.attr('data',r).attr('style',$(this).attr('style'));
				// $(this).hide();
				time_pict(20000);
			});
		});
 	}
}




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

$(function(){

$('.slidefx').hover(function(){
	$('a',this).animate({top:"-16px"},{queue:false,duration:200});
},function(){
	$('a',this).animate({top:"0px"},{queue:false,duration:200});
});


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

time_pict(0); //start

$('#rgt a.thickbox').each(function(){
	if ($('img',this).width() < $('img',this).height())
		$(this).addClass('vert');
});

$('a.thickbox').livequery(function(){
	$(this).colorbox({
		transition:'none',
		opacity:1,
		slideshow:true,
		slideshowAuto:false,
		current: '{current} / {total}',
		maxWidth: 610, //660
		maxHeight: 406
	},function(){
		$('#cboxOverlay').removeClass('vert');
	});
});

$('a.thickbox.vert').livequery(function(){
	$(this).colorbox({
		maxWidth: 294,
		maxHeight: 462 //482
	},function(){
		$('#cboxOverlay').addClass('vert');
	});
});

$().bind('cbox_open',function(){
	time_pict(1000000); // stop pict
}).bind('cbox_closed',function(){
	time_pict(20000); // start pict
});


// ---------- newl ----------

$('#newsl input').focus(function(){
	$(this).val('');
});

$('#newsl a').click(function(){
	var inp = $('#newsl input');
	
	$.get(dir_temp+'newsl.php?'+inp.val(),function(j){
		inp.val(j);
	});
	return false;
});


// ---------- 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').livequery('click',function(){
	var p = $('p',$(this).parent());
	$(this).html( (p.is(':hidden') ? '(&minus;)':'(+)') );
	p.slideToggle('fast');
	return false;
});


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

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

$('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: 525,
		height: 284,
		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'),
				// file: dir_publ+mov,
				// image: dir_publ+mov.replace(/\.(flv)/,'.jpg'),
				controlColor: '0xffffff',
				controlBackColor: '0x000000'
			}
		});
		add = 'flv';
	}
	if (add == 'qkt'){
		options = $.extend(options,{
			height: 298
		});
	}
	$(this).addClass('mov '+add).media(options);
});


});//ready