function reBind()
{
	bind_table_hilights();
	
	$(".delete_button").hover(function(){
			$(this).toggleClass('hover')
		}
	);
	
	// выбор тарифа
	$('#glass').click(function(){
		$(this).hide();
		$('#sel_tarif').hide();
	});

	$('#glass').bind("contextmenu", function(e) {
        e.preventDefault();
    });

	$('#sel_tarif_close').click(function(){
		$('#sel_tarif').hide();
		$('#glass').hide();
	});
	
	$('.tarif_selector_inner').find('tr').click(function(){
		selectTarif($(this));
	});

	$('.tarif_selector_inner').find('tr').hover(function(){
		$(this).toggleClass('hover');	
	})
}

function setBlocksMargin()
{
	var cwidth = $('#blocks').width()-8;
	var bwidth = $('#blocks .block').width();
	var bcount = Math.floor(cwidth/bwidth);
	var cfree = cwidth - bwidth*bcount;
	var nmargin = Math.floor(cfree/(bcount)/2);
	nmargin = nmargin ? nmargin : 1;
	$('#blocks .block').css({'margin-left': nmargin+'px', 'margin-right':nmargin+'px'});  
};

// функции для выбора тарифов
function selectTarifShow()
{
	$('#glass').show();
	$('#sel_tarif').show();
	var m = parseInt($('#product_image').width()) + 50;
	$('.sum').css('marginLeft', m.toString()+'px');
	selectTarif($('.tarif_selector_inner').find('tr:first'));
}

function selectTarif(tarif)
{
	$('#tarif_id').val(tarif.children('td.id').html())
	$('.tarif_selector_inner').find('tr.selected').removeClass('selected');
	tarif.addClass('selected');
	$('.tarif_desc_inner').html(tarif.children('td.desc').html());
	$('#tarif_name').html(tarif.children('td.name').html());
	$('#tarif_price').html(tarif.children('td.price_cl').html());
	var tarif_price = parseInt(tarif.children('td.price_cl').html());
	var product_price = parseInt($('#product_price').html());
	var grand_total = tarif_price+product_price;
	$('#grand_total').html(grand_total.toString());
}


function bind_table_hilights ()
{
	$(".tlist tr").mousemove(function(){
		if (! $(this).hasClass("theader")){ $(this).addClass('hilight') }
	});
	$(".tlist tr").mouseout(function(){
		if (! $(this).hasClass("theader")){ $(this).removeClass('hilight') }
	});
}


function aReq(link, container, callback)
{
	$.ajax({ 
		type: "get",url: link,
		cache: false,  
		beforeSend: function (){
			$("*").toggleClass('wait');
		},   
		success: function(html){ //so, if data is retrieved, store it in html 
			$("*").removeClass('wait');
			
			$(container).show("slow"); //animation 
			$(container).html(html); //show the html inside .content div
			reBind();
			if (callback){ callback.call() }
		} 
    });
	return false;	
}

// prepare the form when the DOM is ready 
function aReqForm(form, container)
{
	form = '#'+form;
	container = '#'+container;
	var url = $(form).attr('action') ? $(form).attr('action') : '/adm/index.cgi';
    var options = { 
        target:        container,   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback	
		url:       	   url         // override for form's 'action' attribute 
    }; 
     
	$(form).ajaxSubmit(options); 
    // !!! Important !!! 
    // always return false to prevent standard browser submit and page navigation 
    return false;    
};

function afterReceive (data, type)
{
	var i = data.indexOf('redirect:', 0);
	if ( i >= 0){
		document.location.href=(data.substr(9, data.length-9));
	}
	return data;
}

// pre-submit callback 
function showRequest(formData, jqForm, options)
{ 
    var queryString = $.param(formData); 
	$("*").addClass('wait');
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)
{ 
	$("*").removeClass('wait');
	reBind();
}

function confirmLink(theLink, theMessage)
{
    // Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
    confirmMsg = ' ';
    if (confirmMsg == '' || typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm(theMessage);
    return is_confirmed;
}

function aReqForm(form, container) {
	form = '#'+form;
	container = '#'+container;
	var url = $(form).attr('action') ? $(form).attr('action') : '/adm/index.cgi';
    var options = { 
        target:        container,   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback
		
		    url:       	   url         // override for form's 'action' attribute 
 
        // other available options: 
        
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000

    }; 
 
    // bind to the form's submit event 

        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        
		$(form).ajaxSubmit(options); 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    
};

