var prosno, tdh, imgh, maxh;
var lang;

$( document ).ready( function() {
	if(document.getElementById("langjs")) {
		lang = document.getElementById("langjs").innerHTML;
	}
	addTopFiveTrigger();
	addClickTriggerForDetails();
});

function addClickTriggerForDetails() {
	$("table.bestella tr .clickable").click(function(event) {
		event.preventDefault();
		enableOpen($(this),0);
	});
	/*.parent().prev().click(function(event) {
		event.preventDefault();
		enableOpen($(this),1);
	}); */
}

function enableOpen(_this,_title) {
	_this.unbind().click(function(event) { event.preventDefault(); });		// once the button is pressed, disable any further action
	if(_title==1) _this = _this.children();
	var line = _this.parent();
	prosno = line.find('.prosno').text();
	openDetail(prosno, line);
}

function enableClose(_this,_title) {
	_this.unbind().click(function(event) { event.preventDefault(); });		// once the button is pressed, disable any further action
	if(_title==1) _this = _this.children();
	var line = _this.parent();
	var tr = line.next();
	line.attr('class', 'collapseTitle');
	line.find('img').attr('src','/files/inc/image/png/expandarrow.png');
	tr.find('td').wrapInner("<div/>").children("div").animate( {height: 0, paddingTop: '0px', marginTop: '0px', paddingBottom: '0px', marginBottom: '0px'}, 1000, function(){
		tr.remove();	// remove detail-tr
		line.find('.clickable').click(function(event) {
			event.preventDefault();
			enableOpen($(this),0);	
		});
		/*.parent().prev().click(function(event) {
			event.preventDefault();
			enableOpen($(this),1);
		}); */
	});	
}

function openDetail(prosno, line) {
	line.find('.expandArrow img').attr('src','/files/inc/image/gif/collapseloader.gif');
	$.ajax({ 
		type: "GET",  
		url: "/files/inc/application/x-httpd-php/bestell_moreinfo.php" , 
		data: "brosch="+prosno+"&lang="+lang,  
		success: function(phpData){
			line.after("<tr cellspacing=\"0\" class=\"detail\"></tr>");
			// detailbox
			var newline = line.next();
			newline.append(phpData);
			tdh = 0; imgh = 0; maxh = 0;
			var tdh = newline.find('td').height();
			var imgh = newline.find('img').height();
			// alert('td :: '+tdh+"\n"+"img::"+imgh);
			var maxh = Math.max(tdh, imgh);
			newline.find('.animateFrame').css({height: 0, paddingTop: '0px', marginTop: '0px', paddingBottom: '0px', marginBottom: '0px' }).animate( {height: maxh , minHeight: imgh, paddingTop:'20px' }, 1000, function() {
				$(this).css({overflow:'hidden'});
				// title
				line.attr('class', 'expandTitle');
				line.find('img').attr('src','/files/inc/image/png/collapsearrow.png');
				line.find('.clickable').unbind().click(function(event) {
					event.preventDefault();
					enableClose($(this),0);
				});
				/*.parent().prev().click(function(event) {
					event.preventDefault();
					enableClose($(this),1);
				}); */
			});
		}
	});
}

function addTopFiveTrigger() {
	$("form.bmu_bestell .topFiveDetail").show();
	$("form.bmu_bestell .topFiveTable .topFivePicture").mouseenter(function(event) {
		var prosno = $(this).parent().parent().attr('id').split('_')[1];
		$('.tF').removeClass('topFiveActive');
		addTopFiveInfos(prosno);
	}).click(function(event) {
		event.preventDefault();
		// jump to detail and open
		var prosno = $(this).parent().parent().attr('id').split('_')[1];
		var line = $('table.bestella tr').find('.prosno').map(function() {
			if($(this).text() == prosno) {		return ($(this).parent());		}	
		}).get(0);
		document.location.href="#"+prosno;
		if(line.attr('class')!="expandTitle")
			openDetail(prosno, line);
	});
	// preset the middle active
	var mid = Math.floor($("form.bmu_bestell .topFiveTable").find('.tF').size()/2); // floor equals round and then substract -1 
	if(mid && mid < 5) {
		var prosno = $("form.bmu_bestell .topFiveTable").find('.tF').eq(mid).attr('id').split('_')[1];
		addTopFiveInfos(prosno);
	}
}

function addTopFiveInfos(prosno) {
	$('#_'+prosno).addClass('topFiveActive');
	$.ajax({ 
		type: "GET",  
		url: "/files/inc/application/x-httpd-php/bestell_moreinfo.php" , 
		data: "topfive="+prosno+"&lang="+lang,  
		success: function(phpData){
			$('.tF').css({textAlign:'center', padding: '0px 0px 25px 0px'});
			$('.topFiveDetail').empty().append(phpData);
		}
	});		
}
