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

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')+'.mp3';
	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} / {total}'
});

// ---------- shop ----------

if ($('#init-client').length){
	$.get(dir_shop+'log/init.php',function(r){
		if (r)
			$('body').append(r); // no-cookie alert
		else
			location.reload(); // for init session vars
	});
}

$('.item-sec').live('click',function(){
	var t = $(this);
	
	if (!t.is('.loaded')){
	
		$.get(dir_shop+'itm/rub.php?g='+t.attr('href').substr(1),function(j){
			t.addClass('loaded').after(j);
		});
	}
	return false;
});

$('.item-upd').livequery('change',function(){
	var itm = $('#shop-item');
	var mta = itm.metadata();
	var str = $(this).attr('name')+'='+$(this).val()+'&id='+mta.id+'&num='+mta.num;
	
	$.getJSON(dir_shop+'itm/upd.php?'+str,function(j){
		
		// price
		$('.price',itm).html(j.price); // replace(/EUR/,'&euro;')
		
		// sizes
		if (j.size)
			$('select[name=size]',itm).html(j.size);
		
		// picts
		$('.pic-main',itm).html(j.pic_main);
		$('.pic-more',itm).html(j.pic_more);
		
		// cart-btn
		$('.btn',itm).attr('href','?c&i='+j.id);
	});
	return false;
});

$('.cart').live('click',function(){
	$('#shop-cart-mini').load(dir_shop+'crt/mini.php'+$(this).attr('href')+'&ajax',function(){
		if ($(this).is(':hidden'))
			$(this).slideDown('fast');
	});
	return false;
});

$('select.cat-drop').change(function(){
	location = window.location.href.replace(/\&cat=\d{1,4}/,'')+'&cat='+$(this).val();
});


// ---------- 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 ----------

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


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

$('a.snd').click(function(){
	$(this).smplay();
	return false;
});


$.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: 456, // 480x270, 640x360
		height: 320,
		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