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


// ---------- header ----------

var header;
function time_header(sec){
	window.clearTimeout(header);
	header = window.setTimeout("load_header()",sec);
}
function load_header(){
	var h = $('#head');
	var i = parseInt($('div',h).attr('data'));
	
	if (i<20){ //max 20 loops
	
		$.get(dir_temp+'load-header.php?g='+$('a',h).attr('rel')+'&p='+h.attr('data'),function(r){
			if (r){
			
				$('<img />').attr('src',r+($.browser.msie ? '?'+rndm():'')).load(function(){
					$('div',h).hide().css('background-image','url('+r+')').attr('data',i+1).fadeIn(1500,function(){
						
						h.attr('data',r).attr('style',$(this).attr('style'));
						$(this).hide();
						time_header(20000);
					});
				});
			} else if (r != 'false'){ //more than 1x pic in group
				time_header(0);
			}
		});
 	}
}



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

$(function(){

// ---------- header ----------

$('#head a').click(function(){
	var h = $('#head div');
	h.attr('data',parseInt(h.attr('data'))-1);
	
	time_header(0);
	return false;
});
time_header(0); //start


// ---------- 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});
	});
	return false;
});
$('.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').live('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;
	});
});

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

$.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: 540, // 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