var last_loaded_cost = 0;

function fastshow (contentId, evt, priceId) {
	//alert ( document.getElementById ( contentId ).outerHTML );
	var content = document.getElementById ( contentId ).innerHTML;	
	var el = document.getElementById("mmoves");
        if (el != null) {
            var parent = el.parentNode;
            even = window.event ? window.event : evt;
            var o = even.srcElement ? even.srcElement : even.targetElement;
            mouseX = even.clientX;
            mouseY = even.clientY;
            //alert ( mouseX + ' = ' + mouseY );
            if (content!='' && el.style.visibility != "visible") {
                    el.innerHTML = '<small>'+content+'</small>';
            }

            if ( typeof external != 'object' ) {
                    var x = mouseX + window.pageXOffset + 10;
                    var y = mouseY + window.pageYOffset + 10;
            } else {	// IE
                    var x = mouseX + document.documentElement.scrollLeft + 10;
                    var y = mouseY + document.documentElement.scrollTop + 10;
            }


            if (mouseX + el.offsetWidth + 10 > document.body.clientWidth) { x-=(mouseX + el.offsetWidth + 3 - document.body.clientWidth); if (x < 0) {x=0}; }
            if (mouseY + el.offsetHeight + 10 > document.body.clientHeight) { y-=(mouseY + el.offsetHeight + 3 - document.body.clientHeight); if (y < 0) {y=0}; }

            var tableO = document.getElementById ( 'priceTable'+priceId );
            var tableX = tableO.offsetLeft;
            var tableY = tableO.offsetTop;
            var i = tableO;
            //while ( i = i.parentNode ) {
            //	if ( !isNaN ( i.offsetLeft ) ) tableX += i.offsetLeft;
            //	if ( !isNaN ( i.offsetTop ) ) tableY += i.offsetTop;
            //}
            //x -= tableX;
            //y -= tableY;
            x += 10;
            y += 20;

            //x = 10;
            //alert ( x + ' = ' + y );

            el.style.left = x + "px";
            el.style.top  = y + "px";

            if (el.style.visibility != "visible") {
                    el.style.visibility = "visible";
            }
        }
}

function hideshow () {
    if (document.getElementById("mmoves") != null)
	document.getElementById("mmoves").style.visibility = 'hidden';
}


	/////////////////////////////////////////////////////////
	//
	//	basket
	//
	/////////////////////////////////////////////////////////
	var pId = 0;
	
	function loadSizes ( productId ) {
		productId = parseInt ( productId );
		if ( productId ) {
			pId = productId;
			$('#sizeDiv').load ( '/include/backend/get_price_sizes.php?item='+productId+'&tmp='+Math.random() );			
		}
		loadCost ( );
	}
	
	function loadCost (countoverwrite ) {
		sizeId = $('#price_size').attr ( 'value' );
		count = $('#price_count').attr ( 'value' );
		if (countoverwrite) {
			count = countoverwrite;
			$('#price_count').attr ( 'value', countoverwrite );
		} 
		
		
		sizeId = parseInt ( sizeId );
		count = parseInt ( count );
		if ( sizeId > 0 && count > 0 ) {
			$.getJSON ( '/include/backend/get_price_cost.php',
						{'item': pId, 'size': sizeId, 'count':count},
						function ( data ) {
							if ( data.error != '' ) {
								alert ( data.error );
							} else {
								$('#itogCost').html ( data.summ + ' р.' );
								$('#bat_summ_cost').html (data.summ + ' р.');
							}
						}
			);
		} else {
			$('#itogCost').html ( '0.00 р.' );
		}
	}
	
	
	
	function addItem ( ) {
		sizeId = $('#price_size').attr ( 'value' );
		count = $('#price_count').attr ( 'value' );
		sizeId = parseInt ( sizeId );
		count = !count ? 0 : parseInt ( count );
		
		if ( count <= 0 ) {
			alert ( "Укажите количество" );
			return false;
		}
		
		if ( pId && sizeId && count ) {
			$.getJSON ( '/include/backend/add_item.php',
						{'item_id': pId, 'size_id': sizeId, 'count':count},
						function ( data ) {						
						//alert ( data );
							if ( data.text != '' ) {
								alert ( data.text );
							}
							$('#basketContent').html ( data.basket );							
							$('#price_count').attr ( 'value', 0 );
						}
			);
		}
		return false;
	}
	
	function recostBasket ( b_id ) {
		b_id = parseInt ( b_id );
		if ( !b_id ) return;
		count = $('#p_count_'+b_id ).attr ( 'value' );
		count = parseInt ( count );
		if ( !count || isNaN(count) ) return;
		
		$.getJSON ( '/include/backend/action_basket.php',
					{'action':'recost', 'b_id':b_id, 'count':count},
					function ( data ) {
						if ( data.error != '' ) {
							alert ( data.error );
						} else {
							$('#p_summ_'+b_id).html ( data.summ + ' р.');
						}
					}
		);
	}
	
	function resaveBasket ( b_id ) {
		b_id = parseInt ( b_id );
		if ( !b_id ) return;
		count = $('#p_count_'+b_id ).attr ( 'value' );
		count = parseInt ( count );
		if ( !count || isNaN(count) ) return;
		
		$.getJSON ( '/include/backend/action_basket.php',
					{'action':'resave', 'b_id':b_id, 'count':count},
					function ( data ) {
						if ( data.error != '' )
							alert ( data.error );

						$('#basketContent').html ( data.basket );						
					}
		);
	}
	
	function deleteBasket ( b_id ) {
		b_id = parseInt ( b_id );
		if ( !b_id ) return;
		$.getJSON ( '/include/backend/action_basket.php',
					{'action':'delete', 'b_id':b_id},
					function ( data ) {
						if ( data.error != '' )
							alert ( data.error );

						$('#basketContent').html ( data.basket );						
					}
		);
	}
	
	function selectItem ( p_id, s_id, a_tag ) {
		p_id = parseInt ( p_id );
		$(a_tag).parent().css({"position": "relative"});
		$(a_tag).after($('#template-block-basket-add'));
		$('#bat_cost_price').html($(a_tag).html());
		$('#template-block-basket-add').show();
		$('#bat_num').attr('value', 1);
		if ( p_id ) {
			pId = p_id;
			$('#price_item').attr ( 'value', pId );
			$('#price_count').attr ( 'value', 0 );			
			$.get ( '/include/backend/get_price_sizes.php',
					{'item':pId},
					function data ( data ) {
						$('#sizeDiv').html ( data );
						$('#price_size').attr ( 'value', s_id );
						loadCost ( 1 );
					}
			);
		}
		loadCost ( 1 );
	}
	
	function reloadCapcha ( o ) {
		if ( !o ) o = document.getElementById('capchaImage');
		o.src = 'capcha.php?tmp='+Math.random();
	}