$(function(){
	$("#contentTable > tbody > tr:not(:has(td))").remove();
	$('dt.thumb > a[href]').lightBox();
	if(navigator.appName.toLowerCase().indexOf("explorer") !== -1 && parseFloat(navigator.appVersion) < 8)
		$("input[type=text]").addClass("ieTextInput");
	
	var shop = $("#shopScroller")
	if(shop.length){
		var hor = 3; 
		$("a", "dt.thumb").each(function () {
			if(!$(this).attr("href")) $(this).removeAttr("rel");
		});
		
		var allInfos = $("dt.info");
		var allThumbs = $("dt.thumb");
		var allDownloads = $("dt.downloads");
		if(allDownloads.length === allInfos.length){
			for(var i = 0, len = allInfos.length; i < len; i += hor){
				var thumbHeight = 0;
				var infoHeight = 0;
				var downloadHeight = 0;
				var itemsLeft = Math.min(hor, len-i);
				for( var j = 0 ; j < itemsLeft ; j++){
					thumbHeight = Math.max(thumbHeight, allThumbs.eq(i+j).height());
					infoHeight = Math.max(infoHeight, allInfos.eq(i+j).height());
					downloadHeight = Math.max(downloadHeight, allDownloads.eq(i+j).height())
				}
				for( var k = 0 ; k < itemsLeft ; k++){
					if(thumbHeight)
						allThumbs.eq(i+k).height(thumbHeight);
					if(infoHeight)
						allInfos.eq(i+k).height(infoHeight);
					if(downloadHeight)
						allDownloads.eq(i+k).height(downloadHeight);
				}
			}
		}
	}
});
function sortHeight(){
	var h = 0;
	var scrollBar = 0;
	if (info.width < 1000)
		scrollBar = 20;
	var head = $("#header"), menu = $("#MainMenu"), bread = $("#breadCrumbs"),back = $("#backButton"), foot = $("#footer");
	if(head.length)
		h += head.outerHeight(true);
	if(menu.length)
		h += menu.outerHeight(true);
	if(bread.length)
		h += bread.outerHeight(true);
	if(back.length){
		var backButton = back.outerHeight(true);
		h += backButton;
	}
	if(foot.length){
		var footer = foot.outerHeight(true);
		h += footer;
	}
	var scroller = $("#shopScroller");
	if(scroller.length){
		var inner = $("#shopScrollerInner");
		var posAr = scroller.offset();
		var scrollerWidth = scroller.parent().width();
		scroller.width(scrollerWidth);
		scroller.height( info.height - (posAr.top + footer + backButton + scrollBar));
		scroller.css("overflow", "auto");
		inner.width(scrollerWidth-30);
	}else{
		var content = $("#contentTable");
		if(content.length){
			if(content.height() < info.height)
				content.height(info.height - (h-scrollBar));
		}
	}
}
function StaffelViewer(ob) {
	var staf = this
		,doc = $(document);
	this.options = jQuery.extend({
		selectors: [
			['staffelClick', '123']
		],
		url: 'DesktopModules/PGShopVetinStaffel.aspx?ProductCode=',
		animTime: 300
	}, ob);
	this.showTable = function () {
		$.prettyLoader.hide();
		var $elem = staf.$elem.data('staffelTable'),
			coor = staf.$elem.offset();
		if (!staf.$elem.data('opened')) {
			$elem.css({
				left: coor.left + 25,
				top: coor.top - 10
			})
			.removeClass('topHalf bottomHalf')
			.addClass(staf.topHalf(staf.$elem)? 'topHalf': 'bottomHalf')
			.show();
			staf.$elem.data('opened', true);
			doc.bind('click', function (e) {
				staf.showTable($elem);
			});
		} else {
			doc.unbind('click');
			$elem.hide();
			staf.$elem.data('opened', false);
		}
	};
	this.getData = function (e) {
		var $this = $(e.currentTarget),
			data = $this.data('staffel'),
			staffelTable = $this.data('staffelTable');
		staf.$elem = $this;
		if (staffelTable) {
			staf.timeout = setTimeout(staf.showTable,  staf.options.animTime);
		} else {
			var tempElem = $('<div class="staffelViewer"><div class="staffelContainer" /><div class="arrow"></div></div>')
					.appendTo(document.body)
					.hide()
					.find('div.staffelContainer')
					.load(staf.options.url + data + ' #lblStaffel')
					.end()
					.hover(staf.cancelHide, staf.cancelGetData);
			staf.timeout = setTimeout(staf.showTable, staf.options.animTime);
			$this.data('staffelTable', tempElem);
		}
	};
	this.cancelGetData = function () {
		staf.timeout && clearTimeout(staf.timeout);
		if (staf.$elem.data('opened')) {
			staf.timeout = setTimeout(staf.showTable, staf.options.animTime);
		}
	};
	this.cancelHide = function () {
		staf.timeout && clearTimeout(staf.timeout);
	};
	this.topHalf = function ($elem) {
		return $elem.offset().top < doc.height() / 2;
	};
	var len = this.options.selectors.length
		,sel = this.options.selectors;
	while (len--) {
		var item = sel[len];
		jQuery('#' + item[0])
		.data('staffel', item[1])
		.hover(this.getData, this.cancelGetData);
	}
};
