/* Start prodcomparison script for Bosch PT OCS | Kittelberger dsp 2009-11-05 */
$(document).ready(function () {
	
	// SETUP 
	var ocRow 		= '.ocRow'; 		// outside container row
	var icRow 		= '.icRow';			// inside container which should be as wide as all blocks
	var pbBlock 		= '.pbBlock'; 		// container block
	var pbBlockFix		= '.pbBlockFix'; 	// container block (fixed)	
	var pbBlockDel		= '.pbBlockDel';	// deletes pbBlock	
	var pbBlockAll		= '.pbBlockAll';	// shows all pbBlock
	var rowElm 		= '.rowElm';  		// row element
	var cPB		 	= 'pbBlock_';  		// classname that is added to each container block
	var cRE			= 'rowElm_';  		// classname that is added to each row element
	var loadingPC		= '#loadingPC';  		// classname that is added to each row element
	var printOnly		='.printOnly'; // printonly Elemnts
	var initCalc		=false;
	var rowCounter		=0;
	var printMode=false;
	
	$(ocRow).css('visibility','hidden');
		//the width of the elements according to the thickbox_width;
		$('.ocRow').css('width', ($('#TB_iframeContent', window.parent.document).width()-36));
		$('.pcHeader').css('width', ($('#TB_iframeContent', window.parent.document).width()-24));
			
			
	
	buildRowElmArrays = function (whichIcRow, className) {
		currentTallest = 0;
		splitClasses = className.split(' ');
		lastClassName = splitClasses[splitClasses.length-1];
		lastClassName = '.'+lastClassName;	
		$(whichIcRow).find(lastClassName).each(function (j) {
			if ($(this).height() > currentTallest) { 
				currentTallest = $(this).outerHeight(true); 
			}
		});	
		$(whichIcRow).find(lastClassName).css('height', currentTallest);
	}
	
	
	reHeightRowElms = function (whichIcRow) {
		$(whichIcRow).find(pbBlock+':visible'+':first').find(rowElm+':visible').each(function () {
			rowElmClassName = $(this).attr('class');
			buildRowElmArrays($(whichIcRow), rowElmClassName);
		});		
	}
	
	
	calcHeight = function (calcOuterHeight) {
		reCalcH = calcOuterHeight;
		// loop through each icRow
		$(icRow).each(function () {
			if(initCalc==false){				
				totalWidth = -1*($(this).find(printOnly).length*$(printOnly).width());
				rowCounter+=1;
				if(rowCounter>=$(icRow).length){
				initCalc=true;
				}
			}
			else{
				totalWidth =0;
			}//begin with negative because hiding '.printOnly'-Elements mru 01-29-10}
			if(printMode==true){
				totalWidth = 1*($(this).find(printOnly).length*$(printOnly).width());
			}
			currentTallest = 0;
			// loop through each pbBlock
			$(this).find(pbBlock+':visible').each(function (i) {
				// icRow total width should be the width of all pbBlocks
				if(!($(this).hasClass(pbBlockFix))) {
					totalWidth = totalWidth + $(this).outerWidth(true); 
				}
				// give all pbBlocks a rising class
				if (reCalcH) {$(this).addClass(cPB+i);}
				
				// loop through each rowElm
				$(this).find(rowElm+':visible').each(function (j) {
					// give all rowElms a rising class
					$(this).addClass(cRE+j);				
					if ($(this).height() > currentTallest) { 
						currentTallest = $(this).outerHeight(true); 
					}
				});	
			});
		
			// IE6 Bug to show x-scroll for ocRow
			/*
			if (totalWidth >= $(this).parent().outerWidth(true)) {
				$(this).parent().css('overflow-x', 'scroll');	
			}
			else {
				$(this).parent().css('overflow-x', 'auto');	
			}
			*/
			
			// set icRows total width to be as wide as all pbBlocks
			$(this).css('width', totalWidth);
			if (reCalcH) {
				reHeightRowElms($(this));
			};
			
		});
		
		if(printMode==true){
			window.print();
			setTimeout('hidePrintOnly()', 1000);
			printMode=false;
			setTimeout('calcHeight(false)', 1000);
		}

	}
	
	
	// set left position for fixed block
	reposFixedBlock = function () {
		leftScPos = $(window).scrollLeft();
		$(pbBlock+pbBlockFix).css('position', 'absolute');
		$(pbBlock+pbBlockFix).css('left', leftScPos);
	}
	
	/* delete pbBlock */		
	$(pbBlockDel).click(function () {
		if ((!($(this).parent().hasClass(pbBlockFix))) && (($(this).parent().parent().find(pbBlock+':visible').length)>2)) {
			$(this).parent().fadeOut(500);			
			setTimeout('calcHeight(false)', 1000);
		}
	});	
	
	
	/* show all pbBlock */		
	$(pbBlockAll).click(function () {
			$(this).parent().parent().find(pbBlock+':hidden').css('display', 'block');
			$(this).parent().parent().find(printOnly).css('display', 'none');	
			setTimeout('calcHeight(false)', 500);
	});	
	
	/*hide additional legends for PrintOnly  mru- 01-29-10*/
	hidePrintOnly = function () {
		$(printOnly).css('display', 'none');	
	};	
	
	/* load functions on winload 	
	$(window).load(function () {
		calcHeight(true);
		reposFixedBlock();	
		$(ocRow).css('visibility','visible');
	});
	 */	
	 /* call when print is invoked   mru 01-29-10*/
onPrint=function(){
		if($('.printOnly')){
			initCalc=false;
			$('.printOnly').css('display', 'block');
			printMode=true;
			setTimeout('calcHeight(false)', 500);

		}
	}
	 
	 
	initSite = function () {
		initCalc=false;
		calcHeight(true);
		reposFixedBlock();
		$(pbBlock).css('width','174px');// IE 6 width-fix mru 29-01-10
		$(ocRow).css('visibility','visible');
		$(loadingPC).css('display','none');
		hidePrintOnly(); /*added by mru- 01-29-10*/
	}
	
	$(window).resize(function () {	
		reposFixedBlock();		
	});		
	
	$(window).scroll(function () {	
		reposFixedBlock();		
	});

});

setTimeout ('initSite()', 2000);
