$(document).ready(function(){
    $('div.buy a').click(function() {
    	var url = $(this).attr('href');
    	$.ajax({
 		   type: "GET",
 		   url: url,
 		   dataType: 'json',
 		   success: function(msg){
    			$('#productsAmount').html( msg.productsAmount );
    			$('#aggregatedPrice').html( parseFloat( msg.aggregatedPrice ).toFixed(2) );
 		   }
 		});
    	return false;
    });
    
    // send to a friend
    $('div.send_to_friend a').click(function() {
		$.ajax({
			url: $('#baseUrl').val() + '/eshop/send-to-friend/productId/' + $('#productId').val(),
			dataType: 'html',
			type: 'GET',
			success: function( msg ) {
				ourPrompt( msg );
				var height = $('#main_container').css('height');
				$('div.cleanbluecontainer').css({'width':'340px'});
				
			}
		})
    });
});


function ourPrompt(msg) {
	$.prompt(msg ,{
		submit: mySubmit,
  		buttons:{ 'Изпрати': 'send', 'Затвори':'close' },
  		prefix:'cleanblue',
  		show:'fadeIn'
	});
}


function mySubmit(v,m){
	if( 'close' == v ) {
		return true;
	}
	else if ('send' ==v ) {
		$.ajax({
			url: $('#baseUrl').val() + '/eshop/send-to-friend/productId/' + $('#productId').val(),
    		dataType: 'html',
    		data: $('#main_container form').serialize(),
    		type: 'POST',
    		success: function( retValue ) {
    			$('#main_container').replaceWith( retValue );
    			// remove the submit button        			
    			if( $('#main_container').find('input#senderEmail').length < 1 ) {
    				$('div#cleanbluebuttons button:first').remove();
    			}
    		}
    	})
		return false;
	}
}
