var test = "";

$(document).ready(function() {
	
	$('div.sbox').each(function() {
		buildSBox($(this));
	});

	$('div.sbox_frame').each(function() {
		buildSBoxFrame($(this));
	});
	
	$('div.article_list_preview').each(function() {
		buildPopBox($(this));
	});
	
	mergeOrderTables();
	
	$('ul.features, div.order_box table, div.order_box p, #cross_selling, #configgroup, div.dialog, div.article_description, table.article_features').each(function() {
		if (!$(this).parent().parent('fieldset').get(0)) buildFrameBox($(this));
	});
	
	$('#categories').children('ul').children('li').children('a').each(function() {
		buildCatHover($(this));
		$(this).html($(this).html().split(' (')[0]);
	});
	
	$('div.preview_image').each(function() {
		applyPreviewLid($(this));
	});
	
	$('a.textbutton').each(function() {
		buildButton($(this));
	});
	
	$(window).resize(function() {
		reScale($(this));
	});
	
	reScale($(window));
	
	$('#article_rating_box').hide();
	
	scaleCrossSelling();
		
});

function showImage(url, bigurl) {
	var img = new Image();
	img.onload = function() {
		$('#video').fadeOut(200);
		$('#big_image').fadeOut(200, function() {
			$('#big_image').css('background-image', 'url(' + url + ')');
			$('#big_image').attr('rel', bigurl);
			$('#big_image').fadeIn(200);
		});
	}
	img.src = url;
	
}

function showVideo() {
	$('#big_image').fadeOut(200);
	$('#video').fadeIn(200);
}

var ls_attempt = 0;

function updateLivesearch() {
	ls_attempt = 0;
	livesearchButton();
}

function livesearchButton() {
	var obj = $('.LiveSearchShowMoreArticleHint').find('input');
	if (obj.get(0)) {
		buildInputButton(obj, false);
	} else {
		ls_attempt ++;
		if (ls_attempt < 10) {
			setTimeout('livesearchButton()', 50);
		}
	}
}

function showRatingBox() {
	setTimeout(function() {
		$('#article_rating_box').hide();
		$('#article_rating_box').slideDown(400);
		$('#show_ratings').slideUp(350);
		$('#rating_image').slideUp(350);
		}, 800);
}

String.prototype.shortenTo = function(max) {
	
	var pos = Math.min(this.length, max);
	
	if (pos == this.length) return this.toString();
	
	while (pos > 30 && !this.substr(pos, 1).match(/([ \.\-\,]+)/)) {
		pos --;
	}
	
	return (pos < this.length)? this.substr(0, pos) + ' ...' : this.toString();
	
}

function reScale(win) {
	var h = Math.max(($('#left').height()), ($('#right').height()));
	$('#center').css('min-height', (h + 100) + 'px');
	if (win.width() > 1200) {
		var dist = win.width() - 1200;
		dist = Math.min(50, Math.round(dist / 5));
		$('#left').css('width', (230 + dist) + 'px');
		$('#right').css('width', (230 + dist) + 'px');
	} else {
		var dist = 1200 - win.width();
		dist = Math.min(30, Math.round(dist / 5));
		$('#left').css('width', (230 - dist) + 'px');
		$('#right').css('width', (230 - dist) + 'px');
	}
}

function initCatSubHover(anchor, lvl) {
	var sub = anchor.parent().next('.level' + lvl + '_sub').children('ul');
	
	sub.children('li').children('a').each(function() {
		initCatSubHover($(this), lvl + 1);
	});

	anchor.mouseover(function() {
		buildCatSubHover(anchor, lvl);
	});
	
	table.mouseover(function() {
		buildCatSubHover(anchor, lvl);
	});

	
	
}

function buildCatHover(anchor) {
	
	var sub = anchor.parent().next('li').children('ul');
	
	if (sub.data('touched')) return;
	
	sub.data('touched', true);
	
	anchor.removeAttr('title');
	
	sub.hide();
	
	if (anchor.height() > 25) {
		anchor.addClass('twolines');
		anchor.parent().addClass('twolines');
		var twolines = 'twolines';
	} else {
		var twolines = '';
	}
	
	var newsub = sub;
	var table = newsub;
	
	newsub.parent().find('ul').addClass('popup').hide();
	
	newsub.addClass('level0');
	
	newsub.find('li').children('a').each(function() {
		$(this).html($(this).html().split(' (')[0]);
		$(this).removeAttr('title');
		var innersub = $(this).parent().next('li').children('ul');
		$(this).mouseover(function() {
			innersub.show();
		});
		$(this).mouseout(function() {
			innersub.hide();
		});
		innersub.mouseover(function() {
			innersub.show();
		});
		innersub.mouseout(function() {
			innersub.hide();
		});
	});
	
	table.hide();	
	
	anchor.mouseover(function() {
		table.show();
	});
	
	anchor.mouseout(function() {
		table.hide();
	});

	table.mouseover(function() {
		anchor.addClass('hover');
		table.show();
	});

	table.mouseout(function() {
		anchor.removeClass('hover');
		table.hide();
	});
}

function buildSBox(box) {
	box.wrapInner('<div class="sbox_center"></div>');
	box.prepend('<div class="sbox_top"></div>');
	box.append('<div class="sbox_bottom"></div>');
	box.removeClass('sbox');
	box.addClass('sbox_wrapper');
}

function buildSBoxFrame(box) {
	box.wrapInner('<div class="sbox_frame_center"></div>');
	box.prepend('<div class="sbox_frame_top"></div>');
	box.append('<div class="sbox_frame_bottom"></div>');
	box.removeClass('sbox_frame');
	box.addClass('sbox_wrapper');
}

function buildPopBox(box, soloBox) {
	var str = '<table class="popbox article_preview">';
	str +=      '<tr><td class="tl"><span class="filler"></span></td><td class="t"><span class="filler"></span></td><td class="tr"><span class="filler"></span></td></tr>';
	str +=      '<tr><td class="l"><span class="filler"></span></td><td class="center"></td><td class="r"><span class="filler"></span></td></tr>';
	str +=      '<tr><td class="bl"><span class="filler"></span></td><td class="b"><span class="filler"></span></td><td class="br"><span class="filler"></span></td></tr>';
	str +=    '</table>';
	
	box.before(str);
	
	var item = box.parent();
	
	var anchor = (item.children('a').get(0))? item.children('a') : ((item.children('div').children('a').get(0))? item.children('div').children('a') : item.children('table'));
	
	var table = box.prev();
	
	var center = table.find('td.center');
	
	var descr = box.find('p');

	//if (descr.get(0)) descr.html(descr.html().shortenTo(180));
	
	box.addClass('wrapped');
	
	box.clone().appendTo(center);
	
	box.detach();
	
	var newbox = center.children('.wrapped');
	
	
	table.hide();
	
	newbox.show();
	
	if (soloBox) {
		
		table.attr('id', newbox.attr('id'));
		newbox.removeAttr('id');
		
	} else {
		
		anchor.mouseover(function() {
			clearTimeout(anchor.data('iv'));
			var iv = setTimeout(function() { fadeInPop(table); }, 350);
			anchor.data('iv', iv);
			item.css('z-index', '500');
		});
		
		anchor.mouseout(function() {
			clearTimeout(anchor.data('iv'));
			var iv = setTimeout(function() { fadeOutPop(table, item); }, 50);
			anchor.data('iv', iv);
		});
	
		table.mouseover(function() {
			clearTimeout(anchor.data('iv'));
			table.fadeIn(250);
			item.css('z-index', '500');
		});
		
		table.mouseout(function() {
			clearTimeout(anchor.data('iv'));
			var iv = setTimeout(function() { fadeOutPop(table, item); }, 50);
			anchor.data('iv', iv);
		});
		
	}
	
}

function fadeOutPop(table, item) {
	table.fadeOut(250, function() { if (item) item.css('z-index', '10'); });
}

function fadeInPop(table) {
	table.fadeIn(250);
}

function mergeOrderTables() {
	var configTable = $('table.Order_ConfigTable');
	var totalsTable = $('table.Order_TotalsTable');
	var couponTable = $('table.Order_CouponTable');
	
	if (totalsTable.get(0)) {
	
		configTable.before('<div id="configgroup"></div>');
		
		var configGroup = configTable.prev('#configgroup');
		
		configTable.clone().appendTo(configGroup);
		couponTable.clone().appendTo(configGroup);
		totalsTable.clone().appendTo(configGroup);
		
		configGroup.append('<hr class="clear" />');
		
		configTable.detach();
		totalsTable.detach();
		couponTable.detach();
		
	}
	
	var refreshTable = $('table.Order_ConfigListTable_2');
	
	if (refreshTable.get(0)) {
		var basketTable = refreshTable.next('table.Order_ArticleTable');
		var link = refreshTable.find('a');
		var title = refreshTable.find('h3').html();
		var basketCols = 4;
		basketTable.prepend('<tr><th colspan="' + basketCols + '"><h3>' + title + '</h3></th></tr>');
		basketTable.append('<tr><td class="refresh_button" colspan="' + basketCols + '"></td></tr>');
		link.clone().appendTo(basketTable.find('td.refresh_button'));
		refreshTable.detach();
	}
	
}

function buildFrameBox(box) {
	
	if (!box.html() || box.html().length < 10) return;
	

	if (box.parent('fieldset.input_fieldset').get(0)) {
		box = box.parent('fieldset.input_fieldset');
	}
	
	switch(box.attr('class').split(" ")[0]) {
		case 'Order_ConfigTable':
		case 'Order_TotalsTable':
		case 'Order_CouponTable':
			var floatleft = 'floatleft';
			break;
		default:
			var floatleft = '';
	}
	
	var str = '<table class="framebox ' + floatleft + '">';
	str +=      '<tr><td class="tl"><span class="filler"></span></td><td class="t"><span class="filler"></span></td><td class="tr"><span class="filler"></span></td></tr>';
	str +=      '<tr><td class="l"><span class="filler"></span></td><td class="center"></td><td class="r"><span class="filler"></span></td></tr>';
	str +=      '<tr><td class="bl"><span class="filler"></span></td><td class="b"><span class="filler"></span></td><td class="br"><span class="filler"></span></td></tr>';
	str +=    '</table>';
	
	box.before(str);
	
	var table = box.prev();
	
	var center = table.find('td.center');
	
	box.addClass('wrapped');
	
	box.clone().appendTo(center);
	
	box.detach();
		
}

function applyPreviewLid(preview) {
	preview.append('<div class="preview_lid"></div>');
}

function scaleCrossSelling() {
	var tbl = $('div.article_maincol table.framebox');
	var h = tbl.height();
	if (h) {
		$('#cross_selling_container').css('height', (h - 77) + 'px');
		$('#cross_selling_container').css('max-height', 'none');
	}
}

function buildButton(anchor) {
	anchor.removeClass('textbutton');
	anchor.addClass('textbutton_wrapper');
	anchor.wrapInner('<span class="center"></span>');
	anchor.prepend('<span class="left"></span>');
	anchor.append('<span class="right"></span>');
	return anchor;
}

function buildInputButton(input, checkout) {
	if (input.parent('.submit_wrapper').get(0) || input.css('display') == 'none') return;
	input.removeClass('submit');
	input.addClass('submit_center');
	input.wrap('<span class="submit_wrapper ' + ((checkout)? 'checkout' : '') + '"></span>');
	input.before('<span class="left"></span>');
	input.after('<span class="right"></span>');
	var wrapper = input.parent('.submit_wrapper');
	var w = 21;
	wrapper.children().each(function() {
		w += $(this).width();
	});
	wrapper.css('min-width', w + 'px');
	//wrapper.parent().css('min-width', (w + 4) + 'px');
}