if ( typeof Popup == 'undefined' )
{
	Popup = function(){};
}

Popup.Auctions = function( params )
{
	this.type = '';
	this.controller = '';
	this.action = '';
	this.status = '';
	this.init( params );
};

Popup.Auctions.prototype.init = function( params )
{
	for ( var i in params)
		this[i] = params[i];
};

//open pop-up
Popup.Auctions.prototype.showPopup = function(type, id) {
	this.type = type;
	if ( id != undefined && id != '' )
		this.lot_id = id;
	if( this.type == 'access_auction' )
		this.popupAuction();
	else 
		this.popup();
	return false;
};

//close pop-up box
Popup.Auctions.prototype.closePopup = function() {
	$('#opaco').toggleClass('hidden').removeAttr('style');

	$('#popup').toggleClass('hidden');
	return false;
};

Popup.Auctions.prototype.popup = function( search ) {
	$('#loading_tree').removeAttr("style");
	
	if ( search != undefined && search != '' ){
		$('#opaco').toggleClass('hidden').removeAttr('style');
		$('#popup').toggleClass('hidden');
	}

	if ( this.controller == 'auctionsup' ){
		address = "/auctionsup/"+this.type+"/";
		data = "search="+$('#search').val()+"&lot_id="+this.lot_id+"&auction_id="+this.auction_id;
	}
	else if ( this.controller == 'auctions' ){
		address = "/auctions/"+this.type+"/";
		data = "search="+$('#search').val()+"&lot_id="+this.lot_id+"&auction_id="+this.auction_id;
	}
	else if ( this.controller == 'tenders' ){
		address = "/tenders/"+this.type+"/";
		data = "search="+$('#search').val()+"&lot_id="+this.lot_id+"&tender_id="+this.tender_id;
	}
	else if ( this.controller == 'quotations' ){
		address = "/quotations/"+this.type+"/";
		data = "search="+$('#search').val()+"&lot_id="+this.lot_id+"&quotation_id="+this.quotation_id;
	}
	else if ( this.controller == 'requestprices' ){
		address = "/requestprices/"+this.type+"/";
		data = "search="+$('#search').val()+"&lot_id="+this.lot_id+"&requestprice_id="+this.requestprice_id;
	}
	else if ( this.controller == 'requestproposals' ){
		address = "/requestproposals/"+this.type+"/";
		data = "search="+$('#search').val()+"&lot_id="+this.lot_id+"&requestproposal_id="+this.requestproposal_id;
	}
	$.ajax({
		type: "POST",
		url: address,
		data: data,
		dataType: "html",
		success: function(data){
			$('#popup').html(data);
			$('#popup').togglePopup();
			$('#popup_bug').show();
			$(".filetree").treeview({
				persist :"location",
				// collapsed: false,
				unique :true,
				animated :"fast"
			});
			$('#loading_tree').attr("style", "display:none;");
		}
	});
};

Popup.Auctions.prototype.showUser = function(id) {
	$.ajax({
		type: "POST",
		url: '/main/showUser/',
		data: "id=" + id + "&controller=" + this.controller,
		dataType: "html",
		success: function(data){
			$('#popup').html(data);
			$('#popup').togglePopup();
			$('#user_show_form').show();
		}
	});
};

Popup.Auctions.prototype.addMenu = function() {
	if ( this.action == 'menu' )
		adress = '/admin/addMenu/';
	else if ( this.action == 'menuBottom' )
		adress = '/admin/addMenuBottom/';
	$.ajax({
		type: "POST",
		url: adress,
		dataType: "html",
		success: function(data){
			$('#popup').html(data);
			$('#popup').togglePopup();
			$('#add_menu').show();
		}
	});
};

Popup.Auctions.prototype.addPopMenu = function() {
	$.ajax({
		type: "POST",
		url: '/admin/addPopMenu/',
		dataType: "html",
		success: function(data){
			$('#popup').html(data);
			$('#popup').togglePopup();
			$('#add_menu').show();
		}
	});
};

Popup.Auctions.prototype.editMenu = function( id ) {
	if ( this.action == 'menu' )
		adress = '/admin/editMenu/';
	else if ( this.action == 'menuBottom' )
		adress = '/admin/editMenuBottom/';
	$.ajax({
		type: "POST",
		url: adress,
		data: 'id=' + id,
		dataType: "html",
		success: function(data){
			$('#popup').html(data);
			$('#popup').togglePopup();
			$('#add_menu').show();
		}
	});
};

Popup.Auctions.prototype.editPopMenu = function( id ) {
	$.ajax({
		type: "POST",
		url: '/admin/editPopMenu/',
		data: 'id=' + id,
		dataType: "html",
		success: function(data){
			$('#popup').html(data);
			$('#popup').togglePopup();
			$('#add_menu').show();
		}
	});
};

Popup.Auctions.prototype.answerGost = function( menu_id, gost_id ) {
	var obSelf = this;
	$.ajax({
			type : "POST",
			url: '/admin/answerGost/',
		   data: 'menu_id=' + menu_id + '&id=' + gost_id,
		   dataType: 'html',
		   success: function(data) {
				$('#popup').html(data);
				$('#popup').togglePopup();
				$('#popup_answer').show();
		   }
	});
};

Popup.Auctions.prototype.editGost = function( menu_id, gost_id ) {
	var obSelf = this;
	$.ajax({
			type : "POST",
			url: '/admin/editGost/',
		   data: 'menu_id=' + menu_id + '&id=' + gost_id,
		   dataType: 'html',
		   success: function(data) {
				$('#popup').html(data);
				$('#popup').togglePopup();
				$('#popup_answer').show();
		   }
	});
};

Popup.Auctions.prototype.addDocs = function() {
	$('#add_docs').toggleClass("hidden");
};

Popup.Auctions.prototype.editCatGaler = function( menu_id ) {
	var obSelf = this;
	$.ajax({
			type : "POST",
			url: '/admin/editCatGaler/',
		   data: 'menu_id=' + menu_id,
		   dataType: 'html',
		   success: function(data) {
				$('#popup').html(data);
				$('#popup').togglePopup();
				$('#edit_cat_galer').show();
		   }
	});
};

Popup.Auctions.prototype.messageForm = function( id_user ) {
	this.closePopup();
	var obSelf = this;
	$.ajax({
			type : "POST",
			url: '/mail/message/',
			data: 'id=' + id_user,
			dataType: 'html',
		   success: function(data) {
				$('#popup').html(data);
				$('#popup').togglePopup();
				$('#message').show();
		   }
	});
};

Popup.Auctions.prototype.sendMessage = function() {
	var obSelf = this;
	$.ajax({
			type : "POST",
			url: '/mail/sendMessage/',
			data: $('#message_form').serialize(),
			dataType: 'html',
		   success: function(data) {
				obSelf.closePopup();
				if ( data == 1 )
					mesag = 'Ваше сообщение отправлено!';
				else
					mesag = data;
				$.blockUI({ 
					message: mesag,
					css: {
		            border: 'none', 
		            padding: '15px', 
		            backgroundColor: '#000', 
		            '-webkit-border-radius': '10px', 
		            '-moz-border-radius': '10px', 
		            opacity: .5,
		            color: '#fff'
		        } }); 
	        setTimeout($.unblockUI, 2000);
		   }
	});
};

Popup.Auctions.prototype.answerMessage = function( id ){
	var obSelf = this;
	$.ajax({
		   type: "POST",
		   url: '/mail/answerMessage/',
		   data: 'id=' + id,
		   dataType: 'html',
		   success: function(data){
				$('#popup').html(data);
				$('#popup').togglePopup();
				$('#message').show();
		   }
		 });
};

Popup.Auctions.prototype.editOption = function( id ) {
	$.ajax({
		type: "POST",
		url: '/admin/editOption/',
		data: 'id=' + id,
		dataType: "html",
		success: function(data){
			$('#popup').html(data);
			$('#popup').togglePopup();
			$('#edit_option').show();
		}
	});
};

Popup.Auctions.prototype.addOption = function() {
	var obSelf = this;
	$.ajax({
			type : "POST",
			url: '/admin/addOption/',
			dataType: 'html',
		   success: function(data) {
				$('#popup').html(data);
				$('#popup').togglePopup();
				$('#edit_option').show();
		   }
	});
};

Popup.Auctions.prototype.closeForm = function() {
	$.unblockUI(); 
	return false;
};

Popup.Auctions.prototype.sendMailForm = function( test_id ) {
	var obSelf = this;
	$.ajax({
			type : "POST",
			url: '/main/loadSendMailForm/',
			data: 'test_id=' + test_id + '&controller=' + obSelf.controller,
			dataType: 'html',
			success: function(data) {
				$('#popup').html(data);
				$('#type_text').val( obSelf.controller );
				$('#popup').togglePopup();
				$('#mail_form').show();
		   }
	});
};

Popup.Auctions.prototype.messageUI = function( mesag ) {
	$.blockUI({ 
		message: mesag,
		css: {
        border: 'none', 
        padding: '15px', 
        backgroundColor: '#000', 
        '-webkit-border-radius': '10px', 
        '-moz-border-radius': '10px', 
        opacity: .5,
        color: '#fff'
    } }); 
	setTimeout($.unblockUI, 2000);
};
