/* WALLSTICKER FUNCTIONS */

var currentCategory = '';
var madeSelections = new Array();

function clickButton(which, imgID) {
	if (!madeSelections[imgID]) {
		alert(alert_select_color);
		return false;
	} else {
		f = g('nav_form');
		f.button.value = which;
		f.imgID.value = imgID;
		f.imgColor.value = madeSelections[imgID];
		if (f.onsubmit)
			f.onsubmit();
		f.submit();
	}
}

function chooseCategory(which) {
  if (which != currentCategory) {
		$('table.inuse').removeClass('inuse').removeClass('glow').addClass('shadow').next().removeClass('hover').removeClass('inuse');
		$('#x_' + which).removeClass('shadow').addClass('inuse').addClass('glow').next().addClass('inuse').addClass('hover');
		currentCategory = which;
		$('table.inuse').trigger('mouseenter');

    window.location.hash = which;

		var req = new AjaxRequest('typo3conf/ext/iwshop/extra/AJAX.php?getWallstickersCategory:' + which, 'GET', '', loadCategory);
		req.send();
	}
}

function loadCategory(XML) {

  var $XML = $(XML);
	var nodes = $XML.find('stickers > numeric');
	var holder = g('ccol');
	// try{ holder.innerHTML = ''; }
	// catch(e) { for(var i = 0; i < holder.childNodes.length; i++)
	// holder.removeChild(holder.childNodes[i]); }
	$(holder).empty();

	uncheckDelayedImage();

	var counter = 0;
	var attrs = [
			'id', 'rel', 'href', 'src', 'title'
	];

	for ( var i = 0; i < nodes.length; i++) {
		var node = nodes[i];
		if (node.nodeType == 3) {
			continue;
		}

		counter++;
		/*
		 * var tpl = g('stickerBoxTemplate').innerHTML; var uid = '';
		 * 
		 * tpl = str_replace('$UID$', uid = getXMLvalue(nodes[i], 'uid'), tpl); tpl =
		 * str_replace('$TITLE$', getXMLvalue(nodes[i], 'title'), tpl); tpl =
		 * str_replace('$AUTHOR$', getXMLvalue(nodes[i], 'authorName'), tpl); tpl =
		 * str_replace('$IMG$', getXMLvalue(nodes[i], 'img_thumb_small'), tpl); tpl =
		 * str_replace('$IMG_LARGE$', getXMLvalue(nodes[i], 'img_thumb_large'),
		 * tpl); tpl = str_replace('$PREVIEW_IMG$', getXMLvalue(nodes[i],
		 * 'preview_thumb_small'), tpl); tpl = str_replace('$PREVIEW_IMG_LARGE$',
		 * getXMLvalue(nodes[i], 'preview_thumb_large'), tpl); tpl =
		 * str_replace('$PRICE$', getXMLvalue(nodes[i], 'price'), tpl); tpl =
		 * str_replace('$SIZE$', getXMLvalue(nodes[i],
		 * 'width')+'x'+getXMLvalue(nodes[i], 'height'), tpl); var colors =
		 * getXMLvalue(nodes[i], 'colors').split(','); var color_str = ''; for(var j =
		 * 0; j < colors.length; j++) { color_str += "<li style='background-color: "+colors[j]+"' onclick='chooseColor(this, "+uid+");'></li>\n"; }
		 * tpl = str_replace('$COLORS$', color_str, tpl);
		 * 
		 * var d = document.createElement('div'); d.className = 'stickerBox';
		 * d.innerHTML = tpl; holder.appendChild(d);
		 */

		var pic = {
			title : getXMLvalue(node, 'title'),
			id : getXMLvalue(node, 'uid'),
			author : getXMLvalue(node, 'authorName'),
			size : getXMLvalue(node, 'width') + 'x' + getXMLvalue(node, 'height') + ' cm',
			prices : [],
			colors : getXMLvalue(node, 'colors').split(','),
			img_thumb_small : getXMLvalue(node, 'img_thumb_small'),
			img_preview_small : getXMLvalue(node, 'preview_thumb_small'),
			img_thumb_large : getXMLvalue(node, 'img_thumb_large'),
			img_preview_large : getXMLvalue(node, 'preview_thumb_large'),
			img_3 : getXMLvalue(node, 'preview_3'),
			img_4 : getXMLvalue(node, 'preview_4'),
			img_5 : getXMLvalue(node, 'preview_5')
		};

		$.each($(node).find('prices > numeric'), function(ix, el) {
			var $el = $(el);
			var price = parseInt($el.text());
			pic.prices.push(price);
		});

		// console.log('node['+i+'] : found pic data');
		// console.log(pic);

		var $dd = $('#stickerBoxTemplate').clone().attr('id', 'stickerBox-' + pic.id).appendTo(holder);

		// console.log('node['+i+'] : found $dd');

		var dd = $dd.find('div.description');
		if (pic.title.length > 20) {
			dd.find('h3.title').text(pic.title.substr(0, 18) + '...').attr('title', pic.title);
		} else
			dd.find('h3.title').text(pic.title);
		dd.find('span:first').text(str_replace('$AUTHOR$', pic.author, dd.find('span:first').text()));
		dd.find('strong.size').text(pic.size);
		dd.find('strong.price').text(pic.prices[0] + afterPriceCurrency);
		dd.find('div.addToCart a').attr('id', 'cartLinkUID-' + pic.id);
		var colorBox = dd.find('ul.colors');
		colorBox.html('');

		$.each(pic.colors, function(ix, el) {
			var classname = '';
			if (ix == 0) {
				classname = 'chosen';
				madeSelections[pic.id] = pic.colors[ix]; // .substr(1);
			}
			var colorChoice = $('<li />');
			colorChoice.addClass(classname).css('background-color', pic.colors[ix]).append('<div>&nbsp;</div>');

			colorChoice.data('sticker', {
				id : pic.id,
				color : pic.colors[ix],
				price : pic.prices[ix]
			}).click(chooseColor);

			colorBox.append(colorChoice);
		});

		dd = $dd.find('div.stickerHolder');
		var tmp = dd.find('a').attr('title', pic.title);
		tmp = tmp.attr('href', str_replace('$IMG_LARGE$', pic.img_thumb_large, str_replace('$UID$', pic.id, tmp.attr('href')))).find('img');
		var imgPath = str_replace('$IMG$', '', tmp.attr('src'));
		tmp.attr('src', imgPath + pic.img_thumb_small).attr('id', 'gallery-' + pic.id + '-img');

		dd = $dd.find('div.imagePreview');

		var previewImage = dd.find('a:first img');

		var goodSrc = str_replace('$PREVIEW_IMG$', pic.img_preview_small, dd.find('img').attr('src'));

		if (counter < 2) {
			previewImage.attr('src', goodSrc);
		} else if (counter < 3) {
			previewImage.attr('src', goodSrc).attr('delayed_source', goodSrc);
		} else {
			previewImage.attr('src', '').attr('delayed_source', goodSrc);
		}

		// dd.find('img').attr( 'src', str_replace( '$IMG$', pic.img_thumb_small,
		// dd.find('img').attr('src') ) );
		tmp = dd.find('a.plus').attr('id', 'gallery-' + pic.id + '-preview-link').attr('title', pic.title);
		tmp.attr('href', str_replace('$PREVIEW_IMG_LARGE$', pic.img_preview_large, str_replace('$UID$', pic.id, tmp.attr('href'))));

		dd = $dd.find('div.gallery');
		dd = dd.attr('id', 'imgGallery' + pic.id).find('div.thumbsHolder a:first');
		tmp = dd.click(function() {
			swapImageSimple($(this).children().attr('src'));
			return false;
		}).find('img');
		tmp.attr('src', str_replace('$IMG_LARGE$', pic.img_thumb_large, tmp.attr('src')));

		dd = dd.next();
		if (pic.img_preview_large != '') {
			tmp = dd.click(function() {
				swapImageSimple($(this).children().attr('src'));
				return false;
			}).find('img');
			tmp.attr('src', str_replace('$PREVIEW_IMG_LARGE$', pic.img_preview_large, tmp.attr('src')));
		} else
			dd.empty();

		dd = dd.next();
		if (pic.img_3 != '') {
			tmp = dd.click(function() {
				swapImageSimple($(this).children().attr('src'));
				return false;
			}).find('img');
			tmp.attr('src', str_replace('$PREVIEW_3$', pic.img_3, tmp.attr('src')));
		} else
			dd.empty();

		dd = dd.next();
		if (pic.img_4 != '') {
			tmp = dd.click(function() {
				swapImageSimple($(this).children().attr('src'));
				return false;
			}).find('img');
			tmp.attr('src', str_replace('$PREVIEW_4$', pic.img_4, tmp.attr('src')));
		} else
			dd.empty();

		dd = dd.next();
		if (pic.img_5 != '') {
			tmp = dd.click(function() {
				swapImageSimple($(this).children().attr('src'));
				return false;
			}).find('img');
			tmp.attr('src', str_replace('$PREVIEW_5$', pic.img_5, tmp.attr('src')));
		} else
			dd.empty();
		/*
		 * var dd = $('#stickerBoxTemplate').clone().attr('id',
		 * 'stickerBox-'+UID).appendTo(holder).show();
		 * 
		 * while(dd.children().length > 0) {
		 * 
		 * dd = dd.children().each( function() {
		 * 
		 * if($(this).children().length > 0) return;
		 * 
		 * var tpl = $(this).html();
		 * 
		 * tpl = str_replace('$COLORS$', color_str, tpl); tpl =
		 * str_replace('$TITLE$', getXMLvalue(nodes[i], 'title'), tpl); tpl =
		 * str_replace('$AUTHOR$', getXMLvalue(nodes[i], 'authorName'), tpl); tpl =
		 * str_replace('$PRICE$', getXMLvalue(nodes[i], 'price'), tpl); tpl =
		 * str_replace('$SIZE$', getXMLvalue(nodes[i],
		 * 'width')+'x'+getXMLvalue(nodes[i], 'height'), tpl);
		 * 
		 * $(this).html( tpl );
		 * 
		 * }).each( function () {
		 * 
		 * for(idx in attrs) { // console.log( 'INNER :: ', this, '
		 * attribute='+attrs[idx], $(this).attr(attrs[idx]) );
		 * 
		 * var tpl = $(this).attr(attrs[idx]);
		 * 
		 * if(typeof(tpl) == 'undefined' || tpl == '') continue;
		 * 
		 * tpl = str_replace('$TITLE$', getXMLvalue(nodes[i], 'title'), tpl); tpl =
		 * str_replace('$UID$', getXMLvalue(nodes[i], 'uid'), tpl); tpl =
		 * str_replace('$IMG$', getXMLvalue(nodes[i], 'img_thumb_small'), tpl); tpl =
		 * str_replace('$PREVIEW_IMG$', getXMLvalue(nodes[i],
		 * 'preview_thumb_small'), tpl); tpl = str_replace('$IMG_LARGE$',
		 * getXMLvalue(nodes[i], 'img_thumb_large'), tpl); tpl =
		 * str_replace('$PREVIEW_IMG_LARGE$', getXMLvalue(nodes[i],
		 * 'preview_thumb_large'), tpl); tpl = str_replace('$PREVIEW_3$',
		 * getXMLvalue(nodes[i], 'preview_3'), tpl); tpl =
		 * str_replace('$PREVIEW_4$', getXMLvalue(nodes[i], 'preview_4'), tpl); tpl =
		 * str_replace('$PREVIEW_5$', getXMLvalue(nodes[i], 'preview_5'), tpl);
		 * 
		 * $(this).attr(attrs[idx], tpl); } }); } if(!getXMLvalue(nodes[i],
		 * 'img_thumb_small')) { $('#stickerBox-'+UID+' .stickerHolder a').remove(); }
		 */
		if (pic.img_preview_large == '')
			$('#stickerBox-' + pic.id + ' .imagePreview').hide();

		$('#stickerBox-' + pic.id + ' .description').children('span:last').append(
				' <span class="colorName" style="display: inline">' + getXMLvalue(node, 'chosen_color') + '</span>');

		$dd.show();
	}
	if (counter == 0) {
		holder.innerHTML = '&nbsp;';
	}

	delayLoadImages();

	tb_init('a.thickbox, area.thickbox, input.thickbox');

	if (typeof (onCategoryLoaded) == 'function')
		onCategoryLoaded();
}

function restoreOldProduct() {
	var list = $('#gallery-' + uid + '-img').parents('.stickerBox').find('ul.colors li');
	$.each(list, function(ix, el) {
		if (color == rgb2hex(el.style.backgroundColor).substr(1)) {
			$(el).trigger('click');
		}
	});
	onCategoryLoaded = null;
}

function chooseColor() {
	var $el = $(this);
	var data = $el.data('sticker');
	$('#stickerBox-' + data.id + ' .description ul li').removeClass('chosen');
	$el.addClass('chosen');

	var str = $el[0].style.backgroundColor;
	if (str.indexOf('rgb(') == 0) {
		str = rgb2hex(str);
	}
	madeSelections[data.id] = str;

	$el.parents('div.stickerBox').find('.price').text(data.price + afterPriceCurrency);

	getColorPreview(data.id, str.substr(1));
}

function getColorPreview(id, color) {
	var req = new AjaxRequest('typo3conf/ext/iwshop/extra/AJAX.php?getWallstickerPreview:' + id + ':' + color, 'GET', '', loadColorPreview);
	req.send();
}

function loadColorPreview(XML) {
	var id = getXMLvalue(XML, 'ID');
	var src = getXMLvalue(XML, 'imgsrc');
	var path = g('gallery-' + id + '-img').src;
	var parts = path.split('/');
	parts[parts.length - 1] = src;
	g('gallery-' + id + '-img').src = parts.join('/');
	$('#stickerBox-' + id + ' span.colorName').text(getXMLvalue(XML, 'chosen_color'));
}

function rgb2hex(str) {
	var list = str.substr(4, str.length - 5).split(', ');
	str = '#' + ((list[0] < 16) ? '0' : '') + parseInt(list[0]).toString(16);
	str += ((list[1] < 16) ? '0' : '') + parseInt(list[1]).toString(16);
	str += ((list[2] < 16) ? '0' : '') + parseInt(list[2]).toString(16);
	return str;
}

