cJarConfigurator = {}

cJarConfigurator.CS_ITEMS_TABLE_ID = 'jar_configurator_items'
cJarConfigurator.CS_TOTAL_PRICE_ID = 'jar_configurator_total_price'

cJarConfigurator.CS_SELECT_MESSAGE = 'избор...'

cJarConfigurator.formatPrice = function( nPrice ){
	var sPrice = nPrice.toString()
	var aPrice = sPrice.match( /\.([0-9]*)$/ )
	if( aPrice == null ){
		return '.00'
	} else if( aPrice[ 1 ].length == 1 ){
		return '0'
	}	else {
		return ''
	}
}

cJarConfigurator.onMenuItemClick = function( hEvent ){
	cDomEvent.init( hEvent )
	if( cDomEvent.target.getAttribute( 'item' ) ){
		var hRow = cJarConfiguratorRow.getRowFromLink( cMoMenu.hLink )
		cMoMenu.hLink.innerHTML = cDomEvent.target.innerHTML
		cMoMenu.hLink.setAttribute( 'title', cDomEvent.target.innerHTML )
		cJarConfiguratorRow.getQtyInput( hRow ).value = '1'
		cJarConfiguratorRow.getPriceInput( hRow ).value = cDomEvent.target.getAttribute( 'price' )
		cJarConfiguratorRow.getIdInput( hRow ).value = cDomEvent.target.getAttribute( 'item' )
		cJarConfiguratorRow.getPCode( hRow ).value = cDomEvent.target.getAttribute( 'dds' )
		cJarConfiguratorRow.getStatus( hRow ).style.backgroundImage = "url('"+cDomEvent.target.getAttribute( 'status' )+"')";
		cJarConfiguratorRow.getStatus( hRow ).title = cDomEvent.target.getAttribute( 'dtitle' );
		cJarConfiguratorRow.getHStatus( hRow ).value = cDomEvent.target.getAttribute( 'stock' );
		cJarConfiguratorRow.getAInfo( hRow ).innerHTML = '<a href="?m=7&i='+cDomEvent.target.getAttribute( 'item' )+'" target="_blank" onclick="openViewWin( this, this.href, \'qv'+cDomEvent.target.getAttribute( 'item' )+'\'); return false;" ><img border="0" alt="" src="/include/img/zoom.gif"/></a>'
		cJarConfiguratorRow.recalc( hRow )
		cMoMenu.hLink.focus()
		cMoMenu.hideMenu()
	}
	cDomEvent.cancelEvent( hEvent )
	return false
}


cJarConfigurator.loadOptions = function( hEvent ){
	cDomEvent.init( hEvent )

	var hContID = false;

	if(  hContID = cDomEvent.target.getAttribute( 'menu' ) ) {
		var menuContDiv = document.getElementById(  hContID );
	  var menuUL      = menuContDiv.getElementsByTagName( 'ul' );
	  var shipSelect  = document.getElementById( 'shipping' );

	  if( menuUL.length ){
			var oldLinks = menuUL.item(0).getElementsByTagName( 'a' );
			if( oldLinks.length == 0 ){
				var url = SITEURL + '?m=24&i=' + hContID.substring( 5 );
		    if( shipSelect.value > 0 ){
					url = url + "&shipping=" + shipSelect.value;
		    }

		    menuUL.item(0).innerHTML = 'loading...';

  	    var ajax = new Ajax.Request( url,{
					method: 'post',
					onSuccess: function(transport){
						menuUL.item(0).innerHTML =  transport.responseText
						var newLinks = menuUL.item(0).getElementsByTagName( 'a' );
						for( var nI = 0; nI < newLinks.length; nI++ ){
							cJarConfigurator.initMenuItem( newLinks[ nI ] )
	          }
					}
				});
	 		}
	  }
	}
}

cJarConfigurator.initDropItem = function( hTdID ){
	var mainTd = document.getElementById( hTdID );
	var toClickA = mainTd.getElementsByTagName( 'a' );
	if( toClickA.length ){
		cDomEvent.addEvent( toClickA.item( 0 ), 'click', cJarConfigurator.loadOptions );
	}
}

cJarConfigurator.initMenuItem = function( hItem ){
	cDomEvent.addEvent( hItem, 'click', cJarConfigurator.onMenuItemClick )
}

cJarConfigurator.initItemsMenu = function( hMenu ){
	var hDivs  = hMenu.getElementsByTagName(  'div' );
	for( var dI = 0; dI < hDivs.length; dI++ ){
		cJarConfigurator.initDropItem( 'jarConfigurator_' + hDivs[dI].id.substring( 5 ) + '_row_0' );
	}
	var hItems = hMenu.getElementsByTagName( 'a' )
	for( var nI = 0; nI < hItems.length; nI++ ){
		cJarConfigurator.initMenuItem( hItems[ nI ] )
	}
}

cJarConfigurator.initItemsRow = function( hRow ){
	cJarConfiguratorRow.initActions ( hRow )
}

cJarConfigurator.recalcTotal = function(){
	var hTable = document.getElementById( 'jar_configurator_items' )
	var hTBody = hTable.tBodies[0]
	var hRows = hTBody.getElementsByTagName( 'tr' )
	var nTotalPrice = 0
	for( var nI = 0; nI < hRows.length; nI++ ){
		if( hRows[ nI ].className.indexOf( 'item_row' ) < 0 || cJarConfiguratorRow.getTotalPriceInput( hRows[ nI ] ).value == '' ){
			continue
		}
		nTotalPrice += parseFloat( cJarConfiguratorRow.getTotalPriceInput( hRows[ nI ] ).value )
	}
	var hTotalPrice = document.getElementById( cJarConfigurator.CS_TOTAL_PRICE_ID )
	nTotalPrice = Math.round( nTotalPrice * 100 ) / 100
	hTotalPrice.value = nTotalPrice.toString() + cJarConfigurator.formatPrice( nTotalPrice )
}

cJarConfiguratorRow = {}

cJarConfiguratorRow.getRowFromLink = function( hLink ){
	return getParentByTagName( hLink, 'TR' )
}

cJarConfiguratorRow.getQtyInput = function( hRow ){
	return hRow.getElementsByTagName( 'input' )[2]
}

cJarConfiguratorRow.getPriceInput = function( hRow ){
	return hRow.getElementsByTagName( 'input' )[3]
}

cJarConfiguratorRow.getTotalPriceInput = function( hRow ){
	return hRow.getElementsByTagName( 'input' )[4]
}

cJarConfiguratorRow.getHStatus = function( hRow ){
	return hRow.getElementsByTagName( 'input' )[5]
}

cJarConfiguratorRow.getStatus = function( hRow ){
	return hRow.getElementsByTagName( 'span' )[0]
}

cJarConfiguratorRow.getPCode = function( hRow ){
	return hRow.getElementsByTagName( 'input' )[1]
}

cJarConfiguratorRow.getAInfo = function( hRow ){
	return hRow.getElementsByTagName( 'div' )[0]
}

cJarConfiguratorRow.getIdInput = function( hRow ){
	return hRow.getElementsByTagName( 'input' )[0]
}

cJarConfiguratorRow.getNameItem = function( hRow ){
	return hRow.getElementsByTagName( 'a' )[0]
}

cJarConfiguratorRow.getClearLink = function( hRow ){
	if( hRow.getElementsByTagName( 'a' ).length == 1 ){
		return null
	}	else {
		return hRow.getElementsByTagName( 'a' )[ hRow.getElementsByTagName( 'a' ).length - 1 ]
	}
}

cJarConfiguratorRow.getClearAllLink = function( hRow ){
	var hAllClear = document.getElementById( 'jar_configurator_clear_all ' )
  if( hAllClear )
		return hAllClear
  else
  	return null
}

cJarConfiguratorRow.onClearLinkClick = function( hEvent ){
	cDomEvent.init( hEvent )

  var hRow = getParentByTagName( cDomEvent.target, 'TR' )
	cJarConfiguratorRow.getQtyInput( hRow ).value = 0;
	hName = cJarConfiguratorRow.getNameItem( hRow );
  cJarConfiguratorRow.getPriceInput( hRow ).value = '0.00';
  cJarConfiguratorRow.getTotalPriceInput( hRow ).value = '0.00';
  cJarConfiguratorRow.getStatus( hRow ).style.backgroundImage = "url('')";
  cJarConfiguratorRow.getHStatus( hRow ).value = 2046;
	cJarConfiguratorRow.getAInfo( hRow ).innerHTML = '&nbsp;'

  hName.innerHTML = hName.getAttribute( 'defaulttitle' ) ? hName.getAttribute( 'defaulttitle' ) : 'choose...';
	cJarConfigurator.recalcTotal();
}

cJarConfigurator.clearAll = function(){
	var hTable = document.getElementById( 'jar_configurator_items' )
	var hTBody = hTable.tBodies[0]
	var hRows = hTBody.getElementsByTagName( 'tr' )
	var nTotalPrice = 0
	var hTPInput
	for( var nI = 0; nI < hRows.length; nI++ ){
		cJarConfiguratorRow.clear( hRows[ nI ], false )
	}
	cJarConfigurator.recalcTotal()
}

cJarConfigurator.onClearAllClick = function(){
	cJarConfigurator.clearAll()
}

cJarConfiguratorRow.getMultiplyLink = function( hRow ){
	if( hRow.getElementsByTagName( 'a' ).length == 1 ){
		return null
	} else {
		return hRow.getElementsByTagName( 'a' )[ hRow.getElementsByTagName( 'a' ).length - 2 ]
	}
}

cJarConfiguratorRow.clear = function( hRow, bRecalc ){
	if( typeof( bRecalc ) == 'undefined' ){
		bRecalc = true
	}

	var price =  cJarConfiguratorRow.getPriceInput( hRow );
	if(price){
		cJarConfiguratorRow.getQtyInput( hRow ).value = 0;
    hName = cJarConfiguratorRow.getNameItem( hRow );
    cJarConfiguratorRow.getPriceInput( hRow ).value = '0.00';
    cJarConfiguratorRow.getTotalPriceInput( hRow ).value = '0.00';
    cJarConfiguratorRow.getStatus( hRow ).style.backgroundImage = "url('')";
    cJarConfiguratorRow.getHStatus( hRow ).value = 2046;
    cJarConfiguratorRow.getAInfo( hRow ).innerHTML = '';
		hName.innerHTML = hName.getAttribute( 'defaulttitle' ) ? hName.getAttribute( 'defaulttitle' ) : 'choose...';
  }

	if( bRecalc ){
		cJarConfigurator.recalcTotal()
	}
}

cJarConfiguratorRow.onQtyChange = function( hEvent ){
	cDomEvent.init( hEvent )
	var hRow = getParentByTagName( cDomEvent.target, 'TR' )
	cJarConfiguratorRow.recalc( hRow )
}

cJarConfiguratorRow.onMultiplyLinkClick = function( hEvent ){
	cDomEvent.init( hEvent )
	var hRow = getParentByTagName( cDomEvent.target, 'TR' )
	if( cDomEvent.target.className == 'configurator_multiply_button' ){
		var nNextRow = cJarConfiguratorRow.getNextRow( hRow )
		var hPrevRow = document.getElementById( cJarConfiguratorRow.getRowPrefix( hRow ) + '_' + ( nNextRow - 1  ) )
		var hNewRow = hRow.cloneNode( true )
		cJarConfiguratorRow.prepareForInsertion( hNewRow, nNextRow )
		var hInsertBefore = getNextNodeSibling( hPrevRow )
		if( hInsertBefore ){
			hRow.parentNode.insertBefore( hNewRow, hPrevRow.nextSibling )
			cJarConfigurator.initDropItem( hNewRow.id );
		} else {
			hRow.parentNode.appendChild( hNewRow )
		}
		var sID = hNewRow.id
		setTimeout( function() { cFader.fadeIn( sID ) }, 1000 )
		cJarConfiguratorRow.initActions( hNewRow )
	} else if( cDomEvent.target.className == 'configurator_remove_button' )	{
		hRow.parentNode.removeChild( hRow )
		cJarConfigurator.recalcTotal()
	}
	cDomEvent.cancelEvent( hEvent )
	return false
}

cJarConfiguratorRow.prepareForInsertion = function( hNewRow, nNextRowNum ){
	//cJarConfiguratorRow.getMultiplyLink( hNewRow ).parentNode.removeChild( cJarConfiguratorRow.getMultiplyLink( hNewRow ) )
	var hLink = cJarConfiguratorRow.getMultiplyLink( hNewRow )
	hLink.className = 'configurator_remove_button'
  var clLink = cJarConfiguratorRow.getClearLink( hNewRow )
	clLink.style.display = 'none'
	cJarConfiguratorRow.getNameItem( hNewRow ).innerHTML = cJarConfigurator.CS_SELECT_MESSAGE
	cJarConfiguratorRow.getQtyInput( hNewRow ).value = '1'
	cJarConfiguratorRow.getPriceInput( hNewRow ).value = ''
	cJarConfiguratorRow.getPCode( hNewRow ).value = ''
	cJarConfiguratorRow.getStatus( hNewRow ).style.backgroundImage = "url('')";
	cJarConfiguratorRow.getHStatus( hNewRow ).value = 2046;
	cJarConfiguratorRow.getTotalPriceInput( hNewRow ).value = ''
	var nI
	var hLabels = hNewRow.getElementsByTagName( 'label' )
	for( nI = 0; nI < hLabels.length; nI++ ){
		hLabels[ nI ].htmlFor = hLabels[ nI ].htmlFor.replace( '_0', '_' + nNextRowNum )
	}
	var hInputs = hNewRow.getElementsByTagName( 'input' )
	for( nI = 0; nI < hInputs.length; nI++ ){
		hInputs[ nI ].id = hInputs[ nI ].id.replace( '_0', '_' + nNextRowNum )
		hInputs[ nI ].name = hInputs[ nI ].name.replace( '_0', '_' + nNextRowNum )
	}
	hNewRow.id = hNewRow.id.replace( '_0', '_' + nNextRowNum )
	hNewRow.className += ' insertedItem'
	hNewRow.style.backgroundColor = cFader.startColor
}

cJarConfiguratorRow.initActions = function( hRow ){
	var hQty = cJarConfiguratorRow.getQtyInput( hRow )
	cDomEvent.addEvent( hQty, 'change', cJarConfiguratorRow.onQtyChange )
	cDomEvent.addEvent( hQty, 'keyup', cJarConfiguratorRow.onQtyChange )
	var hClearLink = cJarConfiguratorRow.getClearLink( hRow )
	if( hClearLink){
	  cDomEvent.addEvent( hClearLink, 'click', cJarConfiguratorRow.onClearLinkClick )
	}
	var hMultiplyLink = cJarConfiguratorRow.getMultiplyLink( hRow )
	if( hMultiplyLink ){
		cDomEvent.addEvent( hMultiplyLink, 'click', cJarConfiguratorRow.onMultiplyLinkClick )
	}
}

cJarConfiguratorRow.recalc = function( hRow ){
	var hQty = cJarConfiguratorRow.getQtyInput( hRow )
	var hPrice = cJarConfiguratorRow.getPriceInput( hRow )
	var hTotalPrice = cJarConfiguratorRow.getTotalPriceInput( hRow )

	var nTotalPrice = parseInt( hQty.value ) * parseFloat( hPrice.value )
	nTotalPrice = Math.round( nTotalPrice * 100 ) / 100

	hTotalPrice.value = nTotalPrice.toString() + cJarConfigurator.formatPrice( nTotalPrice )
	cJarConfigurator.recalcTotal()
}

cJarConfiguratorRow.getNextRow = function( hRow ){
	var sRowId = hRow.id
	var hIDRegEx = /([0-9a-z_]*)_([0-9]*)$/i
	var aRes = hIDRegEx.exec( sRowId )
	var hCounter = 1
	if( aRes != null ){
		hCounter = parseInt( aRes[ 2 ] ) + 1
		while( document.getElementById( aRes[ 1 ] + '_' + hCounter ) != null ){
			hCounter += 1
		}
	}
	return hCounter
}

cJarConfiguratorRow.getRowPrefix = function( hRow ){
	var sRowId = hRow.id
	var hIDRegEx = /([0-9a-z_]*)_([0-9]*)$/i
	var aRes = hIDRegEx.exec( sRowId )
	if( aRes != null ){
		return aRes[ 1 ]
	}
	return null
}

cMoMenu.onShowMenu = function(){
	var hMenuInnerElement = getSubNodeByName( window.moMenu.hElement, 'div' )
	hMenuInnerElement.scrollTop = 0
}

cDomExtensionManager.register( new cDomExtension( document, [ 'tr[class*=item_row]' ], cJarConfigurator.initItemsRow ) )
cDomExtensionManager.register( new cDomExtension( document, [ 'div[class=menuContainer]' ], cJarConfigurator.initItemsMenu ) )

cJarConfigurator.onPageLoad = function(){
	if( document.getElementById( 'jar_configurator_clear_all' ) ){
		cDomEvent.addEvent( document.getElementById( 'jar_configurator_clear_all' ), 'click', cJarConfigurator.onClearAllClick )
	}
	cJarConfigurator.recalcTotal()
}

cDomEvent.addEvent( window, 'load', cJarConfigurator.onPageLoad )
