/* Save Profile */

function saveProfile() {
    	
    	$('#market_ajax-loader').dialog({modal: true, "zIndex": 3999});
		form = $("#market_profile_form");
    	$.post(form.attr('action'), form.serialize(),
            function(data) {
            		$('#market_ajax-loader').dialog('destroy');
                    $('#market_profile .dialog_content').html(data);
            });

return false;
}
    

function register() {
    	$('#market_ajax-loader').dialog({modal: true, "zIndex": 3999});
		form = $("#market_register_form");
    	$.post(form.attr('action'), form.serialize(),
            function(data) {
            		$('#market_ajax-loader').dialog('destroy');
                    $('#market_profile .dialog_content').html(data);
            });

		return false;
}    


 function add2Cart(caller, confirm) {
	 		form = $(caller).parent();
	 		button = $(caller);
	 		url = '/market/add.php';
	 		if (confirm) url+='?CONFIRM=Y';
			$.post(url, form.serialize(),
            function(data) {
            	$('#market_bar_container, #market_fake_bar_container').css({'display':'block'});
            
            	if (data['status'] == 1) {
             		$('#small_basket').html(data['small_basket']);
             		$('#market_added').dialog({'modal': false, "zIndex": 3999});
             		setTimeout( "$('#market_added').dialog('destroy');", 1000);
             	}
             	else {
             		$('#market_order_confirm .dialog_contents').html(data['message']);
             		$('#market_order_confirm').dialog({'modal': true, "zIndex": 3999});
             	}
            },
            'json'
            );

		return false;
}



$(document).ready(function() {
	
	
	//ALERT IF NOENOUFG PRODUCTS
	ybox = $('#yellow_box');
	if (ybox) {
		if (ybox.attr('alert') == 'y') {
			
			$('#market_order_alertne').dialog({'modal': true, "zIndex": 3999});
		}
	}
	
	
	//BUY
	
	 $('.buy').click(function(){
	 		form = $(this).parent();
	 		button = $(this);
			$.post('/market/add.php', $(this).parent().serialize(),
            function(data) {
            	$('#market_bar_container, #market_fake_bar_container').css({'display':'block'});
            
            	if (data['status'] == 1) {
             		$('#small_basket').html(data['small_basket']);
             		$('#market_added').dialog({'modal': true, "zIndex": 3999});
             		setTimeout( "$('#market_added').dialog('destroy');", 1000);
             	}
             	else {
             		$('#market_order_confirm .dialog_content').html(data['message']);
             		$('#market_order_confirm').dialog({'modal': true, "zIndex": 3999});
             		
             		$('#market_order_confirm_button').unbind();

             		$('#market_order_confirm_button').click(function(){
             			add2Cart(button, true);$('#market_order_confirm').dialog('destroy');
             			return false;
             		});
             	}
            },
            'json'
            );

		return false;
    });
	

	//AUTH
    $('#auth_link, #auth_link2, .auth_link').click(function(){
    	$('#market_auth').dialog({modal: true, "zIndex": 39999});
    	return false;
    });
    
    $('#market_auth .market_button').click(function(){
    	$('#market_auth form').submit();
    	return false;
    });
    
    
	//Accept Oferta
	$('#acceptOferta').click(function(){
		if ($(this).attr('checked')) {
				$('#basketOrderButtonA').removeClass('market_button_gray');
				$('#basketOrderButtonA').addClass('market_button_blue');
		}
		else {
				$('#basketOrderButtonA').addClass('market_button_gray');
				$('#basketOrderButtonA').removeClass('market_button_blue');
		}
    	return false;
    });
    
    
    /* Show Oferta */
    
   
    $('#marekt_show_oferta').click(function(){
    	$('#market_oferta').dialog({modal: true, "zIndex": 39999});
    	return false;
    });
    
    
    /* Show Profile */

    $('#market_show_profile').click(function(){
    	
    	$('#market_ajax-loader').dialog({modal: true, "zIndex": 3999});
		$.get('profile.php', {},
            function(data) {
            		$('#market_ajax-loader').dialog('destroy');
                    $('#market_profile').dialog({position: 'top' ,modal: true, "zIndex": 3999});
                    $('#market_profile .dialog_content').html(data);

            });

    	return false;
    });
    
    
    
    /* ORDER */
    
	$('#basketOrderButton').click(function(){
    
   		$('#market_ajax-loader').dialog({modal: true, "zIndex": 3999});
    	form = $("#order_form");
    	$.post(form.attr('action'), form.serialize(),
            function(data) {
            		$('#market_ajax-loader').dialog('destroy');
                    $('#market_order').dialog({modal: true, "zIndex": 3999});
             		$('#market_order .dialog_content').html(data);
            });

    	return false;
    });
    
    //REGISTER
    $('#register_link, #register_link2, .register_link').click(function(){
    	
    	$('#market_ajax-loader').dialog({modal: true, "zIndex": 3999});
		$.get('/personal/register.php', {},
            function(data) {
            		$('#market_ajax-loader').dialog('destroy');
                    $('#market_profile').dialog({position: 'top' ,modal: true, "zIndex": 3999});
                    $('#market_profile .dialog_content').html(data);

            });

    	return false;
    });
    
    
    //EMPRY INPUT QUANTITY 
    
    $('.market_quantity').focus(function(){
    	 $(this).val('');
    });
    
    
    //SHOW MANAGER window
    
    $('.call_manager').click(function(){
    	$('.market_dialog').dialog('destroy');
    	$('#market_manager').dialog({'modal': true, "zIndex": 3999});
    	return false;
    });


   
    
    $('.market_dialog_close').click(function(){
    	$(this).parent().parent().dialog('destroy');
    	return false;
    });



	$('.basketDelete').click(function(){
		$(this).next().val('Y');
		$('#basketRefreshButton').trigger('click');
		return false;
	});
	
	
	$('#basketOrderButton, #basketRefreshButton').css({'display':'none'});
	$('#basketOrderButtonA').click(function(){
		if ($('#acceptOferta').attr('checked')) {
			$('#basketOrderButton').trigger('click');
		}
		return false;
	});
	

	$('#basketRefreshButtonA').click(function(){
		$('#basketRefreshButton').trigger('click');
		return false;
	});
	
	
});
