/***********************************/
/* Behaviour for D&R Kaffeeklatsch */
/***********************************/

/*******************************/
/* Wait until page has loaded! */
/*******************************/
$(document).ready(function() {


/* Extended Table Layout */
$('div.xtable')
	.css('overflow-x', 'auto');
$('div.xtable table')
	.addClass('tborder')
	.css('border-spacing', '1px')
	.css('table-layout', 'fixed');
$('div.xtable tr:odd')
	.addClass('windowbg');
$('div.xtable tr:even')
	.addClass('windowbg2');
$('div.xtable tr:first td')
	.addClass('catbg')
	.css('font-weight', 'bold')
	.css('text-align', 'center');
$('div.xtable tr:not(:first)').click(function() {
	if($(this).hasClass('calendar_highlight')) {
		$(this).removeClass('calendar_highlight');
	} else {
		$(this).addClass('calendar_highlight');
	}
});
$('div.xtable div.empty').each(function() {
	var head = $(this).parent('td');
	$(head)
		.attr('title', $(head).text())
		.text('')
		.css('padding-left', '2px')
		.css('padding-right', '2px');
});

$('div.numtable tr:not(:first-child) td:not(:first-child)').css('text-align', 'right');


$('div.rowdiff').each(function() {
	var endindex = $(this).prevAll()
	var startvalue = parseFloat($(this).closest('tr').children('td').eq(parseInt($(this).text())).text().replace(/,/g, '.'));
	var endvalue = startvalue;
	var found = false;
	$(this).closest('td').prevAll().each(function() {
		var value = parseFloat($(this).text().replace(/,/g, '.'));
		if(!found && value > 0) {
			endvalue = value;
			found = true;
		}
	});
	var result = (endvalue - startvalue).toFixed(1);
	var resulttext = result.toString().replace(/\./g, ',');
	$(this).css('text-align', 'right').text(resulttext);
	if(result > 0) {
		$(this).css('color', 'green');
	}
	if(result < 0) {
		$(this).css('color', 'red');
	}
});


// Ueberraschungs-Kategorie (10): Schleife
$('div.catbg a[name=10], div.catbg2 a[name=10]')
	.parent('div').css('position', 'relative')
	.end()
	.after('<div class="schleife"><img src="/Themes/default/images/schleife.png" alt="" /></div>');


// Galerie Erweiterung
$('.imagelisting').each(function() {
	var imgs = $('img', this);
	var buffer = '';
	for(i = 0; i < imgs.length; i++) {
		url = $(imgs).eq(i).attr('src').replace(/thumb_/, '');
		buffer = buffer + '[img]'+ url + '[/img]' + "\n\n";	
	}
	$(this).append('<tr class="titlebg"><td valign="top"><label for="copybasket">BB Code: </label></td><td colspan="3"><textarea rows="3" id="copybasket" class="copybasket" style="width:100%;">' + buffer + '</textarea></td></tr>');
});

$('.copybasket').focus(function() {
	$(this).select();
});

$('.imagelisting img').each(function() {
	$(this).parents('td').eq(0).append('<br /><span class="smalltext"><a href="#" class="copybbc">[BB Code]</a></span>');
});

$('.copybbc').click(function(e) {
	e.stopPropagation();
	e.preventDefault();	
	var img = '[img]' + $(this).parents('td').eq(0).find('img').eq(0).attr('src').replace(/thumb_/, '') + '[/img]' + "\n\n";
	var basket = $('#copybasket').val() + img;
	$('#copybasket').val(basket);
});

$('.bulkfile').change(function() {
	var idFile = $(this).attr('id');
	var idName = idFile.replace(/bulkfile/, 'bulkname');
	var valFile = $('#' + idFile).val();
	var valName = $('#' + idName).val();
	var newName = valFile.replace(/C:\\fakepath\\/, '');
	if(valName == '') {
		$('#' + idName).val(newName);
	}
});

// Spreadshirt Shop
$('#shopLogin').click(function(e) {
	e.stopPropagation();
	e.preventDefault();
	var url = $('#Spreadshop').attr('src');
	var shop = url.substring(0, url.indexOf('.')).substr(url.indexOf('http://') + 7);
	$('#Spreadshop').attr('src', 'http://kaffeeklatsch.doreen-und-rainer.de/index.php?action=spreadshirt;sa=auth;shop=' + shop);
});

$('div.shopitem').each(function() {
	var ids = $(this).attr('title').split('-');
	if(ids.length == 1) {
		ids[1] = ids[0];
		ids[0] = '';
	}
	var link = '/index.php?action=spreadshirt;sa=shop;aid=' + ids[1];
	var src = 'http://image.spreadshirt.net/image-server/v1/products/' + ids[0] + '/views/1';
	$(this)
		.removeAttr('title')
		.html('<a href="' + link + '"><img src="' + src + '" alt="" /></a>');
});

// Image tooltips
$('.imgtip').each(function() {
	var src = $(this).attr('src');
	var style = $(this).attr('alt');
	var styles = style.split('|');
	var table_style = styles[0] || '';
	var text_style = styles[1] || '';
	var left_style = styles[2] || '';
	var img_style = styles[3] || '';
	var content = $(this).attr('title').split('|');
	var title, text;
	if(content.length > 1) {
		title = '<h3 class="catbg3">' + content.shift() + '</h3>';
	}
	text = content.join('<br />');
	$(this)
		.tooltip({
			id: 'imgtip',
			bodyHandler: function() {
				return $(title + '<table style="' + table_style + '"><tr><td style="' + left_style + '"><img style="' + img_style + '" src="' + src + '" alt="" /></td><td style="' + text_style + '">' + text + '</td></tr></table>');
			}, 
			track: false, 
			delay: 1000,
			fade:250,
			showURL: false,
			extraClass: 'imgtip'
		});
});
	

/*********************/
/* End of page ready */
/*********************/
});



